Skip to content
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
2 changes: 2 additions & 0 deletions apps/content/docs/integrations/trpc.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const orpcRouter = toORPCRouter(trpcRouter)

The result is a regular oRPC router that works with any oRPC feature. For example, you can expose it through an [RPC Handler](/docs/rpc/handler) or [OpenAPI Handler](/docs/openapi/handler), or call it directly with [Server-Side Clients](/docs/client/server-side).

Input and output schemas that implement [Standard Schema](https://standardschema.dev) are carried over, including every chained `.input()`. Validation still runs inside tRPC.

### Error Formatting

`toORPCRouter` does not support [tRPC Error Formatting](https://trpc.io/docs/server/error-formatting). Instead, errors thrown by tRPC are wrapped in `ORPCError`.
Expand Down
73 changes: 72 additions & 1 deletion packages/trpc/src/to-orpc-router.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { OpenAPIMeta } from '@orpc/openapi'
import { getOpenAPIMeta, openapi } from '@orpc/openapi'
import { getOpenAPIMeta, openapi, OpenAPIGenerator } from '@orpc/openapi'
import {
call,
createRouterClient,
Expand Down Expand Up @@ -124,6 +124,77 @@ describe('toORPCRouter', () => {
await expect(call((orpcRouter as any).ping, 'invalid')).rejects.toThrow('Invalid input') // validation happen at tRPC level
})

describe('chained inputs', () => {
const first = z.object({ x: z.string() })
const second = z.object({ y: z.number() })
const third = z.object({ z: z.boolean().optional() })

const orpcRouter = toORPCRouter(
t.router({
two: t.procedure
.input(first)
.input(second)
.query(({ input }) => input),
three: t.procedure
.input(first)
.input(second)
.input(third)
.mutation(({ input }) => input),
}),
)

it('keeps every input schema in the contract', () => {
expect(orpcRouter.two['~orpc'].inputSchemas).toEqual([first, second])
expect(orpcRouter.three['~orpc'].inputSchemas).toEqual([first, second, third])
})

it('skips non-standard parsers', () => {
const mixed = toORPCRouter(
t.router({
ping: (t.procedure.input(first) as any)
.input((value: unknown) => value)
.query(() => 'pong'),
}),
) as any

expect(mixed.ping['~orpc'].inputSchemas).toEqual([first])
})

it('generates an OpenAPI request body with every input', async () => {
const spec = await new OpenAPIGenerator().generate(orpcRouter)

const properties = (path: string) => {
const schema = (spec.paths as any)[path].post.requestBody.content['application/json'].schema

return schema.allOf.flatMap((item: any) => Object.keys(item.properties))
}

expect(properties('/two')).toEqual(['x', 'y'])
expect(properties('/three')).toEqual(['x', 'y', 'z'])
})

it('calls', async () => {
const options = { context: { a: 'test' } }

await expect(call(orpcRouter.two, { x: 'x', y: 1 }, options)).resolves.toEqual({
x: 'x',
y: 1,
})
await expect(call(orpcRouter.three, { x: 'x', y: 1, z: true }, options)).resolves.toEqual({
x: 'x',
y: 1,
z: true,
})

await expect(call(orpcRouter.two, { y: 1 } as any, options)).rejects.toSatisfy(
(err: any) => err instanceof ORPCError && err.code === 'BAD_REQUEST',
)
await expect(call(orpcRouter.three, { x: 'x' } as any, options)).rejects.toSatisfy(
(err: any) => err instanceof ORPCError && err.code === 'BAD_REQUEST',
)
})
})

it('meta', async () => {
const orpcRouter = toORPCRouter(
t.router({
Expand Down
6 changes: 4 additions & 2 deletions packages/trpc/src/to-orpc-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ function recordToORPCRouterRecord(records: AnyRouter['_def']['record']) {
}

function toORPCProcedure(procedure: AnyProcedure) {
const inputSchema = toStandardSchema(procedure._def.inputs.at(-1))
const inputSchemas = procedure._def.inputs
.map(toStandardSchema)
.filter((schema) => schema !== undefined)
const outputSchema = toStandardSchema((procedure._def as any).output)
const meta = (procedure._def.meta ?? {}) as ORPC.Meta

Expand All @@ -86,7 +88,7 @@ function toORPCProcedure(procedure: AnyProcedure) {
// so the tRPC meta must be a plugin too, otherwise it would be dropped.
metaPlugins: [toTRPCMetaPlugin(meta)],
orderedMiddlewares: [],
inputSchemas: inputSchema ? [inputSchema] : undefined,
inputSchemas: inputSchemas.length ? inputSchemas : undefined,
outputSchemas: outputSchema ? [outputSchema] : undefined,
// tRPC procedure calling already validates the input/output
disableInputValidation: true,
Expand Down
Loading