zodvex 0.7.8 → 0.7.9
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/codegen/index.js +2 -1
- package/dist/codegen/index.js.map +1 -1
- package/dist/mini/react/index.js +42 -1
- package/dist/mini/react/index.js.map +1 -1
- package/dist/public/cli/init.d.ts.map +1 -1
- package/dist/public/codegen/generate.d.ts.map +1 -1
- package/dist/public/react/hooks.d.ts +17 -0
- package/dist/public/react/hooks.d.ts.map +1 -1
- package/dist/public/react/index.d.ts +1 -1
- package/dist/public/react/index.d.ts.map +1 -1
- package/dist/react/index.js +42 -1
- package/dist/react/index.js.map +1 -1
- package/package.json +1 -1
- package/src/public/cli/init.ts +1 -0
- package/src/public/codegen/generate.ts +2 -1
- package/src/public/react/hooks.ts +83 -1
- package/src/public/react/index.ts +1 -1
|
@@ -1,10 +1,34 @@
|
|
|
1
1
|
import type { OptionalRestArgsOrSkip } from 'convex/react'
|
|
2
|
+
import * as convexReact from 'convex/react'
|
|
2
3
|
import { useMutation, useQuery } from 'convex/react'
|
|
3
4
|
import type { FunctionArgs, FunctionReference, FunctionReturnType } from 'convex/server'
|
|
4
5
|
import type { BoundaryHelpersOptions } from '../../internal/boundaryHelpers'
|
|
5
6
|
import { createBoundaryHelpers } from '../../internal/boundaryHelpers'
|
|
6
7
|
import type { AnyRegistry } from '../../internal/types'
|
|
7
8
|
|
|
9
|
+
/** Query state, with failures omitted when throwOnError is enabled. */
|
|
10
|
+
export type UseQueryResult<Result, ThrowOnError extends boolean = false> =
|
|
11
|
+
| { status: 'pending' }
|
|
12
|
+
| { status: 'success'; data: Result }
|
|
13
|
+
| (ThrowOnError extends true ? never : { status: 'error'; error: Error })
|
|
14
|
+
|
|
15
|
+
type UseQueryOptions<
|
|
16
|
+
Query extends FunctionReference<'query', any, any, any>,
|
|
17
|
+
ThrowOnError extends boolean
|
|
18
|
+
> = {
|
|
19
|
+
query: Query
|
|
20
|
+
args: FunctionArgs<Query> | 'skip'
|
|
21
|
+
throwOnError?: ThrowOnError
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function codecError(error: unknown, operation: 'encode' | 'decode'): Error {
|
|
25
|
+
return error instanceof Error
|
|
26
|
+
? error
|
|
27
|
+
: new Error(`Failed to ${operation} query ${operation === 'encode' ? 'arguments' : 'result'}`, {
|
|
28
|
+
cause: error
|
|
29
|
+
})
|
|
30
|
+
}
|
|
31
|
+
|
|
8
32
|
/**
|
|
9
33
|
* Creates zodvex-aware React hooks that automatically decode query results
|
|
10
34
|
* and encode mutation arguments using the Zod schemas in the registry.
|
|
@@ -32,6 +56,64 @@ export function createZodvexHooks<R extends AnyRegistry>(
|
|
|
32
56
|
options?: BoundaryHelpersOptions
|
|
33
57
|
) {
|
|
34
58
|
const codec = createBoundaryHelpers(registry, options)
|
|
59
|
+
const experimentalCodec = createBoundaryHelpers(registry, {
|
|
60
|
+
...options,
|
|
61
|
+
onDecodeError: options?.onDecodeError ?? 'throw'
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Convex's object-form query hook with decoded arguments and results (Convex >=1.37).
|
|
66
|
+
* Codec failures return an error state unless throwOnError is true. Decoding is
|
|
67
|
+
* strict by default; explicit onDecodeError: 'warn' preserves the raw fallback.
|
|
68
|
+
*/
|
|
69
|
+
function useQuery_experimental<
|
|
70
|
+
Query extends FunctionReference<'query', any, any, any>,
|
|
71
|
+
ThrowOnError extends boolean = false
|
|
72
|
+
>(
|
|
73
|
+
queryOptions: UseQueryOptions<Query, ThrowOnError>
|
|
74
|
+
): UseQueryResult<FunctionReturnType<Query>, ThrowOnError>
|
|
75
|
+
function useQuery_experimental(
|
|
76
|
+
queryOptions: UseQueryOptions<FunctionReference<'query', any, any, any>, boolean>
|
|
77
|
+
): UseQueryResult<any> {
|
|
78
|
+
// Optional namespace access keeps older SDK imports valid and allows tree shaking.
|
|
79
|
+
const nativeHook = (
|
|
80
|
+
convexReact as unknown as {
|
|
81
|
+
useQuery_experimental?: (
|
|
82
|
+
options: UseQueryOptions<FunctionReference<'query', any, any, any>, boolean>
|
|
83
|
+
) => UseQueryResult<any>
|
|
84
|
+
}
|
|
85
|
+
).useQuery_experimental
|
|
86
|
+
if (typeof nativeHook !== 'function') {
|
|
87
|
+
throw new Error(
|
|
88
|
+
'useQuery_experimental requires Convex >=1.37. Upgrade convex to use this hook.'
|
|
89
|
+
)
|
|
90
|
+
}
|
|
91
|
+
const { query, args, throwOnError } = queryOptions
|
|
92
|
+
let wireArgs: any = 'skip'
|
|
93
|
+
let encodeFailure: Error | undefined
|
|
94
|
+
if (args !== 'skip') {
|
|
95
|
+
try {
|
|
96
|
+
wireArgs = experimentalCodec.encodeArgs(query, args)
|
|
97
|
+
} catch (error) {
|
|
98
|
+
encodeFailure = codecError(error, 'encode')
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Even failed encodes call the real hook; its own synchronous errors still propagate.
|
|
103
|
+
const state = nativeHook({ query, args: wireArgs, throwOnError })
|
|
104
|
+
if (encodeFailure) {
|
|
105
|
+
if (throwOnError) throw encodeFailure
|
|
106
|
+
return { status: 'error', error: encodeFailure }
|
|
107
|
+
}
|
|
108
|
+
if (state.status !== 'success') return state
|
|
109
|
+
try {
|
|
110
|
+
return { status: 'success', data: experimentalCodec.decodeResult(query, state.data) }
|
|
111
|
+
} catch (error) {
|
|
112
|
+
const failure = codecError(error, 'decode')
|
|
113
|
+
if (throwOnError) throw failure
|
|
114
|
+
return { status: 'error', error: failure }
|
|
115
|
+
}
|
|
116
|
+
}
|
|
35
117
|
|
|
36
118
|
/**
|
|
37
119
|
* Drop-in replacement for Convex's `useQuery` with automatic codec decode.
|
|
@@ -114,7 +196,7 @@ export function createZodvexHooks<R extends AnyRegistry>(
|
|
|
114
196
|
}
|
|
115
197
|
}
|
|
116
198
|
|
|
117
|
-
return { useZodQuery, useZodMutation }
|
|
199
|
+
return { useZodQuery, useZodMutation, useQuery_experimental }
|
|
118
200
|
}
|
|
119
201
|
|
|
120
202
|
/**
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type { ZodvexHooks } from './hooks'
|
|
1
|
+
export type { UseQueryResult, ZodvexHooks } from './hooks'
|
|
2
2
|
export { createZodvexHooks } from './hooks'
|
|
3
3
|
export type { ZodvexReactClientOptions } from './zodvexReactClient'
|
|
4
4
|
export { createZodvexReactClient, ZodvexReactClient } from './zodvexReactClient'
|