UNPKG

@apideck/portman

Version:

Port OpenAPI Spec to Postman Collection, with contract & variation tests included

102 lines 5.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PostmanMappedOperation = void 0; var tslib_1 = require("tslib"); var postman_collection_1 = require("postman-collection"); var PostmanMappedOperation = (function () { function PostmanMappedOperation(options) { var _a, _b; var item = options.item, operationIdMap = options.operationIdMap, id = options.id; this.item = item; var request = item.request; this.method = request.method.toUpperCase(); this.path = request.url.path ? "/".concat(request.url.path.join('/')) : '/'; this.pathRef = this.normalizedPathRef(this.method); this.pathVar = this.normalizedPathVar(this.method); this.requestHeaders = request.headers.toJSON().map(function (_a) { var key = _a.key, value = _a.value, description = _a.description; return { name: key, value: value, description: description === null || description === void 0 ? void 0 : description.content }; }); this.queryParams = request.url.query.map(function (_a) { var key = _a.key, value = _a.value; return { name: key, value: value }; }); this.pathParams = request.url.variables.toJSON().map(function (_a) { var key = _a.key, value = _a.value, description = _a.description; return { name: key, value: value, description: description === null || description === void 0 ? void 0 : description.content }; }); this.id = !id && operationIdMap && ((_a = operationIdMap[this.pathRef]) === null || _a === void 0 ? void 0 : _a.id) ? (_b = operationIdMap[this.pathRef]) === null || _b === void 0 ? void 0 : _b.id : id; this.postmanItemId = item === null || item === void 0 ? void 0 : item.id; this.testJsonDataInjected = false; this.mappedVars = []; } PostmanMappedOperation.prototype.getTests = function () { return this.item.events.find(function (e) { return (e === null || e === void 0 ? void 0 : e.listen) === 'test'; }, null); }; PostmanMappedOperation.prototype.getParent = function () { var parent = this.item.parent(); var isParent = postman_collection_1.ItemGroup.isItemGroup(parent); return isParent ? parent : null; }; PostmanMappedOperation.prototype.getParentFolderId = function () { var parent = this.getParent(); return parent ? parent.id : null; }; PostmanMappedOperation.prototype.getParentFolderName = function () { var parent = this.getParent(); return parent ? parent.name : null; }; PostmanMappedOperation.prototype.clone = function (_a) { var _b; var newId = _a.newId, name = _a.name; var clonedJsonItem = tslib_1.__assign({}, this.item.toJSON()); var id = clonedJsonItem.id, clone = tslib_1.__rest(clonedJsonItem, ["id"]); if (name) { clone.name = name; (clone === null || clone === void 0 ? void 0 : clone.request) ? (clone.request.name = name) : null; } if ((_b = clone === null || clone === void 0 ? void 0 : clone.response) === null || _b === void 0 ? void 0 : _b.length) { clone.response = clone.response.map(function (response) { var id = response.id, clonedResponse = tslib_1.__rest(response, ["id"]); return clonedResponse; }); } var clonedPmItem = new postman_collection_1.Item(clone); clonedPmItem.events.clear(); return new PostmanMappedOperation({ item: clonedPmItem, id: newId }); }; PostmanMappedOperation.prototype.normalizedPathRef = function (method) { var _a, _b; var url = this.item.request.url; var path = (_b = (_a = url === null || url === void 0 ? void 0 : url.path) === null || _a === void 0 ? void 0 : _a.map(function (segment) { return segment.startsWith(':') ? "{".concat(segment, "}").replace(':', '') : segment; })) === null || _b === void 0 ? void 0 : _b.map(function (segment) { return segment.includes('{{') ? segment.replace('{{', '{').replace('}}', '}') : segment; }).join('/'); return "".concat(method, "::/").concat(path); }; PostmanMappedOperation.prototype.normalizedPathVar = function (method) { var _a; var url = this.item.request.url; var path = (_a = url === null || url === void 0 ? void 0 : url.path) === null || _a === void 0 ? void 0 : _a.map(function (segment) { return segment.includes(':') ? "{".concat(segment, "}").replace(':', '') : segment; }).join('/').replace(/#|\//g, '-'); return "".concat(method.toLowerCase(), "::").concat(path); }; PostmanMappedOperation.prototype.registerVar = function (variable) { if (!this.mappedVars.includes(variable)) { this.mappedVars.push(variable); return true; } return false; }; return PostmanMappedOperation; }()); exports.PostmanMappedOperation = PostmanMappedOperation; //# sourceMappingURL=PostmanMappedOperation.js.map