* Requests a URL, returning a promise. * * @param {string} url The URL we want to request * @param {object} [options] The options we want to pass to "fetch" * @return {object} An object containing either "data" or "err"
(url, options)
| 18 | * @return {object} An object containing either "data" or "err" |
| 19 | */ |
| 20 | async function request(url, options) { |
| 21 | const response = await fetch(url, options); |
| 22 | |
| 23 | checkStatus(response); |
| 24 | |
| 25 | const data = await response.json(); |
| 26 | |
| 27 | const ret = { |
| 28 | data, |
| 29 | headers: {}, |
| 30 | }; |
| 31 | |
| 32 | if (response.headers.get('x-total-count')) { |
| 33 | ret.headers['x-total-count'] = response.headers.get('x-total-count'); |
| 34 | } |
| 35 | |
| 36 | return ret; |
| 37 | } |
| 38 | |
| 39 | export default request; |