UNPKG

@broadcom/endevor-bridge-for-git-for-zowe-cli

Version:

Endevor Bridge for Git plug-in for Zowe CLI

139 lines (135 loc) 5.63 kB
'use strict'; var tslib_es6 = require('../node_modules/tslib/tslib.es6.js'); var index = require('../node_modules/@broadcom/bridge-for-git-zowe-client/dist/index.js'); var imperative = require('@zowe/imperative'); /* * Copyright (c) 2019 Broadcom. All Rights Reserved. The term * "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. * * This software and all information contained therein is * confidential and proprietary and shall not be duplicated, * used, disclosed, or disseminated in any way except as * authorized by the applicable license agreement, without the * express written permission of Broadcom. All authorized * reproductions must be marked with this language. * * EXCEPT AS SET FORTH IN THE APPLICABLE LICENSE AGREEMENT, TO * THE EXTENT PERMITTED BY APPLICABLE LAW, BROADCOM PROVIDES THIS * SOFTWARE WITHOUT WARRANTY OF ANY KIND, INCLUDING WITHOUT * LIMITATION, ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL BROADCOM * BE LIABLE TO THE END USER OR ANY THIRD PARTY FOR ANY LOSS OR * DAMAGE, DIRECT OR INDIRECT, FROM THE USE OF THIS SOFTWARE, * INCLUDING WITHOUT LIMITATION, LOST PROFITS, BUSINESS * INTERRUPTION, GOODWILL, OR LOST DATA, EVEN IF BROADCOM IS * EXPRESSLY ADVISED OF SUCH LOSS OR DAMAGE. */ const SUCCESS_STATUS = 200; class EBGApiClient { constructor(session) { this.session = session; this.session = session; } send(request) { return tslib_es6.__awaiter(this, void 0, void 0, function* () { let promise; const headers = this.prepareHeaders(request); const body = request.getBody(); const writeData = typeof body === "string" ? JSON.parse(body) : request.getBody(); switch (request.getHttpMethod()) { case index.HttpMethod.POST: { promise = imperative.RestClient.postExpectFullResponse(this.session, { reqHeaders: headers, resource: request.getPathname(), writeData, }); break; } case index.HttpMethod.DELETE: { promise = imperative.RestClient.deleteExpectFullResponse(this.session, { reqHeaders: headers, resource: request.getPathname(), writeData, }); break; } case index.HttpMethod.GET: { promise = imperative.RestClient.getExpectFullResponse(this.session, { reqHeaders: headers, resource: request.getPathname(), writeData, }); break; } case index.HttpMethod.PUT: { promise = imperative.RestClient.putExpectFullResponse(this.session, { reqHeaders: headers, resource: request.getPathname(), writeData, }); break; } default: { promise = Promise.reject(`HTTP method ${request.getHttpMethod()} not supported`); } } let response; try { response = yield promise; } catch (e) { throw this.processError(e); } return new index.ResponseContext(response.response.statusCode || SUCCESS_STATUS, response.response.headers, { text: () => tslib_es6.__awaiter(this, void 0, void 0, function* () { var _a; try { return (_a = response.dataString) !== null && _a !== void 0 ? _a : ""; } catch (e) { throw this.processError(e); } }), binary: () => tslib_es6.__awaiter(this, void 0, void 0, function* () { var _a; try { return Buffer.from((_a = response.dataString) !== null && _a !== void 0 ? _a : ""); } catch (e) { throw this.processError(e); } }), }); }); } prepareHeaders(request) { const acceptHeader = request.getHeaders()["Accept"] || request.getHeaders()["accept"]; if (!acceptHeader || acceptHeader.includes("*/*")) { request.setHeaderParam("Accept", "application/json"); } return Object.entries(request.getHeaders()).map(([name, value]) => ({ [name]: value, })); } // eslint-disable-next-line @typescript-eslint/no-explicit-any processApiError(error) { try { return imperative.JSONUtils.parse(error.causeErrors); } catch (_a) { throw error; } } processError(error) { const response = this.processApiError(error); if (!response) { return error; } // in the case of RestConnectorException error (Authentication issue) the response contains message instead of messages const messages = response.message || (Array.isArray(response.messages) && response.messages.join("\n")) || response; return new imperative.ImperativeError({ msg: messages }); } } exports.EBGApiClient = EBGApiClient;