UNPKG

@apideck/portman

Version:

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

120 lines 5.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.overwriteCollectionSecurityValues = void 0; var tslib_1 = require("tslib"); var overwriteCollectionSecurityValues = function (collectionJson, _a) { var _b, _c, _d; var apiKey = _a.apiKey, basic = _a.basic, bearer = _a.bearer, awsv4 = _a.awsv4, digest = _a.digest, edgegrid = _a.edgegrid, ntlm = _a.ntlm, oauth1 = _a.oauth1, oauth2 = _a.oauth2, remove = _a.remove; var defaultSecurity = false; if (apiKey !== undefined || basic !== undefined || bearer !== undefined) { defaultSecurity = true; } if (remove) { collectionJson.auth = { type: 'noauth' }; return collectionJson; } if ((apiKey === null || apiKey === void 0 ? void 0 : apiKey.value) && defaultSecurity) { if (((_b = collectionJson === null || collectionJson === void 0 ? void 0 : collectionJson.auth) === null || _b === void 0 ? void 0 : _b.apikey) && Array.isArray(collectionJson.auth.apikey)) { collectionJson.auth.apikey = collectionJson.auth.apikey.map(function (el) { return el.key === 'value' ? tslib_1.__assign(tslib_1.__assign({}, el), { value: apiKey.value }) : el; }); if (apiKey === null || apiKey === void 0 ? void 0 : apiKey.key) { collectionJson.auth.apikey = collectionJson.auth.apikey.map(function (el) { return el.key === 'key' ? tslib_1.__assign(tslib_1.__assign({}, el), { value: apiKey.key }) : el; }); } var apikeyInOptions = ['header', 'query']; if ((apiKey === null || apiKey === void 0 ? void 0 : apiKey.in) && apikeyInOptions.includes(apiKey === null || apiKey === void 0 ? void 0 : apiKey.in)) { collectionJson.auth.apikey = collectionJson.auth.apikey.map(function (el) { return el.key === 'in' ? tslib_1.__assign(tslib_1.__assign({}, el), { value: apiKey.in }) : el; }); } } else { var authAttributes = Object.entries(apiKey).map(function (_a) { var _b = tslib_1.__read(_a, 2), key = _b[0], value = _b[1]; return ({ key: key, value: value, type: 'string' }); }); collectionJson.auth = { type: 'apikey' }; collectionJson.auth['apikey'] = authAttributes; } } if ((basic === null || basic === void 0 ? void 0 : basic.username) && (basic === null || basic === void 0 ? void 0 : basic.password) && defaultSecurity) { if (((_c = collectionJson === null || collectionJson === void 0 ? void 0 : collectionJson.auth) === null || _c === void 0 ? void 0 : _c.basic) && Array.isArray(collectionJson.auth.basic)) { collectionJson.auth.basic = collectionJson.auth.basic.map(function (el) { return el.key === 'username' ? tslib_1.__assign(tslib_1.__assign({}, el), { value: basic === null || basic === void 0 ? void 0 : basic.username }) : el; }); collectionJson.auth.basic = collectionJson.auth.basic.map(function (el) { return el.key === 'password' ? tslib_1.__assign(tslib_1.__assign({}, el), { value: basic === null || basic === void 0 ? void 0 : basic.password }) : el; }); } else { var authAttributes = Object.entries(basic).map(function (_a) { var _b = tslib_1.__read(_a, 2), key = _b[0], value = _b[1]; return ({ key: key, value: value, type: 'string' }); }); collectionJson.auth = { type: 'basic' }; collectionJson.auth['basic'] = authAttributes; } } if ((bearer === null || bearer === void 0 ? void 0 : bearer.token) && defaultSecurity) { if (((_d = collectionJson === null || collectionJson === void 0 ? void 0 : collectionJson.auth) === null || _d === void 0 ? void 0 : _d.bearer) && Array.isArray(collectionJson.auth.bearer)) { collectionJson.auth.bearer = collectionJson.auth.bearer.map(function (el) { return el.key === 'token' ? tslib_1.__assign(tslib_1.__assign({}, el), { value: bearer === null || bearer === void 0 ? void 0 : bearer.token }) : el; }); } else { collectionJson.auth = { type: 'bearer' }; collectionJson.auth['bearer'] = [ { key: 'token', value: bearer === null || bearer === void 0 ? void 0 : bearer.token, type: 'string' } ]; } } if (!defaultSecurity && (awsv4 || digest || edgegrid || ntlm || oauth1 || oauth2)) { var type = void 0; var authAttributes = []; if (awsv4) { type = 'awsv4'; authAttributes = awsv4 || []; } if (digest) { type = 'digest'; authAttributes = digest || []; } if (edgegrid) { type = 'edgegrid'; authAttributes = edgegrid || []; } if (ntlm) { type = 'ntlm'; authAttributes = ntlm || []; } if (oauth1) { type = 'oauth1'; authAttributes = oauth1 || []; } if (oauth2) { type = 'oauth2'; authAttributes = oauth2 || []; } if (type) { collectionJson.auth = { type: type }; collectionJson.auth[type] = authAttributes; } } return collectionJson; }; exports.overwriteCollectionSecurityValues = overwriteCollectionSecurityValues; //# sourceMappingURL=overwriteCollectionSecurityValues.js.map