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

18 lines
450 B
TypeScript
Raw Normal View History

2023-02-16 21:01:40 +00:00
import type { NextMiddleware } from "next-api-middleware";
import z from "zod";
2022-07-05 18:12:14 +00:00
const withPage = z.object({
page: z.coerce.number().min(1).optional().default(1),
take: z.coerce.number().min(1).optional().default(10),
});
export const withPagination: NextMiddleware = async (req, _, next) => {
2022-07-08 18:19:51 +00:00
const { page, take } = withPage.parse(req.query);
const skip = (page - 1) * take;
2022-07-05 18:12:14 +00:00
req.pagination = {
take,
skip,
2022-07-05 18:12:14 +00:00
};
await next();
};