From ab6c62b04140284356591ee09e6dd3fcaef6f33a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Louren=C3=A7o?= Date: Fri, 9 Jun 2023 19:21:51 -0300 Subject: [PATCH] docs(www): update documentation for trpc 10.x --- www/docs/main/frameworks/trpc.mdx | 54 ++++++++++++++----------------- 1 file changed, 25 insertions(+), 29 deletions(-) diff --git a/www/docs/main/frameworks/trpc.mdx b/www/docs/main/frameworks/trpc.mdx index 4876053e..9d38b318 100644 --- a/www/docs/main/frameworks/trpc.mdx +++ b/www/docs/main/frameworks/trpc.mdx @@ -30,25 +30,22 @@ import { z } from 'zod'; type CustomContext = { currentDate: Date }; type TrpcContext = TrpcAdapterContext; -const appRouter = trpc - .router() - .query('getUser', { - input: z.string(), - async resolve(req) { - req.input; // string - return { id: req.input, name: 'Bilbo' }; - }, - }) - .mutation('createUser', { +const t = trpc.initTRPC.context().create(); +const appRouter = t.router({ + getUser: t.procedure.input(z.string()).query((req) => { + req.input; // string + return { id: req.input, name: 'Bilbo' }; + }), + createUser: t.procedure // validate input with Zod - input: z.object({ name: z.string().min(5) }), - async resolve({ input }) { + .input(z.object({ name: z.string().min(5) })) + .mutation(({ input }) => { return { created: true, newName: input.name, }; - }, - }); + }) +}); const frameworkOptions: TrpcFrameworkOptions = { createContext: () => ({ currentDate: new Date() }), @@ -93,13 +90,12 @@ import { z } from 'zod'; type TrpcContext = TrpcAdapterContext; -const appRouter = trpc - .router() - .mutation('sqs', { - input: z.object({ - Records: z.array(z.any()), - }), - async resolve({ input, ctx }) { +const t = trpc.initTRPC.context().create(); + +const appRouter = t.router({ + sqs: t.procedure + .input(z.object({Records: z.array(z.any())})) + .mutation(({ctx, input}) => { if (ctx.getHeader('host') !== 'sqs.amazonaws.com') throw new TRPCError({ code: 'UNAUTHORIZED', @@ -110,8 +106,8 @@ const appRouter = trpc // Do whatever you want // and you dont need to return nothing - }, - }); + }), +}); ``` :::warning About another adapters @@ -144,17 +140,17 @@ import { CorsFramework } from '@h4ad/serverless-adapter/lib/frameworks/cors'; type CustomContext = { potato: boolean }; type TrpcContext = TrpcAdapterContext; -const appRouter = trpc - .router() - .mutation('add', { - async resolve({ ctx }) { +const t = trpc.initTRPC.context().create(); +const appRouter = t.router({ + add: t.procedure + .mutation(({ ctx }) => { // get the request url const requestUrl = ctx.getUrl(); // this will change the status code of the request to 204. ctx.setStatus(204); - }, - }); + }), +}); const frameworkOptions: TrpcFrameworkOptions = { // you can return a promise