niko
niko
TtRPC
Created by niko on 11/15/2024 in #❓-help
Building CLI tool for trpc for type generation
I'm building trpc types cli tool for my use case. i use nextjs for api and i want to implement same functionality in another nextjs app without copy pasting whole api folder to that one, i already made type generation tool just testing around if it will work this is what main nextjs api /api/trpc/[trpc]/route.ts code looks like, im allowing to bypass cors issue when making request from another nextjs app in my case localhost:3000 is main and localhost:3001 is second
import { fetchRequestHandler } from "@trpc/server/adapters/fetch";
import { type NextRequest } from "next/server";

import { env } from "@/env";
import { appRouter } from "@/server/api/root";
import { createTRPCContext } from "@/server/api/trpc";

const corsHeaders = {
"Access-Control-Allow-Origin": "http://localhost:3001",
"Access-Control-Request-Method": "*",
"Access-Control-Allow-Methods": "OPTIONS, GET, POST",
"Access-Control-Allow-Headers": "*",
"Access-Control-Allow-Credentials": "true",
};

const createContext = async (req: NextRequest) => {
return createTRPCContext({
headers: req.headers,
});
};

const handler = async (req: NextRequest) => {
const response = await fetchRequestHandler({
endpoint: "/api/trpc",
req,
router: appRouter,
createContext: () => createContext(req),
onError:
env.NODE_ENV === "development"
? ({ path, error }) => {
console.error(
`❌ tRPC failed on ${path ?? "<no-path>"}: ${error.message}`,
);
}
: undefined,
});

// Create a new response with CORS headers
const newResponse = new Response(response.body, response);
newResponse.headers.set("Content-Type", "application/json");

Object.entries(corsHeaders).forEach(([key, value]) => {
newResponse.headers.set(key, value);
});

return newResponse;
};

export { handler as GET, handler as POST, handler as OPTIONS };
import { fetchRequestHandler } from "@trpc/server/adapters/fetch";
import { type NextRequest } from "next/server";

import { env } from "@/env";
import { appRouter } from "@/server/api/root";
import { createTRPCContext } from "@/server/api/trpc";

const corsHeaders = {
"Access-Control-Allow-Origin": "http://localhost:3001",
"Access-Control-Request-Method": "*",
"Access-Control-Allow-Methods": "OPTIONS, GET, POST",
"Access-Control-Allow-Headers": "*",
"Access-Control-Allow-Credentials": "true",
};

const createContext = async (req: NextRequest) => {
return createTRPCContext({
headers: req.headers,
});
};

const handler = async (req: NextRequest) => {
const response = await fetchRequestHandler({
endpoint: "/api/trpc",
req,
router: appRouter,
createContext: () => createContext(req),
onError:
env.NODE_ENV === "development"
? ({ path, error }) => {
console.error(
`❌ tRPC failed on ${path ?? "<no-path>"}: ${error.message}`,
);
}
: undefined,
});

// Create a new response with CORS headers
const newResponse = new Response(response.body, response);
newResponse.headers.set("Content-Type", "application/json");

Object.entries(corsHeaders).forEach(([key, value]) => {
newResponse.headers.set(key, value);
});

return newResponse;
};

export { handler as GET, handler as POST, handler as OPTIONS };
8 replies