-
Notifications
You must be signed in to change notification settings - Fork 2
v5 Week 13: Fetch API
Reid Russom edited this page Oct 8, 2024
·
2 revisions
Week | Topic | Learning Objectives | Key Resources |
---|---|---|---|
13 | Fetch API | Students will be able to explain the fetch API structure including get and post aspects; implement a fetch request by handling the data returned via async/await while accounting for any errors that may happen during the request. Complete the portfolio part and begin their Open API part of their final project. | Week 13 Slides TBD |
- JSON (JavaScript Object Notation) is a standardized format for structuring data
- Based on JavaScript object syntax
- Commonly used for transmitting data on the web
- APIs (Application Programming Interfaces) are servers created for serving data for external use
- Accessed through URLs with specific query parameters
- API keys are often required for authentication and tracking usage
- Securing API keys is important to prevent abuse and unnecessary costs
- Older method: XMLHttpRequest (complex and verbose)
- Modern method:
fetch
(simpler and cleaner syntax) - Example using the Giphy API:
- Sign up for a free API key
- Construct the API URL with the key and search term
- Use
fetch
with the URL and handle the response with.then()
and.catch()
- Extract the desired data from the response object
- CORS (Cross-Origin Resource Sharing) restrictions and the
{mode: 'cors'}
option
-
async
andawait
keywords make asynchronous code more readable -
async
declares an asynchronous function, which always returns a promise -
await
pauses the function execution until a promise is resolved or rejected - Error handling with
try
/catch
blocks insideasync
functions - Practice converting promise-based code to
async
/await
syntax
- Built-in browser function for making HTTP requests
- Uses promises to handle asynchronous requests and responses
- Two parameters:
url
(required) andoptions
(optional)-
options
include:method
,headers
,body
-
- Returns a promise that resolves to a
Response
object - Handling successful and failed responses with
response.ok
andthrow new Error()
- Parsing JSON data with
response.json()
Potential trouble spots for students:
- Incorrect API URL or missing GitHub username in the API URL
- Difficulty understanding the structure of the returned JSON data and accessing the desired properties
- Forgetting to parse the JSON response before accessing the data
- Confusion with chaining multiple .then() methods and handling the response in each step
- Issues with DOM selection and appending elements to the project list
- Creating a sub-repository by cloning the open API project repository inside the portfolio folder
- Struggling to understand the API documentation and determining the required parameters or authentication methods
- Forgetting to handle errors using the .catch() method
- Mixing up the order of git add, git commit, and git push commands
- Merged previous lesson-14 pull request into main branch
- Created new lesson-15 branch from updated main branch
- Created a "GET" request to
https://api.github.com/users/{GITHUB_USERNAME}/repos
using the Fetch API - Chained a
.then()
method to the fetch call to return the response JSON data - Chained another
.then()
method to parse the response and store it in a variable namedrepositories
- Console logged the value of
repositories
to view the returned data
- Chained a
.catch()
function to the fetch call to handle errors from the server
- Created a variable
projectSection
to select the projects section by id using DOM selection - Created a variable
projectList
to select the<ul>
element withinprojectSection
using DOM selection - Created a
for
loop to iterate over therepositories
array - Inside the loop, created a variable
project
to create a new<li>
element - Set the inner text of
project
to the current array element'sname
property - Appended the
project
element to theprojectList
element
- Staged changes using
git add
- Committed changes with a meaningful commit message using
git commit
- Pushed changes to the GitHub repository using
git push
- Created a pull request for the lesson-15 branch
- Familiarized with the documentation of the chosen API
- Created a new repository in GitHub for the open API project
- Connected the repository to the local machine
- Created basic files (index.html, index.js, index.css) with correct linking
- Tested the API fetch and logged the response to the console
- Staged changes using
git add
- Committed changes with a meaningful commit message using
git commit
- Pushed changes to the GitHub repository using
git push
The Intro Guidebook is created by Code the Dream staff and volunteers for Code the Dream volunteers. This is your tool – please feel free to suggest edits or improvements.
Overview of the wiki.
Onboarding guide for new volunteers.
Links to pages for specific assignments, including rubrics, overviews of student content, and mentor-created resources.