From 5d3124a115dc44099fc681eec9592636374f85b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Louren=C3=A7o?= Date: Thu, 8 Jun 2023 23:33:15 -0300 Subject: [PATCH] feat(trpc): bump support for 10.x --- package-lock.json | 12 ++-- package.json | 4 +- src/frameworks/trpc/trpc.framework.ts | 14 +++-- test/frameworks/body-parser.framework.spec.ts | 15 ++--- test/frameworks/cors.framework.spec.ts | 10 +-- test/frameworks/trpc.framework.spec.ts | 63 ++++++++++--------- test/handlers/huawei.handler.spec.ts | 13 ++-- 7 files changed, 68 insertions(+), 63 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4fddf874..e0e6ad4a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26,7 +26,7 @@ "@semantic-release/exec": "^6.0.3", "@semantic-release/git": "^10.0.1", "@semantic-release/github": "^9.0.2", - "@trpc/server": "^9.27.4", + "@trpc/server": "^10.29.1", "@types/aws-lambda": "^8.10.115", "@types/body-parser": "^1.19.2", "@types/cors": "^2.8.13", @@ -74,7 +74,7 @@ "@deepkit/http": ">= 1.0.1-alpha.94", "@google-cloud/functions-framework": ">= 3.0.0", "@hapi/hapi": ">= 21.0.0", - "@trpc/server": ">= 9.0.0", + "@trpc/server": ">= 10.0.0", "@types/aws-lambda": ">= 8.10.92", "@types/body-parser": ">= 1.19.2", "@types/cors": ">= 2.8.12", @@ -3929,9 +3929,13 @@ } }, "node_modules/@trpc/server": { - "version": "9.27.4", + "version": "10.29.1", + "resolved": "https://registry.npmjs.org/@trpc/server/-/server-10.29.1.tgz", + "integrity": "sha512-kNXgMh5ya+awuz2tB4eIyVrRs7nVtqGXwSGabzH3l5ZLWz7rbKJquOJ7h6bjvIfWUpaFG62HJNWxxGUtXCRgRw==", "dev": true, - "license": "MIT" + "funding": [ + "https://trpc.io/sponsor" + ] }, "node_modules/@tsconfig/node10": { "version": "1.0.8", diff --git a/package.json b/package.json index 8f1905c9..23cc1d11 100644 --- a/package.json +++ b/package.json @@ -92,7 +92,7 @@ "@semantic-release/exec": "^6.0.3", "@semantic-release/git": "^10.0.1", "@semantic-release/github": "^9.0.2", - "@trpc/server": "^9.27.4", + "@trpc/server": "^10.29.1", "@types/aws-lambda": "^8.10.115", "@types/body-parser": "^1.19.2", "@types/cors": "^2.8.13", @@ -137,7 +137,7 @@ "@deepkit/http": ">= 1.0.1-alpha.94", "@google-cloud/functions-framework": ">= 3.0.0", "@hapi/hapi": ">= 21.0.0", - "@trpc/server": ">= 9.0.0", + "@trpc/server": ">= 10.0.0", "@types/aws-lambda": ">= 8.10.92", "@types/body-parser": ">= 1.19.2", "@types/cors": ">= 2.8.12", diff --git a/src/frameworks/trpc/trpc.framework.ts b/src/frameworks/trpc/trpc.framework.ts index d3219845..5f423da7 100644 --- a/src/frameworks/trpc/trpc.framework.ts +++ b/src/frameworks/trpc/trpc.framework.ts @@ -134,7 +134,7 @@ export type TrpcAdapterContext = TContext & TrpcAdapterBaseContext; * @public */ export type TrpcFrameworkOptions = Omit< - NodeHTTPHandlerOptions, IncomingMessage, ServerResponse>, + NodeHTTPHandlerOptions, 'router' | 'createContext' > & { createContext?: ( @@ -150,8 +150,10 @@ export type TrpcFrameworkOptions = Omit< * @breadcrumb Frameworks / TrpcFramework * @public */ -export class TrpcFramework - implements FrameworkContract> +export class TrpcFramework< + TContext extends TrpcAdapterBaseContext, + TRouter extends AnyRouter = AnyRouter, +> implements FrameworkContract { //#region Constructor @@ -167,8 +169,8 @@ export class TrpcFramework /** * {@inheritDoc} */ - public sendRequest( - app: AnyRouter, + public sendRequest( + app: TRouter, request: IncomingMessage, response: ServerResponse, ): void { @@ -222,7 +224,7 @@ export class TrpcFramework >, ): TContext | Promise { const createContextFromOptions: NodeHTTPCreateContextFn< - AnyRouter>, + AnyRouter, IncomingMessage, ServerResponse > = getDefaultIfUndefined( diff --git a/test/frameworks/body-parser.framework.spec.ts b/test/frameworks/body-parser.framework.spec.ts index 532bba83..864cf2ab 100644 --- a/test/frameworks/body-parser.framework.spec.ts +++ b/test/frameworks/body-parser.framework.spec.ts @@ -366,11 +366,12 @@ describe('BodyParserFramework', () => { : it; itFn(bodyParserTest.name, async () => { - const app = trpc - .router>() - .mutation('body', { - input: inp => inp, - resolve: ctx => { + const t = trpc.initTRPC.context>().create(); + + const app = t.router({ + body: t.procedure + .input(inp => inp) + .mutation(ctx => { const body = (ctx.ctx.request as any).body; if (bodyParserTest.expectedBody) @@ -378,8 +379,8 @@ describe('BodyParserFramework', () => { else expect(body).not.toEqual(bodyParserTest.notExpectedBody); return 'ok'; - }, - }); + }), + }); await handleRestExpects(app, new TrpcFramework(), bodyParserTest); }); diff --git a/test/frameworks/cors.framework.spec.ts b/test/frameworks/cors.framework.spec.ts index 10095aad..1799d6e7 100644 --- a/test/frameworks/cors.framework.spec.ts +++ b/test/frameworks/cors.framework.spec.ts @@ -215,7 +215,7 @@ async function handleRestExpects( else expect(spySendRequest).not.toHaveBeenCalled(); } -describe(CorsFramework.name, () => { +describe('CorsFramework', () => { describe('express', () => { for (const corsTest of corsOptions) { it(`${corsTest.method}: ${corsTest.name}`, async () => { @@ -275,10 +275,12 @@ describe(CorsFramework.name, () => { describe('trpc', () => { for (const corsTest of corsOptions) { it(`${corsTest.method}: ${corsTest.name}`, async () => { - const app = trpc.router(); + const t = trpc.initTRPC.create(); - app.query('/', { - resolve: () => 'ok', + const app = t.router({ + ['/']: t.procedure.query(() => { + return 'ok'; + }), }); await handleRestExpects(app, new TrpcFramework(), corsTest); diff --git a/test/frameworks/trpc.framework.spec.ts b/test/frameworks/trpc.framework.spec.ts index b06a8526..3baeb6d8 100644 --- a/test/frameworks/trpc.framework.spec.ts +++ b/test/frameworks/trpc.framework.spec.ts @@ -19,12 +19,11 @@ type TrpcContext = TrpcAdapterContext; function createHandler( method: 'get' | 'post' | 'delete' | 'put', -): TestRouteBuilderHandler, AnyRouter> { +): TestRouteBuilderHandler, AnyRouter> { return (app, path, handler) => { if (method === 'get') { - return app.query(path, { - input: inp => inp, - resolve: ({ ctx, input }) => { + return app.router({ + [path]: app.procedure.query(({ ctx, input }) => { const [statusCode, resultBody, headers] = handler( ctx.getHeaders(), input, @@ -36,38 +35,39 @@ function createHandler( ctx.setStatus(statusCode); return resultBody; - }, + }), }); } else { - return app.mutation(path, { - input: inp => inp, - resolve: ({ ctx, input }) => { - const [statusCode, resultBody, headers] = handler( - ctx.getHeaders(), - input, - ); + return app.router({ + [path]: app.procedure + .input(inp => inp) + .mutation(({ ctx, input }) => { + const [statusCode, resultBody, headers] = handler( + ctx.getHeaders(), + input, + ); - for (const header of Object.keys(headers)) - ctx.setHeader(header, headers[header]); + for (const header of Object.keys(headers)) + ctx.setHeader(header, headers[header]); - ctx.setStatus(statusCode); + ctx.setStatus(statusCode); - return resultBody; - }, + return resultBody; + }), }); } }; } function createRouter() { - return trpc.router(); + return trpc.initTRPC.context().create(); } const validTestOptions = frameworkTestOptions.filter( ([method]) => method === 'post' || method === 'get', ); -describe(TrpcFramework.name, () => { +describe('TrpcFramework', () => { for (const [ method, path, @@ -138,11 +138,11 @@ describe(TrpcFramework.name, () => { const resultBody = ServerlessResponse.body(response); - expect(resultBody.toString('utf-8')).toEqual( + expect( expectedValue !== undefined ? expectedValue - : JSON.stringify({ id: null, result: { type: 'data', data: body } }), - ); + : JSON.stringify({ result: { data: body } }), + ).toEqual(resultBody.toString('utf-8')); expect(response.statusCode).toBe(statusCode); expect(ServerlessResponse.headers(response)).toHaveProperty( 'response-header', @@ -156,13 +156,13 @@ describe(TrpcFramework.name, () => { type CustomContext = TrpcAdapterContext; const currentDate = new Date(); - - const app = trpc.router().query('test', { - resolve: function ({ ctx }) { + const t = trpc.initTRPC.context().create(); + const app = t.router({ + test: t.procedure.query(function ({ ctx }) { expect(ctx).toHaveProperty('currentDate'); ctx.setStatus(201); - }, + }), }); const request = new ServerlessRequest({ @@ -201,8 +201,7 @@ describe(TrpcFramework.name, () => { const secondResultBody = ServerlessResponse.body(secondResponse); const emptyResponse = JSON.stringify({ - id: null, - result: { type: 'data' }, + result: {}, }); expect(firstResultBody.toString('utf-8')).toEqual(emptyResponse); @@ -210,8 +209,9 @@ describe(TrpcFramework.name, () => { }); it('should correctly send default methods inside context', async () => { - const app = createRouter().query('test', { - resolve: function ({ ctx }) { + const t = createRouter(); + const app = t.router({ + test: t.procedure.query(({ ctx }) => { expect(ctx.request).toBeDefined(); expect(ctx.response).toBeDefined(); @@ -225,8 +225,9 @@ describe(TrpcFramework.name, () => { ctx.setStatus(204); ctx.setHeader('test2', 'batata'); ctx.removeHeader('test2'); - }, + }), }); + const framework = new TrpcFramework>(); const request = new ServerlessRequest({ diff --git a/test/handlers/huawei.handler.spec.ts b/test/handlers/huawei.handler.spec.ts index a9d7820a..dbb9d027 100644 --- a/test/handlers/huawei.handler.spec.ts +++ b/test/handlers/huawei.handler.spec.ts @@ -1,6 +1,6 @@ import * as http from 'http'; -import { describe, expect, it, vitest } from 'vitest'; import supertest from 'supertest'; +import { describe, expect, it, vitest } from 'vitest'; import { ILogger } from '../../src'; import { DummyAdapter } from '../../src/adapters/dummy'; import { @@ -10,7 +10,7 @@ import { import { DummyResolver } from '../../src/resolvers/dummy'; import { FrameworkMock } from '../mocks/framework.mock'; -describe(HttpHuaweiHandler.name, () => { +describe('HttpHuaweiHandler', () => { const app = null; const response = { batata: true }; @@ -28,7 +28,7 @@ describe(HttpHuaweiHandler.name, () => { it('should create correctly mocked server and test default constants', async () => { const listenMock = vitest.fn(); - const closeMock = vitest.fn(); + const closeMock = vitest.fn(callback => callback()); const addEventListenerMock = vitest.fn(); const createServerMock = vitest.fn( () => @@ -55,12 +55,7 @@ describe(HttpHuaweiHandler.name, () => { logger, ); - expect(addEventListenerMock).toHaveBeenCalledWith( - 'request', - expect.any(Function), - ); - - expect(createServerMock).toHaveBeenCalledWith(app, framework); + expect(createServerMock).toHaveBeenCalledWith(expect.any(Function)); expect(listenMock).toHaveBeenCalledWith( DEFAULT_HUAWEI_LISTEN_PORT, expect.any(Function),