@calmlens/js-sdk 0.0.1 → 0.0.2

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.
Files changed (68) hide show
  1. package/cjs/ApiKey.d.ts +31 -0
  2. package/cjs/ApiKey.js +59 -0
  3. package/cjs/Asset.d.ts +83 -3
  4. package/cjs/Asset.js +198 -28
  5. package/cjs/Auth.d.ts +95 -0
  6. package/cjs/Auth.js +2 -0
  7. package/cjs/CalmLensClient.d.ts +28 -11
  8. package/cjs/CalmLensClient.js +138 -77
  9. package/cjs/CalmLensTypes.d.ts +2 -0
  10. package/cjs/Classification.js +40 -7
  11. package/cjs/DocMetaTypes.d.ts +7 -0
  12. package/cjs/DocMetaTypes.js +7 -0
  13. package/cjs/Page.d.ts +42 -0
  14. package/cjs/Page.js +94 -0
  15. package/cjs/PublicApiSchemas.d.ts +1593 -0
  16. package/cjs/PublicApiSchemas.js +334 -0
  17. package/cjs/RequestInfo.d.ts +23 -0
  18. package/cjs/RequestInfo.js +2 -0
  19. package/cjs/Roles.d.ts +21 -0
  20. package/cjs/Roles.js +84 -0
  21. package/cjs/SharedConstants.d.ts +134 -0
  22. package/cjs/SharedConstants.js +125 -0
  23. package/cjs/SharedTypes.d.ts +6 -1
  24. package/cjs/User.d.ts +17 -0
  25. package/cjs/User.js +51 -0
  26. package/cjs/UtilTypes.d.ts +30 -0
  27. package/cjs/UtilTypes.js +4 -0
  28. package/cjs/Workflow.d.ts +58 -0
  29. package/cjs/Workflow.js +83 -0
  30. package/cjs/ZodUtils.d.ts +39 -0
  31. package/cjs/ZodUtils.js +328 -0
  32. package/cjs/index.js +4 -1
  33. package/esm/ApiKey.d.ts +31 -0
  34. package/esm/ApiKey.js +23 -0
  35. package/esm/Asset.d.ts +83 -3
  36. package/esm/Asset.js +148 -12
  37. package/esm/Auth.d.ts +95 -0
  38. package/esm/Auth.js +1 -0
  39. package/esm/CalmLensClient.d.ts +28 -11
  40. package/esm/CalmLensClient.js +104 -55
  41. package/esm/CalmLensTypes.d.ts +2 -0
  42. package/esm/Classification.js +1 -1
  43. package/esm/DocMetaTypes.d.ts +7 -0
  44. package/esm/DocMetaTypes.js +4 -0
  45. package/esm/Page.d.ts +42 -0
  46. package/esm/Page.js +55 -0
  47. package/esm/PublicApiSchemas.d.ts +1593 -0
  48. package/esm/PublicApiSchemas.js +298 -0
  49. package/esm/RequestInfo.d.ts +23 -0
  50. package/esm/RequestInfo.js +1 -0
  51. package/esm/Roles.d.ts +21 -0
  52. package/esm/Roles.js +45 -0
  53. package/esm/SharedConstants.d.ts +134 -0
  54. package/esm/SharedConstants.js +122 -0
  55. package/esm/SharedTypes.d.ts +6 -1
  56. package/esm/User.d.ts +17 -0
  57. package/esm/User.js +15 -0
  58. package/esm/UtilTypes.d.ts +30 -0
  59. package/esm/UtilTypes.js +1 -0
  60. package/esm/Workflow.d.ts +58 -0
  61. package/esm/Workflow.js +46 -0
  62. package/esm/ZodUtils.d.ts +39 -0
  63. package/esm/ZodUtils.js +266 -0
  64. package/package.json +10 -10
  65. package/cjs/SchemaUtils.d.ts +0 -11
  66. package/cjs/SchemaUtils.js +0 -63
  67. package/esm/SchemaUtils.d.ts +0 -11
  68. package/esm/SchemaUtils.js +0 -46
package/esm/Asset.js CHANGED
@@ -1,10 +1,76 @@
1
1
  import * as zod from "zod/v4";
2
+ import { metaStore } from "zod-meta";
2
3
  import { CLASSIFICATION_REPORT_SCHEMA } from "./Classification";
