UNPKG

@apideck/portman

Version:

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

50 lines 2.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.writeCollectionPreRequestScripts = void 0; var tslib_1 = require("tslib"); var fs_1 = tslib_1.__importDefault(require("fs")); var postman_collection_1 = require("postman-collection"); var writeCollectionPreRequestScripts = function (collection, scripts) { var collectionEvents = (collection === null || collection === void 0 ? void 0 : collection.event) || []; var preRequestEvent = collectionEvents && collectionEvents.find(function (e) { return (e === null || e === void 0 ? void 0 : e.listen) === 'prerequest'; }, null); if (!preRequestEvent) { preRequestEvent = { listen: 'prerequest', script: { exec: [], type: 'text/javascript' } }; } var scriptContents = scripts.map(function (src) { if (src.startsWith('file:')) { return getScriptContent(src.replace('file:', '')); } else { return src; } }); var script = new postman_collection_1.Script(preRequestEvent.script); if (script.exec === undefined) script.exec = []; var exec = Array.isArray(script.exec) ? [].concat(Array.from(script.exec), Array.from(scriptContents)) : [script.exec].concat(Array.from(scriptContents)); script.update({ exec: exec.filter(function (i) { return Boolean(i); }) }); preRequestEvent.script = script.toJSON(); collection.event = (collection === null || collection === void 0 ? void 0 : collection.event) ? [].concat(Array.from(collection.event), [preRequestEvent]) : [preRequestEvent]; return collection; }; exports.writeCollectionPreRequestScripts = writeCollectionPreRequestScripts; function getScriptContent(scriptPath) { try { return fs_1.default.readFileSync(scriptPath, { encoding: 'utf8', flag: 'r' }); } catch (ex) { console.error('\x1b[31m', "Config collection pre-request script file error - no such file or directory \"".concat(scriptPath, "\"")); process.exit(1); } } //# sourceMappingURL=writeCollectionPreRequestScripts.js.map