Skip to content

Instantly share code, notes, and snippets.

@AraCodeIt
Created March 31, 2018 14:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AraCodeIt/23553eee9f99bb17056a4d6bdf55f30a to your computer and use it in GitHub Desktop.
Save AraCodeIt/23553eee9f99bb17056a4d6bdf55f30a to your computer and use it in GitHub Desktop.
class SimpleHttp {
async get(url) {
let response = undefined;
try{
const responseObj= await fetch(url);
const resData = await responseObj.json();
response = [ null , resData ];
}
catch(e){
response = [ e , null ];
}
return response ;
}
async post(url,data) {
let response = undefined;
try{
const responseObj= await fetch(url, {
method: 'POST',
headers: {
'Content-type': 'application/json'
},
body: JSON.stringify(data)
});
const resData = await responseObj.json();
response = [ null , resData ];
}
catch(e){
response = [ e , null ];
}
return response ;
}
async put(url,data) {
let response = undefined;
try{
const responseObj= await fetch(url, {
method: 'PUT',
headers: {
'Content-type': 'application/json'
},
body: JSON.stringify(data)
});
const resData = await responseObj.json();
response = [ null , resData ];
}
catch(e){
response = [ e , null ];
}
return response ;
}
async delete(url) {
let response = undefined;
try{
const responseObj= await fetch(url, {
method: 'DELETE',
headers: {
'Content-type': 'application/json'
},
body: JSON.stringify(data)
});
const resData = await 'OK';
response = [ null , resData ];
}
catch(e){
response = [ e , null ];
}
return response ;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment