UNPKG

@trpc/server

Version:

The tRPC server library

39 lines (37 loc) • 1.29 kB
import { x as TRPCError } from "../asyncGeneratorDelegate-BRQGtkYG.mjs"; import { m as run } from "../codes-BCsCCyMj.mjs"; import { n as nodeHTTPRequestHandler, t as internal_exceptionHandler } from "../node-http-5y0OmcF3.mjs"; import { n as _objectSpread2 } from "../getErrorShape-CVrl8_-w.mjs"; // src/adapters/next.ts /** * Creates a Next.js API route handler for a router, for the Pages Router. * The file must be named `[trpc].ts`. For the App Router, use * `fetchRequestHandler`. * @see https://trpc.io/docs/server/adapters/nextjs#nextjs-example */ function createNextApiHandler(opts) { return async (req, res) => { let path = ""; await run(async () => { path = run(() => { if (typeof req.query["trpc"] === "string") return req.query["trpc"]; if (Array.isArray(req.query["trpc"])) return req.query["trpc"].join("/"); throw new TRPCError({ message: "Query \"trpc\" not found - is the file named `[trpc]`.ts or `[...trpc].ts`?", code: "INTERNAL_SERVER_ERROR" }); }); await nodeHTTPRequestHandler(_objectSpread2(_objectSpread2({}, opts), {}, { req, res, path })); }).catch(internal_exceptionHandler(_objectSpread2({ req, res, path }, opts))); }; } export { createNextApiHandler }; //# sourceMappingURL=next.mjs.map