Skip to content

Instantly share code, notes, and snippets.

@dvidsilva
Created March 12, 2020 19:10
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 dvidsilva/7f6ce7bcea4ada2ddb3967b4e2edf9e8 to your computer and use it in GitHub Desktop.
Save dvidsilva/7f6ce7bcea4ada2ddb3967b4e2edf9e8 to your computer and use it in GitHub Desktop.
sample api comms file
import fetch from 'isomorphic-fetch';
const Post = (path, data, extraConfig = {}) => {
return fetch(path, {
method: 'POST',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Accept': 'application/json',
},
...extraConfig,
}).then(response => {
return response.json().then(data => (
{ data, response }
));
});
};
const Put = (path, data, extraConfig = {}) => {
return fetch(path, {
method: 'PUT',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Accept': 'application/json',
},
...extraConfig,
}).then(response => {
return response.json().then(data => (
{ data, response }
));
});
};
const Get = (path, extraConfig = {}) => {
return fetch(path, {
method: 'GET',
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Accept': 'application/json',
},
...extraConfig,
}).then(response => {
if (response.status === 204) {
return { data: null, response };
}
return response.json().then(data => (
{ data, response }
));
});
};
export default {
Post,
Get,
Put,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment