From cd8323d4db430356ebe9a468225c5df711483217 Mon Sep 17 00:00:00 2001 From: Victor Afanassieff Date: Wed, 24 Apr 2024 13:14:10 +0200 Subject: [PATCH] feat: use trpc 11 --- package-lock.json | 84 ++++++++++++++-------------------------- package.json | 9 +++-- src/createTRPCMsw.ts | 3 +- src/types.ts | 75 +++++++++++++++-------------------- test/integration.test.ts | 2 +- test/setup.ts | 11 +++--- 6 files changed, 76 insertions(+), 108 deletions(-) diff --git a/package-lock.json b/package-lock.json index 32f16a5..6ed8e79 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,17 +1,17 @@ { "name": "msw-trpc", - "version": "2.0.0-beta.0", + "version": "2.0.0-beta.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "msw-trpc", - "version": "2.0.0-beta.0", + "version": "2.0.0-beta.1", "license": "MIT", "devDependencies": { - "@trpc/client": "^10.44.1", - "@trpc/react-query": "^10.44.1", - "@trpc/server": "^10.44.1", + "@trpc/client": "11.0.0-rc.351", + "@trpc/react-query": "11.0.0-rc.351", + "@trpc/server": "11.0.0-rc.351", "@types/jest": "^29.2.6", "expect-type": "^0.15.0", "jest": "^29.3.1", @@ -26,7 +26,7 @@ "zod": "^3.22.4" }, "peerDependencies": { - "@trpc/server": ">=10.9.0", + "@trpc/server": ">=11.0.0-rc.351", "msw": "^2.0.0" } }, @@ -1120,9 +1120,9 @@ } }, "node_modules/@tanstack/query-core": { - "version": "4.36.1", - "resolved": "https://registry.npmjs.org/@tanstack/query-core/-/query-core-4.36.1.tgz", - "integrity": "sha512-DJSilV5+ytBP1FbFcEJovv4rnnm/CokuVvrBEtW/Va9DvuJ3HksbXUJEpI0aV1KtuL4ZoO9AVE6PyNLzF7tLeA==", + "version": "5.32.0", + "resolved": "https://registry.npmjs.org/@tanstack/query-core/-/query-core-5.32.0.tgz", + "integrity": "sha512-Z3flEgCat55DRXU5UMwYU1U+DgFZKA3iufyOKs+II7iRAo0uXkeU7PH5e6sOH1CGEag0IpKmZxlUFpCg6roSKw==", "dev": true, "peer": true, "funding": { @@ -1131,31 +1131,20 @@ } }, "node_modules/@tanstack/react-query": { - "version": "4.36.1", - "resolved": "https://registry.npmjs.org/@tanstack/react-query/-/react-query-4.36.1.tgz", - "integrity": "sha512-y7ySVHFyyQblPl3J3eQBWpXZkliroki3ARnBKsdJchlgt7yJLRDUcf4B8soufgiYt3pEQIkBWBx1N9/ZPIeUWw==", + "version": "5.32.0", + "resolved": "https://registry.npmjs.org/@tanstack/react-query/-/react-query-5.32.0.tgz", + "integrity": "sha512-+E3UudQtarnx9A6xhpgMZapyF+aJfNBGFMgI459FnduEZqT/9KhOWnMOneZahLRt52yzskSA0AuOyLkXHK0yBA==", "dev": true, "peer": true, "dependencies": { - "@tanstack/query-core": "4.36.1", - "use-sync-external-store": "^1.2.0" + "@tanstack/query-core": "5.32.0" }, "funding": { "type": "github", "url": "https://github.com/sponsors/tannerlinsley" }, "peerDependencies": { - "react": "^16.8.0 || ^17.0.0 || ^18.0.0", - "react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0", - "react-native": "*" - }, - "peerDependenciesMeta": { - "react-dom": { - "optional": true - }, - "react-native": { - "optional": true - } + "react": "^18.0.0" } }, "node_modules/@tootallnate/once": { @@ -1168,44 +1157,41 @@ } }, "node_modules/@trpc/client": { - "version": "10.44.1", - "resolved": "https://registry.npmjs.org/@trpc/client/-/client-10.44.1.tgz", - "integrity": "sha512-vTWsykNcgz1LnwePVl2fKZnhvzP9N3GaaLYPkfGINo314ZOS0OBqe9x0ytB2LLUnRVTAAZ2WoONzARd8nHiqrA==", + "version": "11.0.0-rc.351", + "resolved": "https://registry.npmjs.org/@trpc/client/-/client-11.0.0-rc.351.tgz", + "integrity": "sha512-48LKVO6pSk6/NSKmwZ1ixzQm/S3DD8iAI5j+L9Pqou/IPbS52qJepaCWd1bTWO+iDgXa9MxcsD0Gr8/IL5TznQ==", "dev": true, "funding": [ "https://trpc.io/sponsor" ], "peerDependencies": { - "@trpc/server": "10.44.1" + "@trpc/server": "11.0.0-rc.351+a9b03d7f3" } }, "node_modules/@trpc/react-query": { - "version": "10.44.1", - "resolved": "https://registry.npmjs.org/@trpc/react-query/-/react-query-10.44.1.tgz", - "integrity": "sha512-Sgi/v0YtdunOXjBRi7om9gILGkOCFYXPzn5KqLuEHiZw5dr5w4qGHFwCeMAvndZxmwfblJrl1tk2AznmsVu8MA==", + "version": "11.0.0-rc.351", + "resolved": "https://registry.npmjs.org/@trpc/react-query/-/react-query-11.0.0-rc.351.tgz", + "integrity": "sha512-kEhXm/B1m2yIJ3ZiI2MdTHCzveoDBH03HdjD07pSoKuiXfDo221sX4Az9YaZ1wSwCFIt6L03Z5OopOhlxhK3ww==", "dev": true, "funding": [ "https://trpc.io/sponsor" ], "peerDependencies": { - "@tanstack/react-query": "^4.18.0", - "@trpc/client": "10.44.1", - "@trpc/server": "10.44.1", - "react": ">=16.8.0", - "react-dom": ">=16.8.0" + "@tanstack/react-query": "^5.25.0", + "@trpc/client": "11.0.0-rc.351+a9b03d7f3", + "@trpc/server": "11.0.0-rc.351+a9b03d7f3", + "react": ">=18.2.0", + "react-dom": ">=18.2.0" } }, "node_modules/@trpc/server": { - "version": "10.44.1", - "resolved": "https://registry.npmjs.org/@trpc/server/-/server-10.44.1.tgz", - "integrity": "sha512-mF7B+K6LjuboX8I1RZgKE5GA/fJhsJ8tKGK2UBt3Bwik7hepEPb4NJgNr7vO6BK5IYwPdBLRLTctRw6XZx0sRg==", + "version": "11.0.0-rc.351", + "resolved": "https://registry.npmjs.org/@trpc/server/-/server-11.0.0-rc.351.tgz", + "integrity": "sha512-Y1Iw9iiF9LCJ2ldS/af+2RXzIbgWvQ4DHVVgHYOGB+uINpAugsUmogoEkZ6rYOgusNz6qkRzY8JKOSR7FQ1aUg==", "dev": true, "funding": [ "https://trpc.io/sponsor" - ], - "engines": { - "node": ">=18.0.0" - } + ] }, "node_modules/@tsconfig/node10": { "version": "1.0.9", @@ -5196,16 +5182,6 @@ "requires-port": "^1.0.0" } }, - "node_modules/use-sync-external-store": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/use-sync-external-store/-/use-sync-external-store-1.2.0.tgz", - "integrity": "sha512-eEgnFxGQ1Ife9bzYs6VLi8/4X6CObHMw9Qr9tPY43iKwsPw8xE8+EFsf/2cFZ5S3esXgpWgtSCtLNS41F+sKPA==", - "dev": true, - "peer": true, - "peerDependencies": { - "react": "^16.8.0 || ^17.0.0 || ^18.0.0" - } - }, "node_modules/util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", diff --git a/package.json b/package.json index 4cd179e..86bed90 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "scripts": { "prepublish": "npm run build", "test": "tsc --noEmit && jest", + "format": "prettier --write ./src/**/*.ts ./test/**/*.ts", "build": "rimraf dist && tsc -p tsconfig.build.json" }, "repository": { @@ -26,9 +27,9 @@ }, "homepage": "https://github.com/maloguertin/msw-trpc#readme", "devDependencies": { - "@trpc/client": "^10.44.1", - "@trpc/react-query": "^10.44.1", - "@trpc/server": "^10.44.1", + "@trpc/client": "11.0.0-rc.351", + "@trpc/react-query": "11.0.0-rc.351", + "@trpc/server": "11.0.0-rc.351", "@types/jest": "^29.2.6", "expect-type": "^0.15.0", "jest": "^29.3.1", @@ -43,7 +44,7 @@ "zod": "^3.22.4" }, "peerDependencies": { - "@trpc/server": ">=10.9.0", + "@trpc/server": ">=11.0.0-rc.351", "msw": "^2.0.0" } } diff --git a/src/createTRPCMsw.ts b/src/createTRPCMsw.ts index 82b1c4c..7517b74 100644 --- a/src/createTRPCMsw.ts +++ b/src/createTRPCMsw.ts @@ -1,4 +1,5 @@ -import { AnyRouter, CombinedDataTransformer, TRPCError, defaultTransformer } from '@trpc/server' +import { AnyRouter, CombinedDataTransformer, TRPCError } from '@trpc/server' +import { defaultTransformer } from '@trpc/server/unstable-core-do-not-import' import { getHTTPStatusCodeFromError } from '@trpc/server/http' import { HttpResponse, http } from 'msw' diff --git a/src/types.ts b/src/types.ts index 282435c..5b51fb8 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,57 +1,46 @@ -import { AnyRouter, BuildProcedure, Procedure, ProcedureParams, ProcedureType, inferRouterInputs } from '@trpc/server' -import { DefaultBodyType, RequestHandler } from 'msw' -import { RequestHandlerDefaultInfo } from 'msw/lib/core/handlers/RequestHandler' +import type { + inferProcedureInput, + inferProcedureOutput, + TRPCMutationProcedure, + TRPCQueryProcedure, + TRPCSubscriptionProcedure, +} from '@trpc/server' +import { RequestHandler } from 'msw' + +type PromiseOrValue = T | Promise + +type Any = any + +type Router = Record | TRPCMutationProcedure | TRPCSubscriptionProcedure> export type ExtractKeys = T[K] extends - | BuildProcedure<'query', any, any> - | BuildProcedure<'mutation', any, any> - | AnyRouter + | TRPCQueryProcedure + | TRPCMutationProcedure + | Router ? K : never -export type ExtractInput = T extends ProcedureParams - ? P extends DefaultBodyType - ? P - : DefaultBodyType +export type Query = T[K] extends TRPCQueryProcedure + ? { + query: ( + handler: (input: inferProcedureInput) => PromiseOrValue> + ) => RequestHandler + } : never -export type ExtractOutput = T extends Procedure - ? ProcedureParams['_output_out'] extends void - ? void - : ProcedureParams['_output_out'] extends DefaultBodyType - ? ProcedureParams['_output_out'] - : never +export type Mutation = T[K] extends TRPCMutationProcedure + ? { + mutation: ( + handler: (input: inferProcedureInput) => PromiseOrValue> + ) => RequestHandler + } : never -export type WithInput = T[K] extends BuildProcedure - ? T extends AnyRouter - ? inferRouterInputs[K] - : never - : never - -type PromiseOrValue = T | Promise - -export type SetQueryHandler = ( - handler: (input: WithInput) => PromiseOrValue> -) => RequestHandler - -export type SetMutationHandler = ( - handler: (input: WithInput) => PromiseOrValue> -) => RequestHandler - -export type Query = { - query: SetQueryHandler -} - -export type Mutation = { - mutation: SetMutationHandler -} - -type ExtractProcedureHandler = T[K] extends BuildProcedure<'mutation', any, any> +type ExtractProcedureHandler = T[K] extends TRPCMutationProcedure ? Mutation - : T[K] extends BuildProcedure<'query', any, any> + : T[K] extends TRPCQueryProcedure ? Query - : T[K] extends AnyRouter + : T[K] extends Router ? MswTrpc : never diff --git a/test/integration.test.ts b/test/integration.test.ts index 0499146..d3db360 100644 --- a/test/integration.test.ts +++ b/test/integration.test.ts @@ -20,7 +20,7 @@ type NestedMswTrpc = typeof nestedMswTrpc const setupServerWithQueries = (mswTrpc: MswTrpc, nestedMswTrpc: NestedMswTrpc) => { return setupServer( - mswTrpc.userById.query(() => { + mswTrpc.userById.query((toto: string) => { return { id: '1', name: 'Malo' } }), mswTrpc.userByIdAndPost.query(() => { diff --git a/test/setup.ts b/test/setup.ts index 0f30d2b..9beef81 100644 --- a/test/setup.ts +++ b/test/setup.ts @@ -1,9 +1,10 @@ import createTRPCMsw from '../src/createTRPCMsw' import { initTRPC } from '@trpc/server' -import { createTRPCProxyClient, httpBatchLink, httpLink } from '@trpc/client' +import { createTRPCClient, httpLink } from '@trpc/client' import superjson from 'superjson' const t = initTRPC.create() + const tWithSuperJson = initTRPC.create({ transformer: superjson, }) @@ -183,7 +184,7 @@ const nestedRouter = t.router({ export type NestedAppRouter = typeof nestedRouter -export const trpc = createTRPCProxyClient({ +export const trpc = createTRPCClient({ links: [ httpLink({ url: 'http://localhost:3000/trpc', @@ -196,11 +197,11 @@ export const trpc = createTRPCProxyClient({ ], }) -export const trpcWithSuperJson = createTRPCProxyClient({ - transformer: superjson, +export const trpcWithSuperJson = createTRPCClient({ links: [ httpLink({ url: 'http://localhost:3000/trpc', + transformer: superjson, headers() { return { 'content-type': 'application/json', @@ -210,7 +211,7 @@ export const trpcWithSuperJson = createTRPCProxyClient({ ], }) -export const nestedTrpc = createTRPCProxyClient({ +export const nestedTrpc = createTRPCClient({ links: [ httpLink({ url: 'http://localhost:3000/trpc',