2016-12-08 12:27:19 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var Client = {};
|
|
|
|
|
|
|
|
Client.request = function(resource, args) {
|
|
|
|
args = args || {};
|
|
|
|
args.credentials = 'include'
|
2016-12-08 18:44:49 +01:00
|
|
|
args.headers = args.headers || {};
|
|
|
|
args.headers['Accept'] = 'application/json';
|
|
|
|
|
|
|
|
if( args.method && args.method === 'POST') {
|
|
|
|
args.headers['Content-Type'] = 'application/json';
|
|
|
|
|
|
|
|
if(args.data) {
|
|
|
|
if( typeof(args.data) !== "string") {
|
|
|
|
args.data = JSON.stringify(args.data)
|
|
|
|
}
|
|
|
|
args.body = args.data
|
|
|
|
delete args.data
|
|
|
|
}
|
|
|
|
}
|
2016-12-08 12:27:19 +01:00
|
|
|
|
|
|
|
return fetch(`/api/${resource}`, args).then((r) => {
|
|
|
|
if( r.ok ) {
|
|
|
|
return r.json();
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(r);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Client
|