let HowhapList = require('howhap-list');
require('whatwg-fetch');

function makeJson(res) {
	return res.json().then(json => {
		return {res: res, json: json};
	});
}


function processResult(data) {
	if(data.res.status >= 400) {
		throw new HowhapList(data.json);
	}
	return data.json;
}

function processError(err) {
	throw new HowhapList({ network: { message: err.toString(), status: 400 } });
}

let headers = {
	'Accept': 'application/json',
	'Content-Type': 'application/json'
};

module.exports = {
	get: function(url, params) {
		url += '?';
		for(let i in params) {
			url += encodeURIComponent(i)+'='+encodeURIComponent(params[i])+'&';
		}
		url = url.slice(0, -1);
		return fetch(url, {
			credentials: 'same-origin',
			method: 'get',
			headers: headers
		})
		.then(makeJson)
		.then(processResult)
		.catch(processError);
	},
	put: function(url, params) {
		return fetch(url, {
			credentials: 'same-origin',
			method: 'put',
			headers: headers,
			body: JSON.stringify(params || {})
		})
		.then(makeJson)
		.then(processResult)
		.catch(processError);
	},
	post: function(url, params) {
		return fetch(url, {
			credentials: 'same-origin',
			method: 'post',
			headers: headers,
			body: JSON.stringify(params || {})
		})
		.then(makeJson)
		.then(processResult)
		.catch(processError);
	},
	delete: function(url, params) {
		return fetch(url, {
			credentials: 'same-origin',
			method: 'delete',
			headers: headers,
			body: JSON.stringify(params || {})
		})
		.then(makeJson)
		.then(processResult)
		.catch(processError);
	},
	setGlobalHeaders: function(newHeaders) {
		headers = Object.assign(headers, newHeaders);
	},
	deleteGlobalHeader: function(key) {
		delete headers[key];
	}
};
@@ -64,32 +64,187 @@
 );%0A%0A
-function makeJson(res) %7B
+let debug = false;%0A%0Alet headers = %7B%0A%09'Accept': 'application/json',%0A%09'Content-Type': 'application/json'%0A%7D;%0A%0Afunction makeJson(res) %7B%0A%09if(debug) %7B%0A%09%09console.log('makeJson', res);%0A%09%7D
 %0A%09re
@@ -343,24 +343,78 @@
 ult(data) %7B%0A
+%09if(debug) %7B%0A%09%09console.log('processResult', data);%0A%09%7D%0A
 %09if(data.res
@@ -520,16 +520,68 @@
 (err) %7B%0A
+%09if(debug) %7B%0A%09%09console.log('processError', err);%0A%09%7D%0A
 %09throw n
@@ -657,95 +657,8 @@
 %0A%7D%0A%0A
-let headers = %7B%0A%09'Accept': 'application/json',%0A%09'Content-Type': 'application/json'%0A%7D;%0A%0A
 modu
@@ -1871,13 +1871,59 @@
 s%5Bkey%5D;%0A
+%09%7D,%0A%09setDebug: function(val) %7B%0A%09%09debug = val;%0A
 %09%7D%0A%7D;

Add debug option