2017-01-09 21:59:52 +00:00
|
|
|
import 'whatwg-fetch';
|
2016-11-07 20:24:38 +00:00
|
|
|
import {parse as urlParse} from 'url';
|
2017-05-16 17:45:09 +00:00
|
|
|
import {getClientString} from './constants';
|
2016-11-09 03:18:25 +00:00
|
|
|
import * as session from '../sync/session';
|
2016-11-07 20:24:38 +00:00
|
|
|
|
|
|
|
let commandListeners = [];
|
|
|
|
export function onCommand (callback) {
|
|
|
|
commandListeners.push(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function offCommand (callback) {
|
|
|
|
commandListeners = commandListeners.filter(l => l !== callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-03-16 17:51:56 +00:00
|
|
|
export function rawFetch (...args) {
|
|
|
|
return window.fetch(...args);
|
|
|
|
}
|
|
|
|
|
2016-11-07 20:24:38 +00:00
|
|
|
async function _fetch (method, path, json, sessionId = null) {
|
|
|
|
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
|
|
|
|
|
|
|
if (json) {
|
2017-07-27 23:27:51 +00:00
|
|
|
config.body = JSON.stringify(json);
|
2016-11-07 20:24:38 +00:00
|
|
|
config.headers.set('Content-Type', 'application/json');
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if (response.headers.get('content-type') === 'application/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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _getUrl (path) {
|
2017-05-16 17:19:28 +00:00
|
|
|
const baseUrl = process.env.INSOMNIA_SYNC_URL || 'https://api.insomnia.rest';
|
|
|
|
return `${baseUrl}${path}`;
|
2016-11-07 20:24:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|