| 1 | import z from 'zod' |
| 2 | import { Pagination } from '../dtos' |
| 3 | import { createZodDto } from '../utils' |
| 4 | |
| 5 | export function createPaginationVo<T>(dataSchema: z.ZodType<T>, id?: string) { |
| 6 | const PaginationVoSchema = z.object({ |
| 7 | page: z.number().int(), |
| 8 | pageSize: z.number().int(), |
| 9 | totalPages: z.number().int(), |
| 10 | total: z.number().int(), |
| 11 | list: z.array(dataSchema), |
| 12 | }) |
| 13 | |
| 14 | class PaginationVo extends createZodDto(PaginationVoSchema, id) { |
| 15 | constructor(list: T[], total: number, pagination: Pagination) { |
| 16 | super() |
| 17 | Object.assign(this, PaginationVo.create({ |
| 18 | page: pagination.page, |
| 19 | pageSize: pagination.pageSize, |
| 20 | totalPages: Math.ceil(total / pagination.pageSize), |
| 21 | total, |
| 22 | list, |
| 23 | })) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | return PaginationVo |
| 28 | } |
| 29 | |
| 30 | export interface PaginationVo<T> { |
| 31 | page: number |
| 32 | pageSize: number |
| 33 | totalPages: number |
| 34 | total: number |
| 35 | list: T[] |
| 36 | } |
| 37 |