UNPKG

@beignet/core

Version:

Core framework primitives for Beignet

45 lines 1.59 kB
/** * OpenAPI generation handler * OpenAPI helpers for the Beignet server runtime. */ /** * Create a framework-neutral OpenAPI JSON handler. * * The OpenAPI document is generated lazily on the first request and then cached * for the lifetime of the handler. */ export function createOpenAPIHandler(contracts, openapiConfig, _env) { // Lazily generate OpenAPI spec on first request let cachedSpec = null; return async (_req) => { // Generate spec if not cached if (!cachedSpec) { try { // Dynamic import to avoid hard dependency const { contractsToOpenAPI } = await import("../openapi/index.js"); cachedSpec = contractsToOpenAPI(contracts, { title: openapiConfig?.title ?? "Beignet API", version: openapiConfig?.version ?? "1.0.0", description: openapiConfig?.description, }); } catch (error) { // OpenAPI generation failed return { status: 500, body: { error: "OpenAPI generation failed", message: error instanceof Error ? error.message : "Unknown error", }, headers: { "Content-Type": "application/json" }, }; } } return { status: 200, body: cachedSpec, headers: { "Content-Type": "application/json" }, }; }; } //# sourceMappingURL=openapi.js.map