feat(cli): add openapi suppport in orpc (#563)

This commit is contained in:
Aman Varshney
2025-09-04 15:08:44 +05:30
committed by GitHub
parent 50a1b9441f
commit a276addef8
15 changed files with 193 additions and 24 deletions

View File

@@ -5,7 +5,11 @@ import { createContext } from "./lib/context";
import { 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 { onError } from "@orpc/server";
import { appRouter } from "./routers";
{{#if (eq auth "better-auth")}}
import { createContext } from "./lib/context";
@@ -50,9 +54,28 @@ app.use(
{{/if}}
{{#if (eq api "orpc")}}
const handler = new RPCHandler(appRouter);
app.use("/rpc{*path}", async (req, res, next) => {
const { matched } = await handler.handle(req, res, {
const rpcHandler = new RPCHandler(appRouter, {
interceptors: [
onError((error) => {
console.error(error);
}),
],
});
const apiHandler = new OpenAPIHandler(appRouter, {
plugins: [
new OpenAPIReferencePlugin({
schemaConverters: [new ZodToJsonSchemaConverter()],
}),
],
interceptors: [
onError((error) => {
console.error(error);
}),
],
});
app.use(async (req, res, next) => {
const rpcResult = await rpcHandler.handle(req, res, {
prefix: "/rpc",
{{#if (eq auth "better-auth")}}
context: await createContext({ req }),
@@ -60,7 +83,18 @@ app.use("/rpc{*path}", async (req, res, next) => {
context: {},
{{/if}}
});
if (matched) return;
if (rpcResult.matched) return;
const apiResult = await apiHandler.handle(req, res, {
prefix: "/api",
{{#if (eq auth "better-auth")}}
context: await createContext({ req }),
{{else}}
context: {},
{{/if}}
});
if (apiResult.matched) return;
next();
});
{{/if}}