Created
April 10, 2019 07:39
-
-
Save ehsann95/de154ee1d55576deb1556124a80de88b to your computer and use it in GitHub Desktop.
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
const http = new EasyHTTP; | |
// http.get('https://jsonplaceholder.typicode.com/users') | |
// .then(user => user.forEach(usr => console.log(usr.name))); | |
const data = { | |
name: 'Ahsan Nazir', | |
username: 'ahsnann', | |
email: 'ahsan@gmail.com' | |
} | |
// http.post('https://jsonplaceholder.typicode.com/users', data) | |
// .then(user => console.log(user)); | |
// http.put('https://jsonplaceholder.typicode.com/users/2', data) | |
// .then(user => console.log(user)) | |
http.delete('https://jsonplaceholder.typicode.com/users/2') | |
.then(user => console.log(user)) |
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 EasyHTTP { | |
// GET Request | |
async get(url) { | |
const response= await fetch(url); | |
const data = await response.json(); | |
return data; | |
} | |
//POST Request | |
async post(url, data) { | |
const response = await fetch(url, { | |
method: 'POST', | |
headers: {'Content-type': 'application/json'}, | |
body: JSON.stringify(data) | |
}); | |
const dataa = await response.json(); | |
return dataa; | |
} | |
// PUT Request | |
async put(url, data) { | |
const response = await fetch(url, { | |
method: 'PUT', | |
headers: {'Content-type': 'application/json'}, | |
body: JSON.stringify(data) | |
}); | |
const resData = await response.json(); | |
return resData; | |
} | |
// DELETE Request | |
async delete(url) { | |
const response = await fetch(url, { | |
method:'DELETE', | |
headers: {'Content-type': 'application/json'} | |
}); | |
const data = await 'Resource Deleted!!'; | |
return data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment