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:
@@ -5,7 +5,11 @@ import "dotenv/config";
|
||||
import { env } from "cloudflare:workers";
|
||||
{{/if}}
|
||||
{{#if (eq api "orpc")}}
|
||||
import { OpenAPIHandler } from "@orpc/openapi/fetch";
|
||||
import { OpenAPIReferencePlugin } from "@orpc/openapi/plugins";
|
||||
import { ZodToJsonSchemaConverter } from "@orpc/zod/zod4";
|
||||
import { RPCHandler } from "@orpc/server/fetch";
|
||||
import { onError } from "@orpc/server";
|
||||
import { createContext } from "./lib/context";
|
||||
import { appRouter } from "./routers/index";
|
||||
{{/if}}
|
||||
@@ -54,17 +58,48 @@ app.on(["POST", "GET"], "/api/auth/**", (c) => auth.handler(c.req.raw));
|
||||
{{/if}}
|
||||
|
||||
{{#if (eq api "orpc")}}
|
||||
const handler = new RPCHandler(appRouter);
|
||||
app.use("/rpc/*", async (c, next) => {
|
||||
export const apiHandler = new OpenAPIHandler(appRouter, {
|
||||
plugins: [
|
||||
new OpenAPIReferencePlugin({
|
||||
schemaConverters: [new ZodToJsonSchemaConverter()],
|
||||
}),
|
||||
],
|
||||
interceptors: [
|
||||
onError((error) => {
|
||||
console.error(error);
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
export const rpcHandler = new RPCHandler(appRouter, {
|
||||
interceptors: [
|
||||
onError((error) => {
|
||||
console.error(error);
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
app.use("/*", async (c, next) => {
|
||||
const context = await createContext({ context: c });
|
||||
const { matched, response } = await handler.handle(c.req.raw, {
|
||||
|
||||
const rpcResult = await rpcHandler.handle(c.req.raw, {
|
||||
prefix: "/rpc",
|
||||
context: context,
|
||||
});
|
||||
|
||||
if (matched) {
|
||||
return c.newResponse(response.body, response);
|
||||
if (rpcResult.matched) {
|
||||
return c.newResponse(rpcResult.response.body, rpcResult.response);
|
||||
}
|
||||
|
||||
const apiResult = await apiHandler.handle(c.req.raw, {
|
||||
prefix: "/api",
|
||||
context: context,
|
||||
});
|
||||
|
||||
if (apiResult.matched) {
|
||||
return c.newResponse(apiResult.response.body, apiResult.response);
|
||||
}
|
||||
|
||||
await next();
|
||||
});
|
||||
{{/if}}
|
||||
|
||||
Reference in New Issue
Block a user