-
Notifications
You must be signed in to change notification settings - Fork 3
Week 15: Fetch API
Reid Russom edited this page Apr 13, 2024
·
3 revisions
| Week | Topic | Learning Objectives | Key Resources |
|---|---|---|---|
| 15 | 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 15 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
fetchwith 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
-
asyncandawaitkeywords make asynchronous code more readable -
asyncdeclares an asynchronous function, which always returns a promise -
awaitpauses the function execution until a promise is resolved or rejected - Error handling with
try/catchblocks insideasyncfunctions - Practice converting promise-based code to
async/awaitsyntax
- Built-in browser function for making HTTP requests
- Uses promises to handle asynchronous requests and responses
- Two parameters:
url(required) andoptions(optional)-
optionsinclude:method,headers,body
-
- Returns a promise that resolves to a
Responseobject - Handling successful and failed responses with
response.okandthrow 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}/reposusing 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
repositoriesto view the returned data
- Chained a
.catch()function to the fetch call to handle errors from the server
- Created a variable
projectSectionto select the projects section by id using DOM selection - Created a variable
projectListto select the<ul>element withinprojectSectionusing DOM selection - Created a
forloop to iterate over therepositoriesarray - Inside the loop, created a variable
projectto create a new<li>element - Set the inner text of
projectto the current array element'snameproperty - Appended the
projectelement to theprojectListelement
- 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.