Created
May 5, 2021 16:28
-
-
Save passandscore/46d311ec605dfb09334b13e3ffe9d347 to your computer and use it in GitHub Desktop.
httpClient - DB fetch library
This file contains 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 httpClient { | |
// Get Request | |
get(url) { | |
return new Promise((resolve, reject) => { | |
fetch(url) | |
.then((res) => { | |
if (res.ok) { | |
return res.json(); | |
} | |
}).then(data => resolve(data)) | |
.catch(err => reject(err)) | |
}) | |
} | |
// Post Method | |
post(url, data, headers) { | |
return new Promise((resolve, reject) => { | |
fetch(url, { | |
method: 'POST', | |
headers: headers, | |
body: JSON.stringify(data) | |
}) | |
.then((res) => { | |
return res.json(); | |
}).then(data => resolve(data)) | |
.catch(err => reject(err)) | |
}) | |
} | |
put(url, data) { | |
return new Promise((resolve, reject) => { | |
fetch(url, { | |
method: 'PUT', | |
body: JSON.stringify(data) | |
}) | |
.then((res) => { | |
return res.json(); | |
}).then(data => resolve(data)) | |
.catch(err => reject(err)) | |
}) | |
} | |
delete(url) { | |
return new Promise((resolve, reject) => { | |
fetch(url, { | |
method: 'DELETE', | |
}) | |
.then((res) => { | |
return res.json(); | |
}).then(data => resolve(data)) | |
.catch(err => reject(err)) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment