UNPKG

@intuitionrobotics/thunderstorm

Version:
109 lines 4.8 kB
/* * 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. */ import { __stringify, ImplementationMissingException, Module } from "@intuitionrobotics/ts-common"; // noinspection TypeScriptPreferShortImport import {} from "../../../shared/types.js"; import { promisifyRequest } from "../../utils/promisify-request.js"; import { ApiException } from "../../exceptions.js"; import {} from "axios"; export class RemoteProxyCaller extends Module { // noinspection TypeScriptAbstractClassConstructorCanBeMadeProtected constructor(name) { super(name); } init() { if (!this.config) throw new ImplementationMissingException(`MUST specify config for ${this.getName()}`); if (!this.config.proxyId) throw new ImplementationMissingException(`MUST specify the proxyId for ${this.getName()}`); if (!this.config.url) throw new ImplementationMissingException(`MUST specify the url for the remote server for ${this.getName()}`); if (!this.config.secret) throw new 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'; } executeGetRequest = async (url, _params, _headers) => { const resp = await this.executeGetRequestImpl(url, _params, _headers); return resp.data; }; executeGetRequestImpl = async (url, _params, _headers) => { 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: { ..._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); }; executePostRequest = async (url, body, _headers) => { const resp = await this.executePostRequestImpl(url, body, _headers); return resp.data; }; executePostRequestImpl = async (url, body, _headers) => { const proxyRequest = { headers: { ..._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); }; executeRequest = async (proxyRequest) => { const response = await 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 ApiException(500, `Extraneous error ${__stringify(response)}, Proxy Request: ${__stringify(proxyRequest, true)}`); const debugMessage = typeof errorResponse === 'object' ? errorResponse['debugMessage'] : errorResponse; const e = new ApiException(response.status, `Redirect proxy error: ${debugMessage} \n Proxy Request: ${__stringify(proxyRequest, true)}`); if (errorResponse.error) e.setErrorBody(errorResponse.error); throw e; } return response; }; } //# sourceMappingURL=RemoteProxyCaller.js.map