Skip to content

Instantly share code, notes, and snippets.

@evdama
Created July 1, 2019 14:12
Show Gist options
  • Save evdama/89f207589d756741dea26c2600af4f98 to your computer and use it in GitHub Desktop.
Save evdama/89f207589d756741dea26c2600af4f98 to your computer and use it in GitHub Desktop.
server route unchanged from template
const dev = process.env.NODE_ENV === 'development';
const base = dev ? 'http://localhost:3000' : 'likesports-v2.web.app';
function send({ method, path, data, token }) {
const fetch = process.browser ? window.fetch : require('node-fetch').default;
const opts = { method, headers: {} };
if (data) {
opts.headers['Content-Type'] = 'application/json';
opts.body = JSON.stringify(data);
}
if (token) {
opts.headers['Authorization'] = `Token ${token}`;
}
return fetch(`${base}/${path}`, opts)
.then(r => r.text())
.then(json => {
try {
return JSON.parse(json);
} catch (err) {
return json;
}
});
}
export function get(path, token) {
return send({ method: 'GET', path, token });
}
export function del(path, token) {
return send({ method: 'DELETE', path, token });
}
export function post(path, data, token) {
return send({ method: 'POST', path, data, token });
}
export function put(path, data, token) {
return send({ method: 'PUT', path, data, token });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment