Created
October 4, 2019 18:23
-
-
Save zackify/a9436ab7dc9716bca65611937a75dc34 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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