Created
July 2, 2021 14:34
-
-
Save HelloAlberuni/913582bd0e157949181af3f9fac4ddcf to your computer and use it in GitHub Desktop.
Custom HTTP library version 2 using Fetch & Promise
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
let instance = new azHTTPLibrary(); | |
// Make get HTTP request | |
// instance.get('https://jsonplaceholder.typicode.com/posts') | |
// .then( res => console.log(res) ) | |
// .catch( err => console.log(err) ); | |
// Make post HTTP request | |
// instance.post('https://jsonplaceholder.typicode.com/users', { name: 'azadd', username: 'ab_azad' } ) | |
// .then( res => console.log(res) ) | |
// .catch( err => console.log('something wrong') ); | |
// Make PUT HTTP request | |
// instance.put('https://jsonplaceholder.typicode.com/users/2', { name: 'azadd', username: 'ab_azad' } ) | |
// .then( res => console.log(res) ) | |
// .catch( err => console.log('something wrong') ); | |
// Make Delete HTTP request | |
instance.delete('https://jsonplaceholder.typicode.com/users/2' ) | |
.then( res => console.log(res) ) | |
.catch( err => console.log('something wrong') ); |
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
/** | |
* azHTTPLibrary Library | |
* Library for making HTTP requests | |
* | |
* @version 2.0.0 | |
**/ | |
class azHTTPLibrary{ | |
// Make an HTTP GET reuest | |
get( url ){ | |
return new Promise( (resolvee, rejectt) => { | |
fetch( url ) | |
.then( res => res.json() ) | |
.then( data => resolvee(data) ) | |
.catch( err => rejectt(err) ); | |
}); | |
} | |
// Make an HTTP POST reuest | |
post( url, user ){ | |
return new Promise( (resolvee, rejectt) => { | |
fetch( url, { | |
method: 'POST', | |
headers: { | |
'content-type': 'application/json' | |
}, | |
body: JSON.stringify(user) | |
}) | |
.then( res => res.json() ) | |
.then( data => resolvee(data) ) | |
.catch( err => rejectt(err) ); | |
}); | |
} | |
// Make an HTTP PUT reuest | |
put( url, user ){ | |
return new Promise( (resolvee, rejectt) => { | |
fetch( url, { | |
method: 'PUT', | |
headers: { | |
'content-type': 'application/json' | |
}, | |
body: JSON.stringify(user) | |
}) | |
.then( res => res.json() ) | |
.then( data => resolvee(data) ) | |
.catch( err => rejectt(err) ); | |
}); | |
} | |
// Make an HTTP DELETE reuest | |
delete( url, user ){ | |
return new Promise( (resolvee, rejectt) => { | |
fetch( url, { | |
method: 'DELETE' | |
}) | |
.then( res => res.json() ) | |
.then( () => resolvee('User deleted') ) | |
.catch( err => rejectt(err) ); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment