typedefT
tRPC3y ago
22 replies
typedef

Set custom header for fetchRequestHandler

Is this possible? currently getting cors issue. Trying to use nextjs's edge function with trpc..

import { type NextRequest } from "next/server";
import { appRouter, createContext } from "api";
import { fetchRequestHandler } from "@trpc/server/adapters/fetch";

export const config = {
  runtime: "edge",
};

export default async function handler(req: NextRequest) {
  if (req.method === "OPTIONS") {
    return new Response(null, {
      status: 200,
    });
  }

  return fetchRequestHandler({
    endpoint: "/api/trpc",
    router: appRouter,
    createContext,
    req,
    onError:
      process.env.NODE_ENV === "development"
        ? ({ path, error }) => {
            console.error(`[tRPC] failed on ${path}: ${error}`);
          }
        : undefined,
  });
}
Was this page helpful?