API Route Functions and Consumption Quiz

Test your understanding of API route functions and consumption with this summary. Topics covered include authentication checks, request body validation, database operations, and consuming API routes for fetching activities.

Created by
@GratifiedPearl

Questions and Answers

Every quiz on Quizgecko comes with Questions, Flashcards and Study notes to help you learn optimally. Sign up free now.

What is TypeScript?

A superset of JavaScript that adds type annotations and other features to the language

What is the purpose of using TypeScript?

To catch errors early and make code more readable

What does REST stand for?

Representational State Transfer

What does it mean for a web service to be RESTful?

<p>It adheres to the constraints defined by the REST architectural style</p> Signup and view all the answers

What HTTP method is used to create a new activity?

<p>POST</p> Signup and view all the answers

What does the onClick function do if the POST request to create a new activity is unsuccessful?

<p>Shows an error toast and sets the isLoading state to false</p> Signup and view all the answers

Which HTTP method is used to update an existing activity?

<p>PATCH</p> Signup and view all the answers

What does the deleteActivity function do?

<p>Makes a DELETE request to the /api/activities/[activityId] route to delete the activity</p> Signup and view all the answers

What does the onSubmit function do if the PATCH request to update an activity is successful?

<p>No specific action mentioned</p> Signup and view all the answers

What is the initial state of the button when the onClick function is called?

<p>Disabled with a loading spinner</p> Signup and view all the answers

What data is sent in the body of the POST request to create a new activity?

<p>{ name: 'New Activity', colorCode: '#ffffff' }</p> Signup and view all the answers

What does the toast function do if the activity creation is successful?

<p>Shows a success message</p> Signup and view all the answers

Which tool can be used to help debug API routes?

<p>Postman</p> Signup and view all the answers

What is Zod primarily used for?

<p>Building schemas and validating data</p> Signup and view all the answers

How is Zod installed?

<p>npm install zod</p> Signup and view all the answers

What does Zod's parse method do?

<p>Validates data against a schema</p> Signup and view all the answers

What feature does Iotawise use for authentication?

<p>NextAuth.js</p> Signup and view all the answers

Where are the API routes for the activities feature in Iotawise located?

<p>/api/activities/route.ts and /api/activities/[activityId]/route.ts</p> Signup and view all the answers

What does the GET function in /api/activities/route.ts do?

<p>Fetches all user activities from the database and returns them as a JSON string</p> Signup and view all the answers

What is the main purpose of validation in an application?

<p>To prevent errors and improve the user experience</p> Signup and view all the answers

What does Zod support in terms of validation?

<p>Complex object validation, nested schemas, and transformation</p> Signup and view all the answers

What type of database does Iotawise use?

<p>PlanetScale Database (MySQL)</p> Signup and view all the answers

What is the command to import Zod for use in a project?

<p>{ z } from 'zod'</p> Signup and view all the answers

What HTTP methods can be used in RESTful APIs?

<p>GET, POST, PUT, PATCH, DELETE</p> Signup and view all the answers

What keyword does Zod use for creating types that match the shape of data?

<p>infer</p> Signup and view all the answers

How are API routes created in Next.js?

<p>By adding a route.ts file in a /api directory</p> Signup and view all the answers

What does the function for each HTTP verb in the API route file take?

<p>Request object and a params prop</p> Signup and view all the answers

Which HTTP request is used to create new resources in a RESTful API?

<p>POST</p> Signup and view all the answers

What does a GET route in Next.js do?

<p>Fetch data from the database</p> Signup and view all the answers

What is the purpose of a PUT request in a RESTful API?

<p>Update an existing resource</p> Signup and view all the answers

What does a DELETE route in Next.js do?

<p>Delete a user from the database</p> Signup and view all the answers

What status code is returned for successful creation in a RESTful API?

<p>201</p> Signup and view all the answers

When are PATCH requests used in a RESTful API?

<p>To update partial information of an existing resource</p> Signup and view all the answers

What does a successful 200 status code indicate in a RESTful API?

<p>Successful request</p> Signup and view all the answers

What does a successful 204 status code indicate in a RESTful API?

<p>Successful request with no content to return</p> Signup and view all the answers

What do the example code snippets demonstrate in Next.js API routes?

<p>How to handle POST, GET, PUT, PATCH, and DELETE requests</p> Signup and view all the answers

What does the provided code include functions for?

<p>Creating, updating, and deleting activities in an API route</p> Signup and view all the answers

What happens if the request body does not match the schema in the provided code?

<p>The z.ZodError error is thrown, containing validation errors</p> Signup and view all the answers

How are request bodies validated in the provided code?

<p>Using the zod library and specific schemas</p> Signup and view all the answers

What does the getUserActivities function do?

<p>Fetches activities for the selected user using a raw SQL query</p> Signup and view all the answers

What is returned as a result of creating a new activity in the database in the provided code?

<p>The ID of the new activity as a JSON string</p> Signup and view all the answers

What type of error is thrown if the request body does not match the schema?

<p>z.ZodError</p> Signup and view all the answers

What does the PATCH function do in the provided code?

<p>Updates the activity in the database based on the request body</p> Signup and view all the answers

How are API routes consumed in the server component for fetching activities?

<p>Using the getUserActivities function from /lib/api/activities.ts</p> Signup and view all the answers

What type of objects are the results of fetching activities returned as?

<p>An array of UserActivities objects</p> Signup and view all the answers

What is the outcome if the user is not authenticated in the provided code?

<p>Appropriate status codes are returned</p> Signup and view all the answers

What does the DELETE function do in the provided code?

<p>Removes the activity</p> Signup and view all the answers

What library is used for parsing and validating request bodies in the provided code?

<p>zod</p> Signup and view all the answers

Where can TypeScript be implemented?

<p>Class members</p> Signup and view all the answers

In TypeScript, what can interfaces be implemented on?

<p>Only objects</p> Signup and view all the answers

In TypeScript, where can variable declarations be implemented?

<p>Only at the top level of a file</p> Signup and view all the answers

Study Notes

API Route Functions and Consumption Summary

  • The provided code includes functions for creating, updating, and deleting activities in an API route.
  • The functions first check if the user is authenticated and return appropriate status codes if not.
  • The request body is parsed and validated using the zod library and specific schemas.
  • If the request body is valid, a new activity is created in the database and its ID is returned as a JSON string.
  • The z.ZodError error is thrown if the request body does not match the schema, containing validation errors in the issues property.
  • The PATCH and DELETE functions also follow a similar structure, including authentication checks and request context validation.
  • The PATCH function updates the activity in the database based on the request body, while the DELETE function removes the activity.
  • Both functions return appropriate status codes based on the outcome of the operations.
  • API routes are consumed in a server component for fetching activities, using the getUserActivities function from /lib/api/activities.ts.
  • The getUserActivities function accesses the database directly and fetches activities for the selected user using a raw SQL query.
  • The results are returned as an array of UserActivities objects.
  • The provided code demonstrates server-side rendering and direct database access for fetching activities, as well as the structure and validation of API route functions.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

More Quizzes Like This

API 650 Closed Book Test
25 questions

API 650 Closed Book Test

UserReplaceableSmokyQuartz avatar
UserReplaceableSmokyQuartz
API 653 (Part-A) Quiz
5 questions
Postman API Testing Quiz
5 questions

Postman API Testing Quiz

CherishedSerendipity avatar
CherishedSerendipity
API Management Service Quiz
52 questions
Use Quizgecko on...
Browser
Browser