UNPKG

@trpc/server

Version:

The tRPC server library

94 lines (86 loc) • 2.69 kB
/** * If you're making an adapter for tRPC and looking at this file for reference, you should import types and functions from `@trpc/server` and `@trpc/server/http` * * @example * ```ts * import type { AnyTRPCRouter } from '@trpc/server' * import type { HTTPBaseHandlerOptions } from '@trpc/server/http' * ``` */ import type { FastifyReply, FastifyRequest } from 'fastify'; // @trpc/server import type { AnyRouter } from '../../@trpc/server'; // @trpc/server/http import { resolveResponse, type HTTPBaseHandlerOptions, type ResolveHTTPRequestOptionsContextFn, } from '../../@trpc/server/http'; // @trpc/server/node-http import type { NodeHTTPRequest } from '../node-http'; import { incomingMessageToRequest, type NodeHTTPCreateContextOption, } from '../node-http'; /** The options of the Fastify adapter's request handler. */ export type FastifyHandlerOptions< TRouter extends AnyRouter, TRequest extends FastifyRequest, TResponse extends FastifyReply, > = HTTPBaseHandlerOptions<TRouter, TRequest> & { /** * The largest request body, in bytes. A larger one gets a * `PAYLOAD_TOO_LARGE` error. It applies to bodies tRPC streams, such as * `multipart/form-data`; JSON bodies are read by Fastify, so limit them * with Fastify's `bodyLimit`. */ maxBodySize?: number; } & NodeHTTPCreateContextOption<TRouter, TRequest, TResponse>; type FastifyRequestHandlerOptions< TRouter extends AnyRouter, TRequest extends FastifyRequest, TResponse extends FastifyReply, > = FastifyHandlerOptions<TRouter, TRequest, TResponse> & { req: TRequest; res: TResponse; path: string; }; /** * Handles one Fastify request for a router. `fastifyTRPCPlugin` uses it; * call it yourself to mount tRPC on your own route. */ export async function fastifyRequestHandler< TRouter extends AnyRouter, TRequest extends FastifyRequest, TResponse extends FastifyReply, >(opts: FastifyRequestHandlerOptions<TRouter, TRequest, TResponse>) { const createContext: ResolveHTTPRequestOptionsContextFn<TRouter> = async ( innerOpts, ) => { return await opts.createContext?.({ ...opts, ...innerOpts, }); }; const incomingMessage: NodeHTTPRequest = opts.req.raw; // monkey-path body to the IncomingMessage if ('body' in opts.req) { incomingMessage.body = opts.req.body; } const req = incomingMessageToRequest(incomingMessage, opts.res.raw, { maxBodySize: opts.maxBodySize ?? null, }); const res = await resolveResponse({ ...opts, req, error: null, createContext, onError(o) { opts?.onError?.({ ...o, req: opts.req, }); }, }); await opts.res.send(res); }