qstash-js icon indicating copy to clipboard operation
qstash-js copied to clipboard

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

Open p6l-richard opened this issue 1 year ago • 2 comments

Changes

  • renames the verify method to constructEvent (similar to stripe's api)
  • now returns the payload

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

p6l-richard avatar Jun 13 '23 09:06 p6l-richard

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

chronark avatar Jun 13 '23 09:06 chronark

@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.

ogzhanolguncu avatar Nov 14 '23 13:11 ogzhanolguncu