insomnia/packages/insomnia-app/app/network/axios-request.ts
Dimitri Mitropoulos 5f4c19da35
[TypeScript] Phase 1 & 2 (#3370)
Co-authored-by: Opender Singh <opender.singh@konghq.com>
2021-05-12 18:35:00 +12:00

39 lines
979 B
TypeScript

import { parse as urlParse } from 'url';
import { setDefaultProtocol } from 'insomnia-url';
import axios from 'axios';
import * as models from '../models';
import { isDevelopment } from '../common/constants';
export async function axiosRequest(config) {
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 = { ...config, adapter: global.require('axios/lib/adapters/http') };
if (proxyUrl) {
const { hostname, port } = urlParse(setDefaultProtocol(proxyUrl));
finalConfig.proxy = {
host: hostname,
port,
};
}
const response = await axios(finalConfig);
if (isDevelopment()) {
console.log('[axios] Response', {
config,
response,
});
}
return response;
}