3
- import { createdAtField, primaryUuidField, saneStringField, updatedAtField, uuidField, } from "./SchemaUtils";
4
+ import { docPropertyInfo } from "./DocMetaTypes";
5
+ import { WORKFLOW_SCHEMA } from "./Workflow";
6
+ import { createdAtField, primaryUuidField, saneStringField, updatedAtField, uuidField, } from "./ZodUtils";
4
7
  export const ASSET_STATUS_SCHEMA = zod.enum(["pending", "approved", "rejected", "error"]);
5
- export const ASSET_TYPE_SCHEMA = zod.enum(["image", "video", "audio", "document", "website"]);
8
+ export const ASSET_TYPE_SCHEMA = zod.enum([
9
+ "image",
10
+ "video",
11
+ "audio",
12
+ "plaintext-document",
13
+ "multimedia-document",
14
+ "website",
15
+ "file",
16
+ ]);
17
+ /**
18
+ * MIME types that are considered multimedia documents
19
+ * (documents that contain both text and embedded images/media)
20
+ */
21
+ export const MULTIMEDIA_DOCUMENT_MIME_TYPES = [
22
+ "application/pdf",
23
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document", // DOCX
24
+ "application/msword", // DOC
25
+ "application/rtf", // RTF
26
+ "application/vnd.oasis.opendocument.text", // ODT
27
+ ];
28
+ /**
29
+ * Checks if a MIME type is a multimedia document
30
+ */
31
+ export const isMultimediaDocumentMimeType = (mimeType) => {
32
+ return MULTIMEDIA_DOCUMENT_MIME_TYPES.includes(mimeType);
33
+ };
6
34
  export const ASSET_VISIBILITY_SCHEMA = zod.enum(["public", "private"]);
7
- export const ASSET_SCHEMA = zod.object({
35
+ export const CHECKSUMS_SCHEMA = zod.object({
36
+ md5: zod.string().nullish(),
37
+ sha1: zod.string().nullish(),
38
+ sha256: zod.string().nullish(),
39
+ sha384: zod.string().nullish(),
40
+ sha512: zod.string().nullish(),
41
+ });
42
+ export const IMAGE_DATA_SCHEMA = zod.object({
43
+ perceptualHash: zod.string().nullish(),
44
+ });
45
+ export const DOCUMENT_DATA_SCHEMA = zod.object({});
46
+ export const PROCESSING_OPTIONS_SCHEMA = zod
47
+ .object({
48
+ keepAfterProcessing: zod
49
+ .boolean()
50
+ .nullish()
51
+ .meta(metaStore([
52
+ docPropertyInfo({
53
+ description: "Whether to keep the asset after processing",
54
+ defaultValue: false,
55
+ }),
56
+ ])),
57
+ image: zod
58
+ .object({
59
+ ocrEnabled: zod
60
+ .boolean()
61
+ .nullish()
62
+ .meta(metaStore([
63
+ docPropertyInfo({
64
+ description: "Whether to perform OCR on image assets or images extracted from multimedia documents and websites",
65
+ defaultValue: false,
66
+ }),
67
+ ])),
68
+ })
69
+ .nullish(),
70
+ })
71
+ .nullish();
72
+ export const ASSET_SCHEMA = zod
73
+ .object({
8
74
  id: primaryUuidField(),
9
75
  projectId: uuidField(),
10
76
  sizeInBytes: zod.number().nullish(),
@@ -12,25 +78,95 @@ export const ASSET_SCHEMA = zod.object({
12
78
  parentId: uuidField().nullish(),
13
79
  createdAt: createdAtField(),
14
80
  updatedAt: updatedAtField(),
81
+ checksums: CHECKSUMS_SCHEMA.nullish(),
82
+ imageData: IMAGE_DATA_SCHEMA.nullish(),
83
+ documentData: DOCUMENT_DATA_SCHEMA.nullish(),
15
84
  name: saneStringField({
16
85
  type: "large",
17
- }),
86
+ }).meta(metaStore([
87
+ docPropertyInfo({
88
+ description: "Name of the asset",
89
+ placeholder: "my-image.jpg",
90
+ }),
91
+ ])),
18
92
  fileFormat: saneStringField().nullish(),
19
93
  fileExtension: saneStringField().nullish(),
20
94
  description: saneStringField({
21
95
  type: "large",
22
- }).nullish(),
23
- externalId: saneStringField().nullish(),
24
- externalUrl: zod.string().url().nullish(),
96
+ })
97
+ .nullish()
98
+ .meta(metaStore([
99
+ docPropertyInfo({
100
+ description: "Optional description of the asset",
101
+ placeholder: "A beautiful landscape image",
102
+ }),
103
+ ])),
104
+ externalId: saneStringField()
105
+ .nullish()
106
+ .meta(metaStore([
107
+ docPropertyInfo({
108
+ description: "External identifier for the asset",
109
+ placeholder: "ext-12345",
110
+ }),
111
+ ])),
112
+ externalUrl: zod
113
+ .string()
114
+ .url()
115
+ .nullish()
116
+ .meta(metaStore([
117
+ docPropertyInfo({
118
+ description: "External URL associated with the asset",
119
+ placeholder: "https://example.com/asset",
120
+ }),
121
+ ])),
25
122
  previewImageId: saneStringField().nullish(),
26
123
  storageId: saneStringField().nullish(),
27
124
  averageColor: saneStringField().nullish(),
28
125
  type: ASSET_TYPE_SCHEMA,
29
126
  previewHash: saneStringField().nullish(),
30
- metadata: zod.any().nullish(),
31
- tags: zod.array(saneStringField()).nullish(),
127
+ metadata: zod
128
+ .any()
129
+ .nullish()
130
+ .meta(metaStore([
131
+ docPropertyInfo({
132
+ description: "Custom metadata for the asset",
133
+ }),
134
+ ])),
135
+ tags: zod
136
+ .array(saneStringField())
137
+ .nullish()
138
+ .meta(metaStore([
139
+ docPropertyInfo({
140
+ description: "Array of tags for the asset",
141
+ placeholder: '["tag1", "tag2"]',
142
+ }),
143
+ ])),
32
144
  report: CLASSIFICATION_REPORT_SCHEMA.nullish(),
33
145
  status: ASSET_STATUS_SCHEMA,
34
- keepAfterProcessing: zod.boolean().nullish(),
35
- visibility: ASSET_VISIBILITY_SCHEMA.nullish(),
36
- });
146
+ extractedTextId: saneStringField()
147
+ .nullish()
148
+ .meta(metaStore([
149
+ docPropertyInfo({
150
+ description: "ID of the extracted text content stored in R2 (e.g., from OCR or text extraction)",
151
+ }),
152
+ ])),
153
+ processingOptions: PROCESSING_OPTIONS_SCHEMA,
154
+ visibility: ASSET_VISIBILITY_SCHEMA.nullish().meta(metaStore([
155
+ docPropertyInfo({
156
+ description: "Visibility level of the asset",
157
+ defaultValue: "private",
158
+ }),
159
+ ])),
160
+ storageDeletedAt: zod.number().nullish(),
161
+ classificationWorkflow: WORKFLOW_SCHEMA.nullish().meta(metaStore([
162
+ docPropertyInfo({
163
+ description: "Workflow tracking the classification process for this asset",
164
+ }),
165
+ ])),
166
+ })
167
+ .meta(metaStore([
168
+ docPropertyInfo({
169
+ description: "An asset in the system",
170
+ namedType: "Asset",
171
+ }),
172
+ ]));
package/esm/Auth.d.ts ADDED
@@ -0,0 +1,95 @@
1
+ import type { ApiKey } from "./ApiKey";
2
+ import type { Role } from "./Roles";
3
+ import type { User } from "./User";
4
+ export interface UserToken {
5
+ /**
6
+ * Always set to https://securetoken.google.com/GOOGLE_CLOUD_PROJECT
7
+ */
8
+ iss: string;
9
+ /**
10
+ * Always set to GOOGLE_CLOUD_PROJECT
11
+ */
12
+ aud: string;
13
+ /**
14
+ * The user's unique ID
15
+ */
16
+ sub: string;
17
+ /**
18
+ * The token issue time, in seconds since epoch
19
+ */
20
+ iat: number;
21
+ /**
22
+ * The token expiry time, normally 'iat' + 3600
23
+ */
24
+ exp: number;
25
+ /**
26
+ * The user's unique ID. Must be equal to 'sub'
27
+ */
28
+ user_id: string;
29
+ /**
30
+ * The time the user authenticated, normally 'iat'
31
+ */
32
+ auth_time: number;
33
+ /**
34
+ * The sign in provider, only set when the provider is 'anonymous'
35
+ */
36
+ provider_id?: "anonymous";
37
+ /**
38
+ * The user's primary email
39
+ */
40
+ email?: string;
41
+ /**
42
+ * The user's email verification status
43
+ */
44
+ email_verified?: boolean;
45
+ /**
46
+ * The user's primary phone number
47
+ */
48
+ phone_number?: string;
49
+ /**
50
+ * The user's display name
51
+ */
52
+ name?: string;
53
+ /**
54
+ * The user's profile photo URL
55
+ */
56
+ picture?: string;
57
+ /**
58
+ * Information on all identities linked to this user
59
+ */
60
+ firebase: any;
61
+ /**
62
+ * Custom claims set by the developer
63
+ */
64
+ [claim: string]: unknown;
65
+ /**
66
+ * @deprecated use `sub` instead
67
+ */
68
+ uid?: never;
69
+ }
70
+ export interface BaseAuth {
71
+ type: "apiKey" | "user";
72
+ roles: Role[];
73
+ }
74
+ export interface ApiKeyAuth extends BaseAuth {
75
+ type: "apiKey";
76
+ apiKey: ApiKey;
77
+ userId: string | undefined;
78
+ originalUserId: string | undefined;
79
+ getUser: () => Promise<User | undefined>;
80
+ getOriginalUser: () => Promise<User | undefined>;
81
+ }
82
+ export interface UserAuth extends BaseAuth {
83
+ type: "user";
84
+ token: Token;
85
+ idTokenExpiresAt: number;
86
+ provider: string;
87
+ userId: string;
88
+ originalUserId: string;
89
+ getUser: () => Promise<User>;
90
+ getOriginalUser: () => Promise<User>;
91
+ }
92
+ export type Auth = ApiKeyAuth | UserAuth;
93
+ export interface Token extends UserToken {
94
+ roles: Role[];
95
+ }
package/esm/Auth.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -1,4 +1,5 @@
1
1
  import type { Asset } from "./Asset";
