@apideck/portman
Version:
Port OpenAPI Spec to Postman Collection, with contract & variation tests included
62 lines • 2.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.orderCollectionRequests = void 0;
var orderCollectionRequests = function (obj, orderOfOperations) {
if (orderOfOperations === void 0) { orderOfOperations = []; }
var regStart = new RegExp('{', 'g');
var regEnd = new RegExp('}', 'g');
var regEx = new RegExp('/', 'g');
var orderOfOperationsNorm = orderOfOperations.map(function (item) {
item = item.replace(regStart, ':').replace(regEnd, '').replace(regEx, '/');
return item.endsWith('*') ? item : "".concat(item, "$");
});
obj.item.map(function (pmFolder) {
var _a;
if (pmFolder.item && pmFolder.item.length > 0) {
if ((_a = pmFolder.item[0]) === null || _a === void 0 ? void 0 : _a.item)
return;
pmFolder.item.map(function (request) {
var _a, _b, _c, _d;
if (!((_b = (_a = request === null || request === void 0 ? void 0 : request.request) === null || _a === void 0 ? void 0 : _a.url) === null || _b === void 0 ? void 0 : _b.path))
return;
var postmanPath = (_d = (_c = request === null || request === void 0 ? void 0 : request.request) === null || _c === void 0 ? void 0 : _c.url) === null || _d === void 0 ? void 0 : _d.path.join('/');
var postmanMethod = request.request.method;
request._portman_operation = postmanMethod + '::/' + postmanPath;
});
}
if (pmFolder.item) {
pmFolder.item = pmFolder.item.sort(propComparatorPortmanOperation(orderOfOperationsNorm));
}
});
obj.item.map(function (request) {
if (!request.item) {
var postmanPath = request.request.url.path.join('/');
var postmanMethod = request.request.method;
request._portman_operation = postmanMethod + '::/' + postmanPath;
}
});
if (obj.item && obj.item.length > 0) {
obj.item = obj.item.sort(propComparatorPortmanOperation(orderOfOperationsNorm));
}
return obj;
};
exports.orderCollectionRequests = orderCollectionRequests;
var propComparatorPortmanOperation = function (priorityArr) {
return function (a, b) {
if (a['_portman_operation'] === undefined ||
b['_portman_operation'] === undefined ||
a['_portman_operation'] === b['_portman_operation']) {
return 0;
}
if (!Array.isArray(priorityArr)) {
return 0;
}
var ia = priorityArr.findIndex(function (pri) { return a['_portman_operation'].match(pri); });
var ib = priorityArr.findIndex(function (pri) { return b['_portman_operation'].match(pri); });
if (ia !== -1) {
return ib !== -1 ? ia - ib : -1;
}
return ib !== -1 || a > b ? 1 : a < b ? -1 : 0;
};
};
//# sourceMappingURL=orderCollectionRequests.js.map