@longzai-intelligence/pagination 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -207
- package/dist/hono.cjs +1 -0
- package/dist/hono.d.cts +15 -0
- package/dist/hono.d.mts +15 -0
- package/dist/hono.mjs +1 -0
- package/dist/{index-C8FaNzej.d.mts → index-C4IwIpFc.d.mts} +55 -1
- package/dist/{index-CMiyhlfm.d.cts → index-DpbbjAwO.d.cts} +55 -1
- package/dist/index.cjs +1 -33
- package/dist/index.d.cts +80 -2
- package/dist/index.d.mts +80 -2
- package/dist/index.mjs +1 -1
- package/dist/typeorm.cjs +1 -129
- package/dist/typeorm.d.cts +1 -1
- package/dist/typeorm.d.mts +1 -1
- package/dist/typeorm.mjs +1 -1
- package/dist/utils-B200OLT-.cjs +1 -0
- package/dist/utils-DsM7-PZL.mjs +1 -0
- package/package.json +19 -7
- package/.turbo/turbo-build.log +0 -22
- package/.turbo/turbo-lint.log +0 -1
- package/.turbo/turbo-test$colon$coverage.log +0 -36
- package/.turbo/turbo-test.log +0 -14
- package/.turbo/turbo-typecheck.log +0 -4
- package/CHANGELOG.md +0 -18
- package/coverage/base.css +0 -224
- package/coverage/block-navigation.js +0 -87
- package/coverage/coverage-final.json +0 -16
- package/coverage/favicon.png +0 -0
- package/coverage/index.html +0 -176
- package/coverage/prettify.css +0 -1
- package/coverage/prettify.js +0 -2
- package/coverage/sort-arrow-sprite.png +0 -0
- package/coverage/sorter.js +0 -210
- package/coverage/src/adapters/index.html +0 -116
- package/coverage/src/adapters/typeorm.adapter.ts.html +0 -940
- package/coverage/src/index.html +0 -131
- package/coverage/src/index.ts.html +0 -280
- package/coverage/src/schemas/index.html +0 -161
- package/coverage/src/schemas/index.ts.html +0 -169
- package/coverage/src/schemas/pagination.schemas.ts.html +0 -343
- package/coverage/src/schemas/result.schemas.ts.html +0 -724
- package/coverage/src/schemas/sort.schemas.ts.html +0 -544
- package/coverage/src/typeorm.ts.html +0 -154
- package/coverage/src/types/index.html +0 -176
- package/coverage/src/types/index.ts.html +0 -199
- package/coverage/src/types/pagination.types.ts.html +0 -169
- package/coverage/src/types/result.types.ts.html +0 -127
- package/coverage/src/types/sort.types.ts.html +0 -142
- package/coverage/src/types/typeorm.d.ts.html +0 -196
- package/coverage/src/utils/index.html +0 -146
- package/coverage/src/utils/index.ts.html +0 -178
- package/coverage/src/utils/pagination.util.ts.html +0 -703
- package/coverage/src/utils/validation.util.ts.html +0 -535
- package/dist/utils-CV6Ovku6.mjs +0 -1
- package/dist/utils-D_qD2YAX.cjs +0 -1
- package/docs/specs/spec-20260413-typeorm-subpath-export/checklist.md +0 -33
- package/docs/specs/spec-20260413-typeorm-subpath-export/spec.md +0 -104
- package/docs/specs/spec-20260413-typeorm-subpath-export/tasks.md +0 -48
- package/eslint.config.ts +0 -3
- package/src/__tests__/index.test.ts +0 -72
- package/src/__tests__/pagination.util.test.ts +0 -251
- package/src/__tests__/result.schemas.test.ts +0 -142
- package/src/__tests__/sort.schemas.test.ts +0 -157
- package/src/__tests__/typeorm.adapter.test.ts +0 -212
- package/src/__tests__/validation.util.test.ts +0 -120
- package/src/adapters/typeorm/adapter.ts +0 -203
- package/src/adapters/typeorm/index.ts +0 -23
- package/src/adapters/typeorm/typeorm.d.ts +0 -37
- package/src/adapters/typeorm/types.ts +0 -95
- package/src/index.ts +0 -65
- package/src/schemas/index.ts +0 -28
- package/src/schemas/pagination.schemas.ts +0 -86
- package/src/schemas/result.schemas.ts +0 -213
- package/src/schemas/sort.schemas.ts +0 -153
- package/src/typeorm.ts +0 -23
- package/src/types/index.ts +0 -38
- package/src/types/pagination.types.ts +0 -28
- package/src/types/result.types.ts +0 -14
- package/src/types/sort.types.ts +0 -19
- package/src/utils/index.ts +0 -31
- package/src/utils/pagination.util.ts +0 -206
- package/src/utils/validation.util.ts +0 -150
- package/tsconfig/.cache/app.tsbuildinfo +0 -1
- package/tsconfig/.cache/node.tsbuildinfo +0 -1
- package/tsconfig/.cache/test.tsbuildinfo +0 -1
- package/tsconfig/app.json +0 -15
- package/tsconfig/node.json +0 -11
- package/tsconfig/test.json +0 -14
- package/tsconfig.json +0 -9
- package/tsdown.config.ts +0 -13
- package/vitest.config.ts +0 -3
package/README.md
CHANGED
|
@@ -10,213 +10,42 @@ bun add @longzai-intelligence/pagination
|
|
|
10
10
|
|
|
11
11
|
## 特性
|
|
12
12
|
|
|
13
|
-
-
|
|
14
|
-
-
|
|
15
|
-
-
|
|
16
|
-
-
|
|
17
|
-
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
### 参数验证
|
|
49
|
-
|
|
50
|
-
```typescript
|
|
51
|
-
import {
|
|
52
|
-
isValidPage,
|
|
53
|
-
isValidPageSize,
|
|
54
|
-
isValidPagination,
|
|
55
|
-
} from '@longzai-intelligence/pagination';
|
|
56
|
-
|
|
57
|
-
if (isValidPagination(params)) {
|
|
58
|
-
// 参数有效
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
if (isValidPage(1) && isValidPageSize(20)) {
|
|
62
|
-
// 页码和页面大小有效
|
|
63
|
-
}
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
### TypeORM 适配器
|
|
67
|
-
|
|
68
|
-
TypeORM 适配器通过子路径导出,需要单独导入:
|
|
69
|
-
|
|
70
|
-
```typescript
|
|
71
|
-
import {
|
|
72
|
-
paginateWithRepository,
|
|
73
|
-
paginateWithQueryBuilder,
|
|
74
|
-
} from '@longzai-intelligence/pagination/typeorm';
|
|
75
|
-
|
|
76
|
-
// 使用 Repository 分页
|
|
77
|
-
const result = await paginateWithRepository(userRepository, {
|
|
78
|
-
page: 1,
|
|
79
|
-
pageSize: 20,
|
|
80
|
-
}, {
|
|
81
|
-
where: { active: true },
|
|
82
|
-
order: { createdAt: 'DESC' },
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
// 使用 QueryBuilder 分页
|
|
86
|
-
const qb = userRepository.createQueryBuilder('user');
|
|
87
|
-
const result = await paginateWithQueryBuilder(qb, { page: 1, pageSize: 20 });
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
**注意**:TypeORM 适配器需要安装 `typeorm` 作为 peerDependency。
|
|
91
|
-
|
|
92
|
-
## API 文档
|
|
93
|
-
|
|
94
|
-
### 类型
|
|
95
|
-
|
|
96
|
-
#### PaginationParams
|
|
97
|
-
|
|
98
|
-
```typescript
|
|
99
|
-
interface PaginationParams {
|
|
100
|
-
page?: number; // 页码,从 1 开始
|
|
101
|
-
pageSize?: number; // 每页数量
|
|
102
|
-
}
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
#### OffsetPaginationParams
|
|
106
|
-
|
|
107
|
-
```typescript
|
|
108
|
-
interface OffsetPaginationParams {
|
|
109
|
-
limit?: number; // 每页数量
|
|
110
|
-
offset?: number; // 偏移量
|
|
111
|
-
}
|
|
112
|
-
```
|
|
113
|
-
|
|
114
|
-
#### PaginatedResult
|
|
115
|
-
|
|
116
|
-
```typescript
|
|
117
|
-
interface PaginatedResult<T> {
|
|
118
|
-
items: T[]; // 数据项
|
|
119
|
-
total: number; // 总记录数
|
|
120
|
-
page: number; // 当前页码
|
|
121
|
-
pageSize: number; // 每页数量
|
|
122
|
-
totalPages: number; // 总页数
|
|
123
|
-
hasNextPage: boolean; // 是否有下一页
|
|
124
|
-
hasPreviousPage: boolean; // 是否有上一页
|
|
125
|
-
}
|
|
126
|
-
```
|
|
127
|
-
|
|
128
|
-
#### SortParams
|
|
129
|
-
|
|
130
|
-
```typescript
|
|
131
|
-
interface SortParams<T extends string = string> {
|
|
132
|
-
sortBy?: T; // 排序字段
|
|
133
|
-
sortOrder?: 'asc' | 'desc'; // 排序方向
|
|
134
|
-
}
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
### 常量
|
|
138
|
-
|
|
139
|
-
#### PAGINATION_DEFAULTS
|
|
140
|
-
|
|
141
|
-
```typescript
|
|
142
|
-
const PAGINATION_DEFAULTS = {
|
|
143
|
-
page: 1,
|
|
144
|
-
pageSize: 20,
|
|
145
|
-
maxPageSize: 100,
|
|
146
|
-
minPageSize: 1,
|
|
147
|
-
} as const;
|
|
148
|
-
```
|
|
149
|
-
|
|
150
|
-
### 工具函数
|
|
151
|
-
|
|
152
|
-
#### normalizePagination(params?)
|
|
153
|
-
|
|
154
|
-
规范化分页参数,确保参数在有效范围内。
|
|
155
|
-
|
|
156
|
-
```typescript
|
|
157
|
-
const { page, pageSize } = normalizePagination({ page: 2, pageSize: 50 });
|
|
158
|
-
```
|
|
159
|
-
|
|
160
|
-
#### calculateOffset(page, pageSize)
|
|
161
|
-
|
|
162
|
-
计算偏移量。
|
|
163
|
-
|
|
164
|
-
```typescript
|
|
165
|
-
const offset = calculateOffset(2, 20); // 20
|
|
166
|
-
```
|
|
167
|
-
|
|
168
|
-
#### calculateTotalPages(total, pageSize)
|
|
169
|
-
|
|
170
|
-
计算总页数。
|
|
171
|
-
|
|
172
|
-
```typescript
|
|
173
|
-
const totalPages = calculateTotalPages(100, 20); // 5
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
#### hasNextPage(page, totalPages) / hasPreviousPage(page)
|
|
177
|
-
|
|
178
|
-
判断是否有下一页/上一页。
|
|
179
|
-
|
|
180
|
-
```typescript
|
|
181
|
-
hasNextPage(1, 5); // true
|
|
182
|
-
hasPreviousPage(1); // false
|
|
183
|
-
```
|
|
184
|
-
|
|
185
|
-
#### offsetToPage(offset, limit) / pageToOffset(page, pageSize)
|
|
186
|
-
|
|
187
|
-
分页风格转换。
|
|
188
|
-
|
|
189
|
-
```typescript
|
|
190
|
-
offsetToPage(20, 20); // { page: 2, pageSize: 20 }
|
|
191
|
-
pageToOffset(2, 20); // { offset: 20, limit: 20 }
|
|
192
|
-
```
|
|
193
|
-
|
|
194
|
-
### 验证函数
|
|
195
|
-
|
|
196
|
-
#### isValidPage(page)
|
|
197
|
-
|
|
198
|
-
验证页码是否有效(正整数)。
|
|
199
|
-
|
|
200
|
-
#### isValidPageSize(pageSize)
|
|
201
|
-
|
|
202
|
-
验证每页数量是否有效(1-100 之间的整数)。
|
|
203
|
-
|
|
204
|
-
#### isValidOffset(offset)
|
|
205
|
-
|
|
206
|
-
验证偏移量是否有效(非负整数)。
|
|
207
|
-
|
|
208
|
-
#### isValidLimit(limit)
|
|
209
|
-
|
|
210
|
-
验证限制数量是否有效(1-100 之间的整数)。
|
|
211
|
-
|
|
212
|
-
#### isValidPagination(params)
|
|
213
|
-
|
|
214
|
-
验证分页参数对象是否有效。
|
|
215
|
-
|
|
216
|
-
#### isValidOffsetPagination(params)
|
|
217
|
-
|
|
218
|
-
验证 offset 分页参数对象是否有效。
|
|
13
|
+
- 类型安全的分页参数定义(page/pageSize、limit/offset、cursor)
|
|
14
|
+
- Schema 工厂函数动态创建分页 Schema
|
|
15
|
+
- 预设函数提供场景化分页方案
|
|
16
|
+
- 参数验证工具
|
|
17
|
+
- TypeORM 适配器(可选)
|
|
18
|
+
- Hono 适配器(可选)
|
|
19
|
+
|
|
20
|
+
## 导出概览
|
|
21
|
+
|
|
22
|
+
### 主入口 `@longzai-intelligence/pagination`
|
|
23
|
+
|
|
24
|
+
| 分类 | 导出 |
|
|
25
|
+
|------|------|
|
|
26
|
+
| 参数类型 | `PaginationParams`、`OffsetPaginationParams`、`CursorPaginationParams` |
|
|
27
|
+
| 结果类型 | `PaginatedResult`、`OffsetPaginatedResult`、`CursorPaginatedResult`、`CompactPaginatedResult` |
|
|
28
|
+
| 排序类型 | `SortOrder`、`SortParams`、`MultiSortParams` |
|
|
29
|
+
| Schema 工厂 | `createPaginationSchema`、`createResultSchema` |
|
|
30
|
+
| 预设函数 | `createTablePreset`、`createInfiniteScrollPreset`、`createTimelinePreset`、`createCompactListPreset` |
|
|
31
|
+
| 工具函数 | `normalizePagination`、`calculateOffset`、`createPaginatedResult`、`createPaginationDefaults` |
|
|
32
|
+
| 验证函数 | `isValidPage`、`isValidPageSize`、`isValidPagination` 等 |
|
|
33
|
+
|
|
34
|
+
### 子路径导出
|
|
35
|
+
|
|
36
|
+
| 路径 | 说明 |
|
|
37
|
+
|------|------|
|
|
38
|
+
| `@longzai-intelligence/pagination/typeorm` | TypeORM 适配器 |
|
|
39
|
+
| `@longzai-intelligence/pagination/hono` | Hono 适配器 |
|
|
40
|
+
|
|
41
|
+
## 依赖要求
|
|
42
|
+
|
|
43
|
+
| 包 | 版本 | 说明 |
|
|
44
|
+
|------|------|------|
|
|
45
|
+
| typeorm | `^0.3.0` | 可选,使用 TypeORM 适配器时需要 |
|
|
46
|
+
| zod | `^4.0.0` | 可选,使用 Schema 功能时需要 |
|
|
47
|
+
| hono | `^4.0.0` | 可选,使用 Hono 适配器时需要 |
|
|
219
48
|
|
|
220
49
|
## 许可证
|
|
221
50
|
|
|
222
|
-
|
|
51
|
+
UNLICENSED
|
package/dist/hono.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require(`zod`);function t(e){if(e===void 0)return;let t=Number(e);return Number.isNaN(t)?void 0:t}function n(n,r){let i=r.shape,a={};for(let[r]of Object.entries(i))if(n[r]!==void 0){let o=i[r];o instanceof e.z.ZodNumber||o instanceof e.z.ZodOptional&&o.unwrap()instanceof e.z.ZodNumber?a[r]=t(n[r]):a[r]=n[r]}return r.parse(a)}function r(e,t){let n=e.sortBy,r=e.sortOrder;if(n&&t&&!t.includes(n))return{};if(r&&r!==`asc`&&r!==`desc`)return{sortBy:n};let i={};return n&&(i.sortBy=n),r&&(i.sortOrder=r),i}exports.parsePaginationFromQuery=n,exports.parseSortFromQuery=r;
|
package/dist/hono.d.cts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { s as SortParams } from "./index-DpbbjAwO.cjs";
|
|
2
|
+
import * as _$zod from "zod";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
|
|
5
|
+
//#region src/adapters/hono/parse-query.d.ts
|
|
6
|
+
declare function parsePaginationFromQuery(query: Record<string, string | undefined>, schema: z.ZodObject<Record<string, z.ZodTypeAny>>): Record<string, unknown>;
|
|
7
|
+
declare function parseSortFromQuery(query: Record<string, string | undefined>, allowedFields?: readonly string[]): SortParams;
|
|
8
|
+
//#endregion
|
|
9
|
+
//#region src/adapters/hono/types.d.ts
|
|
10
|
+
type HonoParseQueryOptions = {
|
|
11
|
+
paginationSchema?: _$zod.ZodObject<Record<string, _$zod.ZodTypeAny>>;
|
|
12
|
+
sortFields?: readonly string[];
|
|
13
|
+
};
|
|
14
|
+
//#endregion
|
|
15
|
+
export { type HonoParseQueryOptions, parsePaginationFromQuery, parseSortFromQuery };
|
package/dist/hono.d.mts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { s as SortParams } from "./index-C4IwIpFc.mjs";
|
|
2
|
+
import * as _$zod from "zod";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
|
|
5
|
+
//#region src/adapters/hono/parse-query.d.ts
|
|
6
|
+
declare function parsePaginationFromQuery(query: Record<string, string | undefined>, schema: z.ZodObject<Record<string, z.ZodTypeAny>>): Record<string, unknown>;
|
|
7
|
+
declare function parseSortFromQuery(query: Record<string, string | undefined>, allowedFields?: readonly string[]): SortParams;
|
|
8
|
+
//#endregion
|
|
9
|
+
//#region src/adapters/hono/types.d.ts
|
|
10
|
+
type HonoParseQueryOptions = {
|
|
11
|
+
paginationSchema?: _$zod.ZodObject<Record<string, _$zod.ZodTypeAny>>;
|
|
12
|
+
sortFields?: readonly string[];
|
|
13
|
+
};
|
|
14
|
+
//#endregion
|
|
15
|
+
export { type HonoParseQueryOptions, parsePaginationFromQuery, parseSortFromQuery };
|
package/dist/hono.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{z as e}from"zod";function t(e){if(e===void 0)return;let t=Number(e);return Number.isNaN(t)?void 0:t}function n(n,r){let i=r.shape,a={};for(let[r]of Object.entries(i))if(n[r]!==void 0){let o=i[r];o instanceof e.ZodNumber||o instanceof e.ZodOptional&&o.unwrap()instanceof e.ZodNumber?a[r]=t(n[r]):a[r]=n[r]}return r.parse(a)}function r(e,t){let n=e.sortBy,r=e.sortOrder;if(n&&t&&!t.includes(n))return{};if(r&&r!==`asc`&&r!==`desc`)return{sortBy:n};let i={};return n&&(i.sortBy=n),r&&(i.sortOrder=r),i}export{n as parsePaginationFromQuery,r as parseSortFromQuery};
|
|
@@ -23,6 +23,27 @@ declare const CursorPaginationParamsSchema: z.ZodObject<{
|
|
|
23
23
|
limit: z.ZodOptional<z.ZodNumber>;
|
|
24
24
|
}, z.core.$strip>;
|
|
25
25
|
type CursorPaginationParams = z.infer<typeof CursorPaginationParamsSchema>;
|
|
26
|
+
type CreatePaginationSchemaOptions = {
|
|
27
|
+
mode: 'page' | 'offset' | 'cursor';
|
|
28
|
+
pageSizeField?: string;
|
|
29
|
+
cursorField?: string;
|
|
30
|
+
fields?: {
|
|
31
|
+
page?: boolean;
|
|
32
|
+
pageSize?: boolean;
|
|
33
|
+
limit?: boolean;
|
|
34
|
+
offset?: boolean;
|
|
35
|
+
cursor?: boolean;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
declare function createPaginationSchema(options: CreatePaginationSchemaOptions): z.ZodObject<{
|
|
39
|
+
page: z.ZodOptional<z.ZodNumber>;
|
|
40
|
+
} | {
|
|
41
|
+
page?: undefined;
|
|
42
|
+
}, z.core.$strip> | z.ZodObject<{
|
|
43
|
+
limit: z.ZodOptional<z.ZodNumber>;
|
|
44
|
+
} | {
|
|
45
|
+
limit?: undefined;
|
|
46
|
+
}, z.core.$strip>;
|
|
26
47
|
//#endregion
|
|
27
48
|
//#region src/types/pagination.types.d.ts
|
|
28
49
|
declare const PAGINATION_DEFAULTS: {
|
|
@@ -77,6 +98,39 @@ type CursorPaginatedResult<T> = {
|
|
|
77
98
|
previousCursor: string | null;
|
|
78
99
|
hasMore: boolean;
|
|
79
100
|
};
|
|
101
|
+
type CompactPaginatedResult<T> = {
|
|
102
|
+
items: T[];
|
|
103
|
+
total: number;
|
|
104
|
+
hasMore: boolean;
|
|
105
|
+
};
|
|
106
|
+
declare function createCompactPaginatedResultSchema<T extends z.ZodTypeAny>(itemSchema: T): z.ZodObject<{
|
|
107
|
+
items: z.ZodArray<T>;
|
|
108
|
+
total: z.ZodNumber;
|
|
109
|
+
hasMore: z.ZodBoolean;
|
|
110
|
+
}, z.core.$strip>;
|
|
111
|
+
type CreateResultSchemaOptions = {
|
|
112
|
+
mode: 'page' | 'offset' | 'cursor' | 'compact';
|
|
113
|
+
pageSizeField?: string;
|
|
114
|
+
metadata?: ('total' | 'totalPages' | 'hasNextPage' | 'hasPreviousPage' | 'hasMore')[];
|
|
115
|
+
};
|
|
116
|
+
declare function createResultSchema<T extends z.ZodTypeAny>(itemSchema: T, options: CreateResultSchemaOptions): z.ZodObject<{
|
|
117
|
+
[x: string]: z.ZodNumber | z.ZodBoolean | z.ZodArray<T>;
|
|
118
|
+
items: z.ZodArray<T>;
|
|
119
|
+
total: z.ZodNumber;
|
|
120
|
+
page: z.ZodNumber;
|
|
121
|
+
totalPages: z.ZodNumber;
|
|
122
|
+
hasNextPage: z.ZodBoolean;
|
|
123
|
+
hasPreviousPage: z.ZodBoolean;
|
|
124
|
+
}, z.core.$strip> | z.ZodObject<{
|
|
125
|
+
items: z.ZodArray<T>;
|
|
126
|
+
nextCursor: z.ZodNullable<z.ZodString>;
|
|
127
|
+
previousCursor: z.ZodNullable<z.ZodString>;
|
|
128
|
+
hasMore: z.ZodBoolean;
|
|
129
|
+
}, z.core.$strip> | z.ZodObject<{
|
|
130
|
+
items: z.ZodArray<T>;
|
|
131
|
+
total: z.ZodNumber;
|
|
132
|
+
hasMore: z.ZodBoolean;
|
|
133
|
+
}, z.core.$strip>;
|
|
80
134
|
//#endregion
|
|
81
135
|
//#region src/schemas/sort.schemas.d.ts
|
|
82
136
|
declare const SortOrderSchema: z.ZodEnum<{
|
|
@@ -142,4 +196,4 @@ declare const MultiSortParamsSchema: z.ZodObject<{
|
|
|
142
196
|
}, z.core.$strip>>>;
|
|
143
197
|
}, z.core.$strip>;
|
|
144
198
|
//#endregion
|
|
145
|
-
export {
|
|
199
|
+
export { createPaginationSchema as A, CursorPaginationParamsSchema as C, PaginationDefaultsSchema as D, PaginationDefaults as E, PaginationParams as O, CursorPaginationParams as S, OffsetPaginationParamsSchema as T, createCursorPaginatedResultSchema as _, SortOrder as a, createResultSchema as b, SortParamsSchema as c, createSortParamsSchema as d, CompactPaginatedResult as f, createCompactPaginatedResultSchema as g, PaginatedResult as h, SortItemSchema as i, PaginationParamsSchema as k, createMultiSortParamsSchema as l, OffsetPaginatedResult as m, MultiSortParamsSchema as n, SortOrderSchema as o, CursorPaginatedResult as p, SortItem as r, SortParams as s, MultiSortParams as t, createSortItemSchema as u, createOffsetPaginatedResultSchema as v, OffsetPaginationParams as w, PAGINATION_DEFAULTS as x, createPaginatedResultSchema as y };
|
|
@@ -23,6 +23,27 @@ declare const CursorPaginationParamsSchema: z.ZodObject<{
|
|
|
23
23
|
limit: z.ZodOptional<z.ZodNumber>;
|
|
24
24
|
}, z.core.$strip>;
|
|
25
25
|
type CursorPaginationParams = z.infer<typeof CursorPaginationParamsSchema>;
|
|
26
|
+
type CreatePaginationSchemaOptions = {
|
|
27
|
+
mode: 'page' | 'offset' | 'cursor';
|
|
28
|
+
pageSizeField?: string;
|
|
29
|
+
cursorField?: string;
|
|
30
|
+
fields?: {
|
|
31
|
+
page?: boolean;
|
|
32
|
+
pageSize?: boolean;
|
|
33
|
+
limit?: boolean;
|
|
34
|
+
offset?: boolean;
|
|
35
|
+
cursor?: boolean;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
declare function createPaginationSchema(options: CreatePaginationSchemaOptions): z.ZodObject<{
|
|
39
|
+
page: z.ZodOptional<z.ZodNumber>;
|
|
40
|
+
} | {
|
|
41
|
+
page?: undefined;
|
|
42
|
+
}, z.core.$strip> | z.ZodObject<{
|
|
43
|
+
limit: z.ZodOptional<z.ZodNumber>;
|
|
44
|
+
} | {
|
|
45
|
+
limit?: undefined;
|
|
46
|
+
}, z.core.$strip>;
|
|
26
47
|
//#endregion
|
|
27
48
|
//#region src/types/pagination.types.d.ts
|
|
28
49
|
declare const PAGINATION_DEFAULTS: {
|
|
@@ -77,6 +98,39 @@ type CursorPaginatedResult<T> = {
|
|
|
77
98
|
previousCursor: string | null;
|
|
78
99
|
hasMore: boolean;
|
|
79
100
|
};
|
|
101
|
+
type CompactPaginatedResult<T> = {
|
|
102
|
+
items: T[];
|
|
103
|
+
total: number;
|
|
104
|
+
hasMore: boolean;
|
|
105
|
+
};
|
|
106
|
+
declare function createCompactPaginatedResultSchema<T extends z.ZodTypeAny>(itemSchema: T): z.ZodObject<{
|
|
107
|
+
items: z.ZodArray<T>;
|
|
108
|
+
total: z.ZodNumber;
|
|
109
|
+
hasMore: z.ZodBoolean;
|
|
110
|
+
}, z.core.$strip>;
|
|
111
|
+
type CreateResultSchemaOptions = {
|
|
112
|
+
mode: 'page' | 'offset' | 'cursor' | 'compact';
|
|
113
|
+
pageSizeField?: string;
|
|
114
|
+
metadata?: ('total' | 'totalPages' | 'hasNextPage' | 'hasPreviousPage' | 'hasMore')[];
|
|
115
|
+
};
|
|
116
|
+
declare function createResultSchema<T extends z.ZodTypeAny>(itemSchema: T, options: CreateResultSchemaOptions): z.ZodObject<{
|
|
117
|
+
[x: string]: z.ZodNumber | z.ZodBoolean | z.ZodArray<T>;
|
|
118
|
+
items: z.ZodArray<T>;
|
|
119
|
+
total: z.ZodNumber;
|
|
120
|
+
page: z.ZodNumber;
|
|
121
|
+
totalPages: z.ZodNumber;
|
|
122
|
+
hasNextPage: z.ZodBoolean;
|
|
123
|
+
hasPreviousPage: z.ZodBoolean;
|
|
124
|
+
}, z.core.$strip> | z.ZodObject<{
|
|
125
|
+
items: z.ZodArray<T>;
|
|
126
|
+
nextCursor: z.ZodNullable<z.ZodString>;
|
|
127
|
+
previousCursor: z.ZodNullable<z.ZodString>;
|
|
128
|
+
hasMore: z.ZodBoolean;
|
|
129
|
+
}, z.core.$strip> | z.ZodObject<{
|
|
130
|
+
items: z.ZodArray<T>;
|
|
131
|
+
total: z.ZodNumber;
|
|
132
|
+
hasMore: z.ZodBoolean;
|
|
133
|
+
}, z.core.$strip>;
|
|
80
134
|
//#endregion
|
|
81
135
|
//#region src/schemas/sort.schemas.d.ts
|
|
82
136
|
declare const SortOrderSchema: z.ZodEnum<{
|
|
@@ -142,4 +196,4 @@ declare const MultiSortParamsSchema: z.ZodObject<{
|
|
|
142
196
|
}, z.core.$strip>>>;
|
|
143
197
|
}, z.core.$strip>;
|
|
144
198
|
//#endregion
|
|
145
|
-
export {
|
|
199
|
+
export { createPaginationSchema as A, CursorPaginationParamsSchema as C, PaginationDefaultsSchema as D, PaginationDefaults as E, PaginationParams as O, CursorPaginationParams as S, OffsetPaginationParamsSchema as T, createCursorPaginatedResultSchema as _, SortOrder as a, createResultSchema as b, SortParamsSchema as c, createSortParamsSchema as d, CompactPaginatedResult as f, createCompactPaginatedResultSchema as g, PaginatedResult as h, SortItemSchema as i, PaginationParamsSchema as k, createMultiSortParamsSchema as l, OffsetPaginatedResult as m, MultiSortParamsSchema as n, SortOrderSchema as o, CursorPaginatedResult as p, SortItem as r, SortParams as s, MultiSortParams as t, createSortItemSchema as u, createOffsetPaginatedResultSchema as v, OffsetPaginationParams as w, PAGINATION_DEFAULTS as x, createPaginatedResultSchema as y };
|
package/dist/index.cjs
CHANGED
|
@@ -1,33 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,
|
|
2
|
-
const require_utils = require('./utils-DazsDMMu.cjs');
|
|
3
|
-
|
|
4
|
-
exports.CursorPaginationParamsSchema = require_utils.CursorPaginationParamsSchema;
|
|
5
|
-
exports.MultiSortParamsSchema = require_utils.MultiSortParamsSchema;
|
|
6
|
-
exports.OffsetPaginationParamsSchema = require_utils.OffsetPaginationParamsSchema;
|
|
7
|
-
exports.PAGINATION_DEFAULTS = require_utils.PAGINATION_DEFAULTS;
|
|
8
|
-
exports.PaginationDefaultsSchema = require_utils.PaginationDefaultsSchema;
|
|
9
|
-
exports.PaginationParamsSchema = require_utils.PaginationParamsSchema;
|
|
10
|
-
exports.SortItemSchema = require_utils.SortItemSchema;
|
|
11
|
-
exports.SortOrderSchema = require_utils.SortOrderSchema;
|
|
12
|
-
exports.SortParamsSchema = require_utils.SortParamsSchema;
|
|
13
|
-
exports.calculateOffset = require_utils.calculateOffset;
|
|
14
|
-
exports.calculateTotalPages = require_utils.calculateTotalPages;
|
|
15
|
-
exports.createCursorPaginatedResultSchema = require_utils.createCursorPaginatedResultSchema;
|
|
16
|
-
exports.createMultiSortParamsSchema = require_utils.createMultiSortParamsSchema;
|
|
17
|
-
exports.createOffsetPaginatedResultSchema = require_utils.createOffsetPaginatedResultSchema;
|
|
18
|
-
exports.createPaginatedResult = require_utils.createPaginatedResult;
|
|
19
|
-
exports.createPaginatedResultSchema = require_utils.createPaginatedResultSchema;
|
|
20
|
-
exports.createSortItemSchema = require_utils.createSortItemSchema;
|
|
21
|
-
exports.createSortParamsSchema = require_utils.createSortParamsSchema;
|
|
22
|
-
exports.hasNextPage = require_utils.hasNextPage;
|
|
23
|
-
exports.hasPreviousPage = require_utils.hasPreviousPage;
|
|
24
|
-
exports.isValidLimit = require_utils.isValidLimit;
|
|
25
|
-
exports.isValidOffset = require_utils.isValidOffset;
|
|
26
|
-
exports.isValidOffsetPagination = require_utils.isValidOffsetPagination;
|
|
27
|
-
exports.isValidPage = require_utils.isValidPage;
|
|
28
|
-
exports.isValidPageSize = require_utils.isValidPageSize;
|
|
29
|
-
exports.isValidPagination = require_utils.isValidPagination;
|
|
30
|
-
exports.normalizeOffsetPagination = require_utils.normalizeOffsetPagination;
|
|
31
|
-
exports.normalizePagination = require_utils.normalizePagination;
|
|
32
|
-
exports.offsetToPage = require_utils.offsetToPage;
|
|
33
|
-
exports.pageToOffset = require_utils.pageToOffset;
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require(`./utils-B200OLT-.cjs`);let t=require(`zod`);function n(e){if(e===void 0)return;let t=Number(e);return Number.isNaN(t)?void 0:t}function r(r){let i=r?.pageSizeField??`pageSize`,a=e.u({pageSize:r?.defaultPageSize,maxPageSize:r?.maxPageSize}),o=e.P({mode:`page`,pageSizeField:i}),s=r?.sortFields?e.C([...r.sortFields]):t.z.object({}),c=o.merge(s).extend(r?.businessFields??{});return{querySchema:c,resultSchema:e.O(t.z.unknown(),{mode:`page`,pageSizeField:i}),defaults:a,normalize(e){return a.normalize({page:e?.page,pageSize:e?.[i]})},parseQuery(e){let t={};if(e.page!==void 0&&(t.page=n(e.page)),e[i]!==void 0&&(t[i]=n(e[i])),e.sortBy!==void 0&&(t.sortBy=e.sortBy),e.sortOrder!==void 0&&(t.sortOrder=e.sortOrder),r?.businessFields)for(let n of Object.keys(r.businessFields))e[n]!==void 0&&(t[n]=e[n]);return c.parse(t)},buildResult(t,n,r,i){return e.l(t,n,r,i)}}}function i(e){if(e===void 0)return;let t=Number(e);return Number.isNaN(t)?void 0:t}function a(n){let r=n?.cursorField??`cursor`,a=e.P({mode:`cursor`,cursorField:r}).extend(n?.businessFields??{});return{querySchema:a,resultSchema:e.O(t.z.unknown(),{mode:`cursor`}),parseQuery(e){let t={};if(e[r]!==void 0&&(t[r]=e[r]),e.limit!==void 0&&(t.limit=i(e.limit)),n?.businessFields)for(let r of Object.keys(n.businessFields))e[r]!==void 0&&(t[r]=e[r]);return a.parse(t)},buildResult(e,t,n){return{items:e,nextCursor:t,previousCursor:null,hasMore:n}}}}function o(e){if(e===void 0)return;let t=Number(e);return Number.isNaN(t)?void 0:t}function s(n){let r=e.P({mode:`offset`,fields:{limit:!0,offset:!1}}).extend(n?.businessFields??{});return{querySchema:r,resultSchema:e.O(t.z.unknown(),{mode:`compact`}),parseQuery(e){let t={};if(e.limit!==void 0&&(t.limit=o(e.limit)),n?.businessFields)for(let r of Object.keys(n.businessFields))e[r]!==void 0&&(t[r]=e[r]);return r.parse(t)},buildResult(e,t,n){return{items:e,total:t,hasMore:n}}}}function c(e){if(e===void 0)return;let t=Number(e);return Number.isNaN(t)?void 0:t}function l(n){let r=n?.mode??`page`,i=n?.pageSizeField??`pageSize`,a=e.P({mode:r,pageSizeField:i}).extend(n?.businessFields??{});return{querySchema:a,resultSchema:e.O(t.z.unknown(),{mode:`compact`}),parseQuery(e){let t={};if(r===`page`?(e.page!==void 0&&(t.page=c(e.page)),e[i]!==void 0&&(t[i]=c(e[i]))):(e.limit!==void 0&&(t.limit=c(e.limit)),e.offset!==void 0&&(t.offset=c(e.offset))),n?.businessFields)for(let r of Object.keys(n.businessFields))e[r]!==void 0&&(t[r]=e[r]);return a.parse(t)},buildResult(e,t,n){return{items:e,total:t,hasMore:n}}}}exports.CursorPaginationParamsSchema=e.A,exports.MultiSortParamsSchema=e._,exports.OffsetPaginationParamsSchema=e.j,exports.PAGINATION_DEFAULTS=e.k,exports.PaginationDefaultsSchema=e.M,exports.PaginationParamsSchema=e.N,exports.SortItemSchema=e.v,exports.SortOrderSchema=e.y,exports.SortParamsSchema=e.b,exports.calculateOffset=e.s,exports.calculateTotalPages=e.c,exports.createCompactListPreset=l,exports.createCompactPaginatedResultSchema=e.w,exports.createCursorPaginatedResultSchema=e.T,exports.createInfiniteScrollPreset=a,exports.createMultiSortParamsSchema=e.x,exports.createOffsetPaginatedResultSchema=e.E,exports.createPaginatedResult=e.l,exports.createPaginatedResultSchema=e.D,exports.createPaginationDefaults=e.u,exports.createPaginationSchema=e.P,exports.createResultSchema=e.O,exports.createSortItemSchema=e.S,exports.createSortParamsSchema=e.C,exports.createTablePreset=r,exports.createTimelinePreset=s,exports.hasNextPage=e.d,exports.hasPreviousPage=e.f,exports.isValidLimit=e.t,exports.isValidOffset=e.n,exports.isValidOffsetPagination=e.r,exports.isValidPage=e.i,exports.isValidPageSize=e.a,exports.isValidPagination=e.o,exports.normalizeOffsetPagination=e.p,exports.normalizePagination=e.m,exports.offsetToPage=e.h,exports.pageToOffset=e.g;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { C as
|
|
1
|
+
import { A as createPaginationSchema, C as CursorPaginationParamsSchema, D as PaginationDefaultsSchema, E as PaginationDefaults, O as PaginationParams, S as CursorPaginationParams, T as OffsetPaginationParamsSchema, _ as createCursorPaginatedResultSchema, a as SortOrder, b as createResultSchema, c as SortParamsSchema, d as createSortParamsSchema, f as CompactPaginatedResult, g as createCompactPaginatedResultSchema, h as PaginatedResult, i as SortItemSchema, k as PaginationParamsSchema, l as createMultiSortParamsSchema, m as OffsetPaginatedResult, n as MultiSortParamsSchema, o as SortOrderSchema, p as CursorPaginatedResult, r as SortItem, s as SortParams, t as MultiSortParams, u as createSortItemSchema, v as createOffsetPaginatedResultSchema, w as OffsetPaginationParams, x as PAGINATION_DEFAULTS, y as createPaginatedResultSchema } from "./index-DpbbjAwO.cjs";
|
|
2
|
+
import * as _$zod from "zod";
|
|
3
|
+
import { z } from "zod";
|
|
2
4
|
|
|
3
5
|
//#region src/utils/pagination.util.d.ts
|
|
4
6
|
type OffsetPaginationResult = {
|
|
@@ -18,6 +20,20 @@ declare function hasPreviousPage(page: number): boolean;
|
|
|
18
20
|
declare function offsetToPage(offset: number, limit: number): OffsetPaginationResult;
|
|
19
21
|
declare function pageToOffset(page: number, pageSize: number): PagePaginationResult;
|
|
20
22
|
declare function createPaginatedResult<T>(items: T[], total: number, page: number, pageSize: number): PaginatedResult<T>;
|
|
23
|
+
type PaginationDefaultsOverrides = {
|
|
24
|
+
page?: number;
|
|
25
|
+
pageSize?: number;
|
|
26
|
+
maxPageSize?: number;
|
|
27
|
+
minPageSize?: number;
|
|
28
|
+
};
|
|
29
|
+
declare function createPaginationDefaults(overrides?: PaginationDefaultsOverrides): {
|
|
30
|
+
normalize(params?: PaginationParams): Required<PaginationParams>;
|
|
31
|
+
normalizeOffset(params?: OffsetPaginationParams): Required<OffsetPaginationParams>;
|
|
32
|
+
page: number;
|
|
33
|
+
pageSize: number;
|
|
34
|
+
maxPageSize: number;
|
|
35
|
+
minPageSize: number;
|
|
36
|
+
};
|
|
21
37
|
//#endregion
|
|
22
38
|
//#region src/utils/validation.util.d.ts
|
|
23
39
|
type PaginationValidationResult = {
|
|
@@ -35,4 +51,66 @@ declare function isValidLimit(limit: unknown): limit is number;
|
|
|
35
51
|
declare function isValidPagination(params: unknown): params is PaginationValidationResult;
|
|
36
52
|
declare function isValidOffsetPagination(params: unknown): params is OffsetPaginationValidationResult;
|
|
37
53
|
//#endregion
|
|
38
|
-
|
|
54
|
+
//#region src/presets/types.d.ts
|
|
55
|
+
type PaginationPreset<Q, R> = {
|
|
56
|
+
querySchema: _$zod.ZodTypeAny;
|
|
57
|
+
resultSchema: _$zod.ZodTypeAny;
|
|
58
|
+
parseQuery(query: Record<string, string | undefined>): Q;
|
|
59
|
+
buildResult(...args: unknown[]): R;
|
|
60
|
+
};
|
|
61
|
+
type TablePresetConfig = {
|
|
62
|
+
pageSizeField?: 'pageSize' | 'limit';
|
|
63
|
+
defaultPageSize?: number;
|
|
64
|
+
maxPageSize?: number;
|
|
65
|
+
sortFields?: readonly [string, ...string[]];
|
|
66
|
+
businessFields?: Record<string, _$zod.ZodTypeAny>;
|
|
67
|
+
};
|
|
68
|
+
type TablePreset<Q = Record<string, unknown>> = PaginationPreset<Q, PaginatedResult<unknown>> & {
|
|
69
|
+
defaults: ReturnType<typeof createPaginationDefaults>;
|
|
70
|
+
normalize(params?: Record<string, unknown>): {
|
|
71
|
+
page: number;
|
|
72
|
+
pageSize: number;
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
type InfiniteScrollPresetConfig = {
|
|
76
|
+
cursorField?: string;
|
|
77
|
+
defaultPageSize?: number;
|
|
78
|
+
businessFields?: Record<string, _$zod.ZodTypeAny>;
|
|
79
|
+
};
|
|
80
|
+
type InfiniteScrollPreset<Q = Record<string, unknown>> = PaginationPreset<Q, CursorPaginatedResult<unknown>>;
|
|
81
|
+
type TimelinePresetConfig = {
|
|
82
|
+
defaultLimit?: number;
|
|
83
|
+
maxLimit?: number;
|
|
84
|
+
businessFields?: Record<string, _$zod.ZodTypeAny>;
|
|
85
|
+
};
|
|
86
|
+
type TimelinePreset<Q = Record<string, unknown>> = PaginationPreset<Q, CompactPaginatedResult<unknown>>;
|
|
87
|
+
type CompactListPresetConfig = {
|
|
88
|
+
mode?: 'page' | 'offset';
|
|
89
|
+
pageSizeField?: string;
|
|
90
|
+
defaultPageSize?: number;
|
|
91
|
+
businessFields?: Record<string, _$zod.ZodTypeAny>;
|
|
92
|
+
};
|
|
93
|
+
type CompactListPreset<Q = Record<string, unknown>> = PaginationPreset<Q, CompactPaginatedResult<unknown>>;
|
|
94
|
+
//#endregion
|
|
95
|
+
//#region src/presets/table.preset.d.ts
|
|
96
|
+
declare function createTablePreset<TSort extends readonly [string, ...string[]], TBusiness extends Record<string, z.ZodTypeAny>>(config?: TablePresetConfig & {
|
|
97
|
+
sortFields?: TSort;
|
|
98
|
+
businessFields?: TBusiness;
|
|
99
|
+
}): TablePreset;
|
|
100
|
+
//#endregion
|
|
101
|
+
//#region src/presets/infinite-scroll.preset.d.ts
|
|
102
|
+
declare function createInfiniteScrollPreset<TBusiness extends Record<string, z.ZodTypeAny>>(config?: InfiniteScrollPresetConfig & {
|
|
103
|
+
businessFields?: TBusiness;
|
|
104
|
+
}): InfiniteScrollPreset;
|
|
105
|
+
//#endregion
|
|
106
|
+
//#region src/presets/timeline.preset.d.ts
|
|
107
|
+
declare function createTimelinePreset<TBusiness extends Record<string, z.ZodTypeAny>>(config?: TimelinePresetConfig & {
|
|
108
|
+
businessFields?: TBusiness;
|
|
109
|
+
}): TimelinePreset;
|
|
110
|
+
//#endregion
|
|
111
|
+
//#region src/presets/compact-list.preset.d.ts
|
|
112
|
+
declare function createCompactListPreset<TBusiness extends Record<string, z.ZodTypeAny>>(config?: CompactListPresetConfig & {
|
|
113
|
+
businessFields?: TBusiness;
|
|
114
|
+
}): CompactListPreset;
|
|
115
|
+
//#endregion
|
|
116
|
+
export { type CompactListPreset, type CompactListPresetConfig, type CompactPaginatedResult, type CursorPaginatedResult, type CursorPaginationParams, CursorPaginationParamsSchema, type InfiniteScrollPreset, type InfiniteScrollPresetConfig, type MultiSortParams, MultiSortParamsSchema, type OffsetPaginatedResult, type OffsetPaginationParams, OffsetPaginationParamsSchema, type OffsetPaginationResult, type OffsetPaginationValidationResult, PAGINATION_DEFAULTS, type PagePaginationResult, type PaginatedResult, type PaginationDefaults, PaginationDefaultsSchema, type PaginationParams, PaginationParamsSchema, type PaginationPreset, type PaginationValidationResult, type SortItem, SortItemSchema, type SortOrder, SortOrderSchema, type SortParams, SortParamsSchema, type TablePreset, type TablePresetConfig, type TimelinePreset, type TimelinePresetConfig, calculateOffset, calculateTotalPages, createCompactListPreset, createCompactPaginatedResultSchema, createCursorPaginatedResultSchema, createInfiniteScrollPreset, createMultiSortParamsSchema, createOffsetPaginatedResultSchema, createPaginatedResult, createPaginatedResultSchema, createPaginationDefaults, createPaginationSchema, createResultSchema, createSortItemSchema, createSortParamsSchema, createTablePreset, createTimelinePreset, hasNextPage, hasPreviousPage, isValidLimit, isValidOffset, isValidOffsetPagination, isValidPage, isValidPageSize, isValidPagination, normalizeOffsetPagination, normalizePagination, offsetToPage, pageToOffset };
|