25 Zeilen
789 B
JavaScript
25 Zeilen
789 B
JavaScript
|
|
'use strict';
|
||
|
|
|
||
|
|
var API = {
|
||
|
|
base: '/api',
|
||
|
|
|
||
|
|
request: function(method, path, body) {
|
||
|
|
var opts = {
|
||
|
|
method: method,
|
||
|
|
headers: { 'Content-Type': 'application/json' },
|
||
|
|
credentials: 'same-origin'
|
||
|
|
};
|
||
|
|
if (body) opts.body = JSON.stringify(body);
|
||
|
|
return fetch(this.base + path, opts).then(function(resp) {
|
||
|
|
return resp.json().then(function(data) {
|
||
|
|
if (!resp.ok) throw { status: resp.status, message: data.error || 'Fehler' };
|
||
|
|
return data;
|
||
|
|
});
|
||
|
|
});
|
||
|
|
},
|
||
|
|
|
||
|
|
get: function(path) { return this.request('GET', path); },
|
||
|
|
post: function(path, body) { return this.request('POST', path, body); },
|
||
|
|
del: function(path) { return this.request('DELETE', path); }
|
||
|
|
};
|