Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

1.3 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.

// GET /api/v1/openapi.json — the generated OpenAPI 3 spec for the Carbon API.
// Public, so clients can generate a typed client in any language. Memoized at module
// scope (the router and its schemas are static for the process lifetime).

import { CarbonJsonSchemaConverter } from "@carbon/api/schema";
import { OpenAPIGenerator } from "@orpc/openapi";
import { router } from "./lib/router.server";
import { specOptions, withErrorResponses } from "./lib/spec-options.server";

// The promise, not the string: concurrent cold-start requests on this public
// route must share ONE generation of the 1,495-operation spec.
let cachedSpec: Promise<string> | null = null;

async function generateSpec(): Promise<string> {
  const generator = new OpenAPIGenerator({
    schemaConverters: [new CarbonJsonSchemaConverter()]
  });
  const spec = await generator.generate(router, specOptions());
  return JSON.stringify(withErrorResponses(spec as never));
}

export async function loader() {
  cachedSpec ??= generateSpec();
  return new Response(await cachedSpec, {
    headers: { "content-type": "application/json" }
  });
}