UNPKG

@apideck/portman

Version:

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

255 lines 13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PostmanSyncService = void 0; var tslib_1 = require("tslib"); var _1 = require("./"); var PostmanRepo_1 = require("./PostmanRepo"); var Either = tslib_1.__importStar(require("fp-ts/Either")); var PostmanSyncService = (function () { function PostmanSyncService(_a) { var _b = _a.postmanApi, postmanApi = _b === void 0 ? new _1.PostmanApiService() : _b, portmanCollection = _a.portmanCollection, postmanUid = _a.postmanUid, postmanWorkspaceName = _a.postmanWorkspaceName, collectionName = _a.collectionName, cacheFile = _a.cacheFile, postmanFastSync = _a.postmanFastSync, postmanRefreshCache = _a.postmanRefreshCache, syncPostmanCollectionIds = _a.syncPostmanCollectionIds; var _c; this.postmanApi = postmanApi; this.postmanUid = postmanUid; this.postmanWorkspaceName = postmanWorkspaceName; this.cacheFile = cacheFile || "".concat(process.cwd(), "/tmp/.portman.cache.json"); this.postmanRepo = new PostmanRepo_1.PostmanRepo(this.cacheFile, this.postmanApi); this.portmanCollection = portmanCollection; this.collectionName = collectionName || ((_c = portmanCollection === null || portmanCollection === void 0 ? void 0 : portmanCollection.info) === null || _c === void 0 ? void 0 : _c.name); this.postmanFastSync = postmanFastSync !== null && postmanFastSync !== void 0 ? postmanFastSync : false; this.postmanRefreshCache = postmanRefreshCache !== null && postmanRefreshCache !== void 0 ? postmanRefreshCache : false; this.syncPostmanCollectionIds = syncPostmanCollectionIds !== null && syncPostmanCollectionIds !== void 0 ? syncPostmanCollectionIds : false; if (this.postmanUid) { this.postmanFastSync = false; } this.state = {}; if (!this.collectionName) { throw new Error("Postman collection name is required. Please ensure your OpenAPI document has title."); } } PostmanSyncService.prototype.sync = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { var _a, shouldCreate, shouldUpdate, deleteResponse, status_1, collCreate; return tslib_1.__generator(this, function (_b) { switch (_b.label) { case 0: return [4, this.postmanRepo.initCache(this.postmanRefreshCache, this.postmanRefreshCache)]; case 1: _b.sent(); return [4, this.validateState()]; case 2: _b.sent(); _a = this.state, shouldCreate = _a.shouldCreate, shouldUpdate = _a.shouldUpdate; if (!this.postmanFastSync) return [3, 4]; return [4, this.deleteCollection()]; case 3: deleteResponse = _b.sent(); status_1 = JSON.parse(deleteResponse).status; if (status_1 === 'fail') { return [2, deleteResponse]; } _b.label = 4; case 4: if (!((shouldCreate && !shouldUpdate) || this.postmanFastSync)) return [3, 7]; return [4, this.createCollection()]; case 5: collCreate = _b.sent(); return [4, this.postmanRepo.initCache(true, false)]; case 6: _b.sent(); return [2, collCreate]; case 7: if (!(this.syncPostmanCollectionIds && shouldUpdate)) return [3, 9]; return [4, this.synchronizeCollectionIds()]; case 8: _b.sent(); _b.label = 9; case 9: return [4, this.updateCollection()]; case 10: return [2, _b.sent()]; } }); }); }; PostmanSyncService.prototype.validateState = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { var state, _a, _b, shouldCreate, shouldUpdate; return tslib_1.__generator(this, function (_c) { switch (_c.label) { case 0: if (!this.portmanCollection) { throw new Error('No collection provided'); } state = this.state; state.collectionName = this.collectionName; _a = state; return [4, this.lookupWorkspaceId()]; case 1: _a.workspaceId = _c.sent(); state.shouldCreate = this.shouldCreate(); state.shouldUpdate = this.shouldUpdate(); _b = this.state, shouldCreate = _b.shouldCreate, shouldUpdate = _b.shouldUpdate; if (!shouldCreate && !shouldUpdate) { console.log(this.state); throw new Error('Sync is in an invalid state'); } if (state.shouldCreate) { this.postmanUid = undefined; this.state.postmanUid = undefined; } return [2]; } }); }); }; PostmanSyncService.prototype.shouldCreate = function () { if (this.postmanUid && this.shouldUpdate()) return false; var knownCollection = this.lookupCollection(); if (!knownCollection) { return true; } if (this.state.workspaceId) { return !this.existsInWorkspace(); } if (knownCollection) { this.postmanUid = knownCollection.uid; this.state.postmanUid = knownCollection.uid; } return false; }; PostmanSyncService.prototype.shouldUpdate = function () { var knownCollection = this.lookupCollection(); if (knownCollection) { this.postmanUid = knownCollection.uid; this.state.postmanUid = knownCollection.uid; } if (this.state.workspaceId) { return this.existsInWorkspace(); } return !!this.postmanUid && !!knownCollection; }; PostmanSyncService.prototype.lookupCollection = function () { var knownCollection = this.postmanUid ? this.postmanRepo.findCollectionByUid(this.postmanUid) : this.postmanRepo.findCollectionByName(this.collectionName); return knownCollection; }; PostmanSyncService.prototype.existsInWorkspace = function () { var workspaceCollection = this.state.workspaceId ? this.postmanRepo.findWorkspaceCollectionByName(this.collectionName) : undefined; if (workspaceCollection === null || workspaceCollection === void 0 ? void 0 : workspaceCollection.uid) { this.postmanUid = workspaceCollection.uid; this.state.postmanUid = workspaceCollection.uid; } return !!this.state.workspaceId && !!workspaceCollection; }; PostmanSyncService.prototype.lookupWorkspaceId = function () { var _a; return tslib_1.__awaiter(this, void 0, void 0, function () { var workspaceId; return tslib_1.__generator(this, function (_b) { switch (_b.label) { case 0: if (!this.postmanWorkspaceName) return [2, undefined]; workspaceId = (_a = this.postmanRepo.findWorkspaceByName(this.postmanWorkspaceName)) === null || _a === void 0 ? void 0 : _a.id; if (!workspaceId) { throw new Error("Could not find workspace with name ".concat(this.postmanWorkspaceName)); } return [4, this.postmanRepo.getWorkspace(workspaceId)]; case 1: _b.sent(); return [2, workspaceId]; } }); }); }; PostmanSyncService.prototype.createCollection = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { var _a, workspaceId, portmanCollection; return tslib_1.__generator(this, function (_b) { _a = this, workspaceId = _a.state.workspaceId, portmanCollection = _a.portmanCollection; return [2, this.postmanApi.createCollection(portmanCollection, workspaceId)]; }); }); }; PostmanSyncService.prototype.updateCollection = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { var _a, _b, postmanUid, workspaceId, portmanCollection; return tslib_1.__generator(this, function (_c) { _a = this, _b = _a.state, postmanUid = _b.postmanUid, workspaceId = _b.workspaceId, portmanCollection = _a.portmanCollection; return [2, this.postmanApi.updateCollection(portmanCollection, postmanUid, workspaceId)]; }); }); }; PostmanSyncService.prototype.deleteCollection = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { var postmanUid; return tslib_1.__generator(this, function (_a) { postmanUid = this.state.postmanUid; return [2, this.postmanApi.deleteCollection(postmanUid)]; }); }); }; PostmanSyncService.prototype.synchronizeCollectionIds = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { var collectionResult, postmanCollection, portmanCollection; var _this = this; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: if (!this.postmanUid) { throw new Error('Postman uid must be filled in.'); } return [4, this.postmanApi.getCollection(this.postmanUid)]; case 1: collectionResult = _a.sent(); if (Either.isLeft(collectionResult)) { throw collectionResult.left; } postmanCollection = collectionResult.right; portmanCollection = this.portmanCollection; if (!('item' in portmanCollection && postmanCollection && postmanCollection.item)) { throw new Error("Portman/Postman Collection doesn't contains any items."); } postmanCollection.item.forEach(function (postmanItem) { _this.replaceCollectionId(postmanItem, portmanCollection); }); return [2]; } }); }); }; PostmanSyncService.prototype.replaceCollectionId = function (postmanItem, portmanItems) { var _this = this; var _a; var portmanItemCommon = (_a = portmanItems.item) === null || _a === void 0 ? void 0 : _a.find(function (portmanItem) { return portmanItem.name === postmanItem.name; }); if (portmanItemCommon) { portmanItemCommon.id = postmanItem.id; if ('response' in postmanItem && 'response' in portmanItemCommon && postmanItem.response && portmanItemCommon.response) { this.replaceResponsesId(postmanItem.response, portmanItemCommon.response); } else if ('item' in postmanItem && postmanItem.item) { postmanItem.item.forEach(function (item) { if (portmanItemCommon && 'item' in portmanItemCommon && postmanItem) { _this.replaceCollectionId(item, portmanItemCommon); } }); } } }; PostmanSyncService.prototype.replaceResponsesId = function (postmanResponseItems, portmanResponseItems) { postmanResponseItems.forEach(function (postmanResponseItem) { var portmanResponseCommon = portmanResponseItems.find(function (portmanResponseItem) { return portmanResponseItem.name === postmanResponseItem.name; }); if (portmanResponseCommon) { portmanResponseCommon.id = postmanResponseItem.id; } }); }; return PostmanSyncService; }()); exports.PostmanSyncService = PostmanSyncService; //# sourceMappingURL=PostmanSyncService.js.map