mirror of
https://github.com/FranP-code/create-better-t-stack.git
synced 2025-10-12 23:52:15 +00:00
feat(cli): add openapi suppport in orpc (#563)
This commit is contained in:
@@ -9,8 +9,12 @@ import { appRouter, type AppRouter } from "./routers/index";
|
||||
{{/if}}
|
||||
|
||||
{{#if (eq api "orpc")}}
|
||||
import { OpenAPIHandler } from "@orpc/openapi/node";
|
||||
import { OpenAPIReferencePlugin } from "@orpc/openapi/plugins";
|
||||
import { ZodToJsonSchemaConverter } from "@orpc/zod/zod4";
|
||||
import { RPCHandler } from "@orpc/server/node";
|
||||
import { CORSPlugin } from "@orpc/server/plugins";
|
||||
import { onError } from "@orpc/server";
|
||||
import { appRouter } from "./routers/index";
|
||||
import { createServer } from "node:http";
|
||||
{{#if (eq auth "better-auth")}}
|
||||
@@ -40,7 +44,7 @@ const baseCorsConfig = {
|
||||
};
|
||||
|
||||
{{#if (eq api "orpc")}}
|
||||
const handler = new RPCHandler(appRouter, {
|
||||
const rpcHandler = new RPCHandler(appRouter, {
|
||||
plugins: [
|
||||
new CORSPlugin({
|
||||
origin: process.env.CORS_ORIGIN,
|
||||
@@ -48,13 +52,31 @@ const handler = new RPCHandler(appRouter, {
|
||||
allowHeaders: ["Content-Type", "Authorization"],
|
||||
}),
|
||||
],
|
||||
interceptors: [
|
||||
onError((error) => {
|
||||
console.error(error);
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
const apiHandler = new OpenAPIHandler(appRouter, {
|
||||
plugins: [
|
||||
new OpenAPIReferencePlugin({
|
||||
schemaConverters: [new ZodToJsonSchemaConverter()],
|
||||
}),
|
||||
],
|
||||
interceptors: [
|
||||
onError((error) => {
|
||||
console.error(error);
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
const fastify = Fastify({
|
||||
logger: true,
|
||||
serverFactory: (fastifyHandler) => {
|
||||
const server = createServer(async (req, res) => {
|
||||
const { matched } = await handler.handle(req, res, {
|
||||
const { matched } = await rpcHandler.handle(req, res, {
|
||||
context: await createContext(req.headers),
|
||||
prefix: "/rpc",
|
||||
});
|
||||
@@ -63,6 +85,15 @@ const fastify = Fastify({
|
||||
return;
|
||||
}
|
||||
|
||||
const apiResult = await apiHandler.handle(req, res, {
|
||||
context: await createContext(req.headers),
|
||||
prefix: "/api",
|
||||
});
|
||||
|
||||
if (apiResult.matched) {
|
||||
return;
|
||||
}
|
||||
|
||||
fastifyHandler(req, res);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user