Skip to content

Commit

Permalink
trying something
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalk007 committed Oct 16, 2024
1 parent 65c3e9f commit 39a2038
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 41 deletions.
20 changes: 13 additions & 7 deletions package-lock.json

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

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"dist/**/*"
],
"dependencies": {
"axios": "^0.28.0",
"axios": "^1.7.7",
"axios-proxy": "^0.0.1-security",
"axios-retry": "~3.2.5",
"https-proxy-agent": "^5.0.1"
},
Expand All @@ -42,11 +43,11 @@
"eslint-config-prettier": "^8.5.0",
"faker": "5.5.3",
"http-proxy": "^1.18.1",
"jest": "^29.3.1",
"jest": "^29.7.0",
"nock": "^13.2.4",
"prettier": "^2.6.2",
"tmp": "^0.2.1",
"ts-jest": "^29.0.3",
"ts-jest": "^29.2.5",
"typescript": "^4.7.2"
},
"keywords": [
Expand Down
41 changes: 10 additions & 31 deletions src/HttpClient.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import axios, {
AxiosError,
AxiosInstance,
AxiosProxyConfig,
AxiosRequestConfig,
AxiosResponse,
AxiosResponseHeaders
} from 'axios';
import axios, { AxiosError, AxiosInstance, AxiosProxyConfig, AxiosRequestConfig } from 'axios';
import { IClientResponse, ILogger, IProxyConfig, RetryOnStatusCode } from '../model';
import axiosRetry, { IAxiosRetryConfig } from 'axios-retry';
import { HttpsProxyAgent } from 'https-proxy-agent';
Expand All @@ -21,10 +14,12 @@ export class HttpClient {
private readonly _basicAuth: BasicAuth;
private readonly _accessToken: string;
private readonly _axiosInstance: AxiosInstance;
private readonly _proxy: any

constructor(config: IHttpConfig, private logger?: ILogger) {
config.headers = config.headers || {};
this.addUserAgentHeader(config.headers);
this._proxy = this.getAxiosProxyConfig(config.proxy);
this._axiosInstance = axios.create({
baseURL: config.serverUrl,
headers: config.headers,
Expand Down Expand Up @@ -63,15 +58,16 @@ export class HttpClient {
}

public async doRequest(requestParams: IRequestParams): Promise<IClientResponse> {
const response : AxiosResponse = await this._axiosInstance(requestParams);
// Example proxy configuration

return {
data: response.data,
headers: this.mapHeaders(response.headers), // Use your mapHeaders function
status: response.status,
// Merge the proxy config into the request params
const requestWithProxy : any = {
...requestParams, // Keep original request params
proxy: this._proxy // Add proxy configuration
};
}

return await this._axiosInstance(requestWithProxy);
}


public async doAuthRequest(requestParams: IRequestParams): Promise<IClientResponse> {
Expand Down Expand Up @@ -109,23 +105,6 @@ export class HttpClient {
}
}

private mapHeaders(headers?: AxiosResponseHeaders): { [key: string]: string } {
const mappedHeaders: { [key: string]: string } = {};

if (!headers) {
return mappedHeaders;
}

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

return mappedHeaders;
}

/**
* Use to create httpsAgent to handle Http proxy sending to a https server.
* (Artifactory is https server, proxy protocol can be both)
Expand Down

0 comments on commit 39a2038

Please sign in to comment.