From 09940fec175d969e7866b33fa855435390922133 Mon Sep 17 00:00:00 2001 From: MarcusHurney Date: Fri, 15 Mar 2019 04:17:09 -0700 Subject: [PATCH] [DDW-599] Creates axios request instance in request.js --- source/renderer/app/api/utils/request.js | 70 +++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/source/renderer/app/api/utils/request.js b/source/renderer/app/api/utils/request.js index b5cb123616..b56d8c7ac6 100644 --- a/source/renderer/app/api/utils/request.js +++ b/source/renderer/app/api/utils/request.js @@ -1,5 +1,6 @@ // @flow -import { size, has, get, omit } from 'lodash'; +import axios from 'axios'; +import { size, has, get, omit, isEmpty } from 'lodash'; import querystring from 'querystring'; import { encryptPassphrase, getContentLength } from '.'; @@ -17,6 +18,66 @@ export type RequestOptions = { }, }; +const transformBodyData = (rawBodyParams): string => { + if (!rawBodyParams || isEmpty(rawBodyParams)) { + return ''; + } + return JSON.stringify(rawBodyParams); +}; + +const transformQueryParams = (queryParams: ?Object): ?Object => { + if (queryParams && !isEmpty(queryParams) && has(queryParams, 'passphrase')) { + return { ...queryParams, passphrase: getEncryptedPassphrase(queryParams) }; + } + return queryParams; +}; + +const getEncryptedPassphrase = (queryParams: ?Object): string => { + let queryString = ''; + if (!queryParams || isEmpty(queryParams) || size(queryParams) <= 0) { + return queryString; + } + const unencryptedPassphrase = has(queryParams, 'passphrase') ? get(queryParams, 'passphrase') : queryString; + if (unencryptedPassphrase) { + // If passphrase is present it must be encrypted + const encryptedPassphrase = encryptPassphrase(unencryptedPassphrase); + queryString = `?passphrase=${encryptedPassphrase}`; + } + return queryString; +}; + +const typedAxiosRequest = async ( + httpOptions: RequestOptions, + queryParams?: {}, + rawBodyParams?: any, + requestOptions?: { returnMeta: boolean }, +) => { + const { hostname, method, path, port, ...restOptions } = httpOptions; + const requestBody = transformBodyData(rawBodyParams); + const requestBodyLength = getContentLength(requestBody); + const headers = { + 'Content-Length': requestBodyLength, + 'Content-Type': 'application/json; charset=utf-8', + Accept: 'application/json; charset=utf-8', + }; + const params = transformQueryParams(queryParams); + const auth = { ...restOptions }; // contains key, cert, ca... where do they go? + + const requestInstance = axios.create({ + url: path, + method, + baseURL: `https://${hostname}:${port}`, + headers, + params, + data: requestBody, + responseType: 'json', + responseEncoding: 'utf8', + }); + + // begin response + const response = await requestInstance(); +}; + function typedRequest( httpOptions: RequestOptions, queryParams?: {}, @@ -24,6 +85,13 @@ function typedRequest( requestOptions?: { returnMeta: boolean }, ): Promise { return new Promise((resolve, reject) => { + + console.log("**** --- HERE ARE THE typedRequest ARGUEMENTS -------------------------------------->"); + console.log(`httpOptions: ${JSON.stringify(httpOptions, null, 0)}`); + console.log(`queryParams: ${JSON.stringify(queryParams, null, 2)}`); + console.log(`rawBodyParams: ${rawBodyParams}`); + console.log(`requestOptions: ${requestOptions}`); + const options: RequestOptions = Object.assign({}, httpOptions); const { returnMeta } = Object.assign({}, requestOptions); let hasRequestBody = false;