cal.pub0.org/lib/helpers/withPagination.ts

27 lines
563 B
TypeScript
Raw Normal View History

2022-07-05 18:12:14 +00:00
import { NextMiddleware } from "next-api-middleware";
import z from "zod";
2022-07-05 18:12:14 +00:00
const withPage = z.object({
pageNumber: z
.string()
.min(1)
.default("1")
.transform((n) => parseInt(n)),
take: z
.string()
.min(10)
.max(100)
.default("10")
.transform((n) => parseInt(n)),
});
export const withPagination: NextMiddleware = async (req, _, next) => {
const { pageNumber, take } = withPage.parse(req.query);
const skip = pageNumber * take;
2022-07-05 18:12:14 +00:00
req.pagination = {
take: take || 10,
2022-07-05 18:12:14 +00:00
skip: skip || 0,
};
await next();
};