2017-01-09 21:59:52 +00:00
|
|
|
import 'whatwg-fetch';
|
2018-06-25 17:42:50 +00:00
|
|
|
import { parse as urlParse } from 'url';
|
|
|
|
import { getClientString } from './constants';
|
2016-11-09 03:18:25 +00:00
|
|
|
import * as session from '../sync/session';
|
2017-08-01 02:33:46 +00:00
|
|
|
import * as zlib from 'zlib';
|
2016-11-07 20:24:38 +00:00
|
|
|
|
|
|
|
let commandListeners = [];
|
2017-07-31 20:46:04 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function onCommand(callback) {
|
2016-11-07 20:24:38 +00:00
|
|
|
commandListeners.push(callback);
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function offCommand(callback) {
|
2016-11-07 20:24:38 +00:00
|
|
|
commandListeners = commandListeners.filter(l => l !== callback);
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function post(path, obj) {
|
2017-03-03 20:09:08 +00:00
|
|
|
return _fetch('POST', path, obj);
|
2016-11-07 20:24:38 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function get(path, sessionId = null) {
|
2017-03-03 20:09:08 +00:00
|
|
|
return _fetch('GET', path, null, sessionId);
|
2016-11-07 20:24:38 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function del(path, sessionId = null) {
|
2017-03-03 20:09:08 +00:00
|
|
|
return _fetch('DELETE', path, null, sessionId);
|
2016-11-07 20:24:38 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function put(path, sessionId = null) {
|
2017-03-03 20:09:08 +00:00
|
|
|
return _fetch('PUT', path, null, sessionId);
|
2016-11-07 20:24:38 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function rawFetch(...args) {
|
2017-03-16 17:51:56 +00:00
|
|
|
return window.fetch(...args);
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
async function _fetch(method, path, obj, sessionId = null) {
|
2016-11-07 20:24:38 +00:00
|
|
|
const config = {
|
|
|
|
method: method,
|
2017-03-03 20:09:08 +00:00
|
|
|
headers: new window.Headers()
|
2016-11-07 20:24:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Set some client information
|
2016-11-10 02:40:53 +00:00
|
|
|
config.headers.set('X-Insomnia-Client', getClientString());
|
2016-11-07 20:24:38 +00:00
|
|
|
|
2017-08-01 02:33:46 +00:00
|
|
|
if (obj) {
|
|
|
|
config.body = zlib.gzipSync(JSON.stringify(obj));
|
2016-11-07 20:24:38 +00:00
|
|
|
config.headers.set('Content-Type', 'application/json');
|
2017-08-01 02:33:46 +00:00
|
|
|
config.headers.set('Content-Encoding', 'gzip');
|
2016-11-07 20:24:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sessionId = sessionId || session.getCurrentSessionId();
|
|
|
|
if (sessionId) {
|
2017-03-03 20:09:08 +00:00
|
|
|
config.headers.set('X-Session-Id', sessionId);
|
2016-11-07 20:24:38 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 20:09:08 +00:00
|
|
|
const response = await window.fetch(_getUrl(path), config);
|
2016-11-07 20:24:38 +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();
|
2017-01-11 18:35:44 +00:00
|
|
|
err.statusCode = response.status;
|
2017-03-03 20:09:08 +00:00
|
|
|
throw err;
|
2016-11-07 20:24:38 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
if (
|
|
|
|
response.headers.get('content-type') === 'application/json' ||
|
|
|
|
path.match(/\.json$/)
|
|
|
|
) {
|
2017-03-03 20:09:08 +00:00
|
|
|
return response.json();
|
2016-11-07 20:24:38 +00:00
|
|
|
} else {
|
2017-03-03 20:09:08 +00:00
|
|
|
return response.text();
|
2016-11-07 20:24:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
function _getUrl(path) {
|
2017-05-16 17:19:28 +00:00
|
|
|
const baseUrl = process.env.INSOMNIA_SYNC_URL || 'https://api.insomnia.rest';
|
2017-08-01 02:33:46 +00:00
|
|
|
return `${baseUrl}${path}`;
|
2016-11-07 20:24:38 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
function _notifyCommandListeners(uri) {
|
2016-11-07 20:24:38 +00:00
|
|
|
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));
|
|
|
|
}
|