zodvex 0.1.0
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/LICENSE +21 -0
- package/README.md +166 -0
- package/dist/index.d.mts +128 -0
- package/dist/index.d.ts +128 -0
- package/dist/index.js +518 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +496 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +66 -0
- package/src/codec.ts +145 -0
- package/src/custom.ts +155 -0
- package/src/mapping.ts +190 -0
- package/src/tables.ts +58 -0
- package/src/types.ts +78 -0
- package/src/utils.ts +10 -0
- package/src/wrappers.ts +184 -0
- package/src/zodV4Compat.ts +36 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 zodvex contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# zodvex
|
|
2
|
+
#### [Zod](https://zod.dev/) + [Convex](https://www.convex.dev/)
|
|
3
|
+
|
|
4
|
+
> Heavily inspired by [convex-helpers](https://github.com/get-convex/convex-helpers?tab=readme-ov-file). Some of those utilities are still being directly imported.
|
|
5
|
+
|
|
6
|
+
Zod v4 → Convex validator mapping with correct optional vs nullable semantics. Thin, practical glue around `convex-helpers` that preserves Convex’s notion of optional fields and offers ergonomic wrappers/codecs.
|
|
7
|
+
|
|
8
|
+
Why
|
|
9
|
+
|
|
10
|
+
- Convex treats optional object fields only when wrapped with `v.optional(...)`.
|
|
11
|
+
- Some mappers represent `.optional()` as `v.union(T, v.null())`, which still requires the key and changes semantics.
|
|
12
|
+
- zodvex normalizes per-field validators so that:
|
|
13
|
+
- optional only → `v.optional(T)`
|
|
14
|
+
- nullable only → `v.union(T, v.null())`
|
|
15
|
+
- optional + nullable → `v.optional(v.union(T, v.null()))`
|
|
16
|
+
|
|
17
|
+
What’s Included
|
|
18
|
+
|
|
19
|
+
- Low-level mapping helpers
|
|
20
|
+
- `zodToConvex(z: ZodTypeAny): Validator`
|
|
21
|
+
- `zodToConvexFields(shapeOrObject: ZodRawShape | Record<string, ZodTypeAny> | ZodObject<any>): Record<string, Validator>`
|
|
22
|
+
- Function wrappers (typed, Zod-validated)
|
|
23
|
+
- `zQuery(query, input, handler, { returns? })`
|
|
24
|
+
- `zMutation(mutation, input, handler, { returns? })`
|
|
25
|
+
- `zAction(action, input, handler, { returns? })`
|
|
26
|
+
- `zInternalQuery(...)`, `zInternalMutation(...)`, `zInternalAction(...)`
|
|
27
|
+
- Input can be a Zod object, a raw shape object, or a single Zod type. If a single Zod type is provided, args normalize to `{ value: ... }`.
|
|
28
|
+
- Optional `returns` lets you specify a Zod schema for return validation (see notes below for Dates/encoding).
|
|
29
|
+
- Codecs
|
|
30
|
+
- `convexCodec(schema)` with:
|
|
31
|
+
- `toConvexSchema()` → Convex validators (object fields or a single validator)
|
|
32
|
+
- `encode(data)` → zod-shaped data → Convex-safe JSON (omits `undefined`, converts `Date` → timestamp)
|
|
33
|
+
- `decode(data)` → Convex JSON → zod-shaped data (timestamps → `Date`)
|
|
34
|
+
- `pick({ key: true })` → derive a sub-codec (ZodObject only)
|
|
35
|
+
- Table helper
|
|
36
|
+
- `zodTable(name, zodObject)` → returns a helper with:
|
|
37
|
+
- `.table` → feed into `defineSchema`
|
|
38
|
+
- `.schema` → original Zod schema
|
|
39
|
+
- `.codec` → codec built from the schema (`toConvexSchema/encode/decode`)
|
|
40
|
+
- CRUD helper
|
|
41
|
+
- `zCrud(table, queryBuilder, mutationBuilder)` → returns `{ create, read, paginate, update, destroy }` using Zod-validated args and `zid(tableName)`
|
|
42
|
+
|
|
43
|
+
Usage
|
|
44
|
+
|
|
45
|
+
Define a table from Zod in one place
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { z } from 'zod'
|
|
49
|
+
import { zodTable } from 'zodvex'
|
|
50
|
+
|
|
51
|
+
const zAgencies = z.object({
|
|
52
|
+
name: z.string(),
|
|
53
|
+
managerList: z.array(z.string()).optional() // → v.optional(v.array(v.string()))
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
export const Agencies = zodTable('agencies', zAgencies)
|
|
57
|
+
// In schema.ts → Agencies.table.index(...)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Zod-validated Convex functions
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
import { z } from 'zod'
|
|
64
|
+
import { query, mutation } from './_generated/server'
|
|
65
|
+
import { zQuery, zMutation } from 'zodvex'
|
|
66
|
+
|
|
67
|
+
export const getById = zQuery(
|
|
68
|
+
query,
|
|
69
|
+
{ id: z.string() },
|
|
70
|
+
async (ctx, { id }) => ctx.db.get(id)
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
export const setName = zMutation(
|
|
74
|
+
mutation,
|
|
75
|
+
z.object({ id: z.string(), name: z.string() }),
|
|
76
|
+
async (ctx, { id, name }) => ctx.db.patch(id, { name })
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
// Single-value args normalize to { value }
|
|
80
|
+
export const ping = zQuery(query, z.string(), async (ctx, { value }) => value)
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
CRUD scaffold from a table
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
import { zCrud } from 'zodvex'
|
|
87
|
+
import { query, mutation } from './_generated/server'
|
|
88
|
+
import { Agencies } from './schemas/agencies'
|
|
89
|
+
|
|
90
|
+
export const agenciesCrud = zCrud(Agencies, query, mutation)
|
|
91
|
+
// agenciesCrud.create/read/paginate/update/destroy
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Codec usage
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
import { convexCodec } from 'zodvex'
|
|
98
|
+
import { z } from 'zod'
|
|
99
|
+
|
|
100
|
+
const zUser = z.object({
|
|
101
|
+
name: z.string(),
|
|
102
|
+
birthday: z.date().optional()
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
const UserCodec = convexCodec(zUser)
|
|
106
|
+
const validators = UserCodec.toConvexSchema() // for Table(...)
|
|
107
|
+
const toWrite = UserCodec.encode({ name: 'A', birthday: new Date() }) // Date → timestamp
|
|
108
|
+
const toRead = UserCodec.decode({ name: 'A', birthday: Date.now() }) // timestamp → Date
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Behavior & Semantics
|
|
112
|
+
|
|
113
|
+
- Optional vs nullable
|
|
114
|
+
- `.optional()` becomes `v.optional(T)`
|
|
115
|
+
- `.nullable()` becomes `v.union(T, v.null())`
|
|
116
|
+
- Both → `v.optional(v.union(T, v.null()))`
|
|
117
|
+
- Arrays/objects/records
|
|
118
|
+
- Mapping uses Zod v4 public APIs (`shape`, `element`, `options`, `valueType`).
|
|
119
|
+
- Returns validation
|
|
120
|
+
- If you supply `returns`, wrappers validate the handler’s return value with the Zod schema and encode it to Convex-safe JSON (e.g., `z.date()` → timestamp) before returning.
|
|
121
|
+
- Numbers
|
|
122
|
+
- `z.number()` maps to `v.float64()` to match Convex JSON. If you need integers, prefer `z.bigint()` (→ `v.int64()`) or handle integer checks at runtime.
|
|
123
|
+
- IDs
|
|
124
|
+
- Use `zid('table')` from `convex-helpers/server/zodV4` inside Zod schemas for Convex Ids.
|
|
125
|
+
- Fallbacks
|
|
126
|
+
- Unsupported Zod types (e.g., `ZodEffects` transforms, `ZodTuple`, `ZodIntersection`, maps/sets) fall back to `v.any()`.
|
|
127
|
+
|
|
128
|
+
Compatibility
|
|
129
|
+
|
|
130
|
+
- Zod v4 only. This library uses only Zod’s public v4 APIs and does not attempt v3 compatibility.
|
|
131
|
+
|
|
132
|
+
Notes
|
|
133
|
+
|
|
134
|
+
- Defaults imply optional at the Convex schema level; apply runtime defaults in app code as needed.
|
|
135
|
+
- This package builds on `convex-helpers/server/zodV4` and post-processes validators to preserve Convex’s optional/null semantics.
|
|
136
|
+
|
|
137
|
+
Install
|
|
138
|
+
|
|
139
|
+
- Peer dependencies (must be installed by the host app):
|
|
140
|
+
- `zod` (v4)
|
|
141
|
+
- `convex` (>= 1.27)
|
|
142
|
+
- `convex-helpers` (>= 0.1.101-alpha.1)
|
|
143
|
+
|
|
144
|
+
Using pnpm:
|
|
145
|
+
|
|
146
|
+
```bash
|
|
147
|
+
# add peers (if not already present)
|
|
148
|
+
pnpm add zod convex convex-helpers
|
|
149
|
+
|
|
150
|
+
# add this package
|
|
151
|
+
pnpm add zodvex
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
Using npm:
|
|
155
|
+
|
|
156
|
+
```bash
|
|
157
|
+
npm install zod convex convex-helpers
|
|
158
|
+
npm install zodvex
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
Using yarn:
|
|
162
|
+
|
|
163
|
+
```bash
|
|
164
|
+
yarn add zod convex convex-helpers
|
|
165
|
+
yarn add zodvex
|
|
166
|
+
```
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import * as convex_values from 'convex/values';
|
|
2
|
+
import { Validator, PropertyValidators } from 'convex/values';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import * as convex_server from 'convex/server';
|
|
5
|
+
import { QueryBuilder, RegisteredQuery, DefaultFunctionArgs, MutationBuilder, RegisteredMutation, ActionBuilder, RegisteredAction, FunctionVisibility, GenericDataModel, GenericQueryCtx, GenericMutationCtx, GenericActionCtx } from 'convex/server';
|
|
6
|
+
import { CustomBuilder, Customization } from 'convex-helpers/server/customFunctions';
|
|
7
|
+
import { VFloat64 } from './validators.js';
|
|
8
|
+
|
|
9
|
+
declare function makeUnion(members: any[]): any;
|
|
10
|
+
declare function getObjectShape(obj: z.ZodObject<any>): Record<string, z.ZodTypeAny>;
|
|
11
|
+
declare function analyzeZod(schema: z.ZodTypeAny): {
|
|
12
|
+
base: z.ZodTypeAny;
|
|
13
|
+
optional: boolean;
|
|
14
|
+
nullable: boolean;
|
|
15
|
+
hasDefault: boolean;
|
|
16
|
+
};
|
|
17
|
+
declare function simpleToConvex(schema: z.ZodTypeAny): any;
|
|
18
|
+
declare function zodToConvex(schema: z.ZodTypeAny): Validator<any, any, any>;
|
|
19
|
+
declare function zodToConvexFields(shapeOrObject: z.ZodRawShape | Record<string, z.ZodTypeAny> | z.ZodObject<any>): Record<string, Validator<any, any, any>>;
|
|
20
|
+
|
|
21
|
+
type ConvexCodec<T = any> = {
|
|
22
|
+
schema: z.ZodTypeAny;
|
|
23
|
+
toConvexSchema: () => any;
|
|
24
|
+
encode: (data: T) => any;
|
|
25
|
+
decode: (data: any) => T;
|
|
26
|
+
pick: (keys: Record<string, true>) => ConvexCodec<any>;
|
|
27
|
+
};
|
|
28
|
+
declare function toConvexJS(value: any): any;
|
|
29
|
+
declare function toConvexJS(schema: z.ZodTypeAny, value: any): any;
|
|
30
|
+
declare function fromConvexJS(value: any, schema: z.ZodTypeAny): any;
|
|
31
|
+
declare function convexCodec<T = any>(schema: z.ZodTypeAny): ConvexCodec<T>;
|
|
32
|
+
|
|
33
|
+
type InferArgs<A> = A extends z.ZodObject<infer S> ? z.infer<z.ZodObject<S>> : A extends Record<string, z.ZodTypeAny> ? {
|
|
34
|
+
[K in keyof A]: z.infer<A[K]>;
|
|
35
|
+
} : A extends z.ZodTypeAny ? z.infer<A> : Record<string, never>;
|
|
36
|
+
type InferReturns<R> = R extends z.ZodTypeAny ? z.output<R> : R extends undefined ? any : R;
|
|
37
|
+
type ExtractCtx<Builder> = Builder extends {
|
|
38
|
+
(fn: {
|
|
39
|
+
handler: (ctx: infer Ctx, ...args: any[]) => any;
|
|
40
|
+
}): any;
|
|
41
|
+
} ? Ctx : never;
|
|
42
|
+
type PreserveReturnType<Builder, ArgsType, ReturnsType> = Builder extends QueryBuilder<any, infer Visibility> ? RegisteredQuery<Visibility, ArgsType extends DefaultFunctionArgs ? ArgsType : DefaultFunctionArgs, Promise<ReturnsType>> : Builder extends MutationBuilder<any, infer Visibility> ? RegisteredMutation<Visibility, ArgsType extends DefaultFunctionArgs ? ArgsType : DefaultFunctionArgs, Promise<ReturnsType>> : Builder extends ActionBuilder<any, infer Visibility> ? RegisteredAction<Visibility, ArgsType extends DefaultFunctionArgs ? ArgsType : DefaultFunctionArgs, Promise<ReturnsType>> : Builder extends CustomBuilder<'query', any, any, any, any, infer V, any> ? RegisteredQuery<V, ArgsType extends DefaultFunctionArgs ? ArgsType : DefaultFunctionArgs, Promise<ReturnsType>> : Builder extends CustomBuilder<'mutation', any, any, any, any, infer V, any> ? RegisteredMutation<V, ArgsType extends DefaultFunctionArgs ? ArgsType : DefaultFunctionArgs, Promise<ReturnsType>> : Builder extends CustomBuilder<'action', any, any, any, any, infer V, any> ? RegisteredAction<V, ArgsType extends DefaultFunctionArgs ? ArgsType : DefaultFunctionArgs, Promise<ReturnsType>> : Builder extends (...args: any[]) => any ? ReturnType<Builder> : never;
|
|
43
|
+
type ZodToConvexArgs<A> = A extends z.ZodObject<infer Shape> ? {
|
|
44
|
+
[K in keyof Shape]: any;
|
|
45
|
+
} : A extends Record<string, z.ZodTypeAny> ? {
|
|
46
|
+
[K in keyof A]: any;
|
|
47
|
+
} : A extends z.ZodTypeAny ? {
|
|
48
|
+
value: any;
|
|
49
|
+
} : Record<string, never>;
|
|
50
|
+
|
|
51
|
+
declare function zQuery<Builder extends (fn: any) => any, A extends z.ZodTypeAny | Record<string, z.ZodTypeAny>, R extends z.ZodTypeAny | undefined = undefined>(query: Builder, input: A, handler: (ctx: ExtractCtx<Builder>, args: InferArgs<A>) => Promise<InferReturns<R>> | InferReturns<R>, options?: {
|
|
52
|
+
returns?: R;
|
|
53
|
+
}): PreserveReturnType<Builder, ZodToConvexArgs<A>, InferReturns<R>>;
|
|
54
|
+
declare function zInternalQuery<Builder extends (fn: any) => any, A extends z.ZodTypeAny | Record<string, z.ZodTypeAny>, R extends z.ZodTypeAny | undefined = undefined>(internalQuery: Builder, input: A, handler: (ctx: ExtractCtx<Builder>, args: InferArgs<A>) => Promise<InferReturns<R>> | InferReturns<R>, options?: {
|
|
55
|
+
returns?: R;
|
|
56
|
+
}): PreserveReturnType<Builder, ZodToConvexArgs<A>, InferReturns<R>>;
|
|
57
|
+
declare function zMutation<Builder extends (fn: any) => any, A extends z.ZodTypeAny | Record<string, z.ZodTypeAny>, R extends z.ZodTypeAny | undefined = undefined>(mutation: Builder, input: A, handler: (ctx: ExtractCtx<Builder>, args: InferArgs<A>) => Promise<InferReturns<R>> | InferReturns<R>, options?: {
|
|
58
|
+
returns?: R;
|
|
59
|
+
}): PreserveReturnType<Builder, ZodToConvexArgs<A>, InferReturns<R>>;
|
|
60
|
+
declare function zInternalMutation<Builder extends (fn: any) => any, A extends z.ZodTypeAny | Record<string, z.ZodTypeAny>, R extends z.ZodTypeAny | undefined = undefined>(internalMutation: Builder, input: A, handler: (ctx: ExtractCtx<Builder>, args: InferArgs<A>) => Promise<InferReturns<R>> | InferReturns<R>, options?: {
|
|
61
|
+
returns?: R;
|
|
62
|
+
}): PreserveReturnType<Builder, ZodToConvexArgs<A>, InferReturns<R>>;
|
|
63
|
+
declare function zAction<Builder extends (fn: any) => any, A extends z.ZodTypeAny | Record<string, z.ZodTypeAny>, R extends z.ZodTypeAny | undefined = undefined>(action: Builder, input: A, handler: (ctx: ExtractCtx<Builder>, args: InferArgs<A>) => Promise<InferReturns<R>> | InferReturns<R>, options?: {
|
|
64
|
+
returns?: R;
|
|
65
|
+
}): PreserveReturnType<Builder, ZodToConvexArgs<A>, InferReturns<R>>;
|
|
66
|
+
declare function zInternalAction<Builder extends (fn: any) => any, A extends z.ZodTypeAny | Record<string, z.ZodTypeAny>, R extends z.ZodTypeAny | undefined = undefined>(internalAction: Builder, input: A, handler: (ctx: ExtractCtx<Builder>, args: InferArgs<A>) => Promise<InferReturns<R>> | InferReturns<R>, options?: {
|
|
67
|
+
returns?: R;
|
|
68
|
+
}): PreserveReturnType<Builder, ZodToConvexArgs<A>, InferReturns<R>>;
|
|
69
|
+
|
|
70
|
+
declare function zCustomQuery<CustomArgsValidator extends PropertyValidators, CustomCtx extends Record<string, any>, CustomMadeArgs extends Record<string, any>, Visibility extends FunctionVisibility, DataModel extends GenericDataModel>(query: any, customization: Customization<GenericQueryCtx<DataModel>, CustomArgsValidator, CustomCtx, CustomMadeArgs>): CustomBuilder<"query", CustomArgsValidator, CustomCtx, CustomMadeArgs, GenericQueryCtx<DataModel>, Visibility, Record<string, any>>;
|
|
71
|
+
declare function zCustomMutation<CustomArgsValidator extends PropertyValidators, CustomCtx extends Record<string, any>, CustomMadeArgs extends Record<string, any>, Visibility extends FunctionVisibility, DataModel extends GenericDataModel>(mutation: any, customization: Customization<GenericMutationCtx<DataModel>, CustomArgsValidator, CustomCtx, CustomMadeArgs>): CustomBuilder<"mutation", CustomArgsValidator, CustomCtx, CustomMadeArgs, GenericMutationCtx<DataModel>, Visibility, Record<string, any>>;
|
|
72
|
+
declare function zCustomAction<CustomArgsValidator extends PropertyValidators, CustomCtx extends Record<string, any>, CustomMadeArgs extends Record<string, any>, Visibility extends FunctionVisibility, DataModel extends GenericDataModel>(action: any, customization: Customization<GenericActionCtx<DataModel>, CustomArgsValidator, CustomCtx, CustomMadeArgs>): CustomBuilder<"action", CustomArgsValidator, CustomCtx, CustomMadeArgs, GenericActionCtx<DataModel>, Visibility, Record<string, any>>;
|
|
73
|
+
|
|
74
|
+
declare function zodTable<T extends z.ZodObject<any>, TableName extends string>(name: TableName, schema: T): {
|
|
75
|
+
codec: ConvexCodec<any>;
|
|
76
|
+
schema: T;
|
|
77
|
+
name: TableName;
|
|
78
|
+
table: convex_server.TableDefinition<convex_values.VObject<{
|
|
79
|
+
[x: string]: any;
|
|
80
|
+
[x: number]: any;
|
|
81
|
+
[x: symbol]: any;
|
|
82
|
+
}, any, "required", string>, {}, {}, {}>;
|
|
83
|
+
doc: convex_values.VObject<{
|
|
84
|
+
[x: string]: any;
|
|
85
|
+
[x: number]: any;
|
|
86
|
+
[x: symbol]: any;
|
|
87
|
+
}, {
|
|
88
|
+
[x: string]: any;
|
|
89
|
+
}, "required", string>;
|
|
90
|
+
withoutSystemFields: any;
|
|
91
|
+
withSystemFields: {
|
|
92
|
+
[x: string]: any;
|
|
93
|
+
};
|
|
94
|
+
systemFields: {
|
|
95
|
+
_id: convex_values.VId<convex_values.GenericId<TableName>, "required">;
|
|
96
|
+
_creationTime: VFloat64<number, "required">;
|
|
97
|
+
};
|
|
98
|
+
_id: convex_values.VId<convex_values.GenericId<TableName>, "required">;
|
|
99
|
+
};
|
|
100
|
+
declare function zCrud<TableDefinition extends ReturnType<typeof zodTable>, QueryBuilder extends (fn: any) => any, MutationBuilder extends (fn: any) => any>(table: TableDefinition, queryBuilder: QueryBuilder, mutationBuilder: MutationBuilder): {
|
|
101
|
+
create: PreserveReturnType<MutationBuilder, {
|
|
102
|
+
[x: string]: any;
|
|
103
|
+
}, any>;
|
|
104
|
+
read: PreserveReturnType<QueryBuilder, {
|
|
105
|
+
id: any;
|
|
106
|
+
}, any>;
|
|
107
|
+
paginate: any;
|
|
108
|
+
update: PreserveReturnType<MutationBuilder, {
|
|
109
|
+
id: any;
|
|
110
|
+
patch: any;
|
|
111
|
+
}, any>;
|
|
112
|
+
destroy: PreserveReturnType<MutationBuilder, {
|
|
113
|
+
id: any;
|
|
114
|
+
}, any>;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Minimal compatibility for convex-helpers/server/zodV4 imports
|
|
119
|
+
* This library itself IS the Zod v4 compatibility layer
|
|
120
|
+
*/
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Create a Zod validator for a Convex Id
|
|
124
|
+
*/
|
|
125
|
+
declare function zid<TableName extends string>(tableName: TableName): z.ZodTypeAny;
|
|
126
|
+
type Zid<TableName extends string> = ReturnType<typeof zid<TableName>>;
|
|
127
|
+
|
|
128
|
+
export { type ConvexCodec, type ExtractCtx, type InferArgs, type InferReturns, type PreserveReturnType, type Zid, type ZodToConvexArgs, analyzeZod, convexCodec, fromConvexJS, getObjectShape, makeUnion, simpleToConvex, toConvexJS, zAction, zCrud, zCustomAction, zCustomMutation, zCustomQuery, zInternalAction, zInternalMutation, zInternalQuery, zMutation, zQuery, zid, zodTable, zodToConvex, zodToConvexFields };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import * as convex_values from 'convex/values';
|
|
2
|
+
import { Validator, PropertyValidators } from 'convex/values';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import * as convex_server from 'convex/server';
|
|
5
|
+
import { QueryBuilder, RegisteredQuery, DefaultFunctionArgs, MutationBuilder, RegisteredMutation, ActionBuilder, RegisteredAction, FunctionVisibility, GenericDataModel, GenericQueryCtx, GenericMutationCtx, GenericActionCtx } from 'convex/server';
|
|
6
|
+
import { CustomBuilder, Customization } from 'convex-helpers/server/customFunctions';
|
|
7
|
+
import { VFloat64 } from './validators.js';
|
|
8
|
+
|
|
9
|
+
declare function makeUnion(members: any[]): any;
|
|
10
|
+
declare function getObjectShape(obj: z.ZodObject<any>): Record<string, z.ZodTypeAny>;
|
|
11
|
+
declare function analyzeZod(schema: z.ZodTypeAny): {
|
|
12
|
+
base: z.ZodTypeAny;
|
|
13
|
+
optional: boolean;
|
|
14
|
+
nullable: boolean;
|
|
15
|
+
hasDefault: boolean;
|
|
16
|
+
};
|
|
17
|
+
declare function simpleToConvex(schema: z.ZodTypeAny): any;
|
|
18
|
+
declare function zodToConvex(schema: z.ZodTypeAny): Validator<any, any, any>;
|
|
19
|
+
declare function zodToConvexFields(shapeOrObject: z.ZodRawShape | Record<string, z.ZodTypeAny> | z.ZodObject<any>): Record<string, Validator<any, any, any>>;
|
|
20
|
+
|
|
21
|
+
type ConvexCodec<T = any> = {
|
|
22
|
+
schema: z.ZodTypeAny;
|
|
23
|
+
toConvexSchema: () => any;
|
|
24
|
+
encode: (data: T) => any;
|
|
25
|
+
decode: (data: any) => T;
|
|
26
|
+
pick: (keys: Record<string, true>) => ConvexCodec<any>;
|
|
27
|
+
};
|
|
28
|
+
declare function toConvexJS(value: any): any;
|
|
29
|
+
declare function toConvexJS(schema: z.ZodTypeAny, value: any): any;
|
|
30
|
+
declare function fromConvexJS(value: any, schema: z.ZodTypeAny): any;
|
|
31
|
+
declare function convexCodec<T = any>(schema: z.ZodTypeAny): ConvexCodec<T>;
|
|
32
|
+
|
|
33
|
+
type InferArgs<A> = A extends z.ZodObject<infer S> ? z.infer<z.ZodObject<S>> : A extends Record<string, z.ZodTypeAny> ? {
|
|
34
|
+
[K in keyof A]: z.infer<A[K]>;
|
|
35
|
+
} : A extends z.ZodTypeAny ? z.infer<A> : Record<string, never>;
|
|
36
|
+
type InferReturns<R> = R extends z.ZodTypeAny ? z.output<R> : R extends undefined ? any : R;
|
|
37
|
+
type ExtractCtx<Builder> = Builder extends {
|
|
38
|
+
(fn: {
|
|
39
|
+
handler: (ctx: infer Ctx, ...args: any[]) => any;
|
|
40
|
+
}): any;
|
|
41
|
+
} ? Ctx : never;
|
|
42
|
+
type PreserveReturnType<Builder, ArgsType, ReturnsType> = Builder extends QueryBuilder<any, infer Visibility> ? RegisteredQuery<Visibility, ArgsType extends DefaultFunctionArgs ? ArgsType : DefaultFunctionArgs, Promise<ReturnsType>> : Builder extends MutationBuilder<any, infer Visibility> ? RegisteredMutation<Visibility, ArgsType extends DefaultFunctionArgs ? ArgsType : DefaultFunctionArgs, Promise<ReturnsType>> : Builder extends ActionBuilder<any, infer Visibility> ? RegisteredAction<Visibility, ArgsType extends DefaultFunctionArgs ? ArgsType : DefaultFunctionArgs, Promise<ReturnsType>> : Builder extends CustomBuilder<'query', any, any, any, any, infer V, any> ? RegisteredQuery<V, ArgsType extends DefaultFunctionArgs ? ArgsType : DefaultFunctionArgs, Promise<ReturnsType>> : Builder extends CustomBuilder<'mutation', any, any, any, any, infer V, any> ? RegisteredMutation<V, ArgsType extends DefaultFunctionArgs ? ArgsType : DefaultFunctionArgs, Promise<ReturnsType>> : Builder extends CustomBuilder<'action', any, any, any, any, infer V, any> ? RegisteredAction<V, ArgsType extends DefaultFunctionArgs ? ArgsType : DefaultFunctionArgs, Promise<ReturnsType>> : Builder extends (...args: any[]) => any ? ReturnType<Builder> : never;
|
|
43
|
+
type ZodToConvexArgs<A> = A extends z.ZodObject<infer Shape> ? {
|
|
44
|
+
[K in keyof Shape]: any;
|
|
45
|
+
} : A extends Record<string, z.ZodTypeAny> ? {
|
|
46
|
+
[K in keyof A]: any;
|
|
47
|
+
} : A extends z.ZodTypeAny ? {
|
|
48
|
+
value: any;
|
|
49
|
+
} : Record<string, never>;
|
|
50
|
+
|
|
51
|
+
declare function zQuery<Builder extends (fn: any) => any, A extends z.ZodTypeAny | Record<string, z.ZodTypeAny>, R extends z.ZodTypeAny | undefined = undefined>(query: Builder, input: A, handler: (ctx: ExtractCtx<Builder>, args: InferArgs<A>) => Promise<InferReturns<R>> | InferReturns<R>, options?: {
|
|
52
|
+
returns?: R;
|
|
53
|
+
}): PreserveReturnType<Builder, ZodToConvexArgs<A>, InferReturns<R>>;
|
|
54
|
+
declare function zInternalQuery<Builder extends (fn: any) => any, A extends z.ZodTypeAny | Record<string, z.ZodTypeAny>, R extends z.ZodTypeAny | undefined = undefined>(internalQuery: Builder, input: A, handler: (ctx: ExtractCtx<Builder>, args: InferArgs<A>) => Promise<InferReturns<R>> | InferReturns<R>, options?: {
|
|
55
|
+
returns?: R;
|
|
56
|
+
}): PreserveReturnType<Builder, ZodToConvexArgs<A>, InferReturns<R>>;
|
|
57
|
+
declare function zMutation<Builder extends (fn: any) => any, A extends z.ZodTypeAny | Record<string, z.ZodTypeAny>, R extends z.ZodTypeAny | undefined = undefined>(mutation: Builder, input: A, handler: (ctx: ExtractCtx<Builder>, args: InferArgs<A>) => Promise<InferReturns<R>> | InferReturns<R>, options?: {
|
|
58
|
+
returns?: R;
|
|
59
|
+
}): PreserveReturnType<Builder, ZodToConvexArgs<A>, InferReturns<R>>;
|
|
60
|
+
declare function zInternalMutation<Builder extends (fn: any) => any, A extends z.ZodTypeAny | Record<string, z.ZodTypeAny>, R extends z.ZodTypeAny | undefined = undefined>(internalMutation: Builder, input: A, handler: (ctx: ExtractCtx<Builder>, args: InferArgs<A>) => Promise<InferReturns<R>> | InferReturns<R>, options?: {
|
|
61
|
+
returns?: R;
|
|
62
|
+
}): PreserveReturnType<Builder, ZodToConvexArgs<A>, InferReturns<R>>;
|
|
63
|
+
declare function zAction<Builder extends (fn: any) => any, A extends z.ZodTypeAny | Record<string, z.ZodTypeAny>, R extends z.ZodTypeAny | undefined = undefined>(action: Builder, input: A, handler: (ctx: ExtractCtx<Builder>, args: InferArgs<A>) => Promise<InferReturns<R>> | InferReturns<R>, options?: {
|
|
64
|
+
returns?: R;
|
|
65
|
+
}): PreserveReturnType<Builder, ZodToConvexArgs<A>, InferReturns<R>>;
|
|
66
|
+
declare function zInternalAction<Builder extends (fn: any) => any, A extends z.ZodTypeAny | Record<string, z.ZodTypeAny>, R extends z.ZodTypeAny | undefined = undefined>(internalAction: Builder, input: A, handler: (ctx: ExtractCtx<Builder>, args: InferArgs<A>) => Promise<InferReturns<R>> | InferReturns<R>, options?: {
|
|
67
|
+
returns?: R;
|
|
68
|
+
}): PreserveReturnType<Builder, ZodToConvexArgs<A>, InferReturns<R>>;
|
|
69
|
+
|
|
70
|
+
declare function zCustomQuery<CustomArgsValidator extends PropertyValidators, CustomCtx extends Record<string, any>, CustomMadeArgs extends Record<string, any>, Visibility extends FunctionVisibility, DataModel extends GenericDataModel>(query: any, customization: Customization<GenericQueryCtx<DataModel>, CustomArgsValidator, CustomCtx, CustomMadeArgs>): CustomBuilder<"query", CustomArgsValidator, CustomCtx, CustomMadeArgs, GenericQueryCtx<DataModel>, Visibility, Record<string, any>>;
|
|
71
|
+
declare function zCustomMutation<CustomArgsValidator extends PropertyValidators, CustomCtx extends Record<string, any>, CustomMadeArgs extends Record<string, any>, Visibility extends FunctionVisibility, DataModel extends GenericDataModel>(mutation: any, customization: Customization<GenericMutationCtx<DataModel>, CustomArgsValidator, CustomCtx, CustomMadeArgs>): CustomBuilder<"mutation", CustomArgsValidator, CustomCtx, CustomMadeArgs, GenericMutationCtx<DataModel>, Visibility, Record<string, any>>;
|
|
72
|
+
declare function zCustomAction<CustomArgsValidator extends PropertyValidators, CustomCtx extends Record<string, any>, CustomMadeArgs extends Record<string, any>, Visibility extends FunctionVisibility, DataModel extends GenericDataModel>(action: any, customization: Customization<GenericActionCtx<DataModel>, CustomArgsValidator, CustomCtx, CustomMadeArgs>): CustomBuilder<"action", CustomArgsValidator, CustomCtx, CustomMadeArgs, GenericActionCtx<DataModel>, Visibility, Record<string, any>>;
|
|
73
|
+
|
|
74
|
+
declare function zodTable<T extends z.ZodObject<any>, TableName extends string>(name: TableName, schema: T): {
|
|
75
|
+
codec: ConvexCodec<any>;
|
|
76
|
+
schema: T;
|
|
77
|
+
name: TableName;
|
|
78
|
+
table: convex_server.TableDefinition<convex_values.VObject<{
|
|
79
|
+
[x: string]: any;
|
|
80
|
+
[x: number]: any;
|
|
81
|
+
[x: symbol]: any;
|
|
82
|
+
}, any, "required", string>, {}, {}, {}>;
|
|
83
|
+
doc: convex_values.VObject<{
|
|
84
|
+
[x: string]: any;
|
|
85
|
+
[x: number]: any;
|
|
86
|
+
[x: symbol]: any;
|
|
87
|
+
}, {
|
|
88
|
+
[x: string]: any;
|
|
89
|
+
}, "required", string>;
|
|
90
|
+
withoutSystemFields: any;
|
|
91
|
+
withSystemFields: {
|
|
92
|
+
[x: string]: any;
|
|
93
|
+
};
|
|
94
|
+
systemFields: {
|
|
95
|
+
_id: convex_values.VId<convex_values.GenericId<TableName>, "required">;
|
|
96
|
+
_creationTime: VFloat64<number, "required">;
|
|
97
|
+
};
|
|
98
|
+
_id: convex_values.VId<convex_values.GenericId<TableName>, "required">;
|
|
99
|
+
};
|
|
100
|
+
declare function zCrud<TableDefinition extends ReturnType<typeof zodTable>, QueryBuilder extends (fn: any) => any, MutationBuilder extends (fn: any) => any>(table: TableDefinition, queryBuilder: QueryBuilder, mutationBuilder: MutationBuilder): {
|
|
101
|
+
create: PreserveReturnType<MutationBuilder, {
|
|
102
|
+
[x: string]: any;
|
|
103
|
+
}, any>;
|
|
104
|
+
read: PreserveReturnType<QueryBuilder, {
|
|
105
|
+
id: any;
|
|
106
|
+
}, any>;
|
|
107
|
+
paginate: any;
|
|
108
|
+
update: PreserveReturnType<MutationBuilder, {
|
|
109
|
+
id: any;
|
|
110
|
+
patch: any;
|
|
111
|
+
}, any>;
|
|
112
|
+
destroy: PreserveReturnType<MutationBuilder, {
|
|
113
|
+
id: any;
|
|
114
|
+
}, any>;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Minimal compatibility for convex-helpers/server/zodV4 imports
|
|
119
|
+
* This library itself IS the Zod v4 compatibility layer
|
|
120
|
+
*/
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Create a Zod validator for a Convex Id
|
|
124
|
+
*/
|
|
125
|
+
declare function zid<TableName extends string>(tableName: TableName): z.ZodTypeAny;
|
|
126
|
+
type Zid<TableName extends string> = ReturnType<typeof zid<TableName>>;
|
|
127
|
+
|
|
128
|
+
export { type ConvexCodec, type ExtractCtx, type InferArgs, type InferReturns, type PreserveReturnType, type Zid, type ZodToConvexArgs, analyzeZod, convexCodec, fromConvexJS, getObjectShape, makeUnion, simpleToConvex, toConvexJS, zAction, zCrud, zCustomAction, zCustomMutation, zCustomQuery, zInternalAction, zInternalMutation, zInternalQuery, zMutation, zQuery, zid, zodTable, zodToConvex, zodToConvexFields };
|