2019-04-18 00:50:03 +00:00
|
|
|
import { delay } from '../common/misc';
|
|
|
|
import { parse as urlParse } from 'url';
|
|
|
|
import zlib from 'zlib';
|
|
|
|
let _userAgent = '';
|
|
|
|
let _baseUrl = '';
|
2021-05-18 20:32:18 +00:00
|
|
|
const _commandListeners: Function[] = [];
|
2019-04-18 00:50:03 +00:00
|
|
|
|
|
|
|
export function setup(userAgent, baseUrl) {
|
|
|
|
_userAgent = userAgent;
|
|
|
|
_baseUrl = baseUrl;
|
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function onCommand(callback: Function) {
|
2019-04-18 00:50:03 +00:00
|
|
|
_commandListeners.push(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function post(path, obj, sessionId, compressBody = false) {
|
|
|
|
return _fetch('POST', path, obj, sessionId, compressBody);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function put(path, obj, sessionId, compressBody = false) {
|
|
|
|
return _fetch('PUT', path, obj, sessionId, compressBody);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function get(path, sessionId) {
|
|
|
|
return _fetch('GET', path, null, sessionId);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function _fetch(method, path, obj, sessionId, compressBody = false, retries = 0) {
|
|
|
|
if (sessionId === undefined) {
|
|
|
|
throw new Error(`No session ID provided to ${method}:${path}`);
|
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
const config: {
|
|
|
|
method: string;
|
|
|
|
headers: HeadersInit;
|
|
|
|
body?: string | Buffer;
|
|
|
|
} = {
|
2019-04-18 00:50:03 +00:00
|
|
|
method: method,
|
|
|
|
headers: {},
|
|
|
|
};
|
|
|
|
|
|
|
|
// Set some client information
|
|
|
|
if (_userAgent) {
|
|
|
|
config.headers['X-Insomnia-Client'] = _userAgent;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (obj && compressBody) {
|
|
|
|
config.body = zlib.gzipSync(JSON.stringify(obj));
|
|
|
|
config.headers['Content-Type'] = 'application/json';
|
|
|
|
config.headers['Content-Encoding'] = 'gzip';
|
|
|
|
} else if (obj) {
|
|
|
|
config.body = JSON.stringify(obj);
|
|
|
|
config.headers['Content-Type'] = 'application/json';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sessionId) {
|
|
|
|
config.headers['X-Session-Id'] = sessionId;
|
|
|
|
}
|
|
|
|
|
|
|
|
let response;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2019-04-18 00:50:03 +00:00
|
|
|
const url = _getUrl(path);
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2019-04-18 00:50:03 +00:00
|
|
|
try {
|
|
|
|
response = await window.fetch(url, config);
|
|
|
|
|
|
|
|
// Exponential backoff for 502 errors
|
|
|
|
if (response.status === 502 && retries < 5) {
|
|
|
|
retries++;
|
|
|
|
await delay(retries * 200);
|
|
|
|
return this._fetch(method, path, obj, sessionId, compressBody, retries);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
throw new Error(`Failed to fetch '${url}'`);
|
|
|
|
}
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2019-04-18 00:50:03 +00:00
|
|
|
const uri = response.headers.get('x-insomnia-command');
|
|
|
|
uri && _notifyCommandListeners(uri);
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
const err = new Error(`Response ${response.status} for ${path}`);
|
|
|
|
err.message = await response.text();
|
2021-05-12 06:35:00 +00:00
|
|
|
// @ts-expect-error -- TSCONVERSION
|
2019-04-18 00:50:03 +00:00
|
|
|
err.statusCode = response.status;
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.headers.get('content-type') === 'application/json' || path.match(/\.json$/)) {
|
|
|
|
return response.json();
|
|
|
|
} else {
|
|
|
|
return response.text();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _getUrl(path) {
|
|
|
|
if (!_baseUrl) {
|
|
|
|
throw new Error('API base URL not configured!');
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${_baseUrl}${path}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _notifyCommandListeners(uri) {
|
|
|
|
const parsed = urlParse(uri, true);
|
|
|
|
const command = `${parsed.hostname}${parsed.pathname}`;
|
|
|
|
const args = JSON.parse(JSON.stringify(parsed.query));
|
|
|
|
|
|
|
|
_commandListeners.map(fn => fn(command, args));
|
|
|
|
}
|