diff options
Diffstat (limited to 'www/src/request.js')
-rw-r--r-- | www/src/request.js | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/www/src/request.js b/www/src/request.js new file mode 100644 index 0000000..68ec8c3 --- /dev/null +++ b/www/src/request.js @@ -0,0 +1,49 @@ +module.exports = (api) => { + // Default request options + var defaults = { + headers: { + accept: 'application/json' + } + }; + + // Helper for constructing url + var url = (thing) => { + return api + thing; + }; + + var _fetch = (thing, data, options) => { + // Merge defaults with options + options = Object.assign({}, defaults, options); + + // data is only allowed for POST and PUT + if (data) { + options.body = new FormData(); + + for (var k in data) { + options.body.set(k, data[k]); + } + } + + return fetch(url(thing), options); + }; + + // GET/POST/DELETE/PUT helpers + // These helpers extend the defaults that get passed to fetch() + return { + get: (thing) => { + return _fetch(thing, null, {method: 'GET'}); + }, + + post: (thing, data) => { + return _fetch(thing, data, {method: 'POST'}); + }, + + put: (thing, data) => { + return _fetch(thing, data, {method: 'PUT'}); + }, + + delete: (thing, data) => { + return _fetch(thing, data, {method: 'DELETE'}); + } + }; +}; |