feat(api): define public problem errors, pagination, and retry contract
This commit is contained in:
1 parent
b2f6e87bfe
commit
565ca94bbb
86 files changed
+8200
-154
No files matched your search
@@ -1,16 +1,70 @@
|
||||
import { defineConfig } from "orval";
|
||||
import { defineConfig, defineTransformer, type Options } from "orval";
|
||||
|
||||
export default defineConfig({
|
||||
lincodex: {
|
||||
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<Record<string, unknown>>,
|
||||
): 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 `<Name>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"];
|
||||
|
||||
function project(inputTarget: string, endpoints: string, schemas: string): Options {
|
||||
return {
|
||||
input: {
|
||||
target: "../../api/core/openapi.bundle.yml",
|
||||
target: inputTarget,
|
||||
override: {
|
||||
transformer: inlineReusableResponses,
|
||||
},
|
||||
},
|
||||
output: {
|
||||
mode: "tags-split",
|
||||
target: "../src/shared/api/endpoints",
|
||||
target: endpoints,
|
||||
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: "../src/shared/api/schemas",
|
||||
path: schemas,
|
||||
type: "zod",
|
||||
splitByTags: true,
|
||||
},
|
||||
@@ -31,7 +85,31 @@ export default defineConfig({
|
||||
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,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
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`,
|
||||
),
|
||||
},
|
||||
);
|
||||
Reference in new issue
Block a user