Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ctx.data arguments type with output parser #21

Merged
merged 1 commit into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AnyRouter, BuildProcedure, ProcedureParams, inferRouterInputs } from '@trpc/server'
import { AnyRouter, BuildProcedure, Procedure, ProcedureParams, ProcedureType, inferRouterInputs } from '@trpc/server'
import {
DefaultBodyType,
MockedRequest,
Expand All @@ -11,7 +11,9 @@ import {
} from 'msw'

export type ContextWithDataTransformer<T> = RestContext & {
data: (data: T extends BuildProcedure<any, any, infer P> ? P : never) => ResponseTransformer<DefaultBodyType, any>
data: (
data: T extends Procedure<ProcedureType, infer ProcedureParams> ? ProcedureParams['_output_out'] : never
) => ResponseTransformer<DefaultBodyType, any>
}

export type ExtractKeys<T, K extends keyof T = keyof T> = T[K] extends
Expand Down
30 changes: 30 additions & 0 deletions test/createTRPCMsw.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,34 @@ describe('proxy returned by createMswTrpc', () => {
>()
})
})

describe('with output transformer', () => {
it('query context.data should consider output transformer', () => {
expectTypeOf(mswTrpc.userByName.query).toEqualTypeOf<
(
handler: ResponseResolver<
RestRequest<never, PathParams<string>> & { getInput: () => string },
RestContext & {
data: (data: User | undefined) => ResponseTransformer<DefaultBodyType, any>
},
DefaultBodyType
>
) => RestHandler<MockedRequest<DefaultBodyType>>
>()
})

it('mutation context.data should consider output transformer', () => {
expectTypeOf(mswTrpc.updateUser.mutation).toEqualTypeOf<
(
handler: ResponseResolver<
RestRequest<User, PathParams> & { getInput: () => Promise<User> },
RestContext & {
data: (data: User) => ResponseTransformer<DefaultBodyType, any>
},
DefaultBodyType
>
) => RestHandler<MockedRequest<DefaultBodyType>>
>()
})
})
})
52 changes: 52 additions & 0 deletions test/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,31 @@ const appRouter = t.router({

return { ...user, posts: ['1'] }
}),
userByName: t.procedure
.input((val: unknown) => {
if (typeof val === 'string') return val

throw new Error(`Invalid input: ${typeof val}`)
})
.output((val: unknown) => {
if (typeof val === 'undefined') {
return val
}
if (typeof val !== 'object' || val === null) {
throw new Error(`Invalid output: ${typeof val}`)
}
if (!('id' in val) || typeof val.id !== 'string' || !('name' in val) || typeof val.name !== 'string') {
throw new Error(`Invalid output: ${typeof val}`)
}
return val as User
})
.query(req => {
const { input } = req

const user = userList.find(u => u.name === input)

return user
}),
createUser: t.procedure
.input((val: unknown) => {
if (typeof val === 'string') return val
Expand All @@ -62,6 +87,33 @@ const appRouter = t.router({
name: input,
} as User
}),
updateUser: t.procedure
.input((val: unknown) => {
if (typeof val !== 'object' || val === null) {
throw new Error(`Invalid input: ${typeof val}`)
}
if (!('id' in val) || !('name' in val)) {
throw new Error(`Invalid input: ${typeof val}`)
}
if (typeof val.id !== 'string' || typeof val.name !== 'string') {
throw new Error(`Invalid input: ${typeof val}`)
}
return val as User
})
.output((val: unknown) => {
if (typeof val !== 'object' || val === null) {
throw new Error(`Invalid output: ${typeof val}`)
}
if (!('id' in val) || typeof val.id !== 'string' || !('name' in val) || typeof val.name !== 'string') {
throw new Error(`Invalid output: ${typeof val}`)
}
return val as User
})
.mutation(req => {
const { input } = req

return input
}),
})

const appRouterWithSuperJson = tWithSuperJson.router({
Expand Down