UNPKG

openapi-typescript

Version:

Generate TypeScript types from Swagger OpenAPI specs

58 lines 2.56 kB
import { comment, transformRef, tsReadonly } from "../utils"; import { transformHeaderObjMap } from "./headers"; import { transformSchemaObj } from "./schema"; const resType = (res) => (res === 204 || (res >= 300 && res < 400) ? "never" : "unknown"); export function transformResponsesObj(responsesObj, ctx) { const readonly = tsReadonly(ctx.immutableTypes); let output = ""; for (const httpStatusCode of Object.keys(responsesObj)) { const statusCode = Number(httpStatusCode) || `"${httpStatusCode}"`; const response = responsesObj[httpStatusCode]; if (response.description) output += comment(response.description); if (response.$ref) { output += ` ${readonly}${statusCode}: ${transformRef(response.$ref)};\n`; continue; } if ((!response.content && !response.schema) || (response.content && !Object.keys(response.content).length)) { output += ` ${readonly}${statusCode}: ${resType(statusCode)};\n`; continue; } output += ` ${readonly}${statusCode}: {\n`; if (response.headers && Object.keys(response.headers).length) { if (response.headers.$ref) { output += ` ${readonly}headers: ${transformRef(response.headers.$ref)};\n`; } else { output += ` ${readonly}headers: {\n ${transformHeaderObjMap(response.headers, { ...ctx, required: new Set(), })}\n }\n`; } } switch (ctx.version) { case 3: { output += ` ${readonly}content: {\n`; for (const contentType of Object.keys(response.content)) { const contentResponse = response.content[contentType]; const responseType = contentResponse && contentResponse?.schema ? transformSchemaObj(contentResponse.schema, { ...ctx, required: new Set() }) : "unknown"; output += ` ${readonly}"${contentType}": ${responseType};\n`; } output += ` }\n`; break; } case 2: { output += ` ${readonly} schema: ${transformSchemaObj(response.schema, { ...ctx, required: new Set(), })};\n`; break; } } output += ` }\n`; } return output; } //# sourceMappingURL=responses.js.map