UNPKG

@nimbella/postman-api

Version:

Postman Collection to Nimbella Project: Take your APIs seamlessly into Serverless world with this API

97 lines (96 loc) 4.1 kB
"use strict"; /* * Copyright (c) 2019 - present Nimbella Corp. * * This file is licensed to you under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy * of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS * OF ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const axios_1 = tslib_1.__importDefault(require("axios")); const rax = tslib_1.__importStar(require("retry-axios")); const chalk_1 = require("chalk"); const logger_1 = tslib_1.__importDefault(require("./logger")); /* Postman’s bag is always heavy because it carries the life itself: It carries all the sorrows and all the joys, all the worries and all the hopes! */ class PostmanSyncer { constructor(apiKey) { this.baseUrl = 'https://api.getpostman.com/'; this.createCollection = async (collectionData) => { let output = ''; try { output = await (await this.instance.post(`${this.baseUrl}collections`, collectionData)).data; } catch (error) { logger_1.default.error(error); console.log(`Couldn't create collection at: ${this.baseUrl}`); } return output; }; this.updateCollection = async (collectionId, collectionData) => { let output; try { output = await (await this.instance.put(`${this.baseUrl}collections/${collectionId}`, collectionData)).data; console.log(`the updated collection ${chalk_1.green(output.collection.name)} has been synced back to Postman`); } catch (error) { logger_1.default.error(error); console.log(`Couldn't update collection at: ${this.baseUrl}`); } return output; }; this.deleteCollection = async (collectionId) => { let output = ''; try { output = await (await this.instance.delete(`${this.baseUrl}collections/${collectionId}`)).data; } catch (error) { logger_1.default.error(error); console.log(`Couldn't delete collection from: ${this.baseUrl}`); } return output; }; this.forkCollection = async (collectionId, workspaceId) => { let output = ''; try { output = await (await this.instance.post(`${this.baseUrl}collections/fork/${collectionId}?workspace=${workspaceId}`)).data; } catch (error) { logger_1.default.error(error); console.log(`Couldn't from collection from: ${this.baseUrl}`); } return output; }; this.mergeCollection = async (sourceCollectionId, destinationCollectionId) => { let output = ''; const payload = { strategy: 'deleteSource', source: sourceCollectionId, destination: destinationCollectionId, }; try { output = await (await this.instance.post(`${this.baseUrl}collections/merge`, JSON.stringify(payload))).data; } catch (error) { logger_1.default.error(error); console.log(`Couldn't merge collection at: ${this.baseUrl}`); } return output; }; this.apiKey = apiKey; this.instance = axios_1.default.create({ baseURL: this.baseUrl, headers: { 'X-Api-Key': this.apiKey, 'Content-Type': 'application/json' }, }); rax.attach(this.instance); } } exports.default = PostmanSyncer;