UNPKG

@apideck/portman

Version:

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

413 lines 22.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PostmanApiService = void 0; var tslib_1 = require("tslib"); var axios_1 = tslib_1.__importDefault(require("axios")); var chalk_1 = tslib_1.__importDefault(require("chalk")); var Either = tslib_1.__importStar(require("fp-ts/lib/Either")); var ora_1 = tslib_1.__importDefault(require("ora")); var PostmanApiService = (function () { function PostmanApiService() { this.baseUrl = 'https://api.getpostman.com'; this.apiKey = "".concat(process.env.POSTMAN_API_KEY); } PostmanApiService.prototype.getWorkspaces = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { var config, res, data, error_1; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: config = { method: 'get', url: "".concat(this.baseUrl, "/workspaces"), headers: { 'Content-Type': 'application/json', 'X-API-Key': this.apiKey } }; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4, (0, axios_1.default)(config)]; case 2: res = _a.sent(); data = res.data; if (Array.isArray(data === null || data === void 0 ? void 0 : data.workspaces)) { return [2, Either.right(data.workspaces)]; } else { return [2, Either.left('No workspaces found')]; } return [3, 4]; case 3: error_1 = _a.sent(); return [2, Either.left("Postman API List Workspaces ".concat(error_1.toString()))]; case 4: return [2]; } }); }); }; PostmanApiService.prototype.getWorkspace = function (id) { return tslib_1.__awaiter(this, void 0, void 0, function () { var config, res, data, error_2; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: config = { method: 'get', url: "".concat(this.baseUrl, "/workspaces/").concat(id), headers: { 'Content-Type': 'application/json', 'X-API-Key': this.apiKey } }; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4, (0, axios_1.default)(config)]; case 2: res = _a.sent(); data = res.data; if (data === null || data === void 0 ? void 0 : data.workspace) { return [2, Either.right(data.workspace)]; } else { return [2, Either.left('Workspace not found')]; } return [3, 4]; case 3: error_2 = _a.sent(); return [2, Either.left("Postman API Get Workspace ".concat(error_2.toString(), " (Workspace ID: ").concat(id, ")"))]; case 4: return [2]; } }); }); }; PostmanApiService.prototype.getCollections = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { var config, res, data, error_3; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: config = { method: 'get', url: "".concat(this.baseUrl, "/collections"), headers: { 'Content-Type': 'application/json', 'X-API-Key': this.apiKey } }; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4, (0, axios_1.default)(config)]; case 2: res = _a.sent(); data = res.data; if (Array.isArray(data === null || data === void 0 ? void 0 : data.collections)) { return [2, Either.right(data.collections)]; } else { return [2, Either.left('No collections found')]; } return [3, 4]; case 3: error_3 = _a.sent(); return [2, Either.left("Postman API Get Collections ".concat(error_3.toString()))]; case 4: return [2]; } }); }); }; PostmanApiService.prototype.getCollection = function (collectionId) { var _a, _b; return tslib_1.__awaiter(this, void 0, void 0, function () { var config, res, data, error_4; return tslib_1.__generator(this, function (_c) { switch (_c.label) { case 0: config = { method: 'get', url: "".concat(this.baseUrl, "/collections/").concat(collectionId), headers: { 'Content-Type': 'application/json', 'X-API-Key': this.apiKey } }; _c.label = 1; case 1: _c.trys.push([1, 3, , 4]); return [4, (0, axios_1.default)(config)]; case 2: res = _c.sent(); data = res.data; collectionId = collectionId.replace("".concat(collectionId.split('-')[0], "-"), ''); if (((_b = (_a = data === null || data === void 0 ? void 0 : data.collection) === null || _a === void 0 ? void 0 : _a.info) === null || _b === void 0 ? void 0 : _b._postman_id) === collectionId) { return [2, Either.right(data.collection)]; } else { return [2, Either.left("The collection with the id \"".concat(collectionId, "\" was not found."))]; } return [3, 4]; case 3: error_4 = _c.sent(); return [2, Either.left("Postman API Get Collections ".concat(error_4.toString()))]; case 4: return [2]; } }); }); }; PostmanApiService.prototype.createCollection = function (collection, workspaceId) { var _a; return tslib_1.__awaiter(this, void 0, void 0, function () { var data, workspaceIdParam, config, spinner, responseStatusCode, response, error, err_1, respData, error_5; return tslib_1.__generator(this, function (_b) { switch (_b.label) { case 0: data = JSON.stringify({ collection: collection }); workspaceIdParam = workspaceId ? "?workspace=".concat(workspaceId) : ''; config = { method: 'post', url: "".concat(this.baseUrl, "/collections").concat(workspaceIdParam), headers: { 'Content-Type': 'application/json', 'X-API-Key': this.apiKey }, data: data }; spinner = (0, ora_1.default)({ prefixText: ' ', text: 'Creating collection in Postman ...\n' }); spinner.start(); _b.label = 1; case 1: _b.trys.push([1, 6, , 7]); axios_1.default.interceptors.request.use(function (req) { spinner.text = "Executing Request. Waiting on response...\n"; return req; }); axios_1.default.interceptors.response.use(function (response) { spinner.text = "Response Received: ".concat(response === null || response === void 0 ? void 0 : response.status, "\n"); responseStatusCode = response.status; return response; }, function (error) { var _a; if (!error.response) { error.response = {}; } responseStatusCode = ((_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.status) || (error === null || error === void 0 ? void 0 : error.code); return error; }); response = void 0; error = void 0; _b.label = 2; case 2: _b.trys.push([2, 4, , 5]); return [4, axios_1.default.request(config)]; case 3: response = _b.sent(); return [3, 5]; case 4: err_1 = _b.sent(); error = err_1; response = error === null || error === void 0 ? void 0 : error.response; return [3, 5]; case 5: respData = response === null || response === void 0 ? void 0 : response.data; if (responseStatusCode < 300) { spinner.succeed('Upload to Postman Success'); return [2, JSON.stringify({ status: 'success', data: respData }, null, 2)]; } else { spinner.fail(chalk_1.default.red("Upload to Postman Failed with status: ".concat(responseStatusCode))); return [2, JSON.stringify({ status: 'fail', data: respData }, null, 2)]; } return [3, 7]; case 6: error_5 = _b.sent(); spinner.fail(chalk_1.default.red("Upload to Postman Failed: ".concat(responseStatusCode))); spinner.clear(); return [2, JSON.stringify({ status: 'fail', data: ((_a = error_5 === null || error_5 === void 0 ? void 0 : error_5.response) === null || _a === void 0 ? void 0 : _a.data) || error_5.response || (error_5 === null || error_5 === void 0 ? void 0 : error_5.toJSON()) || (error_5 === null || error_5 === void 0 ? void 0 : error_5.toString()) }, null, 2)]; case 7: return [2]; } }); }); }; PostmanApiService.prototype.updateCollection = function (collection, postmanUid, workspaceId) { var _a, _b; return tslib_1.__awaiter(this, void 0, void 0, function () { var data, workspaceIdParam, config, spinner, responseStatusCode, responseStatusMessage, response, error, err_2, respData, error_6; return tslib_1.__generator(this, function (_c) { switch (_c.label) { case 0: data = JSON.stringify({ collection: collection }); workspaceIdParam = workspaceId ? "?workspace=".concat(workspaceId) : ''; config = { method: 'put', url: "".concat(this.baseUrl, "/collections/").concat(postmanUid).concat(workspaceIdParam), maxContentLength: Infinity, maxBodyLength: Infinity, headers: { 'Content-Type': 'application/json', 'X-API-Key': this.apiKey }, data: data }; spinner = (0, ora_1.default)({ prefixText: ' ', text: 'Updating collection in Postman ...\n' }); spinner.start(); _c.label = 1; case 1: _c.trys.push([1, 6, , 7]); axios_1.default.interceptors.request.use(function (req) { spinner.text = "Executing Request. Waiting on response...\n"; return req; }); axios_1.default.interceptors.response.use(function (response) { spinner.text = "Response Received: ".concat(response === null || response === void 0 ? void 0 : response.status, "\n"); responseStatusCode = response.status; responseStatusMessage = response.statusText; return response; }, function (error) { var _a, _b; if (!error.response) { error.response = {}; } responseStatusCode = ((_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.status) || (error === null || error === void 0 ? void 0 : error.code); responseStatusMessage = ((_b = error === null || error === void 0 ? void 0 : error.response) === null || _b === void 0 ? void 0 : _b.statusText) || (error === null || error === void 0 ? void 0 : error.code); return error; }); response = void 0; error = void 0; _c.label = 2; case 2: _c.trys.push([2, 4, , 5]); return [4, axios_1.default.request(config)]; case 3: response = _c.sent(); return [3, 5]; case 4: err_2 = _c.sent(); error = err_2; response = error === null || error === void 0 ? void 0 : error.response; return [3, 5]; case 5: respData = (_a = response === null || response === void 0 ? void 0 : response.data) !== null && _a !== void 0 ? _a : {}; if (responseStatusCode < 300) { spinner.succeed("Upload to Postman Succeeded"); } else { spinner.succeed("Upload to Postman completed with status: ".concat(responseStatusCode, " - ").concat(responseStatusMessage, " \n\n Please review your collection within Postman as they can respond with a 5xx but still import your collection")); } return [2, JSON.stringify({ status: 'success', data: tslib_1.__assign(tslib_1.__assign({}, respData), { collection: collection }) }, null, 2)]; case 6: error_6 = _c.sent(); spinner.fail(chalk_1.default.red("Upload to Postman Failed: ".concat(responseStatusCode, " - ").concat(responseStatusMessage))); spinner.clear(); return [2, JSON.stringify({ status: 'fail', data: ((_b = error_6 === null || error_6 === void 0 ? void 0 : error_6.response) === null || _b === void 0 ? void 0 : _b.data) || error_6.response || (error_6 === null || error_6 === void 0 ? void 0 : error_6.toJSON()) || (error_6 === null || error_6 === void 0 ? void 0 : error_6.toString()) }, null, 2)]; case 7: return [2]; } }); }); }; PostmanApiService.prototype.deleteCollection = function (postmanUid) { var _a, _b, _c; return tslib_1.__awaiter(this, void 0, void 0, function () { var config, spinner, responseStatusCode, response, error, err_3, respData, error_7; return tslib_1.__generator(this, function (_d) { switch (_d.label) { case 0: config = { method: 'delete', url: "".concat(this.baseUrl, "/collections/").concat(postmanUid), headers: { 'Content-Type': 'application/json', 'X-API-Key': this.apiKey } }; spinner = (0, ora_1.default)({ prefixText: ' ', text: 'Deleting collection in Postman ...\n' }); spinner.start(); _d.label = 1; case 1: _d.trys.push([1, 6, , 7]); axios_1.default.interceptors.request.use(function (req) { spinner.text = "Executing Request. Waiting on response...\n"; return req; }); axios_1.default.interceptors.response.use(function (response) { spinner.text = "Response Received: ".concat(response === null || response === void 0 ? void 0 : response.status, "\n"); responseStatusCode = response.status; return response; }, function (error) { var _a; if (!error.response) { error.response = {}; } responseStatusCode = ((_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.status) || (error === null || error === void 0 ? void 0 : error.code); return error; }); response = void 0; error = void 0; _d.label = 2; case 2: _d.trys.push([2, 4, , 5]); return [4, axios_1.default.request(config)]; case 3: response = _d.sent(); return [3, 5]; case 4: err_3 = _d.sent(); error = err_3; response = error === null || error === void 0 ? void 0 : error.response; return [3, 5]; case 5: respData = (_a = response === null || response === void 0 ? void 0 : response.data) !== null && _a !== void 0 ? _a : {}; if (responseStatusCode < 300 || responseStatusCode === 404) { spinner.succeed("Delete from Postman Completed"); return [2, JSON.stringify({ status: 'success', data: tslib_1.__assign({}, respData) }, null, 2)]; } else { spinner.fail("Delete from Postman Failed: ".concat(responseStatusCode)); spinner.clear(); return [2, JSON.stringify({ status: 'fail', data: ((_b = error === null || error === void 0 ? void 0 : error.response) === null || _b === void 0 ? void 0 : _b.data) || (error === null || error === void 0 ? void 0 : error.response) || (error === null || error === void 0 ? void 0 : error.toJSON()) || (error === null || error === void 0 ? void 0 : error.toString()) }, null, 2)]; } return [3, 7]; case 6: error_7 = _d.sent(); spinner.fail(chalk_1.default.red("Delete from Postman Failed: ".concat(responseStatusCode))); spinner.clear(); return [2, JSON.stringify({ status: 'fail', data: ((_c = error_7 === null || error_7 === void 0 ? void 0 : error_7.response) === null || _c === void 0 ? void 0 : _c.data) || error_7.response || (error_7 === null || error_7 === void 0 ? void 0 : error_7.toJSON()) || (error_7 === null || error_7 === void 0 ? void 0 : error_7.toString()) }, null, 2)]; case 7: return [2]; } }); }); }; PostmanApiService.prototype.isGuid = function (value) { return /^\{?[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\}?$/.test(value); }; return PostmanApiService; }()); exports.PostmanApiService = PostmanApiService; //# sourceMappingURL=PostmanApiService.js.map