Skip to content

Commit

Permalink
refactor: follow review on route.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
ogatalars authored Mar 4, 2024
1 parent fe176bc commit 2928419
Showing 1 changed file with 13 additions and 20 deletions.
33 changes: 13 additions & 20 deletions src/app/api/users/route.ts
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 2928419

Please sign in to comment.