Skip to content

Instantly share code, notes, and snippets.

@zackify
Created October 4, 2019 18:23
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 zackify/a9436ab7dc9716bca65611937a75dc34 to your computer and use it in GitHub Desktop.
Save zackify/a9436ab7dc9716bca65611937a75dc34 to your computer and use it in GitHub Desktop.
export const api = ({ url, headers = {}, body, method }) =>
new Promise(async (resolve, reject) => {
try {
body = JSON.stringify(body);
headers['Content-Type'] = 'application/json';
} catch (e) {}
//intercept before if i want
let token = await storage.get('userToken')
headers.Authorization = token
try {
let response = await fetch(url, {
headers,
body,
method: method || 'GET',
});
let json = await response.json();
resolve(json);
} catch (e) {
reject(e);
}
});
// usage
await api({
url: 'http://test.com',
method: 'POST',
body: { test: 'test json' },
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment