UNPKG

@trpc/server

Version:

The tRPC server library

79 lines (77 loc) • 3.44 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const require_getErrorShape = require("../../getErrorShape-BAuHhNuh.cjs"); const require_resolveResponse = require("../../resolveResponse-CoiQ9diV.cjs"); const require_node_http = require("../../node-http-vS2eeV-2.cjs"); const require_ws = require("../../ws-zjwsyre9.cjs"); //#region src/adapters/fastify/fastifyRequestHandler.ts /** * Handles one Fastify request for a router. `fastifyTRPCPlugin` uses it; * call it yourself to mount tRPC on your own route. */ async function fastifyRequestHandler(opts) { var _opts$maxBodySize; const createContext = async (innerOpts) => { var _opts$createContext; return await ((_opts$createContext = opts.createContext) === null || _opts$createContext === void 0 ? void 0 : _opts$createContext.call(opts, require_getErrorShape._objectSpread2(require_getErrorShape._objectSpread2({}, opts), innerOpts))); }; const incomingMessage = opts.req.raw; if ("body" in opts.req) incomingMessage.body = opts.req.body; const req = require_node_http.incomingMessageToRequest(incomingMessage, opts.res.raw, { maxBodySize: (_opts$maxBodySize = opts.maxBodySize) !== null && _opts$maxBodySize !== void 0 ? _opts$maxBodySize : null }); const res = await require_resolveResponse.resolveResponse(require_getErrorShape._objectSpread2(require_getErrorShape._objectSpread2({}, opts), {}, { req, error: null, createContext, onError(o) { var _opts$onError; opts === null || opts === void 0 || (_opts$onError = opts.onError) === null || _opts$onError === void 0 || _opts$onError.call(opts, require_getErrorShape._objectSpread2(require_getErrorShape._objectSpread2({}, o), {}, { req: opts.req })); } })); await opts.res.send(res); } //#endregion //#region src/adapters/fastify/fastifyTRPCPlugin.ts /** * A Fastify plugin that serves a router over HTTP, and over WebSockets * with `useWSS`. * @example * ```ts * server.register(fastifyTRPCPlugin, { * prefix: '/trpc', * trpcOptions: { router: appRouter, createContext }, * }); * ``` * @see https://trpc.io/docs/server/adapters/fastify#create-fastify-server */ function fastifyTRPCPlugin(fastify, opts, done) { var _opts$prefix; fastify.removeContentTypeParser("application/json"); fastify.addContentTypeParser("application/json", { parseAs: "string" }, function(_, body, _done) { _done(null, body); }); fastify.removeContentTypeParser("multipart/form-data"); fastify.addContentTypeParser("multipart/form-data", {}, function(_, body, _done) { _done(null, body); }); let prefix = (_opts$prefix = opts.prefix) !== null && _opts$prefix !== void 0 ? _opts$prefix : ""; if (typeof fastifyTRPCPlugin.default !== "function") prefix = ""; fastify.all(`${prefix}/:path`, async (req, res) => { const path = req.params.path; await fastifyRequestHandler(require_getErrorShape._objectSpread2(require_getErrorShape._objectSpread2({}, opts.trpcOptions), {}, { req, res, path })); }); if (opts.useWSS) { var _prefix; const trpcOptions = opts.trpcOptions; const onConnection = require_ws.getWSConnectionHandler(require_getErrorShape._objectSpread2({}, trpcOptions)); fastify.get((_prefix = prefix) !== null && _prefix !== void 0 ? _prefix : "/", { websocket: true }, (socket, req) => { onConnection(socket, req.raw); }); } done(); } //#endregion exports.fastifyRequestHandler = fastifyRequestHandler; exports.fastifyTRPCPlugin = fastifyTRPCPlugin;