Skip to content

Instantly share code, notes, and snippets.

@davidgilbertson
Last active December 31, 2019 03:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidgilbertson/539eddf9e3e12dcbd8f9ee442f0b2d24 to your computer and use it in GitHub Desktop.
Save davidgilbertson/539eddf9e3e12dcbd8f9ee442f0b2d24 to your computer and use it in GitHub Desktop.
const fetchJson = (url, opts = {}) => fetch(url, {
method: opts.method,
headers: {'Content-Type': 'application/json'},
body: opts.body && JSON.stringify(opts.body),
}).then(res => res.json());
export default {
create: body => fetchJson(API_URL, {
method: 'POST',
body,
}),
read: id => fetchJson(`${API_URL}/${id}`),
update: (id, body) => fetchJson(`${API_URL}/${id}`, {
method: 'PUT',
body,
}),
delete: id => fetchJson(`${API_URL}/${id}`, {
method: 'DELETE',
}),
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment