upstash / qstash-js

Message queue for serverless
https://docs.upstash.com/qstash
MIT License
133 stars 11 forks source link

RFC: Update receiver.ts public method to return payload event #36

Closed p6l-richard closed 1 month ago

p6l-richard commented 1 year ago

Changes

Context

This is useful to provide storngly-typed objects back to the caller. E.g. with tRPC (which validates with zod), I can pass that to an input I've defined previously:

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
  const receiver = new Receiver({
    currentSigningKey: process.env.QSTASH_CURRENT_SIGNING_KEY!,
    nextSigningKey: process.env.QSTASH_NEXT_SIGNING_KEY!,
  });

 // validates but also provides a strongly-typed `event` I can handle just how I expect.
  const event = await receiver.constructEvent(req);

  const ctx = await createTRPCContext({ req, res });
  const caller = appRouter.createCaller(ctx);

  // I've defined the `downloadArticlesOfAssociation` mutation with a zod schema for an input so that I can still rely on my own validation
  return caller.mandate.downloadArticlesOfAssociation({ event });
};

NB: The types here are missing, but it would make for a nicer DX imho

chronark commented 1 year ago

Really good idea

Let's add this along side of verify though in order to not break anyone's code wdyt?

Also instead of returning any, we can make it generic to allow for better typing

ogzhanolguncu commented 7 months ago

@p6l-richard, like @chronark said it would be better to add this as an addition to verify. If you're open to making these adjustments, everything seems to be in order.