UNPKG

@capawesome/cli

Version:

The Capawesome Cloud Command Line Interface (CLI) to manage Live Updates and more.

102 lines (101 loc) 4.93 kB
import { createRequire } from 'module'; import configService from '../services/config.js'; import axios from 'axios'; import axiosRetry from 'axios-retry'; import { HttpProxyAgent } from 'http-proxy-agent'; import { HttpsProxyAgent } from 'https-proxy-agent'; const require = createRequire(import.meta.url); const pkg = require('../../package.json'); // Register middleware to retry failed requests axiosRetry(axios, { retries: 3, retryDelay: axiosRetry.exponentialDelay, retryCondition: (error) => { // Network errors and 5xx responses are retried return (axiosRetry.isNetworkOrIdempotentRequestError(error) || (error.response?.status !== undefined && error.response.status >= 500)); }, }); /** * Gets the appropriate proxy agent based on the target URL protocol and environment variables. * This ensures that HTTPS requests use HTTPS even when the proxy itself is accessed via HTTP. */ function getProxyAgent(targetUrl) { const isHttps = targetUrl.startsWith('https://'); const proxyUrl = isHttps ? process.env.HTTPS_PROXY || process.env.https_proxy : process.env.HTTP_PROXY || process.env.http_proxy; if (!proxyUrl) { return undefined; } // Use the appropriate agent based on the TARGET protocol, not the proxy protocol // This allows using an HTTP proxy for HTTPS requests return isHttps ? new HttpsProxyAgent(proxyUrl) : new HttpProxyAgent(proxyUrl); } class HttpClientImpl { baseHeaders = { 'User-Agent': `Capawesome CLI v${pkg.version}`, }; async delete(url, config) { const baseUrl = await configService.getValueForKey('API_BASE_URL'); const urlWithHost = url.startsWith('http') ? url : baseUrl + url; const proxyAgent = getProxyAgent(urlWithHost); const axiosConfig = { ...config, headers: { ...this.baseHeaders, ...config?.headers }, ...(proxyAgent && urlWithHost.startsWith('https://') ? { httpsAgent: proxyAgent, proxy: false } : {}), ...(proxyAgent && urlWithHost.startsWith('http://') ? { httpAgent: proxyAgent, proxy: false } : {}), }; return axios.delete(urlWithHost, axiosConfig); } async get(url, config) { const baseUrl = await configService.getValueForKey('API_BASE_URL'); const urlWithHost = url.startsWith('http') ? url : baseUrl + url; const proxyAgent = getProxyAgent(urlWithHost); const axiosConfig = { ...config, headers: { ...this.baseHeaders, ...config?.headers }, ...(proxyAgent && urlWithHost.startsWith('https://') ? { httpsAgent: proxyAgent, proxy: false } : {}), ...(proxyAgent && urlWithHost.startsWith('http://') ? { httpAgent: proxyAgent, proxy: false } : {}), }; return axios.get(urlWithHost, axiosConfig); } async patch(url, data, config) { const baseUrl = await configService.getValueForKey('API_BASE_URL'); const urlWithHost = url.startsWith('http') ? url : baseUrl + url; const proxyAgent = getProxyAgent(urlWithHost); const axiosConfig = { ...config, headers: { ...this.baseHeaders, ...config?.headers }, ...(proxyAgent && urlWithHost.startsWith('https://') ? { httpsAgent: proxyAgent, proxy: false } : {}), ...(proxyAgent && urlWithHost.startsWith('http://') ? { httpAgent: proxyAgent, proxy: false } : {}), }; return axios.patch(urlWithHost, data, axiosConfig); } async post(url, data, config) { const baseUrl = await configService.getValueForKey('API_BASE_URL'); const urlWithHost = url.startsWith('http') ? url : baseUrl + url; const proxyAgent = getProxyAgent(urlWithHost); const axiosConfig = { ...config, headers: { ...this.baseHeaders, ...config?.headers }, ...(proxyAgent && urlWithHost.startsWith('https://') ? { httpsAgent: proxyAgent, proxy: false } : {}), ...(proxyAgent && urlWithHost.startsWith('http://') ? { httpAgent: proxyAgent, proxy: false } : {}), }; return axios.post(urlWithHost, data, axiosConfig); } async put(url, data, config) { const baseUrl = await configService.getValueForKey('API_BASE_URL'); const urlWithHost = url.startsWith('http') ? url : baseUrl + url; const proxyAgent = getProxyAgent(urlWithHost); const axiosConfig = { ...config, headers: { ...this.baseHeaders, ...config?.headers }, ...(proxyAgent && urlWithHost.startsWith('https://') ? { httpsAgent: proxyAgent, proxy: false } : {}), ...(proxyAgent && urlWithHost.startsWith('http://') ? { httpAgent: proxyAgent, proxy: false } : {}), }; return axios.put(urlWithHost, data, axiosConfig); } } let httpClient = new HttpClientImpl(); export default httpClient;