UNPKG

@intuitionrobotics/thunderstorm

Version:
110 lines 6.01 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RemoteProxyCaller = void 0; /* * Thunderstorm is a full web app framework! * * Typescript & Express backend infrastructure that natively runs on firebase function * Typescript & React frontend infrastructure * * Copyright (C) 2020 Intuition Robotics * * Licensed 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 CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ const ts_common_1 = require("@intuitionrobotics/ts-common"); const promisify_request_1 = require("../../utils/promisify-request"); const exceptions_1 = require("../../exceptions"); class RemoteProxyCaller extends ts_common_1.Module { // noinspection TypeScriptAbstractClassConstructorCanBeMadeProtected constructor(name) { super(name); this.executeGetRequest = (url, _params, _headers) => __awaiter(this, void 0, void 0, function* () { const resp = yield this.executeGetRequestImpl(url, _params, _headers); return resp.data; }); this.executeGetRequestImpl = (url, _params, _headers) => __awaiter(this, void 0, void 0, function* () { const params = _params && Object.keys(_params).map((key) => { return `${key}=${_params[key]}`; }); let urlParams = ""; if (params && params.length > 0) urlParams = `?${params.join("&")}`; const proxyRequest = { headers: Object.assign(Object.assign({}, _headers), { [this.config.secretHeaderName]: this.config.secret, [this.config.proxyHeaderName]: this.config.proxyId }), url: `${this.config.url}${url}${urlParams}`, method: 'GET', responseType: 'json' }; return this.executeRequest(proxyRequest); }); this.executePostRequest = (url, body, _headers) => __awaiter(this, void 0, void 0, function* () { const resp = yield this.executePostRequestImpl(url, body, _headers); return resp.data; }); this.executePostRequestImpl = (url, body, _headers) => __awaiter(this, void 0, void 0, function* () { const proxyRequest = { headers: Object.assign(Object.assign({}, _headers), { 'Content-Type': 'application/json', [this.config.secretHeaderName]: this.config.secret, [this.config.proxyHeaderName]: this.config.proxyId }), responseType: "json", url: `${this.config.url}${url}`, data: body, method: 'POST' }; return this.executeRequest(proxyRequest); }); this.executeRequest = (proxyRequest) => __awaiter(this, void 0, void 0, function* () { const response = yield (0, promisify_request_1.promisifyRequest)(proxyRequest); if (proxyRequest.headers) { delete proxyRequest.headers[this.config.secretHeaderName]; delete proxyRequest.headers["Authorization"]; } const statusCode = response.status; // TODO: need to handle 1XX and 3XX if (statusCode < 200 || statusCode >= 300) { const errorResponse = response.data; if (!errorResponse) throw new exceptions_1.ApiException(500, `Extraneous error ${(0, ts_common_1.__stringify)(response)}, Proxy Request: ${(0, ts_common_1.__stringify)(proxyRequest, true)}`); const debugMessage = typeof errorResponse === 'object' ? errorResponse['debugMessage'] : errorResponse; const e = new exceptions_1.ApiException(response.status, `Redirect proxy error: ${debugMessage} \n Proxy Request: ${(0, ts_common_1.__stringify)(proxyRequest, true)}`); if (errorResponse.error) e.setErrorBody(errorResponse.error); throw e; } return response; }); } init() { if (!this.config) throw new ts_common_1.ImplementationMissingException(`MUST specify config for ${this.getName()}`); if (!this.config.proxyId) throw new ts_common_1.ImplementationMissingException(`MUST specify the proxyId for ${this.getName()}`); if (!this.config.url) throw new ts_common_1.ImplementationMissingException(`MUST specify the url for the remote server for ${this.getName()}`); if (!this.config.secret) throw new ts_common_1.ImplementationMissingException(`MUST specify the secret for the remote server for ${this.getName()}`); if (!this.config.secretHeaderName) this.config.secretHeaderName = 'x-secret'; if (!this.config.proxyHeaderName) this.config.proxyHeaderName = 'x-proxy'; } } exports.RemoteProxyCaller = RemoteProxyCaller; //# sourceMappingURL=RemoteProxyCaller.js.map