Skip to content

Instantly share code, notes, and snippets.

@akhyaruu
Created January 23, 2020 13:22
Show Gist options
  • Save akhyaruu/436b39bd4709165b5a94fc8f8fdcbcba to your computer and use it in GitHub Desktop.
Save akhyaruu/436b39bd4709165b5a94fc8f8fdcbcba to your computer and use it in GitHub Desktop.
const request = new Request;
const jsonData = {
id: 42,
title: 'Meaning of Life',
};
const getJsonResponse = request.get("https://httpbin.org/get");
getJsonResponse.then(data => data).then(data => console.log(data));
const postJsonResponse = request.post("https://httpbin.org/post", jsonData);
postJsonResponse.then(data => data).then(data => console.log(data));
const putJsonResponse = request.put("https://httpbin.org/put");
putJsonResponse.then(data => data).then(data => console.log(data));
const deleteJsonResponse = request.delete("https://httpbin.org/delete");
deleteJsonResponse.then(data => data).then(data => console.log(data));
class Request {
async get(url) {
const response = await fetch(url);
const responseData = response.json();
return responseData;
}
async delete(url) {
const response = fetch(url, {
method: 'DELETE',
headers: {
'Content-type': 'application/json'
},
})
const responseData = await 'Resource deleted...';
return responseData;
}
async put(url, dataBaru) {
const response = await fetch(url, {
method: 'PUT',
headers: {
'Content-type': 'application/json'
},
body: JSON.stringify(dataBaru)
})
const responseData = await response.json();
return responseData;
}
async post(url, dataBaru) {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-type': 'application/json'
},
body: JSON.stringify(dataBaru)
})
const responseData = await response.json();
return responseData;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment