insomnia/packages/insomnia-app/app/network/axios-request.ts

40 lines
980 B
TypeScript
Raw Normal View History

2020-04-26 20:33:39 +00:00
import axios from 'axios';
2021-07-22 23:04:56 +00:00
import { setDefaultProtocol } from 'insomnia-url';
import { parse as urlParse } from 'url';
2020-04-26 20:33:39 +00:00
import { isDevelopment } from '../common/constants';
2021-07-22 23:04:56 +00:00
import * as models from '../models';
2020-04-26 20:33:39 +00:00
export async function axiosRequest(config) {
const settings = await models.settings.getOrCreate();
const isHttps = config.url.indexOf('https:') === 0;
let proxyUrl: string | null = null;
2020-04-26 20:33:39 +00:00
if (isHttps && settings.httpsProxy) {
proxyUrl = settings.httpsProxy;
} else if (settings.httpProxy) {
proxyUrl = settings.httpProxy;
}
const finalConfig = { ...config, adapter: global.require('axios/lib/adapters/http') };
2020-04-26 20:33:39 +00:00
if (proxyUrl) {
const { hostname, port } = urlParse(setDefaultProtocol(proxyUrl));
finalConfig.proxy = {
host: hostname,
port,
};
2020-04-26 20:33:39 +00:00
}
const response = await axios(finalConfig);
if (isDevelopment()) {
console.log('[axios] Response', {
config,
response,
});
2020-04-26 20:33:39 +00:00
}
return response;
}