feat: complete TASK-WP3-02 runtime recommendation
This commit is contained in:
@@ -5,17 +5,19 @@ function identifier(value) {
|
||||
return value.replaceAll(/[^A-Za-z0-9_$]/g, "_");
|
||||
}
|
||||
|
||||
function schemaType(schema) {
|
||||
function schemaType(schema, namedSchemas = []) {
|
||||
if (!schema || typeof schema !== "object") return "unknown";
|
||||
if (schema.$ref) return identifier(schema.$ref.split("/").at(-1));
|
||||
const named = namedSchemas.find(([, candidate]) => JSON.stringify(candidate) === JSON.stringify(schema));
|
||||
if (named) return identifier(named[0]);
|
||||
if (Object.hasOwn(schema, "const")) return JSON.stringify(schema.const);
|
||||
if (schema.enum) return schema.enum.map((value) => JSON.stringify(value)).join(" | ");
|
||||
if (schema.anyOf) return schema.anyOf.map(schemaType).join(" | ");
|
||||
if (schema.oneOf) return schema.oneOf.map(schemaType).join(" | ");
|
||||
if (schema.anyOf) return schema.anyOf.map((item) => schemaType(item, namedSchemas)).join(" | ");
|
||||
if (schema.oneOf) return schema.oneOf.map((item) => schemaType(item, namedSchemas)).join(" | ");
|
||||
if (Array.isArray(schema.type)) {
|
||||
return schema.type.map((type) => schemaType({ ...schema, type })).join(" | ");
|
||||
return schema.type.map((type) => schemaType({ ...schema, type }, namedSchemas)).join(" | ");
|
||||
}
|
||||
if (schema.type === "array") return `Array<${schemaType(schema.items)}>`;
|
||||
if (schema.type === "array") return `Array<${schemaType(schema.items, namedSchemas)}>`;
|
||||
if (schema.type === "boolean") return "boolean";
|
||||
if (schema.type === "integer" || schema.type === "number") return "number";
|
||||
if (schema.type === "null") return "null";
|
||||
@@ -23,27 +25,31 @@ function schemaType(schema) {
|
||||
if (schema.type === "object" || schema.properties) {
|
||||
const required = new Set(schema.required ?? []);
|
||||
const fields = Object.entries(schema.properties ?? {}).map(
|
||||
([name, child]) => ` ${JSON.stringify(name)}${required.has(name) ? "" : "?"}: ${schemaType(child)};`,
|
||||
([name, child]) => ` ${JSON.stringify(name)}${required.has(name) ? "" : "?"}: ${schemaType(child, namedSchemas)};`,
|
||||
);
|
||||
if (!fields.length && schema.additionalProperties && typeof schema.additionalProperties === "object") {
|
||||
return `Record<string, ${schemaType(schema.additionalProperties, namedSchemas)}>`;
|
||||
}
|
||||
if (!fields.length && schema.additionalProperties === true) return "Record<string, unknown>";
|
||||
return fields.length ? `{\n${fields.join("\n")}\n}` : "Record<string, never>";
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
function operationResult(operation) {
|
||||
function operationResult(operation, namedSchemas) {
|
||||
const response = operation.responses?.["200"];
|
||||
if (!response) return "unknown";
|
||||
if (response.content) {
|
||||
const media = response.content["application/json"] ?? response.content["text/event-stream"] ?? Object.values(response.content)[0];
|
||||
if (media?.schema) return media.schema.format === "binary" ? "Blob" : schemaType(media.schema);
|
||||
if (media?.schema) return media.schema.format === "binary" ? "Blob" : schemaType(media.schema, namedSchemas);
|
||||
}
|
||||
return response.schema ? schemaType(response.schema) : "unknown";
|
||||
return response.schema ? schemaType(response.schema, namedSchemas) : "unknown";
|
||||
}
|
||||
|
||||
function operationBodyType(operation) {
|
||||
function operationBodyType(operation, namedSchemas) {
|
||||
const content = operation.requestBody?.content;
|
||||
const jsonSchema = content?.["application/json"]?.schema;
|
||||
if (jsonSchema) return schemaType(jsonSchema);
|
||||
if (jsonSchema) return schemaType(jsonSchema, namedSchemas);
|
||||
if (content?.["multipart/form-data"]?.schema) return "FormData";
|
||||
return undefined;
|
||||
}
|
||||
@@ -90,16 +96,16 @@ export function generateClient(input, output) {
|
||||
const operationList = operations(document);
|
||||
const builtInTypes = new Set(["Blob", "FormData", "boolean", "number", "string", "unknown"]);
|
||||
const importedTypes = [...new Set(operationList.flatMap(({ operation }) => [
|
||||
operationResult(operation),
|
||||
operationBodyType(operation),
|
||||
operationResult(operation, schemas),
|
||||
operationBodyType(operation, schemas),
|
||||
]).filter((type) => type && !builtInTypes.has(type) && /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(type)))];
|
||||
const sdk = [
|
||||
"// Generated from openapi/openapi.json. Do not edit by hand.",
|
||||
importedTypes.length ? `import type { ${importedTypes.join(", ")} } from "./types.gen.js";` : "",
|
||||
"export interface ClientOptions { baseUrl?: string; fetch?: typeof globalThis.fetch; headers?: HeadersInit; }",
|
||||
...operationList.map(({ method, operation, path }) => {
|
||||
const resultType = operationResult(operation);
|
||||
const bodyType = operationBodyType(operation);
|
||||
const resultType = operationResult(operation, schemas);
|
||||
const bodyType = operationBodyType(operation, schemas);
|
||||
const requestMediaType = operationRequestMediaType(operation);
|
||||
if (operationMediaType(operation) === "text/event-stream") {
|
||||
return `export function ${identifier(operation.operationId)}(options: Pick<ClientOptions, "baseUrl"> = {}): string {\n return \`${"${options.baseUrl ?? \"\"}"}${path}\`;\n}`;
|
||||
|
||||
Reference in New Issue
Block a user