@apideck/portman
Version:
Port OpenAPI Spec to Postman Collection, with contract & variation tests included
121 lines • 5.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenApiParser = void 0;
var tslib_1 = require("tslib");
var swagger_parser_1 = tslib_1.__importDefault(require("@apidevtools/swagger-parser"));
var path_1 = tslib_1.__importDefault(require("path"));
var utils_1 = require("../utils");
var OasMappedOperation_1 = require("./OasMappedOperation");
var OpenApiParser = (function () {
function OpenApiParser() {
var _this = this;
this.pathsToOperations = function () {
var paths = _this.oas.paths;
Object.entries(paths).map(function (path) {
if ((path === null || path === void 0 ? void 0 : path[1]) && path[1].parameters) {
Object.keys(path[1]).forEach(function (key) {
var _a, _b, _c;
if (key !== 'parameters' && ((_a = path === null || path === void 0 ? void 0 : path[1]) === null || _a === void 0 ? void 0 : _a.parameters) && ((_c = (_b = path === null || path === void 0 ? void 0 : path[1]) === null || _b === void 0 ? void 0 : _b[key]) === null || _c === void 0 ? void 0 : _c.parameters)) {
path[1][key].parameters = tslib_1.__spreadArray(tslib_1.__spreadArray([], tslib_1.__read(path[1].parameters), false), tslib_1.__read(path[1][key].parameters), false);
}
});
}
});
var mappedOperations = Object.entries(paths)
.filter(function (_a) {
var _b = tslib_1.__read(_a, 2), _path = _b[0], operations = _b[1];
return !!operations;
})
.map(function (_a) {
var _b = tslib_1.__read(_a, 2), path = _b[0], operations = _b[1];
return (operations &&
Object.entries(operations).map(function (_a) {
var _b = tslib_1.__read(_a, 2), method = _b[0], operation = _b[1];
return new OasMappedOperation_1.OasMappedOperation(path, method, operation);
}));
})
.reduce(function (acc, resource) {
resource && resource.map(function (item) { return acc.push(item); });
return acc;
}, []);
_this.mappedOperations = mappedOperations;
return _this.mappedOperations;
};
this.mapOperationIds = function () {
var operationIdMap = _this.mappedOperations.reduce(function (acc, operation) {
acc[operation.pathRef] = operation;
return acc;
}, {});
_this.operationIdMap = operationIdMap;
};
}
OpenApiParser.prototype.convert = function (options) {
var _a;
return tslib_1.__awaiter(this, void 0, void 0, function () {
var inputFile, ignoreCircularRefs, api, oasDoc;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
inputFile = path_1.default.resolve(options.inputFile);
ignoreCircularRefs = (_a = !!options.ignoreCircularRefs) !== null && _a !== void 0 ? _a : false;
return [4, swagger_parser_1.default.dereference(inputFile, {
dereference: {
circular: ignoreCircularRefs ? 'ignore' : false
}
})];
case 1:
api = _b.sent();
return [4, swagger_parser_1.default.bundle(api, {
parse: {
json: false,
yaml: {
allowEmpty: false
}
},
dereference: {
circular: ignoreCircularRefs ? 'ignore' : false
},
validate: {
spec: false
}
})];
case 2:
oasDoc = (_b.sent());
this.oas = oasDoc;
this.pathsToOperations();
this.mapOperationIds();
return [2, oasDoc];
}
});
});
};
OpenApiParser.prototype.getOperationById = function (operationId) {
return this.mappedOperations.find(function (_a) {
var id = _a.id;
return id === operationId;
}) || null;
};
OpenApiParser.prototype.getOperationByPath = function (path) {
return this.mappedOperations.find(function (_a) {
var pathRef = _a.pathRef;
return pathRef === path;
}) || null;
};
OpenApiParser.prototype.getOperationsByIds = function (operationIds) {
return this.mappedOperations.filter(function (_a) {
var id = _a.id;
return id && operationIds.includes(id);
});
};
OpenApiParser.prototype.getOperationsByPath = function (path) {
var targetSplit = path.split('::');
var targetMethod = targetSplit[0].includes('*') ? utils_1.METHODS : targetSplit[0];
var targetPath = targetSplit[1];
return this.mappedOperations.filter(function (mappedOperation) {
return (targetMethod.includes(mappedOperation.method) && (0, utils_1.matchPath)(targetPath, mappedOperation.path));
});
};
return OpenApiParser;
}());
exports.OpenApiParser = OpenApiParser;
//# sourceMappingURL=OpenApiParser.js.map