Skip to content

Commit

Permalink
updated axios
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalk007 committed Oct 16, 2024
1 parent 9864fe0 commit 867b92f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"dist/**/*"
],
"dependencies": {
"axios": "^1.7.7",
"axios": "^0.28.0",
"axios-retry": "~3.2.5",
"https-proxy-agent": "^5.0.1"
},
Expand Down
48 changes: 46 additions & 2 deletions src/HttpClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import axios, { AxiosError, AxiosInstance, AxiosProxyConfig, AxiosRequestConfig } from 'axios';
import axios, {
AxiosError,
AxiosInstance,
AxiosProxyConfig,
AxiosRequestConfig,
AxiosResponse,
AxiosResponseHeaders
} from 'axios';
import { IClientResponse, ILogger, IProxyConfig, RetryOnStatusCode } from '../model';
import axiosRetry, { IAxiosRetryConfig } from 'axios-retry';
import { HttpsProxyAgent } from 'https-proxy-agent';
Expand Down Expand Up @@ -56,9 +63,32 @@ export class HttpClient {
}

public async doRequest(requestParams: IRequestParams): Promise<IClientResponse> {
return await this._axiosInstance(requestParams);
const response : AxiosResponse = await this._axiosInstance(requestParams);
return {
data: response.data,
headers: this.mapHeaders(response.headers),
status: response.status
};
}

private mapHeaders(headers?: AxiosResponseHeaders): { [key: string]: string } {
const mappedHeaders: { [key: string]: string } = {};
if (!headers) {
return mappedHeaders; // Return an empty object if headers are undefined
}

Object.keys(headers).forEach((key) => {
const headerValue : string | undefined = headers[key];
if (headerValue !== undefined) {
mappedHeaders[key] = headerValue.toString();
}
});

return mappedHeaders;
}



public async doAuthRequest(requestParams: IRequestParams): Promise<IClientResponse> {
if (this._accessToken !== '') {
this.addAuthHeader(requestParams);
Expand All @@ -85,6 +115,20 @@ export class HttpClient {
}
}

private transformHeaders(axiosHeaders: AxiosResponseHeaders): { [key: string]: string } | undefined {
const transformedHeaders: { [key: string]: string } = {};

if (axiosHeaders) {
for (const key in axiosHeaders) {
if (axiosHeaders.hasOwnProperty(key)) {

Check failure on line 123 in src/HttpClient.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

Do not access Object.prototype method 'hasOwnProperty' from target object

Check failure on line 123 in src/HttpClient.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest)

Do not access Object.prototype method 'hasOwnProperty' from target object

Check failure on line 123 in src/HttpClient.ts

View workflow job for this annotation

GitHub Actions / test (macOS-latest)

Do not access Object.prototype method 'hasOwnProperty' from target object
transformedHeaders[key] = axiosHeaders[key] as string; // Ensure it's a string
}
}
}

return Object.keys(transformedHeaders).length > 0 ? transformedHeaders : undefined;
}

private addAuthHeader(requestParams: IRequestParams) {
if (!requestParams.headers) {
requestParams.headers = {};
Expand Down

0 comments on commit 867b92f

Please sign in to comment.