UNPKG

@trpc/server

Version:

The tRPC server library

1 lines 1.82 kB
{"version":3,"file":"express.mjs","names":[],"sources":["../../src/adapters/express.ts"],"sourcesContent":["/**\n * 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`\n *\n * @example\n * ```ts\n * import type { AnyTRPCRouter } from '@trpc/server'\n * import type { HTTPBaseHandlerOptions } from '@trpc/server/http'\n * ```\n */\nimport type * as express from 'express';\nimport type { AnyRouter } from '../@trpc/server';\n// eslint-disable-next-line no-restricted-imports\nimport { run } from '../unstable-core-do-not-import';\nimport type {\n NodeHTTPCreateContextFnOptions,\n NodeHTTPHandlerOptions,\n} from './node-http';\nimport { internal_exceptionHandler, nodeHTTPRequestHandler } from './node-http';\n\nexport type CreateExpressContextOptions = NodeHTTPCreateContextFnOptions<\n express.Request,\n express.Response\n>;\n\nexport function createExpressMiddleware<TRouter extends AnyRouter>(\n opts: NodeHTTPHandlerOptions<TRouter, express.Request, express.Response>,\n): express.Handler {\n return (req, res) => {\n let path = '';\n run(async () => {\n path = req.path.slice(req.path.lastIndexOf('/') + 1);\n\n await nodeHTTPRequestHandler({\n ...(opts as any),\n req,\n res,\n path,\n });\n }).catch(\n internal_exceptionHandler({\n req,\n res,\n path,\n ...opts,\n }),\n );\n };\n}\n"],"mappings":";;;;AAwBA,SAAgB,wBACd,MACiB;CACjB,QAAQ,KAAK,QAAQ;EACnB,IAAI,OAAO;EACX,IAAI,YAAY;GACd,OAAO,IAAI,KAAK,MAAM,IAAI,KAAK,YAAY,GAAG,IAAI,CAAC;GAEnD,MAAM,uBAAA,eAAA,eAAA,CAAA,GACA,IAAA,GAAA,CAAA,GAAA;IACJ;IACA;IACA;GACF,CAAA,CAAC;EACH,CAAC,CAAC,CAAC,MACD,0BAAA,eAAA;GACE;GACA;GACA;EACG,GAAA,IACL,CAAC,CACH;CACF;AACF"}