zodvex 0.7.9 → 0.7.10
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/dist/cli/index.js +3 -1
- package/dist/cli/index.js.map +1 -1
- package/dist/client/index.js +84 -11
- package/dist/client/index.js.map +1 -1
- package/dist/codegen/index.js +2 -1
- package/dist/codegen/index.js.map +1 -1
- package/dist/core/index.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/internal/boundaryHelpers.d.ts +1 -0
- package/dist/internal/boundaryHelpers.d.ts.map +1 -1
- package/dist/internal/paginationCodec.d.ts +8 -0
- package/dist/internal/paginationCodec.d.ts.map +1 -0
- package/dist/mini/client/index.js +84 -11
- package/dist/mini/client/index.js.map +1 -1
- package/dist/mini/index.js.map +1 -1
- package/dist/mini/react/index.js +74 -3
- package/dist/mini/react/index.js.map +1 -1
- package/dist/mini/server/index.js.map +1 -1
- package/dist/public/cli/init.d.ts.map +1 -1
- package/dist/public/client/index.d.ts +1 -1
- package/dist/public/client/index.d.ts.map +1 -1
- package/dist/public/client/zodvexClient.d.ts +23 -9
- package/dist/public/client/zodvexClient.d.ts.map +1 -1
- package/dist/public/codegen/generate.d.ts.map +1 -1
- package/dist/public/react/hooks.d.ts +11 -2
- package/dist/public/react/hooks.d.ts.map +1 -1
- package/dist/react/index.js +74 -3
- package/dist/react/index.js.map +1 -1
- package/dist/server/index.js.map +1 -1
- package/package.json +7 -4
- package/src/internal/boundaryHelpers.ts +1 -1
- package/src/internal/paginationCodec.ts +60 -0
- package/src/public/cli/init.ts +1 -0
- package/src/public/client/index.ts +1 -1
- package/src/public/client/zodvexClient.ts +70 -21
- package/src/public/codegen/generate.ts +2 -1
- package/src/public/react/hooks.ts +55 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zodvex",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.10",
|
|
4
4
|
"description": "Codec-first Zod v4 integration for Convex -- type-safe validation, encoding, DB wrapping, and codegen.",
|
|
5
5
|
"keywords": ["zod", "convex", "validators", "codec", "mapping", "schema", "validation"],
|
|
6
6
|
"homepage": "https://github.com/panzacoder/zodvex#readme",
|
|
@@ -115,15 +115,18 @@
|
|
|
115
115
|
"ts-morph": "^25.0.0"
|
|
116
116
|
},
|
|
117
117
|
"devDependencies": {
|
|
118
|
-
"zod": "4.5.4",
|
|
119
|
-
"zod-to-mini": "workspace:*",
|
|
120
118
|
"@biomejs/biome": "^2.2.6",
|
|
121
119
|
"@types/bun": "^1.3.0",
|
|
122
120
|
"@types/node": "^24.8.0",
|
|
121
|
+
"@types/react": "19.2.14",
|
|
122
|
+
"@types/react-test-renderer": "19.1.0",
|
|
123
123
|
"ai": "^6.0.6",
|
|
124
|
+
"react-test-renderer": "19.2.4",
|
|
124
125
|
"tsup": "^8.5.0",
|
|
125
126
|
"typescript": "^5.9.3",
|
|
126
|
-
"vitest": "^4.1.0"
|
|
127
|
+
"vitest": "^4.1.0",
|
|
128
|
+
"zod": "4.5.4",
|
|
129
|
+
"zod-to-mini": "workspace:*"
|
|
127
130
|
},
|
|
128
131
|
"engines": {
|
|
129
132
|
"node": ">=20",
|
|
@@ -14,7 +14,7 @@ import { $ZodError, safeParse } from './zod-core'
|
|
|
14
14
|
*/
|
|
15
15
|
const functionNameSymbol = Symbol.for('functionName')
|
|
16
16
|
|
|
17
|
-
function resolveFunctionPath(ref: FunctionReference<any, any, any, any>): string | null {
|
|
17
|
+
export function resolveFunctionPath(ref: FunctionReference<any, any, any, any>): string | null {
|
|
18
18
|
if (typeof ref === 'string') return ref
|
|
19
19
|
const name = (ref as any)[functionNameSymbol]
|
|
20
20
|
if (!name) return null
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { FunctionReference } from 'convex/server'
|
|
2
|
+
import { createBoundaryHelpers, resolveFunctionPath } from './boundaryHelpers'
|
|
3
|
+
import type { AnyRegistry } from './types'
|
|
4
|
+
import { $ZodArray, $ZodObject, clone } from './zod-core'
|
|
5
|
+
|
|
6
|
+
/** Aggregate pagination only exposes items, not the original page envelopes. */
|
|
7
|
+
export function createPaginationCodec(registry: AnyRegistry) {
|
|
8
|
+
const argsRegistry: AnyRegistry = Object.create(null)
|
|
9
|
+
const resultsRegistry: AnyRegistry = Object.create(null)
|
|
10
|
+
const argsCodec = createBoundaryHelpers(argsRegistry, { onDecodeError: 'throw' })
|
|
11
|
+
const resultsCodec = createBoundaryHelpers(resultsRegistry, { onDecodeError: 'throw' })
|
|
12
|
+
|
|
13
|
+
function unsupported(path: string, boundary: string): never {
|
|
14
|
+
throw new Error(
|
|
15
|
+
`[zodvex] Unsupported pagination ${boundary} schema for "${path}". ` +
|
|
16
|
+
'Use an unrefined argument object and an unrefined return object with an unrefined page array. ' +
|
|
17
|
+
'Item schemas may contain codecs and refinements. For whole-page checks or transforms, ' +
|
|
18
|
+
'use query or subscribe with explicit paginationOpts.'
|
|
19
|
+
)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function encodeArgs(ref: FunctionReference<'query'>, args: unknown): any {
|
|
23
|
+
const path = resolveFunctionPath(ref)
|
|
24
|
+
if (path !== null && !Object.hasOwn(argsRegistry, path)) {
|
|
25
|
+
const schema = registry[path]?.args
|
|
26
|
+
if (schema) {
|
|
27
|
+
if (!(schema instanceof $ZodObject) || schema._zod.def.checks?.length) {
|
|
28
|
+
unsupported(path, 'argument')
|
|
29
|
+
}
|
|
30
|
+
const { paginationOpts: _paginationOpts, ...shape } = schema._zod.def.shape
|
|
31
|
+
argsRegistry[path] = { args: clone(schema, { ...schema._zod.def, shape }) }
|
|
32
|
+
} else {
|
|
33
|
+
argsRegistry[path] = {}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return argsCodec.encodeArgs(ref, args)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function decodeResults(ref: FunctionReference<'query'>, results: unknown[]): any[] {
|
|
40
|
+
const path = resolveFunctionPath(ref)
|
|
41
|
+
if (path !== null && !Object.hasOwn(resultsRegistry, path)) {
|
|
42
|
+
const schema = registry[path]?.returns
|
|
43
|
+
if (schema) {
|
|
44
|
+
if (!(schema instanceof $ZodObject) || schema._zod.def.checks?.length) {
|
|
45
|
+
unsupported(path, 'return')
|
|
46
|
+
}
|
|
47
|
+
const page = schema._zod.def.shape.page
|
|
48
|
+
if (!(page instanceof $ZodArray) || page._zod.def.checks?.length) {
|
|
49
|
+
unsupported(path, 'page')
|
|
50
|
+
}
|
|
51
|
+
resultsRegistry[path] = { returns: page }
|
|
52
|
+
} else {
|
|
53
|
+
resultsRegistry[path] = {}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return resultsCodec.decodeResult(ref, results)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return { encodeArgs, decodeResults }
|
|
60
|
+
}
|
package/src/public/cli/init.ts
CHANGED
|
@@ -77,6 +77,7 @@ import { zodvexRegistry } from './api'
|
|
|
77
77
|
|
|
78
78
|
export const useZodQuery = undefined as any
|
|
79
79
|
export const useZodMutation = undefined as any
|
|
80
|
+
export const useZodPaginatedQuery = undefined as any
|
|
80
81
|
export const useQuery_experimental = undefined as any
|
|
81
82
|
export const createClient = undefined as any
|
|
82
83
|
`
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export type { ZodvexClientOptions } from './zodvexClient'
|
|
1
|
+
export type { PaginatedResult, PaginatedSubscription, ZodvexClientOptions } from './zodvexClient'
|
|
2
2
|
export { createZodvexClient, ZodvexClient } from './zodvexClient'
|
|
@@ -1,10 +1,29 @@
|
|
|
1
1
|
import type { AuthTokenFetcher, ConnectionState, MutationOptions } from 'convex/browser'
|
|
2
2
|
import { ConvexClient } from 'convex/browser'
|
|
3
|
-
import type {
|
|
3
|
+
import type {
|
|
4
|
+
FunctionArgs,
|
|
5
|
+
FunctionReference,
|
|
6
|
+
FunctionReturnType,
|
|
7
|
+
PaginationOptions
|
|
8
|
+
} from 'convex/server'
|
|
4
9
|
import type { BoundaryHelpersOptions } from '../../internal/boundaryHelpers'
|
|
5
10
|
import { createBoundaryHelpers } from '../../internal/boundaryHelpers'
|
|
11
|
+
import { createPaginationCodec } from '../../internal/paginationCodec'
|
|
6
12
|
import type { AnyRegistry } from '../../internal/types'
|
|
7
13
|
|
|
14
|
+
/** Accumulated decoded items, with pagination controlled by Convex. */
|
|
15
|
+
export type PaginatedResult<Item> = {
|
|
16
|
+
results: Item[]
|
|
17
|
+
status: 'LoadingFirstPage' | 'CanLoadMore' | 'LoadingMore' | 'Exhausted'
|
|
18
|
+
loadMore: (numItems: number) => boolean
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type PaginatedSubscription<Item> = (() => void) & {
|
|
22
|
+
unsubscribe: () => void
|
|
23
|
+
getCurrentValue: () => PaginatedResult<Item> | undefined
|
|
24
|
+
getQueryLogs: () => string[] | undefined
|
|
25
|
+
}
|
|
26
|
+
|
|
8
27
|
export type ZodvexClientOptions = (
|
|
9
28
|
| { url: string; token?: string | null }
|
|
10
29
|
| { client: ConvexClient }
|
|
@@ -18,12 +37,14 @@ function tokenToFetcher(token: string): AuthTokenFetcher {
|
|
|
18
37
|
|
|
19
38
|
export class ZodvexClient<R extends AnyRegistry = AnyRegistry> {
|
|
20
39
|
private codec: ReturnType<typeof createBoundaryHelpers>
|
|
40
|
+
private paginationCodec: ReturnType<typeof createPaginationCodec>
|
|
21
41
|
private innerClient?: ConvexClient
|
|
22
42
|
private url?: string
|
|
23
43
|
private pendingAuthFetcher?: AuthTokenFetcher
|
|
24
44
|
private pendingAuthOnChange?: (isAuthenticated: boolean) => void
|
|
25
45
|
|
|
26
46
|
constructor(registry: R, options: ZodvexClientOptions) {
|
|
47
|
+
this.paginationCodec = createPaginationCodec(registry)
|
|
27
48
|
this.codec = createBoundaryHelpers(registry, {
|
|
28
49
|
onDecodeError: options.onDecodeError,
|
|
29
50
|
warnWirePreview: options.warnWirePreview
|
|
@@ -128,34 +149,62 @@ export class ZodvexClient<R extends AnyRegistry = AnyRegistry> {
|
|
|
128
149
|
}
|
|
129
150
|
|
|
130
151
|
/**
|
|
131
|
-
* Experimental
|
|
132
|
-
*
|
|
152
|
+
* Experimental Convex pagination with encoded filters and strictly decoded items.
|
|
153
|
+
* Returns accumulated results, not page envelopes. Both callbacks and current-value
|
|
154
|
+
* reads decode items; whole-page checks require explicit paginationOpts queries.
|
|
133
155
|
*/
|
|
134
|
-
onPaginatedUpdate_experimental<
|
|
156
|
+
onPaginatedUpdate_experimental<
|
|
157
|
+
Q extends FunctionReference<
|
|
158
|
+
'query',
|
|
159
|
+
'public',
|
|
160
|
+
{ paginationOpts?: PaginationOptions },
|
|
161
|
+
{ page: any[]; isDone: boolean; continueCursor: string }
|
|
162
|
+
>
|
|
163
|
+
>(
|
|
135
164
|
ref: Q,
|
|
136
|
-
args: Q
|
|
165
|
+
args: Omit<FunctionArgs<Q>, 'paginationOpts'>,
|
|
137
166
|
options: { initialNumItems: number },
|
|
138
|
-
callback: (result:
|
|
139
|
-
page: Q['_returnType'][]
|
|
140
|
-
isDone: boolean
|
|
141
|
-
continueCursor: string
|
|
142
|
-
[key: string]: unknown
|
|
143
|
-
}) => void,
|
|
167
|
+
callback: (result: PaginatedResult<FunctionReturnType<Q>['page'][number]>) => void,
|
|
144
168
|
onError?: (e: Error) => void
|
|
145
|
-
):
|
|
146
|
-
const wireArgs = this.
|
|
147
|
-
|
|
169
|
+
): PaginatedSubscription<FunctionReturnType<Q>['page'][number]> {
|
|
170
|
+
const wireArgs = this.paginationCodec.encodeArgs(ref, args)
|
|
171
|
+
const decode = (wire: PaginatedResult<unknown>) => ({
|
|
172
|
+
...wire,
|
|
173
|
+
results: this.paginationCodec.decodeResults(ref, wire.results)
|
|
174
|
+
})
|
|
175
|
+
// Convex's experimental callback declaration describes a page envelope, but its
|
|
176
|
+
// implementation dispatches aggregate results. Keep that SDK mismatch at this boundary.
|
|
177
|
+
const nativeCallback = ((wire: PaginatedResult<unknown>) => {
|
|
178
|
+
let decoded: ReturnType<typeof decode>
|
|
179
|
+
try {
|
|
180
|
+
decoded = decode(wire)
|
|
181
|
+
} catch (error) {
|
|
182
|
+
if (!onError) throw error
|
|
183
|
+
onError(
|
|
184
|
+
error instanceof Error
|
|
185
|
+
? error
|
|
186
|
+
: new Error('Failed to decode paginated results', { cause: error })
|
|
187
|
+
)
|
|
188
|
+
return
|
|
189
|
+
}
|
|
190
|
+
callback(decoded)
|
|
191
|
+
}) as unknown as Parameters<ConvexClient['onPaginatedUpdate_experimental']>[3]
|
|
192
|
+
const native = this.getConvex().onPaginatedUpdate_experimental(
|
|
148
193
|
ref,
|
|
149
194
|
wireArgs,
|
|
150
195
|
options,
|
|
151
|
-
|
|
152
|
-
callback({
|
|
153
|
-
...wireResult,
|
|
154
|
-
page: wireResult.page.map((item: any) => this.codec.decodeResult(ref, item))
|
|
155
|
-
})
|
|
156
|
-
},
|
|
196
|
+
nativeCallback,
|
|
157
197
|
onError
|
|
158
|
-
)
|
|
198
|
+
)
|
|
199
|
+
const withLogs = native as typeof native & { getQueryLogs?: () => string[] | undefined }
|
|
200
|
+
return Object.assign(() => native(), {
|
|
201
|
+
unsubscribe: () => native.unsubscribe(),
|
|
202
|
+
getCurrentValue: () => {
|
|
203
|
+
const wire = native.getCurrentValue()
|
|
204
|
+
return wire === undefined ? undefined : decode(wire)
|
|
205
|
+
},
|
|
206
|
+
getQueryLogs: () => withLogs.getQueryLogs?.()
|
|
207
|
+
})
|
|
159
208
|
}
|
|
160
209
|
|
|
161
210
|
/**
|
|
@@ -690,7 +690,7 @@ export function generateClientFile(options?: { mini?: boolean }): GeneratedFile
|
|
|
690
690
|
]
|
|
691
691
|
|
|
692
692
|
const jsExports = [
|
|
693
|
-
'export const { useZodQuery, useZodMutation, useQuery_experimental } = createZodvexHooks(zodvexRegistry)',
|
|
693
|
+
'export const { useZodQuery, useZodMutation, useZodPaginatedQuery, useQuery_experimental } = createZodvexHooks(zodvexRegistry)',
|
|
694
694
|
'',
|
|
695
695
|
'export const createClient = (options) =>',
|
|
696
696
|
' createZodvexClient(zodvexRegistry, options)',
|
|
@@ -713,6 +713,7 @@ export function generateClientFile(options?: { mini?: boolean }): GeneratedFile
|
|
|
713
713
|
|
|
714
714
|
const dtsDeclarations = [
|
|
715
715
|
"export declare const useZodQuery: ZodvexHooks['useZodQuery']",
|
|
716
|
+
"export declare const useZodPaginatedQuery: ZodvexHooks['useZodPaginatedQuery']",
|
|
716
717
|
"export declare const useQuery_experimental: ZodvexHooks['useQuery_experimental']",
|
|
717
718
|
"export declare const useZodMutation: ZodvexHooks['useZodMutation']",
|
|
718
719
|
'',
|
|
@@ -1,9 +1,20 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
OptionalRestArgsOrSkip,
|
|
3
|
+
PaginatedQueryReference,
|
|
4
|
+
UsePaginatedQueryResult
|
|
5
|
+
} from 'convex/react'
|
|
2
6
|
import * as convexReact from 'convex/react'
|
|
3
|
-
import { useMutation, useQuery } from 'convex/react'
|
|
4
|
-
import type {
|
|
7
|
+
import { useMutation, usePaginatedQuery, useQuery } from 'convex/react'
|
|
8
|
+
import type {
|
|
9
|
+
FunctionArgs,
|
|
10
|
+
FunctionReference,
|
|
11
|
+
FunctionReturnType,
|
|
12
|
+
PaginationOptions
|
|
13
|
+
} from 'convex/server'
|
|
14
|
+
import { useMemo } from 'react'
|
|
5
15
|
import type { BoundaryHelpersOptions } from '../../internal/boundaryHelpers'
|
|
6
|
-
import { createBoundaryHelpers } from '../../internal/boundaryHelpers'
|
|
16
|
+
import { createBoundaryHelpers, resolveFunctionPath } from '../../internal/boundaryHelpers'
|
|
17
|
+
import { createPaginationCodec } from '../../internal/paginationCodec'
|
|
7
18
|
import type { AnyRegistry } from '../../internal/types'
|
|
8
19
|
|
|
9
20
|
/** Query state, with failures omitted when throwOnError is enabled. */
|
|
@@ -56,11 +67,50 @@ export function createZodvexHooks<R extends AnyRegistry>(
|
|
|
56
67
|
options?: BoundaryHelpersOptions
|
|
57
68
|
) {
|
|
58
69
|
const codec = createBoundaryHelpers(registry, options)
|
|
70
|
+
const paginationCodec = createPaginationCodec(registry)
|
|
59
71
|
const experimentalCodec = createBoundaryHelpers(registry, {
|
|
60
72
|
...options,
|
|
61
73
|
onDecodeError: options?.onDecodeError ?? 'throw'
|
|
62
74
|
})
|
|
63
75
|
|
|
76
|
+
/**
|
|
77
|
+
* Convex pagination with encoded filters and decoded items. Pagination metadata
|
|
78
|
+
* stays under Convex's control. Codec errors throw, including in warn mode.
|
|
79
|
+
* Outer object/array refinements and transforms require explicit page queries.
|
|
80
|
+
*/
|
|
81
|
+
function useZodPaginatedQuery<
|
|
82
|
+
Query extends FunctionReference<
|
|
83
|
+
'query',
|
|
84
|
+
'public',
|
|
85
|
+
{ paginationOpts?: PaginationOptions },
|
|
86
|
+
{ page: any[]; isDone: boolean; continueCursor: string }
|
|
87
|
+
>
|
|
88
|
+
>(
|
|
89
|
+
ref: Query,
|
|
90
|
+
args: Omit<FunctionArgs<Query>, 'paginationOpts'> | 'skip',
|
|
91
|
+
paginationOptions: { initialNumItems: number }
|
|
92
|
+
): UsePaginatedQueryResult<FunctionReturnType<Query>['page'][number]> {
|
|
93
|
+
let wireArgs: any = 'skip'
|
|
94
|
+
let encodeFailure: { error: unknown } | undefined
|
|
95
|
+
if (args !== 'skip') {
|
|
96
|
+
try {
|
|
97
|
+
wireArgs = paginationCodec.encodeArgs(ref, args)
|
|
98
|
+
} catch (error) {
|
|
99
|
+
encodeFailure = { error }
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const state = usePaginatedQuery(ref as PaginatedQueryReference, wireArgs, paginationOptions)
|
|
103
|
+
const skipped = wireArgs === 'skip'
|
|
104
|
+
const queryKey = resolveFunctionPath(ref) ?? ref
|
|
105
|
+
const results = useMemo(
|
|
106
|
+
() => (skipped ? state.results : paginationCodec.decodeResults(ref, state.results)),
|
|
107
|
+
[queryKey, state.results, skipped]
|
|
108
|
+
)
|
|
109
|
+
// Call both hooks on failed encodes too, so a failure never changes hook order.
|
|
110
|
+
if (encodeFailure) throw encodeFailure.error
|
|
111
|
+
return { ...state, results }
|
|
112
|
+
}
|
|
113
|
+
|
|
64
114
|
/**
|
|
65
115
|
* Convex's object-form query hook with decoded arguments and results (Convex >=1.37).
|
|
66
116
|
* Codec failures return an error state unless throwOnError is true. Decoding is
|
|
@@ -196,7 +246,7 @@ export function createZodvexHooks<R extends AnyRegistry>(
|
|
|
196
246
|
}
|
|
197
247
|
}
|
|
198
248
|
|
|
199
|
-
return { useZodQuery, useZodMutation, useQuery_experimental }
|
|
249
|
+
return { useZodQuery, useZodMutation, useZodPaginatedQuery, useQuery_experimental }
|
|
200
250
|
}
|
|
201
251
|
|
|
202
252
|
/**
|