insomnia/packages/insomnia-app/app/network/axios-request.ts
JasonCubic 4f93186e93
Fix for git sync noProxy issue (#4304)
* git sync respect noProxy settings

* added testing and more edge cases

* removed dev time logging

* rename urlInNoProxy to isUrlMatchedInNoProxyRule

* added a validation check and test for invalid url edge case

* removed dev time script

* change git-sync proxy UX behavior to match the rest of insomnia

* refactor conditional to highlight opinionated intent more clearly

* minor refactor to satisfy sonarlint

* remove uneccesary tests

Co-authored-by: jacubic <jason.cubic@daimler.com>
Co-authored-by: Jack Kavanagh <jackkav@gmail.com>
2022-01-12 15:22:56 +01:00

53 lines
1.5 KiB
TypeScript

import axios, { AxiosRequestConfig } from 'axios';
import * as https from 'https';
import { setDefaultProtocol } from 'insomnia-url';
import { parse as urlParse } from 'url';
import { isDevelopment } from '../common/constants';
import * as models from '../models';
import { isUrlMatchedInNoProxyRule } from './is-url-matched-in-no-proxy-rule';
export async function axiosRequest(config: AxiosRequestConfig) {
const settings = await models.settings.getOrCreate();
const isHttps = config.url?.indexOf('https:') === 0;
let proxyUrl: string | null = null;
if (isHttps && settings.httpsProxy) {
proxyUrl = settings.httpsProxy;
} else if (settings.httpProxy) {
proxyUrl = settings.httpProxy;
}
const finalConfig: AxiosRequestConfig = {
...config,
adapter: global.require('axios/lib/adapters/http'),
httpsAgent: new https.Agent({
rejectUnauthorized: settings.validateSSL,
}),
};
// ignore HTTP_PROXY, HTTPS_PROXY, NO_PROXY environment variables
finalConfig.proxy = false;
if (settings.proxyEnabled && proxyUrl && !isUrlMatchedInNoProxyRule(finalConfig.url, settings.noProxy)) {
const { hostname, port } = urlParse(setDefaultProtocol(proxyUrl));
if (hostname && port) {
finalConfig.proxy = {
host: hostname,
port: parseInt(port, 10),
};
}
}
const response = await axios(finalConfig);
if (isDevelopment()) {
console.log('[axios] Response', {
config,
response,
});
}
return response;
}