import fetch from 'node-fetch' import { session } from 'app/session' 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.set(key, data[key])) } return url } export function _get_headers() { return { method: 'GET', credentials: 'same-origin', headers: { 'Accept': 'application/json', 'Authorization': "Bearer " + session.get("access_token"), }, } } export function post(data) { return { method: 'POST', body: JSON.stringify(data), credentials: 'same-origin', headers: { 'Accept': 'application/json', 'Authorization': "Bearer " + session.get("access_token"), 'Content-Type': 'application/json' }, } } export function postBody(data) { return { method: 'POST', body: data, credentials: 'same-origin', headers: { 'Accept': 'application/json', 'Authorization': "Bearer " + session.get("access_token"), }, } } export function put(data) { return { method: 'PUT', body: JSON.stringify(data), credentials: 'same-origin', headers: { 'Accept': 'application/json', 'Authorization': "Bearer " + session.get("access_token"), 'Content-Type': 'application/json' }, } } export function destroy(data) { return { method: 'DELETE', body: JSON.stringify(data), credentials: 'same-origin', headers: { 'Accept': 'application/json', 'Authorization': "Bearer " + session.get("access_token"), 'Content-Type': 'application/json' }, } } function error(err) { console.warn(err) }