diff --git a/src/app/api/users/route.ts b/src/app/api/users/route.ts index d905dbe..778d381 100644 --- a/src/app/api/users/route.ts +++ b/src/app/api/users/route.ts @@ -1,30 +1,23 @@ import { user } from "@/dao/user.dao"; -import { NextResponse, NextRequest } from "next/server"; +import { NextResponse } from "next/server"; import { z } from "zod"; -const checkGetAllUsersParams = z.object({ +const DEFAULT_PAGE = 1; +const DEFAULT_PAGE_SIZE = 10; + +const ListAllUsersParamsSchema = z.object({ username: z.string().optional(), email: z.string().optional(), - page: z.number().optional().transform((val) => (val ? Number(val) : undefined)), - pageSize: z.number().optional().transform((val) => (val ? Number(val) : undefined)), + page: z.number().default(DEFAULT_PAGE), + pageSize: z.number().default(DEFAULT_PAGE_SIZE), }); -export async function GET(req: NextRequest) { - try { - - const queryParams = checkGetAllUsersParams.parse(Object.fromEntries(req.nextUrl.searchParams)); +export async function GET(req) { + const queryParams = ListAllUsersParamsSchema.parse(Object.fromEntries(req.nextUrl.searchParams)); + + const { username, email, page, pageSize } = queryParams; - const { username, email, page, pageSize } = queryParams; - - const users = await user.fetchUsers({ username, email }, page, pageSize); + const users = await user.fetchUsers({ username, email }, page, pageSize); - return new NextResponse(JSON.stringify(users), { - status: 200, - }); - } catch (error) { - console.error(error); - return new NextResponse(JSON.stringify({ error: "Internal Server Error" }), { - status: 500, - }); - } + return NextResponse.json(users); }