Last active
April 26, 2020 08:18
-
-
Save oluosiname/04f824927fdfeb1e5e73b1b1b8bd02e3 to your computer and use it in GitHub Desktop.
Fetch Http wrapper
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
| class Http { | |
| constructor() { | |
| this.postInit = { method: 'POST' } | |
| this.getInit = { method: 'GET' } | |
| } | |
| async get({ url, ...rest }) { | |
| const init = {...this.getInit, ...rest}; | |
| try { | |
| const response = await fetch.get(url, init); | |
| return response.json(); | |
| } catch (e) { | |
| throw e; | |
| } | |
| } | |
| async post({ url, body, ...rest }) { | |
| const init = {...this.postInit, ...rest}; | |
| try { | |
| const response = await fetch(url, init); | |
| return response.json(); | |
| } catch (e) { | |
| throw e; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment