Created
March 31, 2018 14:41
-
-
Save AraCodeIt/23553eee9f99bb17056a4d6bdf55f30a 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
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