fix: add optional to pagination query's
parent
c8829fc08b
commit
6971ba249f
|
@ -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();
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue