From 9c099b899c42e30dfcd919a2d329722461e6b72d Mon Sep 17 00:00:00 2001 From: sabhas Date: Thu, 9 Sep 2021 11:03:48 +0500 Subject: [PATCH] fix: If the request client has already been instantiated, update config --- src/SASjs.ts | 5 +++++ src/request/RequestClient.ts | 40 +++++++++++++++++++++--------------- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/SASjs.ts b/src/SASjs.ts index 9eff5e6..ea2fff5 100644 --- a/src/SASjs.ts +++ b/src/SASjs.ts @@ -917,6 +917,11 @@ export default class SASjs { this.sasjsConfig.serverUrl, this.sasjsConfig.allowInsecureRequests ) + } else { + this.requestClient.setConfig( + this.sasjsConfig.serverUrl, + this.sasjsConfig.allowInsecureRequests + ) } this.jobsPath = diff --git a/src/request/RequestClient.ts b/src/request/RequestClient.ts index 53444e9..5e84187 100644 --- a/src/request/RequestClient.ts +++ b/src/request/RequestClient.ts @@ -52,25 +52,14 @@ export class RequestClient implements HttpClient { protected csrfToken: CsrfToken = { headerName: '', value: '' } protected fileUploadCsrfToken: CsrfToken | undefined - protected httpClient: AxiosInstance + protected httpClient!: AxiosInstance constructor(protected baseUrl: string, allowInsecure = false) { - const https = require('https') - if (allowInsecure && https.Agent) { - this.httpClient = axios.create({ - baseURL: baseUrl, - httpsAgent: new https.Agent({ - rejectUnauthorized: !allowInsecure - }) - }) - } else { - this.httpClient = axios.create({ - baseURL: baseUrl - }) - } + this.instantiateHttpClient(baseUrl, allowInsecure) + } - this.httpClient.defaults.validateStatus = (status) => - status >= 200 && status < 305 + public setConfig(baseUrl: string, allowInsecure = false) { + this.instantiateHttpClient(baseUrl, allowInsecure) } public getCsrfToken(type: 'general' | 'file' = 'general') { @@ -517,6 +506,25 @@ export class RequestClient implements HttpClient { return responseToReturn } + + private instantiateHttpClient(baseUrl: string, allowInsecure = false) { + const https = require('https') + if (allowInsecure && https.Agent) { + this.httpClient = axios.create({ + baseURL: baseUrl, + httpsAgent: new https.Agent({ + rejectUnauthorized: !allowInsecure + }) + }) + } else { + this.httpClient = axios.create({ + baseURL: baseUrl + }) + } + + this.httpClient.defaults.validateStatus = (status) => + status >= 200 && status < 305 + } } export const throwIfError = (response: AxiosResponse) => {