triggerdotdev / trigger.dev

Trigger.dev is the open source background jobs platform.
https://trigger.dev/changelog
Apache License 2.0
8.37k stars 495 forks source link

[TRI-1772] Allow defining Zod schemas for HTTP Endpoints #799

Open ericallam opened 8 months ago

ericallam commented 8 months ago

It would be nice to be able to attach a Zod schema on a HTTP endpoint which would then correctly type the results of request.json:

const endpoint = client.defineHttpEndpoint({
  id: "tRPC",
  source: "unkey.dev",
  verify: async (req) => {
     // verify here
  },
  schema: z.object({ }) // schema here
})

TRI-1772

PradeepJoshiFreelancer commented 7 months ago

Hi @ericallam ,

I wanted to give this a try, but had few queries. Can you please let me know:

  1. Where can I find the schemas for request.json for different calls.
  2. Do we need to implement the validation similar to verifyRequestSignature method. If yes, Can we return { success: false, reason: "Schema validation failed." } or is there any other message to return.
  3. There is an existing Zod type "RequestWithRawBodySchema" can we use the same to infer in EndpointOptions or should we keep it generic z.ZodType<z.ZodSchema>
  4. If you can provide an access to TRI-1772

Can you please review if I am on right trace? https://github.com/triggerdotdev/trigger.dev/compare/main...PradeepJoshiFreelancer:trigger.dev:TRI-1772?expand=1

Thanks