This repository was archived by the owner on Apr 23, 2026. It is now read-only.
forked from JayPNanduri/ast-cli-javascript-wrapper-jay
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHttpClient.ts
More file actions
44 lines (40 loc) · 1.76 KB
/
HttpClient.ts
File metadata and controls
44 lines (40 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
import { logger } from '../wrapper/loggerConfig';
import { Client } from "./Client";
import { HttpsProxyAgent } from 'https-proxy-agent'; // Import as a function
export class HttpClient implements Client {
private readonly axiosConfig: AxiosRequestConfig;
private readonly domainErrMsg = 'Unable to download the CLI from the URL. Try adding the domain: \'download.checkmarx.com\' to your allow list.';
constructor() {
this.axiosConfig = {
responseType: 'stream',
httpsAgent: this.getProxyConfig(), // Use httpsAgent instead of proxy
};
}
public getProxyConfig() {
const proxyUrl = process.env.HTTP_PROXY;
if (proxyUrl) {
logger.info(`Detected proxy configuration in HTTP_PROXY`);
return new HttpsProxyAgent(proxyUrl);
}
logger.info('No proxy configuration detected.');
return undefined;
}
public async request(url: string, method: string, data: any): Promise<AxiosResponse<any, any>> {
logger.info(`Sending ${method} request to URL: ${url}`);
if (this.axiosConfig.httpsAgent) {
logger.info(`Using proxy - URL: ${process.env.HTTP_PROXY}`);
}
try {
const response = await axios({ ...this.axiosConfig, url, method, data });
logger.info(`Request completed successfully.`);
return response;
} catch (error) {
logger.error(`Error sending ${method} request to ${url}: ${error.message || error}`);
if (this.axiosConfig.httpsAgent !== undefined) {
throw new Error(`${this.domainErrMsg} \nError: ${error.message || error}`);
}
throw error;
}
}
}