UNPKG

@apideck/portman

Version:

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

74 lines 3.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.overwriteRequestSecurity = void 0; var tslib_1 = require("tslib"); var postman_collection_1 = require("postman-collection"); var overwriteRequestSecurity = function (overwrite, pmOperation) { var _a, _b, _c; if (overwrite === null || overwrite === void 0 ? void 0 : overwrite.remove) { var authType_1 = (_a = pmOperation.item.request.auth) === null || _a === void 0 ? void 0 : _a.type; if (authType_1) { (_b = pmOperation.item.request.auth) === null || _b === void 0 ? void 0 : _b.clear(authType_1); pmOperation.item.request.authorizeUsing('noauth'); } return pmOperation; } var authConfig = pmOperation.item.getAuth(); var authTypes = [ 'oauth2', 'hawk', 'noauth', 'basic', 'oauth1', 'apikey', 'digest', 'bearer', 'awsv4', 'edgegrid', 'ntlm', undefined ]; var authType = (authConfig === null || authConfig === void 0 ? void 0 : authConfig.type) || undefined; var newAuthDefinition = {}; var authMap = {}; if (authType && !Object.keys(overwrite).includes(authType)) { (_c = pmOperation.item.request.auth) === null || _c === void 0 ? void 0 : _c.clear(authType); } Object.entries(overwrite).forEach(function (_a) { var _b; var _c = tslib_1.__read(_a, 1), authKeyRaw = _c[0]; var authKey = authKeyRaw.toLowerCase(); if (!authTypes.includes(authType)) { return; } authMap[authKey] = new Map(); if (authConfig && authConfig[authKey]) { var authParams = authConfig[authKey].toJSON(); authParams.forEach(function (member) { authMap[authKey].set(member.key, member.value); }); } if (Array.isArray(overwrite[authKeyRaw])) { overwrite[authKeyRaw].forEach(function (member) { authMap[authKey].set(member.key, member.value); }); } else if (typeof overwrite[authKeyRaw] === 'object') { Object.entries(overwrite[authKeyRaw]).forEach(function (_a) { var _b = tslib_1.__read(_a, 2), key = _b[0], value = _b[1]; authMap[authKey].set(key, value); }); } newAuthDefinition.type = authKey; newAuthDefinition[authKey] = Array.from(authMap[authKey], function (_a) { var _b = tslib_1.__read(_a, 2), key = _b[0], value = _b[1]; return ({ key: key, value: value }); }); var requestAuth = new postman_collection_1.RequestAuth(newAuthDefinition); (_b = pmOperation.item.request.auth) === null || _b === void 0 ? void 0 : _b.clear(authType); pmOperation.item.request.auth = requestAuth; }); return pmOperation; }; exports.overwriteRequestSecurity = overwriteRequestSecurity; //# sourceMappingURL=overwriteRequestSecurity.js.map