UNPKG

@intuitionrobotics/thunderstorm

Version:
116 lines 5.11 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); } // Was init(): config is asserted and defaulted lazily, on the first proxied request. assertedProxyConfig; get proxyConfig() { if (this.assertedProxyConfig) return this.assertedProxyConfig; 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()}`); return this.assertedProxyConfig = { proxyId: this.config.proxyId, url: this.config.url, secret: this.config.secret, secretHeaderName: this.config.secretHeaderName || 'x-secret', 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 proxyConfig = this.proxyConfig; const proxyRequest = { headers: { ..._headers, [proxyConfig.secretHeaderName]: proxyConfig.secret, [proxyConfig.proxyHeaderName]: proxyConfig.proxyId }, url: `${proxyConfig.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 proxyConfig = this.proxyConfig; const proxyRequest = { headers: { ..._headers, 'Content-Type': 'application/json', [proxyConfig.secretHeaderName]: proxyConfig.secret, [proxyConfig.proxyHeaderName]: proxyConfig.proxyId }, responseType: "json", url: `${proxyConfig.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.proxyConfig.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