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:
@@ -2,18 +2,47 @@
|
||||
import { createContext } from '@/lib/context'
|
||||
{{/if}}
|
||||
import { appRouter } from '@/routers'
|
||||
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 { NextRequest } from 'next/server'
|
||||
|
||||
const handler = new RPCHandler(appRouter)
|
||||
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)
|
||||
}),
|
||||
],
|
||||
})
|
||||
|
||||
async function handleRequest(req: NextRequest) {
|
||||
const { response } = await handler.handle(req, {
|
||||
const rpcResult = await rpcHandler.handle(req, {
|
||||
prefix: '/rpc',
|
||||
context: {{#if (eq auth "better-auth")}}await createContext(req){{else}}{}{{/if}},
|
||||
})
|
||||
if (rpcResult.response) return rpcResult.response
|
||||
|
||||
return response ?? new Response('Not found', { status: 404 })
|
||||
const apiResult = await apiHandler.handle(req, {
|
||||
prefix: '/rpc/api',
|
||||
context: {{#if (eq auth "better-auth")}}await createContext(req){{else}}{}{{/if}},
|
||||
})
|
||||
if (apiResult.response) return apiResult.response
|
||||
|
||||
return new Response('Not found', { status: 404 })
|
||||
}
|
||||
|
||||
export const GET = handleRequest
|
||||
|
||||
@@ -10,7 +10,11 @@ import { appRouter } from "./routers/index";
|
||||
import { fetchRequestHandler } from "@trpc/server/adapters/fetch";
|
||||
{{/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 { appRouter } from "./routers";
|
||||
import { createContext } from "./lib/context";
|
||||
{{/if}}
|
||||
@@ -19,7 +23,25 @@ import { auth } from "./lib/auth";
|
||||
{{/if}}
|
||||
|
||||
{{#if (eq api "orpc")}}
|
||||
const handler = new RPCHandler(appRouter);
|
||||
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);
|
||||
}),
|
||||
],
|
||||
});
|
||||
{{/if}}
|
||||
|
||||
{{#if (eq runtime "node")}}
|
||||
@@ -48,12 +70,19 @@ const app = new Elysia()
|
||||
{{/if}}
|
||||
{{#if (eq api "orpc")}}
|
||||
.all('/rpc*', async (context) => {
|
||||
const { response } = await handler.handle(context.request, {
|
||||
const { response } = await rpcHandler.handle(context.request, {
|
||||
prefix: '/rpc',
|
||||
context: await createContext({ context })
|
||||
})
|
||||
return response ?? new Response('Not Found', { status: 404 })
|
||||
})
|
||||
.all('/api*', async (context) => {
|
||||
const { response } = await apiHandler.handle(context.request, {
|
||||
prefix: '/api',
|
||||
context: await createContext({ context })
|
||||
})
|
||||
return response ?? new Response('Not Found', { status: 404 })
|
||||
})
|
||||
{{/if}}
|
||||
{{#if (eq api "trpc")}}
|
||||
.all("/trpc/*", async (context) => {
|
||||
|
||||
@@ -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}}
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
|
||||
@@ -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