Skip to main content

apps/gateway/src/routes/openapi.ts

Metadata

Indexed Symbols

No indexed functions/methods detected in this file.

Markdown Headings (if applicable)

No markdown headings detected.

Source Preview

import { readFile } from "node:fs/promises";
import { existsSync } from "node:fs";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import type { FastifyPluginAsync } from "fastify";

export const openApiRoutes: FastifyPluginAsync = async (app) => {
app.get("/api/v1/openapi", async (_request, reply) => {
const currentDir = dirname(fileURLToPath(import.meta.url));
const filePathCandidates = [
resolve(process.cwd(), "packages/contracts/src/openapi/openapi.yaml"),
resolve(process.cwd(), "../../packages/contracts/src/openapi/openapi.yaml"),
resolve(currentDir, "../../../../packages/contracts/src/openapi/openapi.yaml")
];
const filePath = filePathCandidates.find((path) => existsSync(path));
if (!filePath) {
reply.code(500);
return { error: "OpenAPI specification file was not found" };
}
const yaml = await readFile(filePath, "utf8");
reply.header("content-type", "application/yaml");
return yaml;
});
};