UNPKG

@apideck/portman

Version:

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

90 lines 4.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.overwriteRequestPathVariables = void 0; var tslib_1 = require("tslib"); var utils_1 = require("../../utils"); var overwriteRequestPathVariables = function (dto) { var overwriteValues = dto.overwriteValues, pmOperation = dto.pmOperation, oaOperation = dto.oaOperation, globals = dto.globals; if (!(overwriteValues instanceof Array)) return pmOperation; var pathVarKeys = pmOperation.item.request.url.variables.map(function (_a) { var key = _a.key; return key; }); var insertNewKeys = overwriteValues.filter(function (x) { return !pathVarKeys.includes(x.key); }); pmOperation.item.request.url.variables.each(function (variable) { overwriteValues.forEach(function (overwriteItem) { if (!(overwriteItem.key && variable.key && overwriteItem.key === variable.key)) { return; } var generatedName = (0, utils_1.parseTpl)({ template: overwriteItem === null || overwriteItem === void 0 ? void 0 : overwriteItem.value, oaOperation: oaOperation, options: { casing: globals === null || globals === void 0 ? void 0 : globals.variableCasing } }); var overwriteValue = (overwriteItem === null || overwriteItem === void 0 ? void 0 : overwriteItem.value) && (0, utils_1.hasTpl)(overwriteItem.value) ? generatedName : overwriteItem === null || overwriteItem === void 0 ? void 0 : overwriteItem.value; if (overwriteItem.key && variable.key && overwriteItem.key === variable.key && overwriteValue !== undefined) { var orgValue = (variable === null || variable === void 0 ? void 0 : variable.value) || null; var newValue = overwriteValue; if (overwriteItem.overwrite === false) { newValue = orgValue + newValue; } variable.type = 'string'; variable.value = (newValue !== undefined && newValue !== null) || 'boolean' === typeof newValue ? "".concat(newValue).toString() : ''; } }); }); overwriteValues .filter(function (_a) { var remove = _a.remove; return remove; }) .map(function (pathVarToRemove) { var _a, _b, _c; if ((_c = (_b = (_a = pmOperation === null || pmOperation === void 0 ? void 0 : pmOperation.item) === null || _a === void 0 ? void 0 : _a.request) === null || _b === void 0 ? void 0 : _b.url) === null || _c === void 0 ? void 0 : _c.path) { var newPath = pmOperation.item.request.url.path.filter(function (variable) { return variable !== ":".concat(pathVarToRemove.key); }, null); pmOperation.item.request.url.path = newPath; } pmOperation.item.request.url.variables.remove(function (variable) { return variable.key === pathVarToRemove.key; }, null); }); insertNewKeys .filter(function (overwriteItem) { return !(overwriteItem.insert === false); }) .filter(function (overwriteItem) { return !(overwriteItem.remove === true); }) .map(function (pathToInsert) { var _a, _b, _c; var newPmPathVar = { key: pathToInsert.key, value: '', disabled: false }; if (pathToInsert.value) newPmPathVar.value = pathToInsert.value; if (pathToInsert.disable === true) newPmPathVar.disabled = true; if (pathToInsert.description) newPmPathVar.description = pathToInsert.description; pmOperation.item.request.url.variables.upsert(newPmPathVar); if ((_c = (_b = (_a = pmOperation === null || pmOperation === void 0 ? void 0 : pmOperation.item) === null || _a === void 0 ? void 0 : _a.request) === null || _b === void 0 ? void 0 : _b.url) === null || _c === void 0 ? void 0 : _c.path) { pmOperation.item.request.url.path.push(":".concat(pathToInsert.key)); } if ((pmOperation === null || pmOperation === void 0 ? void 0 : pmOperation.pathParams) && Array.isArray(pmOperation.pathParams)) { var disabled = newPmPathVar.disabled, reqPathVar = tslib_1.__rest(newPmPathVar, ["disabled"]); pmOperation.pathParams.push(reqPathVar); } }); return pmOperation; }; exports.overwriteRequestPathVariables = overwriteRequestPathVariables; //# sourceMappingURL=overwriteRequestPathVariables.js.map