Create the API endpoint src/pages/api/projects.ts. Here is the description: Next.js API route for projects. Be sure to add the proper extension when creating the file. Here are the instructions:
Issue Description
In this issue, we will create a Next.js API route for managing projects. The API route will be located at src/pages/api/projects.ts. This route will handle the following actions:
GET: Retrieve a list of all projects for the authenticated user.
POST: Create a new project for the authenticated user.
Step-by-step Instructions
Create a new file named projects.ts inside the src/pages/api directory.
Import the necessary modules and types:
import { NextApiRequest, NextApiResponse } from "next";
import { Prisma } from "@prisma/client";
import { z } from "zod";
import { prisma } from "~/server/db";
import { getServerAuthSession } from "~/server/auth";
import { User, Project, projectSchema } from "~/src/types";
Define the handler function with the following signature:
Inside the handler function, call getServerAuthSession({ req, res }) to retrieve the user's session. If there is no session, return a 401 Unauthorized status code and an error message:
const session = await getServerAuthSession({ req, res });
if (!session) {
res.status(401).json({ error: "Unauthorized" });
return;
}
Now, handle the different request methods (GET and POST) using a switch statement:
switch (req.method) {
case "GET":
// Handle GET requests
break;
case "POST":
// Handle POST requests
break;
default:
res.setHeader("Allow", ["GET", "POST"]);
res.status(405).end(`Method ${req.method} Not Allowed`);
}
For the GET case, query the database to retrieve all projects for the authenticated user:
Finally, export the handler function as the default export:
export default handler;
Summary
In this issue, we created a Next.js API route for managing projects at src/pages/api/projects.ts. The route supports GET and POST methods for retrieving a list of projects and creating a new project for the authenticated user. We used the getServerAuthSession function for authentication, Prisma for database interaction, and Zod for data validation.
Create the API endpoint src/pages/api/projects.ts. Here is the description: Next.js API route for projects. Be sure to add the proper extension when creating the file. Here are the instructions:
Issue Description
In this issue, we will create a Next.js API route for managing projects. The API route will be located at
src/pages/api/projects.ts
. This route will handle the following actions:Step-by-step Instructions
Create a new file named
projects.ts
inside thesrc/pages/api
directory.Import the necessary modules and types:
handler
function with the following signature:handler
function, callgetServerAuthSession({ req, res })
to retrieve the user's session. If there is no session, return a 401 Unauthorized status code and an error message:projectSchema
from thetypes.ts
file:projectSchema
from thetypes.ts
file:handler
function as the default export:Summary
In this issue, we created a Next.js API route for managing projects at
src/pages/api/projects.ts
. The route supports GET and POST methods for retrieving a list of projects and creating a new project for the authenticated user. We used thegetServerAuthSession
function for authentication, Prisma for database interaction, and Zod for data validation.