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
.string()
.min(1)
.optional()
.default("1")
.transform((n) => parseInt(n)),
take: z
.string()
.min(10)
.max(100)
.optional()
.default("10")
.transform((n) => parseInt(n)),
});
export const withPagination: NextMiddleware = async (req, _, next) => {
const { page, take } = withPage.parse(req.query);
const skip = page * take;
const skip = page * take || 0;
req.pagination = {
take: take || 10,
skip: skip || 0,
take,
skip,
};
await next();
};