Outline
Here is one possible solution for the assignment. Remember to use the --allow-net
flag to run the file.
// network-request.ts
fetch('https://jsonplaceholder.typicode.com/users/1')
.then(res => res.json())
.then(data => {
console.log(`The user's name is ${data.name}. Username: ${data.username}, email: ${data.email}`);
})
.catch(err => console.error(err));
// Extra Credit: Create a new user on JSON Placeholder API
fetch('https://jsonplaceholder.typicode.com/users', {
method: 'POST',
body: JSON.stringify({
name: 'New User',
username: 'newuser',
email: 'new@user.com'
})
})
.then(res => res.json())
.then(data => console.log(data))
.catch(err => console.error(err));