UNPKG

@nimbella/postman-api

Version:

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

95 lines (94 loc) 3.81 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 postman_collection_1 = require("postman-collection"); const logger_1 = tslib_1.__importDefault(require("./logger")); /* I don't celebrate because I'm only doing my job. When a postman delivers letters, does he celebrate? */ class PostmanFetcher { constructor(apiKey) { this.baseUrl = 'https://api.getpostman.com/'; this.getAllCollections = async () => { let output = []; try { output = await (await this.instance.get(`${this.baseUrl}collections`)) .data.collections; } catch (error) { logger_1.default.error(error); } return output; }; this.getCollection = async (collectionId) => { let output; try { output = await (await this.instance.get(`${this.baseUrl}collections/${collectionId}`)).data; } catch (error) { const msg = `Could not fetch the collection ${collectionId} from server! Please check if it exists.`; logger_1.default.warn(msg); throw new Error(msg); } return output; }; this.getCollectionGuid = async (name) => { try { const collections = await this.getAllCollections(); return collections.find(item => item.name.toUpperCase() === name.toUpperCase()).id; } catch (error) { const msg = `Could not fetch the collection ${name} from server! Please check if it exists.`; console.warn(msg); logger_1.default.warn(error); throw new Error(error.message); } }; this.getCollectionWithVersion = async (id) => { try { const collectionData = await this.getCollection(id); const { collection } = collectionData; return { isVersion2X: ((collection.info || '').schema || '').includes('v2'), collection: new postman_collection_1.Collection(collection), }; } catch (error) { console.log(`Error getting collection ${id} from the Postman API`); return { isVersion2X: false, collection: undefined }; } }; this.getUser = async () => { let output = ''; try { output = await (await this.instance.get(`${this.baseUrl}me/`)).data; } catch (error) { logger_1.default.error(error); } return output; }; this.apiKey = apiKey; rax.attach(); this.instance = axios_1.default.create({ baseURL: this.baseUrl, headers: { 'X-Api-Key': this.apiKey }, }); rax.attach(this.instance); } } exports.default = PostmanFetcher;