fix: add optional to pagination query's

pull/9078/head
Agusti Fernandez Pardo 2022-07-08 21:47:31 +02:00
parent c8829fc08b
commit 6971ba249f
1 changed files with 5 additions and 3 deletions

View File

@ -5,22 +5,24 @@ const withPage = z.object({
page: z page: z
.string() .string()
.min(1) .min(1)
.optional()
.default("1") .default("1")
.transform((n) => parseInt(n)), .transform((n) => parseInt(n)),
take: z take: z
.string() .string()
.min(10) .min(10)
.max(100) .max(100)
.optional()
.default("10") .default("10")
.transform((n) => parseInt(n)), .transform((n) => parseInt(n)),
}); });
export const withPagination: NextMiddleware = async (req, _, next) => { export const withPagination: NextMiddleware = async (req, _, next) => {
const { page, take } = withPage.parse(req.query); const { page, take } = withPage.parse(req.query);
const skip = page * take; const skip = page * take || 0;
req.pagination = { req.pagination = {
take: take || 10, take,
skip: skip || 0, skip,
}; };
await next(); await next();
}; };