openapi-typescript
Version:
Generate TypeScript types from Swagger OpenAPI specs
48 lines • 1.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformPathsObj = void 0;
const utils_1 = require("../utils");
const operation_1 = require("./operation");
const parameters_1 = require("./parameters");
function transformPathsObj(paths, options) {
const { globalParameters, operations, ...ctx } = options;
const readonly = utils_1.tsReadonly(ctx.immutableTypes);
let output = "";
for (const [url, pathItem] of Object.entries(paths)) {
if (pathItem.description)
output += utils_1.comment(pathItem.description);
if (pathItem.$ref) {
output += ` ${readonly}"${url}": ${utils_1.transformRef(pathItem.$ref)};\n`;
continue;
}
output += ` ${readonly}"${url}": {\n`;
for (const method of ["get", "put", "post", "delete", "options", "head", "patch", "trace"]) {
const operation = pathItem[method];
if (!operation)
continue;
if (operation.description)
output += utils_1.comment(operation.description);
if (operation.operationId) {
operations[operation.operationId] = { operation, pathItem };
output += ` ${readonly}"${method}": operations["${operation.operationId}"];\n`;
}
else {
output += ` ${readonly}"${method}": {\n ${operation_1.transformOperationObj(operation, {
...ctx,
globalParameters,
pathItem,
})}\n }\n`;
}
}
if (pathItem.parameters) {
output += ` ${readonly}parameters: {\n ${parameters_1.transformParametersArray(pathItem.parameters, {
...ctx,
globalParameters,
})}\n }\n`;
}
output += ` }\n`;
}
return output;
}
exports.transformPathsObj = transformPathsObj;
//# sourceMappingURL=paths.js.map