Skip to content

Instantly share code, notes, and snippets.

@ehsann95
Created April 10, 2019 07:39
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 ehsann95/de154ee1d55576deb1556124a80de88b to your computer and use it in GitHub Desktop.
Save ehsann95/de154ee1d55576deb1556124a80de88b to your computer and use it in GitHub Desktop.
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))
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