feat: complete TASK-WP2-07 latest exports

This commit is contained in:
suyx
2026-08-03 01:43:09 +08:00
parent 2bb4f86d04
commit 1b0a05bbcf
20 changed files with 1791 additions and 18 deletions
+27 -7
View File
@@ -34,15 +34,30 @@ function operationResult(operation) {
const response = operation.responses?.["200"];
if (!response) return "unknown";
if (response.content) {
const media = response.content["application/json"] ?? response.content["text/event-stream"];
if (media?.schema) return schemaType(media.schema);
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);
}
return response.schema ? schemaType(response.schema) : "unknown";
}
function operationBodyType(operation) {
const schema = operation.requestBody?.content?.["application/json"]?.schema;
return schema ? schemaType(schema) : undefined;
const content = operation.requestBody?.content;
const jsonSchema = content?.["application/json"]?.schema;
if (jsonSchema) return schemaType(jsonSchema);
if (content?.["multipart/form-data"]?.schema) return "FormData";
return undefined;
}
function operationRequestMediaType(operation) {
const content = operation.requestBody?.content;
if (content?.["application/json"]) return "application/json";
if (content?.["multipart/form-data"]) return "multipart/form-data";
return undefined;
}
function operationReturnsBinary(operation) {
return Object.values(operation.responses?.["200"]?.content ?? {})
.some((media) => media?.schema?.format === "binary");
}
function operationMediaType(operation) {
@@ -73,10 +88,11 @@ export function generateClient(input, output) {
].join("\n\n");
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),
]).filter((type) => type && /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(type)))];
]).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";` : "",
@@ -84,13 +100,17 @@ export function generateClient(input, output) {
...operationList.map(({ method, operation, path }) => {
const resultType = operationResult(operation);
const bodyType = operationBodyType(operation);
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}`;
}
if (bodyType) {
return `export async function ${identifier(operation.operationId)}(body: ${bodyType}, options: ClientOptions = {}): Promise<${resultType}> {\n const request = options.fetch ?? globalThis.fetch;\n const headers = new Headers(options.headers);\n headers.set("Content-Type", "application/json");\n const response = await request(\`${"${options.baseUrl ?? \"\"}"}${path}\`, { body: JSON.stringify(body), method: "${method.toUpperCase()}", headers });\n if (!response.ok) throw new Error(\`HTTP ${"${response.status}"}\`);\n return response.json() as Promise<${resultType}>;\n}`;
const bodyExpression = requestMediaType === "application/json" ? "JSON.stringify(body)" : "body";
const contentType = requestMediaType === "application/json" ? '\n headers.set("Content-Type", "application/json");' : "";
return `export async function ${identifier(operation.operationId)}(body: ${bodyType}, options: ClientOptions = {}): Promise<${resultType}> {\n const request = options.fetch ?? globalThis.fetch;\n const headers = new Headers(options.headers);${contentType}\n const response = await request(\`${"${options.baseUrl ?? \"\"}"}${path}\`, { body: ${bodyExpression}, method: "${method.toUpperCase()}", headers });\n if (!response.ok) throw new Error(\`HTTP ${"${response.status}"}\`);\n return response.json() as Promise<${resultType}>;\n}`;
}
return `export async function ${identifier(operation.operationId)}(options: ClientOptions = {}): Promise<${resultType}> {\n const request = options.fetch ?? globalThis.fetch;\n const response = await request(\`${"${options.baseUrl ?? \"\"}"}${path}\`, { method: "${method.toUpperCase()}", headers: options.headers ?? {} });\n if (!response.ok) throw new Error(\`HTTP ${"${response.status}"}\`);\n return response.json() as Promise<${resultType}>;\n}`;
const responseReader = operationReturnsBinary(operation) ? "response.blob()" : "response.json()";
return `export async function ${identifier(operation.operationId)}(options: ClientOptions = {}): Promise<${resultType}> {\n const request = options.fetch ?? globalThis.fetch;\n const response = await request(\`${"${options.baseUrl ?? \"\"}"}${path}\`, { method: "${method.toUpperCase()}", headers: options.headers ?? {} });\n if (!response.ok) throw new Error(\`HTTP ${"${response.status}"}\`);\n return ${responseReader} as Promise<${resultType}>;\n}`;
}),
"",
].filter(Boolean).join("\n\n");