@apideck/portman
Version:
Port OpenAPI Spec to Postman Collection, with contract & variation tests included
101 lines • 4.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PostmanParser = void 0;
var tslib_1 = require("tslib");
var fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
var path_1 = tslib_1.__importDefault(require("path"));
var postman_collection_1 = require("postman-collection");
var utils_1 = require("../utils");
var PostmanMappedOperation_1 = require("./PostmanMappedOperation");
var PostmanParser = (function () {
function PostmanParser(options) {
var _this = this;
this.getItems = function (item) {
if (postman_collection_1.Item.isItem(item)) {
var request = item.request;
if (request && postman_collection_1.Request.isRequest(request)) {
_this.pmItems.push(item);
}
}
else if (postman_collection_1.ItemGroup.isItemGroup(item)) {
var items = item === null || item === void 0 ? void 0 : item.items;
if (items && items.count() === 0)
return;
items.map(function (item) {
return _this.getItems(item);
});
}
};
this.itemsToOperations = function () {
var _a;
var collection = _this.collection;
collection === null || collection === void 0 ? void 0 : collection.items.map(function (item) {
if (postman_collection_1.Item.isItem(item)) {
return _this.getItems(item);
}
else if (postman_collection_1.ItemGroup.isItemGroup(item)) {
var items = item.items;
items.each(function (item) {
return _this.getItems(item);
});
}
});
var operationIdMap = ((_a = _this.oasParser) === null || _a === void 0 ? void 0 : _a.operationIdMap) || {};
_this.mappedOperations = _this.pmItems.map(function (item) {
return new PostmanMappedOperation_1.PostmanMappedOperation({ item: item, operationIdMap: operationIdMap });
});
};
var inputFile = options.inputFile, collection = options.collection, oasParser = options.oasParser;
this.oasParser = oasParser;
if (collection) {
this.collection = collection;
}
else if (inputFile) {
var postmanJson = path_1.default.resolve(inputFile);
this.collection = new postman_collection_1.Collection(JSON.parse(fs_extra_1.default.readFileSync(postmanJson).toString()));
}
this.map();
}
PostmanParser.prototype.map = function (collection) {
if (collection) {
this.collection = new postman_collection_1.Collection(collection);
}
this.pmItems = [];
this.itemsToOperations();
};
PostmanParser.prototype.getOperationByPath = function (path) {
return this.mappedOperations.find(function (_a) {
var pathRef = _a.pathRef;
return pathRef === path;
}) || null;
};
PostmanParser.prototype.getOperationById = function (operationId) {
return this.mappedOperations.find(function (_a) {
var id = _a.id;
return id === operationId;
}) || null;
};
PostmanParser.prototype.getOperationsByIds = function (operationIds) {
return this.mappedOperations.filter(function (_a) {
var id = _a.id;
return id && operationIds.includes(id);
});
};
PostmanParser.prototype.getOperationByItemId = function (itemId) {
if (itemId == undefined)
return null;
return this.mappedOperations.find(function (mappedOperation) { return mappedOperation.item.id === itemId; }) || null;
};
PostmanParser.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.pathRef.split('::')[1]));
});
};
return PostmanParser;
}());
exports.PostmanParser = PostmanParser;
//# sourceMappingURL=PostmanParser.js.map