@trpc/server
Version:
40 lines (38 loc) • 1.31 kB
JavaScript
import { m as run } from "../codes-nbrm_zWY.mjs";
import { n as _objectSpread2 } from "../getErrorShape-B5jPKHJ3.mjs";
import { x as TRPCError } from "../asyncGeneratorDelegate-JWx0LJUD.mjs";
import { n as nodeHTTPRequestHandler, t as internal_exceptionHandler } from "../node-http-Df_b1FfK.mjs";
//#region 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)));
};
}
//#endregion
export { createNextApiHandler };
//# sourceMappingURL=next.mjs.map