Skip to content

Commit

Permalink
fix(trpc): add openApiHandler to api route
Browse files Browse the repository at this point in the history
  • Loading branch information
Siumauricio committed Jun 29, 2024
1 parent b28bf5f commit 7e9e9dc
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 46 deletions.
29 changes: 29 additions & 0 deletions pages/api/[...trpc].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { createOpenApiNextHandler } from "@dokploy/trpc-openapi";
import { appRouter } from "@/server/api/root";
import { createTRPCContext } from "@/server/api/trpc";
import { validateBearerToken } from "@/server/auth/token";
import { validateRequest } from "@/server/auth/auth";

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
let { session, user } = await validateBearerToken(req);

if (!session) {
const cookieResult = await validateRequest(req, res);
session = cookieResult.session;
user = cookieResult.user;
}

if (!user || !session) {
res.status(401).json({ message: "Unauthorized" });
return;
}

// @ts-ignore
return createOpenApiNextHandler({
router: appRouter,
createContext: createTRPCContext,
})(req, res);
};

export default handler;
85 changes: 40 additions & 45 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/api/routers/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ export const settingsRouter = createTRPCRouter({
getOpenApiDocument: protectedProcedure.query(
async ({ ctx }): Promise<unknown> => {
const protocol = ctx.req.headers["x-forwarded-proto"];
const url = `${protocol}://${ctx.req.headers.host}/api/trpc`;
const url = `${protocol}://${ctx.req.headers.host}/api`;
const openApiDocument = generateOpenApiDocument(appRouter, {
title: "tRPC OpenAPI",
version: "1.0.0",
Expand Down

0 comments on commit 7e9e9dc

Please sign in to comment.