Skip to content

Commit

Permalink
feat: use trpc 11
Browse files Browse the repository at this point in the history
  • Loading branch information
vafanassieff committed Apr 24, 2024
1 parent 1f60386 commit cd8323d
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 108 deletions.
84 changes: 30 additions & 54 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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",
Expand All @@ -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"
}
}
3 changes: 2 additions & 1 deletion src/createTRPCMsw.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
75 changes: 32 additions & 43 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -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> = T | Promise<T>

type Any = any

type Router = Record<string, TRPCQueryProcedure<Any> | TRPCMutationProcedure<Any> | TRPCSubscriptionProcedure<Any>>

export type ExtractKeys<T, K extends keyof T = keyof T> = T[K] extends
| BuildProcedure<'query', any, any>
| BuildProcedure<'mutation', any, any>
| AnyRouter
| TRPCQueryProcedure<Any>
| TRPCMutationProcedure<Any>
| Router
? K
: never

export type ExtractInput<T extends ProcedureParams> = T extends ProcedureParams<any, any, any, infer P>
? P extends DefaultBodyType
? P
: DefaultBodyType
export type Query<T, K extends keyof T> = T[K] extends TRPCQueryProcedure<Any>
? {
query: (
handler: (input: inferProcedureInput<T[K]>) => PromiseOrValue<inferProcedureOutput<T[K]>>
) => RequestHandler
}
: never

export type ExtractOutput<T> = T extends Procedure<ProcedureType, infer ProcedureParams>
? ProcedureParams['_output_out'] extends void
? void
: ProcedureParams['_output_out'] extends DefaultBodyType
? ProcedureParams['_output_out']
: never
export type Mutation<T, K extends keyof T> = T[K] extends TRPCMutationProcedure<Any>
? {
mutation: (
handler: (input: inferProcedureInput<T[K]>) => PromiseOrValue<inferProcedureOutput<T[K]>>
) => RequestHandler
}
: never

export type WithInput<T, K extends keyof T = keyof T> = T[K] extends BuildProcedure<any, any, any>
? T extends AnyRouter
? inferRouterInputs<T>[K]
: never
: never

type PromiseOrValue<T> = T | Promise<T>

export type SetQueryHandler<T, K extends keyof T> = (
handler: (input: WithInput<T, K>) => PromiseOrValue<ExtractOutput<T[K]>>
) => RequestHandler

export type SetMutationHandler<T, K extends keyof T> = (
handler: (input: WithInput<T, K>) => PromiseOrValue<ExtractOutput<T[K]>>
) => RequestHandler

export type Query<T, K extends keyof T> = {
query: SetQueryHandler<T, K>
}

export type Mutation<T, K extends keyof T> = {
mutation: SetMutationHandler<T, K>
}

type ExtractProcedureHandler<T, K extends keyof T> = T[K] extends BuildProcedure<'mutation', any, any>
type ExtractProcedureHandler<T, K extends keyof T> = T[K] extends TRPCMutationProcedure<Any>
? Mutation<T, K>
: T[K] extends BuildProcedure<'query', any, any>
: T[K] extends TRPCQueryProcedure<Any>
? Query<T, K>
: T[K] extends AnyRouter
: T[K] extends Router
? MswTrpc<T[K]>
: never

Expand Down
2 changes: 1 addition & 1 deletion test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
11 changes: 6 additions & 5 deletions test/setup.ts
Original file line number Diff line number Diff line change
@@ -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,
})
Expand Down Expand Up @@ -183,7 +184,7 @@ const nestedRouter = t.router({

export type NestedAppRouter = typeof nestedRouter

export const trpc = createTRPCProxyClient<AppRouter>({
export const trpc = createTRPCClient<AppRouter>({
links: [
httpLink({
url: 'http://localhost:3000/trpc',
Expand All @@ -196,11 +197,11 @@ export const trpc = createTRPCProxyClient<AppRouter>({
],
})

export const trpcWithSuperJson = createTRPCProxyClient<AppRouterWithSuperJson>({
transformer: superjson,
export const trpcWithSuperJson = createTRPCClient<AppRouterWithSuperJson>({
links: [
httpLink({
url: 'http://localhost:3000/trpc',
transformer: superjson,
headers() {
return {
'content-type': 'application/json',
Expand All @@ -210,7 +211,7 @@ export const trpcWithSuperJson = createTRPCProxyClient<AppRouterWithSuperJson>({
],
})

export const nestedTrpc = createTRPCProxyClient<NestedAppRouter>({
export const nestedTrpc = createTRPCClient<NestedAppRouter>({
links: [
httpLink({
url: 'http://localhost:3000/trpc',
Expand Down

0 comments on commit cd8323d

Please sign in to comment.