openapi-typescript
Version:
Generate TypeScript types from Swagger OpenAPI specs
62 lines • 2.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformResponsesObj = void 0;
const utils_1 = require("../utils");
const headers_1 = require("./headers");
const schema_1 = require("./schema");
const resType = (res) => (res === 204 || (res >= 300 && res < 400) ? "never" : "unknown");
function transformResponsesObj(responsesObj, ctx) {
const readonly = utils_1.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 += utils_1.comment(response.description);
if (response.$ref) {
output += ` ${readonly}${statusCode}: ${utils_1.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: ${utils_1.transformRef(response.headers.$ref)};\n`;
}
else {
output += ` ${readonly}headers: {\n ${headers_1.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 === null || contentResponse === void 0 ? void 0 : contentResponse.schema)
? schema_1.transformSchemaObj(contentResponse.schema, { ...ctx, required: new Set() })
: "unknown";
output += ` ${readonly}"${contentType}": ${responseType};\n`;
}
output += ` }\n`;
break;
}
case 2: {
output += ` ${readonly} schema: ${schema_1.transformSchemaObj(response.schema, {
...ctx,
required: new Set(),
})};\n`;
break;
}
}
output += ` }\n`;
}
return output;
}
exports.transformResponsesObj = transformResponsesObj;
//# sourceMappingURL=responses.js.map