diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2020-07-22 14:05:15 +0200 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2020-07-22 14:05:15 +0200 |
| commit | ef78bc6a084f92b4794e987b5832240d85b6479e (patch) | |
| tree | b314b630800db6aa60f28ef0b115625e6ca176db /animism-align/frontend/api/crud.fetch.js | |
| parent | 85d4cb9addf9ca887d3440b2786665d67d9917c4 (diff) | |
refactor app using babel module-resolver
Diffstat (limited to 'animism-align/frontend/api/crud.fetch.js')
| -rw-r--r-- | animism-align/frontend/api/crud.fetch.js | 105 |
1 files changed, 0 insertions, 105 deletions
diff --git a/animism-align/frontend/api/crud.fetch.js b/animism-align/frontend/api/crud.fetch.js deleted file mode 100644 index c88225e..0000000 --- a/animism-align/frontend/api/crud.fetch.js +++ /dev/null @@ -1,105 +0,0 @@ -import fetch from 'node-fetch' - -export function crud_fetch(type, tag) { - const uri = '/api/v1/' + type + '/' + (tag || '') - return { - index: q => { - return fetch(_get_url(uri, q), _get_headers()) - .then(req => req.json()) - .catch(error) - }, - - show: id => { - let url; - if (typeof id === 'object') { - url = _get_url(uri + id[0] + '/', id[1]) - } else { - url = _get_url(uri + id + '/') - } - return fetch(url, _get_headers()) - .then(req => req.json()) - .catch(error) - }, - - create: data => { - return fetch(uri, post(data)) - .then(req => req.json()) - .catch(error) - }, - - update: data => { - return fetch(uri + data.id + '/', put(data)) - .then(req => req.json()) - .catch(error) - }, - - destroy: data => { - return fetch(uri + data.id + '/', destroy(data)) - .then(req => req.json()) - .catch(error) - }, - } -} - -function _get_url(_url, data) { - const url = new URL(window.location.origin + _url) - if (data) { - Object.keys(data).forEach(key => url.searchParams.append(key, data[key])) - } - return url -} -export function _get_headers() { - return { - method: 'GET', - credentials: 'same-origin', - headers: { - 'Accept': 'application/json', - }, - } -} -export function post(data) { - return { - method: 'POST', - body: JSON.stringify(data), - credentials: 'same-origin', - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json' - }, - } -} -export function postBody(data) { - return { - method: 'POST', - body: data, - credentials: 'same-origin', - headers: { - 'Accept': 'application/json', - }, - } -} -export function put(data) { - return { - method: 'PUT', - body: JSON.stringify(data), - credentials: 'same-origin', - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json' - }, - } -} -export function destroy(data) { - return { - method: 'DELETE', - body: JSON.stringify(data), - credentials: 'same-origin', - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json' - }, - } -} -function error(err) { - console.warn(err) -} |
