You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
122 lines
2.9 KiB
JavaScript
122 lines
2.9 KiB
JavaScript
12 months ago
|
//Note: don't add a semicolon at the end of then().
|
||
|
//Fetch answers must be inside the await () for each function.
|
||
|
//Each function will be used to check the fetch answers.
|
||
|
//Don't console log the result/json, return it.
|
||
|
|
||
|
// Get Single To Do [Sample]
|
||
|
/* async function getSingleToDo() {
|
||
|
|
||
|
return await (
|
||
|
|
||
|
);
|
||
|
|
||
|
} */
|
||
|
|
||
|
|
||
|
|
||
|
// Getting all to do list item
|
||
|
async function getAllToDo() {
|
||
|
|
||
|
return await (
|
||
|
fetch('https://jsonplaceholder.typicode.com/todos')
|
||
|
.then(response => response.json())
|
||
|
.then(data => {
|
||
|
data.map((elem) => {
|
||
|
return elem.title
|
||
|
})
|
||
|
})
|
||
|
);
|
||
|
|
||
|
}
|
||
|
|
||
|
// [Section] Getting a specific to do list item
|
||
|
async function getSpecificToDo() {
|
||
|
|
||
|
return await (
|
||
|
|
||
|
//Add fetch here.
|
||
|
fetch('https://jsonplaceholder.typicode.com/todos/1')
|
||
|
.then((response) => response.json())
|
||
|
.then((json) => json)
|
||
|
);
|
||
|
|
||
|
}
|
||
|
|
||
|
// [Section] Creating a to do list item using POST method
|
||
|
async function createToDo() {
|
||
|
|
||
|
return await (
|
||
|
|
||
|
fetch("https://jsonplaceholder.typicode.com/todos", {
|
||
|
method: "POST",
|
||
|
headers: {
|
||
|
'Content-Type': 'Application/json'
|
||
|
},
|
||
|
body: JSON.stringify({
|
||
|
"userId": 1,
|
||
|
"title": "Actio non",
|
||
|
"completed": false
|
||
|
})
|
||
|
})
|
||
|
.then(response => response.json())
|
||
|
.then((json) => json)
|
||
|
|
||
|
|
||
|
);
|
||
|
|
||
|
}
|
||
|
|
||
|
// [Section] Updating a to do list item using PUT method
|
||
|
async function updateToDo() {
|
||
|
|
||
|
return await (
|
||
|
|
||
|
//Add fetch here.
|
||
|
fetch('https://jsonplaceholder.typicode.com/todos/1', {
|
||
|
method: "PUT",
|
||
|
headers: {
|
||
|
'Content-Type': 'application/json'
|
||
|
},
|
||
|
body: JSON.stringify({
|
||
|
id: 1,
|
||
|
title: 'Updated To Do List Item',
|
||
|
description: 'To update my to do list with a different data structure',
|
||
|
status: 'Pending',
|
||
|
dateCompleted: 'Pending',
|
||
|
userId: 1
|
||
|
})
|
||
|
})
|
||
|
.then(response => response.json())
|
||
|
.then(json => console.log(json))
|
||
|
);
|
||
|
};
|
||
|
|
||
|
// [Section] Deleting a to do list item
|
||
|
async function deleteToDo() {
|
||
|
|
||
|
return await (
|
||
|
fetch("https://jsonplaceholder.typicode.com/todos/1", {
|
||
|
method: "DELETE"
|
||
|
})
|
||
|
|
||
|
);
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
//Do not modify
|
||
|
//For exporting to test.js
|
||
|
try {
|
||
|
module.exports = {
|
||
|
getSingleToDo: typeof getSingleToDo !== 'undefined' ? getSingleToDo : null,
|
||
|
getAllToDo: typeof getAllToDo !== 'undefined' ? getAllToDo : null,
|
||
|
getSpecificToDo: typeof getSpecificToDo !== 'undefined' ? getSpecificToDo : null,
|
||
|
createToDo: typeof createToDo !== 'undefined' ? createToDo : null,
|
||
|
updateToDo: typeof updateToDo !== 'undefined' ? updateToDo : null,
|
||
|
deleteToDo: typeof deleteToDo !== 'undefined' ? deleteToDo : null,
|
||
|
}
|
||
|
} catch (err) {
|
||
|
|
||
|
}
|