2023-02-16 21:01:40 +00:00
|
|
|
import type { 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({
|
2023-02-08 00:20:21 +00:00
|
|
|
page: z.coerce.number().min(1).optional().default(1),
|
|
|
|
take: z.coerce.number().min(1).optional().default(10),
|
2022-07-08 18:15:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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();
|
|
|
|
};
|