2
+ import type { GetAssetChildrenQuery, GetAssetChildrenResponse, GetAssetResponse, GetAssetsPageQuery, GetAssetsPageResponse } from "./PublicApiSchemas";
2
3
  import type { SubmitAssetOptions } from "./SharedTypes";
3
4
  import type { CalmLensClientOptions } from "./CalmLensTypes";
4
5
  export default class CalmLensClient {
@@ -27,15 +28,31 @@ export default class CalmLensClient {
27
28
  */
28
29
  static verifyWebhookSignature(payload: string | object, signatureHex: string, secret: string): Promise<boolean>;
29
30
  constructor(options: CalmLensClientOptions);
30
- submitAsset(options: SubmitAssetOptions): Promise<Asset>;
31
- getAsset(assetId: string): Promise<Asset>;
32
- listAssets(options?: {
33
- pageIndex?: number;
34
- pageSize?: number;
35
- }): Promise<{
36
- items: Asset[];
37
- total: number;
38
- pageIndex: number;
39
- pageSize: number;
40
- }>;
31
+ get assets(): {
32
+ /**
33
+ * List all assets for a project
34
+ * @param options - Pagination and filtering options
35
+ * @returns Promise with paginated assets
36
+ */
37
+ list: (options: GetAssetsPageQuery) => Promise<GetAssetsPageResponse>;
38
+ /**
39
+ * Retrieve a specific asset by ID
40
+ * @param assetId - The asset ID
41
+ * @returns Promise with asset data
42
+ */
43
+ retrieve: (assetId: string) => Promise<GetAssetResponse>;
44
+ /**
45
+ * Get child assets of a parent asset
46
+ * @param assetId - The parent asset ID
47
+ * @param options - Pagination options
48
+ * @returns Promise with paginated child assets
49
+ */
50
+ listChildren: (assetId: string, options: GetAssetChildrenQuery) => Promise<GetAssetChildrenResponse>;
51
+ /**
52
+ * Submit a new asset for processing
53
+ * @param options - Asset submission options
54
+ * @returns Promise with created asset
55
+ */
56
+ create: (options: SubmitAssetOptions) => Promise<Asset>;
57
+ };
41
58
  }
@@ -8,6 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { Api } from "api-def";
11
+ import { DOMAIN } from "./SharedConstants";
11
12
  import { verifyWebhookSignature } from "./CalmLensUtils";
12
13
  const createApi = (options) => {
13
14
  const api = new Api({
@@ -22,40 +23,56 @@ const createApi = (options) => {
22
23
  },
23
24
  },
24
25
  ],
26
+ requestBackend: options.requestBackend,
25
27
  });
