2022-07-05 18:12:14 +00:00
|
|
|
import { NextMiddleware } from "next-api-middleware";
|
2022-07-08 18:15:25 +00:00
|
|
|
import z from "zod";
|
2022-07-05 18:12:14 +00:00
|
|
|
|
2022-07-08 18:15:25 +00:00
|
|
|
const withPage = z.object({
|
2022-07-08 18:19:51 +00:00
|
|
|
page: z
|
2022-07-08 18:15:25 +00:00
|
|
|
.string()
|
2022-07-08 19:47:31 +00:00
|
|
|
.optional()
|
2022-07-08 18:15:25 +00:00
|
|
|
.default("1")
|
|
|
|
.transform((n) => parseInt(n)),
|
|
|
|
take: z
|
|
|
|
.string()
|
2022-07-08 19:47:31 +00:00
|
|
|
.optional()
|
2022-07-08 18:15:25 +00:00
|
|
|
.default("10")
|
|
|
|
.transform((n) => parseInt(n)),
|
|
|
|
});
|
|
|
|
|
|
|
|
export const withPagination: NextMiddleware = async (req, _, next) => {
|
2022-07-08 18:19:51 +00:00
|
|
|
const { page, take } = withPage.parse(req.query);
|
2023-01-18 19:49:31 +00:00
|
|
|
const skip = (page - 1) * take;
|
2022-07-05 18:12:14 +00:00
|
|
|
req.pagination = {
|
2022-07-08 19:47:31 +00:00
|
|
|
take,
|
|
|
|
skip,
|
2022-07-05 18:12:14 +00:00
|
|
|
};
|
|
|
|
await next();
|
|
|
|
};
|