mirror of
https://github.com/Kong/insomnia
synced 2024-11-08 14:49:53 +00:00
36 lines
941 B
JavaScript
36 lines
941 B
JavaScript
|
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 = 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;
|
||
|
}
|