Skip to content

Instantly share code, notes, and snippets.

@passandscore
Created May 5, 2021 16:28
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 passandscore/46d311ec605dfb09334b13e3ffe9d347 to your computer and use it in GitHub Desktop.
Save passandscore/46d311ec605dfb09334b13e3ffe9d347 to your computer and use it in GitHub Desktop.
httpClient - DB fetch library
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