UNPKG

@apideck/portman

Version:

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

190 lines 8.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PostmanRepo = void 0; var tslib_1 = require("tslib"); var Either = tslib_1.__importStar(require("fp-ts/lib/Either")); var fs_extra_1 = tslib_1.__importDefault(require("fs-extra")); var PostmanRepo = (function () { function PostmanRepo(cacheFile, postmanApi) { this.cacheFile = cacheFile; this.postmanApi = postmanApi; this.cacheFile = cacheFile; } PostmanRepo.prototype.initCache = function (refreshCollectionCache, refreshWorkspaceCache) { var _a, _b, _c, _d, _e, _f; return tslib_1.__awaiter(this, void 0, void 0, function () { var _g, error_1, oneHour, collExpired, workspaceExpired, _h; return tslib_1.__generator(this, function (_j) { switch (_j.label) { case 0: _j.trys.push([0, 2, , 4]); _g = this; return [4, fs_extra_1.default.readJSON(this.cacheFile)]; case 1: _g.cache = _j.sent(); return [3, 4]; case 2: error_1 = _j.sent(); this.cache = { collectionsLastUpdated: new Date().getTime(), workspacesLastUpdated: new Date().getTime(), collections: [], workspaces: [] }; return [4, this.persistCache()]; case 3: _j.sent(); return [3, 4]; case 4: oneHour = 60 * 60 * 1000; collExpired = new Date().getTime() - ((_a = this.cache) === null || _a === void 0 ? void 0 : _a.workspacesLastUpdated) > oneHour; workspaceExpired = new Date().getTime() - ((_b = this.cache) === null || _b === void 0 ? void 0 : _b.workspacesLastUpdated) > oneHour; if (!(refreshCollectionCache || ((_d = (_c = this.cache) === null || _c === void 0 ? void 0 : _c.collections) === null || _d === void 0 ? void 0 : _d.length) === 0 || collExpired)) return [3, 6]; return [4, this.getCollections()]; case 5: _j.sent(); _j.label = 6; case 6: if (!(refreshWorkspaceCache || ((_f = (_e = this.cache) === null || _e === void 0 ? void 0 : _e.workspaces) === null || _f === void 0 ? void 0 : _f.length) === 0 || workspaceExpired)) return [3, 8]; return [4, this.getWorkspaces()]; case 7: _j.sent(); _j.label = 8; case 8: _h = this; return [4, fs_extra_1.default.readJSON(this.cacheFile)]; case 9: _h.cache = _j.sent(); return [2]; } }); }); }; PostmanRepo.prototype.persistCache = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: this.cache.collectionsLastUpdated = new Date().getTime(); this.cache.workspacesLastUpdated = new Date().getTime(); return [4, fs_extra_1.default.writeJSON(this.cacheFile, this.cache)]; case 1: _a.sent(); return [2]; } }); }); }; PostmanRepo.prototype.getWorkspaces = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { var workspacesResult, workspaces; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4, this.postmanApi.getWorkspaces()]; case 1: workspacesResult = _a.sent(); if (Either.isLeft(workspacesResult)) { throw workspacesResult.left; } workspaces = workspacesResult.right; this.cache.workspaces = workspaces; return [4, this.persistCache()]; case 2: _a.sent(); return [2, workspaces]; } }); }); }; PostmanRepo.prototype.getWorkspace = function (id) { return tslib_1.__awaiter(this, void 0, void 0, function () { var workspaceResult, workspace; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4, this.postmanApi.getWorkspace(id)]; case 1: workspaceResult = _a.sent(); if (Either.isLeft(workspaceResult)) { throw workspaceResult.left; } workspace = workspaceResult.right; this.cache.workspace = workspace; return [4, this.persistCache()]; case 2: _a.sent(); return [2, workspace]; } }); }); }; PostmanRepo.prototype.getCollections = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { var collectionsResult, collections; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4, this.postmanApi.getCollections()]; case 1: collectionsResult = _a.sent(); if (Either.isLeft(collectionsResult)) { throw collectionsResult.left; } collections = collectionsResult.right; this.cache.collections = collections; return [4, this.persistCache()]; case 2: _a.sent(); return [2, collections]; } }); }); }; PostmanRepo.prototype.findCollectionByName = function (name) { if (!this.cache.collections) { return undefined; } var results = this.cache.collections.filter(function (collection) { return collection.name.toLowerCase() === name.toLowerCase(); }); if (results.length === 0) { return undefined; } if (results.length > 1) { console.log("\n Note: Multiple Postman collection found matching \"".concat(name, "\", the most recent collection is updated.")); results.sort(function (a, b) { return new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime(); }); } return results[0]; }; PostmanRepo.prototype.findCollectionByUid = function (uid) { if (!this.cache.collections) { return undefined; } return this.cache.collections.find(function (collection) { return collection.uid === uid; }); }; PostmanRepo.prototype.findWorkspaceCollectionByName = function (name) { var _a; if (!this.cache.workspace || !name) { return undefined; } var collections = (_a = this.cache.workspace.collections) !== null && _a !== void 0 ? _a : []; var results = collections.filter(function (collection) { return collection.name.toLowerCase() === name.toLowerCase(); }); if (results.length === 0) { return undefined; } return results[0]; }; PostmanRepo.prototype.findWorkspaceByName = function (name) { if (!this.cache.workspaces) { return undefined; } return this.cache.workspaces.find(function (workspace) { return workspace.name.toLowerCase() === name.toLowerCase(); }); }; PostmanRepo.prototype.findWorkspaceById = function (id) { if (!this.cache.workspaces) { return undefined; } return this.cache.workspaces.find(function (workspace) { return workspace.id === id; }); }; return PostmanRepo; }()); exports.PostmanRepo = PostmanRepo; //# sourceMappingURL=PostmanRepo.js.map