26
28
  return api;
27
29
  };
28
- // API endpoint definitions
30
+ // API endpoint definitions using schemas like CalmLensApi.ts
29
31
  const createEndpoints = (api) => {
30
- const postUploadAsset = api
32
+ const getAssetsPage = api
31
33
  .endpoint()
32
- .bodyOf()
34
+ .queryOf()
33
35
  .paramsOf()
34
36
  .responseOf()
35
37
  .build({
36
- id: "postUploadAsset",
37
- method: "post",
38
- path: "/projects/:projectId/assets",
38
+ id: "getAssetsPage",
39
+ method: "get",
40
+ path: "/projects/:projectId/assets/page",
39
41
  });
40
- const getAsset = api.endpoint().paramsOf().responseOf().build({
42
+ const getAsset = api
43
+ .endpoint()
44
+ .paramsOf()
45
+ .responseOf()
46
+ .build({
41
47
  id: "getAsset",
42
48
  method: "get",
43
49
  path: "/projects/:projectId/assets/:assetId",
44
50
  });
45
- const getAssetsPage = api
51
+ const getAssetChildren = api
46
52
  .endpoint()
47
- .queryOf()
48
53
  .paramsOf()
54
+ .queryOf()
49
55
  .responseOf()
50
56
  .build({
51
- id: "getAssetsPage",
57
+ id: "getAssetChildren",
52
58
  method: "get",
53
- path: "/projects/:projectId/assets/page",
59
+ path: "/projects/:projectId/assets/:assetId/children",
60
+ });
61
+ const postUploadAsset = api
62
+ .endpoint()
63
+ .bodyOf()
64
+ .paramsOf()
65
+ .responseOf()
66
+ .build({
67
+ id: "postUploadAsset",
68
+ method: "post",
69
+ path: "/projects/:projectId/assets",
54
70
  });
55
71
  return {
56
- postUploadAsset,
57
- getAsset,
58
72
  getAssetsPage,
73
+ getAsset,
74
+ getAssetChildren,
75
+ postUploadAsset,
59
76
  };
60
77
  };
61
78
  export default class CalmLensClient {
@@ -93,50 +110,82 @@ export default class CalmLensClient {
93
110
  if (!options.projectId) {
94
111
  throw new Error("Project ID is required");
95
112
  }
96
- this.options.baseUrl = (_a = this.options.baseUrl) !== null && _a !== void 0 ? _a : "https://api.calmlens.com";
113
+ this.options.baseUrl = (_a = this.options.baseUrl) !== null && _a !== void 0 ? _a : `https://api.${DOMAIN}/v1`;
97
114
  this.api = createApi(this.options);
98
115
  this.endpoints = createEndpoints(this.api);
99
116
  }
100
- submitAsset(options) {
101
- return __awaiter(this, void 0, void 0, function* () {
102
- if (!("file" in options) && !("url" in options)) {
103
- throw new Error("Either file content or URL is required");
104
- }
105
- if (!options.name) {
106
- throw new Error("Asset name is required");
107
- }
108
- const result = yield this.endpoints.postUploadAsset.submit({
109
- body: options,
110
- params: {
111
- projectId: this.options.projectId,
112
- },
113
- });
114
- return result.data;
115
- });
116
- }
117
- getAsset(assetId) {
118
- return __awaiter(this, void 0, void 0, function* () {
119
- if (!assetId) {
120
- throw new Error("Asset ID is required");
121
- }
122
- const result = yield this.endpoints.getAsset.submit({
123
- params: {
124
- projectId: this.options.projectId,
125
- assetId,
126
- },
127
- });
128
- return result.data;
129
- });
130
- }
131
- listAssets() {
132
- return __awaiter(this, arguments, void 0, function* (options = {}) {
133
- const result = yield this.endpoints.getAssetsPage.submit({
134
- params: {
135
- projectId: this.options.projectId,
136
- },
137
- query: options,
138
- });
139
- return result.data;
140
- });
117
+ get assets() {
118
+ return {
119
+ /**
120
+ * List all assets for a project
121
+ * @param options - Pagination and filtering options
122
+ * @returns Promise with paginated assets
123
+ */
124
+ list: (options) => __awaiter(this, void 0, void 0, function* () {
125
+ const result = yield this.endpoints.getAssetsPage.submit({
126
+ params: {
127
+ projectId: this.options.projectId,
128
+ },
129
+ query: options,
130
+ });
131
+ return result.data;
132
+ }),
133
+ /**
134
+ * Retrieve a specific asset by ID
135
+ * @param assetId - The asset ID
136
+ * @returns Promise with asset data
137
+ */
138
+ retrieve: (assetId) => __awaiter(this, void 0, void 0, function* () {
139
+ if (!assetId) {
140
+ throw new Error("Asset ID is required");
141
+ }
142
+ const result = yield this.endpoints.getAsset.submit({
143
+ params: {
144
+ projectId: this.options.projectId,
145
+ assetId,
146
+ },
147
+ });
148
+ return result.data;
149
+ }),
150
+ /**
151
+ * Get child assets of a parent asset
152
+ * @param assetId - The parent asset ID
153
+ * @param options - Pagination options
154
+ * @returns Promise with paginated child assets
155
+ */
156
+ listChildren: (assetId, options) => __awaiter(this, void 0, void 0, function* () {
157
+ if (!assetId) {
158
+ throw new Error("Asset ID is required");
159
+ }
160
+ const result = yield this.endpoints.getAssetChildren.submit({
161
+ params: {
162
+ projectId: this.options.projectId,
163
+ assetId,
164
+ },
165
+ query: options,
166
+ });
167
+ return result.data;
168
+ }),
169
+ /**
170
+ * Submit a new asset for processing
171
+ * @param options - Asset submission options
172
+ * @returns Promise with created asset
173
+ */
174
+ create: (options) => __awaiter(this, void 0, void 0, function* () {
175
+ if (!("file" in options) && !("url" in options)) {
176
+ throw new Error("Either file content or URL is required");
177
+ }
178
+ if (!options.name) {
179
+ throw new Error("Asset name is required");
180
+ }
181
+ const result = yield this.endpoints.postUploadAsset.submit({
182
+ body: options,
183
+ params: {
184
+ projectId: this.options.projectId,
185
+ },
186
+ });
187
+ return result.data;
188
+ }),
189
+ };
141
190
  }
142
191
  }
@@ -1,5 +1,7 @@
1
+ import type { RequestBackend } from "api-def";
1
2
  export interface CalmLensClientOptions {
2
3
  apiKey: string;
3
4
  projectId: string;
4
5
  baseUrl?: string;
6
+ requestBackend?: RequestBackend;
5
7
  }
@@ -1,5 +1,5 @@
1
1
  import * as zod from "zod/v4";
2
- import { saneStringField } from "./SchemaUtils";
2
+ import { saneStringField } from "./ZodUtils";
3
3
  // OpenAI Content Moderation Categories (Merged subcategories)
4
4
  export const CLASSIFICATION_CATEGORIES = [
5
5
  "sexual",
@@ -0,0 +1,7 @@
1
+ export declare const docPropertyInfo: import("zod-meta").ZodMetaFactory<{
2
+ description: string;
3
+ defaultValue?: string | number | boolean;
4
+ placeholder?: string;
5
+ namedType?: string;
6
+ example?: any;
7
+ }>;
@@ -0,0 +1,4 @@
1
+ import { createMetaType } from "zod-meta";
2
+ export const docPropertyInfo = createMetaType({
3
+ id: "docPropertyInfo",
4
+ });
package/esm/Page.d.ts ADDED
@@ -0,0 +1,42 @@
1
+ import * as zod from "zod/v4";
2
+ export declare const DEFAULT_PAGE_SIZE = 10;
3
+ export declare const ORDER_SCHEMA: zod.ZodObject<{
4
+ field: zod.ZodString;
5
+ direction: zod.ZodEnum<{
6
+ ASC: "ASC";
7
+ DESC: "DESC";
8
+ }>;
9
+ }, zod.z.core.$strip>;
10
+ export type Order = zod.infer<typeof ORDER_SCHEMA>;
11
+ export declare const PAGE_QUERY_SCHEMA: zod.ZodObject<{
12
+ pageIndex: zod.ZodDefault<zod.ZodNumber>;
13
+ pageSize: zod.ZodOptional<zod.ZodNumber>;
14
+ ordering: zod.ZodOptional<zod.ZodArray<zod.ZodObject<{
15
+ field: zod.ZodString;
16
+ direction: zod.ZodEnum<{
17
+ ASC: "ASC";
18
+ DESC: "DESC";
19
+ }>;
20
+ }, zod.z.core.$strip>>>;
21
+ }, zod.z.core.$strip>;
22
+ export type PageQuery = zod.infer<typeof PAGE_QUERY_SCHEMA>;
23
+ export interface Page<T> extends Required<PageQuery> {
24
+ items: T[];
25
+ total: number;
26
+ hasMore: boolean;
27
+ }
28
+ export declare const createPageSchema: <T extends zod.ZodType>(itemSchema: T) => zod.ZodIntersection<zod.ZodObject<{
29
+ pageIndex: zod.ZodNonOptional<zod.ZodDefault<zod.ZodNumber>>;
30
+ pageSize: zod.ZodNonOptional<zod.ZodOptional<zod.ZodNumber>>;
31
+ ordering: zod.ZodNonOptional<zod.ZodOptional<zod.ZodArray<zod.ZodObject<{
32
+ field: zod.ZodString;
33
+ direction: zod.ZodEnum<{
34
+ ASC: "ASC";
35
+ DESC: "DESC";
36
+ }>;
37
+ }, zod.z.core.$strip>>>>;
38
+ }, zod.z.core.$strip>, zod.ZodObject<{
39
+ items: zod.ZodArray<T>;
40
+ total: zod.ZodNumber;
41
+ hasMore: zod.ZodBoolean;
42
+ }, zod.z.core.$strip>>;
package/esm/Page.js ADDED
@@ -0,0 +1,55 @@
1
+ import * as zod from "zod/v4";
2
+ import { metaStore } from "zod-meta";
3
+ import { docPropertyInfo } from "./DocMetaTypes";
4
+ export const DEFAULT_PAGE_SIZE = 10;
5
+ export const ORDER_SCHEMA = zod.object({
6
+ field: zod.string().meta(metaStore([
7
+ docPropertyInfo({
8
+ description: "The field name to order by",
9
+ placeholder: "createdAt",
10
+ }),
11
+ ])),
12
+ direction: zod.enum(["ASC", "DESC"]).meta(metaStore([
13
+ docPropertyInfo({
14
+ description: "The sort direction",
15
+ defaultValue: "ASC",
16
+ }),
17
+ ])),
18
+ });
19
+ export const PAGE_QUERY_SCHEMA = zod.object({
20
+ pageIndex: zod
21
+ .number()
22
+ .min(0)
23
+ .default(0)
24
+ .meta(metaStore([
25
+ docPropertyInfo({
26
+ description: "The zero-based page index",
27
+ defaultValue: "0",
28
+ }),
29
+ ])),
30
+ pageSize: zod
31
+ .number()
32
+ .min(1)
33
+ .max(100)
34
+ .optional()
35
+ .meta(metaStore([
36
+ docPropertyInfo({
37
+ description: "Number of items per page (1-100)",
38
+ placeholder: "10",
39
+ }),
40
+ ])),
41
+ ordering: zod
42
+ .array(ORDER_SCHEMA)
43
+ .optional()
44
+ .meta(metaStore([
45
+ docPropertyInfo({
46
+ description: "Array of field ordering specifications",
47
+ placeholder: '[{"field": "createdAt", "direction": "DESC"}]',
48
+ }),
49
+ ])),
50
+ });
51
+ export const createPageSchema = (itemSchema) => PAGE_QUERY_SCHEMA.required().and(zod.object({
52
+ items: zod.array(itemSchema),
53
+ total: zod.number(),
54
+ hasMore: zod.boolean(),
55
+ }));