import { defineConfig, defineTransformer, type Options } from "orval"; const responseReferencePrefix = "#/components/responses/"; // Replaces each reference to components/responses with the referenced response // object. Schema, header, and example references inside it stay references. function inlineResponseReferences( value: unknown, responses: Readonly>, ): unknown { if (Array.isArray(value)) { return value.map((item) => inlineResponseReferences(item, responses)); } if (value === null || typeof value !== "object") { return value; } const reference = (value as { $ref?: unknown }).$ref; if (typeof reference === "string" && reference.startsWith(responseReferencePrefix)) { const name = reference.slice(responseReferencePrefix.length); if (!Object.hasOwn(responses, name)) { throw new Error(`Unknown response reference ${reference}.`); } return inlineResponseReferences(responses[name], responses); } return Object.fromEntries( Object.entries(value).map(([key, item]) => [key, inlineResponseReferences(item, responses)]), ); } // Orval 8.40 writes no Zod schema for components/responses entries, yet an // operation that references one imports a `Response` type that is never // generated. Inlining the references keeps the contract's reusable responses // while generated operations import only the component schemas Orval writes. const inlineReusableResponses = defineTransformer((spec) => { if (spec.paths === undefined) { return spec; } const responses = spec.components?.responses ?? {}; return { ...spec, paths: inlineResponseReferences(spec.paths, responses) as typeof spec.paths, }; }); // The API compatibility check sets this variable to a temporary directory to // generate the OpenAPI 3.2 fixture with the production settings. const compatOutput = process.env["ORVAL_COMPAT_OUTPUT"]; // `clean: true` empties the output directories, so an empty value, which // would place them at the filesystem root, is rejected. if (compatOutput !== undefined && compatOutput.trim() === "") { throw new Error("ORVAL_COMPAT_OUTPUT must name a directory when it is set."); } function project(inputTarget: string, endpoints: string, schemas: string): Options { return { input: { target: inputTarget, override: { transformer: inlineReusableResponses, }, }, output: { mode: "tags-split", target: endpoints, // The endpoint and schema directories hold only generated files. A schema // used by several tags moves from a tag directory to the schema root, so // the directories are cleared first to drop files the contract no longer // produces. clean: true, indexFiles: true, // Generated operations take header parameters such as Idempotency-Key // and If-Match as a typed argument instead of dropping them. headers: true, schemas: { path: schemas, type: "zod", splitByTags: true, }, client: "react-query", httpClient: "fetch", mock: { indexMockFiles: true, generators: [{ type: "msw" }], }, override: { // With Zod schemas, the default enum mode makes the MSW mocks pick // from `Object.values()`, which yields Zod internals rather // than the enum values. The union mode inlines the literal values in // the mocks; the Zod schemas and the types do not change. enumGenerationType: "union", mutator: { path: "../src/shared/api/client.ts", name: "apiRequest", }, fetch: { includeHttpResponseReturnType: false, }, query: { signal: true, }, // Optional properties are typed `{ x?: T }`, which the strict // exactOptionalPropertyTypes setting needs for optional header // parameters to fit RequestInit headers. zod: { exactOptional: true, }, // The same strictness applies to the MSW mocks: an optional property // is either absent or valid, never present with the value undefined, // which the exact optional property types reject. mock: { exactOptional: true, }, }, }, }; } export default defineConfig( compatOutput === undefined ? { lincodex: project( "../../api/core/openapi.bundle.yml", "../src/shared/api/endpoints", "../src/shared/api/schemas", ), } : { compat: project( "../../api/testdata/openapi-3.2.0.yml", `${compatOutput}/endpoints`, `${compatOutput}/schemas`, ), }, );