mirror of
https://github.com/status-im/fathom.git
synced 2025-03-01 03:20:27 +00:00
48 lines
890 B
JavaScript
48 lines
890 B
JavaScript
'use strict';
|
|
|
|
var Client = {};
|
|
Client.request = function(resource, args) {
|
|
args = args || {};
|
|
args.credentials = 'same-origin'
|
|
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
|
|
}
|
|
}
|
|
|
|
return fetch(`/api/${resource}`, args)
|
|
.then(handleRequestErrors)
|
|
.then(parseJSON)
|
|
.then(parseData)
|
|
}
|
|
|
|
function parseJSON(r) {
|
|
return r.json()
|
|
}
|
|
|
|
function handleRequestErrors(r) {
|
|
if (!r.ok) {
|
|
throw new Error(r.statusText);
|
|
}
|
|
return r;
|
|
}
|
|
|
|
function parseData(d) {
|
|
if(d.Error) {
|
|
throw new Error(d.Error)
|
|
}
|
|
|
|
return d.Data
|
|
}
|
|
|
|
export default Client
|