fluent-convex 0.7.2 → 0.8.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.
@@ -1,129 +1,95 @@
1
1
  import type { GenericDataModel } from "convex/server";
2
- import { InferredArgs } from "./types";
3
- import { InferHandlerReturn } from "./types";
4
2
  import { ConvexBuilderDef } from "./types";
5
- import { CallableBuilder } from "./types";
6
- import { ConvexBuilderWithHandler } from "./ConvexBuilderWithHandler";
7
- import type { ConvexMiddleware, AnyConvexMiddleware } from "./middleware";
3
+ import { ConvexBuilderWithFunctionKind } from "./ConvexBuilderWithFunctionKind";
4
+ import type { ConvexMiddleware } from "./middleware";
8
5
  import type {
9
- FunctionType,
10
- Context,
11
- EmptyObject,
12
- ConvexArgsValidator,
13
- ConvexReturnsValidator,
14
6
  QueryCtx,
15
7
  MutationCtx,
16
8
  ActionCtx,
9
+ Context,
10
+ EmptyObject,
17
11
  } from "./types";
18
- import {
19
- type ValidatorInput,
20
- type ToConvexArgsValidator,
21
- isZodSchema,
22
- toConvexValidator,
23
- type ReturnsValidatorInput,
24
- type ToConvexReturnsValidator,
25
- } from "./zod_support";
26
12
 
27
13
  export class ConvexBuilder<
28
14
  TDataModel extends GenericDataModel = GenericDataModel,
29
- TFunctionType extends FunctionType | undefined = undefined,
30
- TInitialContext extends Context = EmptyObject,
31
- TCurrentContext extends Context = EmptyObject,
32
- TArgsValidator extends ConvexArgsValidator | undefined = undefined,
33
- TReturnsValidator extends ConvexReturnsValidator | undefined = undefined,
34
15
  > {
35
- protected def: ConvexBuilderDef<
36
- TFunctionType,
37
- TArgsValidator,
38
- TReturnsValidator
39
- >;
16
+ private def: ConvexBuilderDef<undefined, undefined, undefined, "public">;
40
17
 
41
18
  constructor(
42
- def: ConvexBuilderDef<TFunctionType, TArgsValidator, TReturnsValidator>
19
+ def: ConvexBuilderDef<undefined, undefined, undefined, "public">
43
20
  ) {
44
21
  this.def = def;
45
22
  }
46
23
 
47
- query(
48
- this: ConvexBuilder<
49
- TDataModel,
50
- undefined,
51
- TInitialContext,
52
- TCurrentContext,
53
- TArgsValidator,
54
- TReturnsValidator
55
- >
56
- ): ConvexBuilder<
24
+ query(): ConvexBuilderWithFunctionKind<
57
25
  TDataModel,
58
26
  "query",
59
27
  QueryCtx<TDataModel>,
60
28
  QueryCtx<TDataModel>
61
29
  > {
62
- return new ConvexBuilder({
30
+ return new ConvexBuilderWithFunctionKind<
31
+ TDataModel,
32
+ "query",
33
+ QueryCtx<TDataModel>,
34
+ QueryCtx<TDataModel>
35
+ >({
63
36
  ...this.def,
64
37
  functionType: "query",
65
- }) as any;
38
+ });
66
39
  }
67
40
 
68
- mutation(
69
- this: ConvexBuilder<
70
- TDataModel,
71
- undefined,
72
- TInitialContext,
73
- TCurrentContext,
74
- TArgsValidator,
75
- TReturnsValidator
76
- >
77
- ): ConvexBuilder<
41
+ mutation(): ConvexBuilderWithFunctionKind<
78
42
  TDataModel,
79
43
  "mutation",
80
44
  MutationCtx<TDataModel>,
81
45
  MutationCtx<TDataModel>
82
46
  > {
83
- return new ConvexBuilder({
47
+ return new ConvexBuilderWithFunctionKind<
48
+ TDataModel,
49
+ "mutation",
50
+ MutationCtx<TDataModel>,
51
+ MutationCtx<TDataModel>
52
+ >({
84
53
  ...this.def,
85
54
  functionType: "mutation",
86
- }) as any;
55
+ });
87
56
  }
88
57
 
89
- action(
90
- this: ConvexBuilder<
91
- TDataModel,
92
- undefined,
93
- TInitialContext,
94
- TCurrentContext,
95
- TArgsValidator,
96
- TReturnsValidator
97
- >
98
- ): ConvexBuilder<
58
+ action(): ConvexBuilderWithFunctionKind<
99
59
  TDataModel,
100
60
  "action",
101
61
  ActionCtx<TDataModel>,
102
62
  ActionCtx<TDataModel>
103
63
  > {
104
- return new ConvexBuilder({
64
+ return new ConvexBuilderWithFunctionKind<
65
+ TDataModel,
66
+ "action",
67
+ ActionCtx<TDataModel>,
68
+ ActionCtx<TDataModel>
69
+ >({
105
70
  ...this.def,
106
71
  functionType: "action",
107
- }) as any;
72
+ });
108
73
  }
109
74
 
110
- $context<U extends Context>(): ConvexBuilder<
111
- TDataModel,
112
- TFunctionType,
113
- U & EmptyObject,
114
- U,
115
- TArgsValidator,
116
- TReturnsValidator
117
- > {
118
- return new ConvexBuilder({
119
- ...this.def,
120
- middlewares: [],
121
- }) as any;
75
+ $context<U extends Context>(): {
76
+ middleware<UOutContext extends Context>(
77
+ middleware: ConvexMiddleware<U, UOutContext>
78
+ ): ConvexMiddleware<U, UOutContext>;
79
+ } {
80
+ // Return an object that allows middleware creation with a specific context type
81
+ return {
82
+ middleware<UOutContext extends Context>(
83
+ middleware: ConvexMiddleware<U, UOutContext>
84
+ ): ConvexMiddleware<U, UOutContext> {
85
+ return middleware;
86
+ },
87
+ };
122
88
  }
123
89
 
124
90
  middleware<UOutContext extends Context>(
125
- middleware: ConvexMiddleware<TInitialContext, UOutContext>
126
- ): ConvexMiddleware<TInitialContext, UOutContext>;
91
+ middleware: ConvexMiddleware<EmptyObject, UOutContext>
92
+ ): ConvexMiddleware<EmptyObject, UOutContext>;
127
93
  middleware<UInContext extends Context, UOutContext extends Context>(
128
94
  middleware: ConvexMiddleware<UInContext, UOutContext>
129
95
  ): ConvexMiddleware<UInContext, UOutContext>;
@@ -132,146 +98,4 @@ export class ConvexBuilder<
132
98
  ): ConvexMiddleware<UInContext, UOutContext> {
133
99
  return middleware;
134
100
  }
135
-
136
- use<UOutContext extends Context>(
137
- this: ConvexBuilder<
138
- TDataModel,
139
- FunctionType,
140
- TInitialContext,
141
- TCurrentContext,
142
- TArgsValidator,
143
- TReturnsValidator
144
- >,
145
- middleware: ConvexMiddleware<TCurrentContext, UOutContext>
146
- ): ConvexBuilder<
147
- TDataModel,
148
- TFunctionType,
149
- TInitialContext,
150
- TCurrentContext & UOutContext,
151
- TArgsValidator,
152
- TReturnsValidator
153
- > {
154
- return new ConvexBuilder({
155
- ...this.def,
156
- middlewares: [...this.def.middlewares, middleware as AnyConvexMiddleware],
157
- }) as any;
158
- }
159
-
160
- input<UInput extends ValidatorInput>(
161
- this: ConvexBuilder<
162
- TDataModel,
163
- FunctionType,
164
- TInitialContext,
165
- TCurrentContext,
166
- TArgsValidator,
167
- TReturnsValidator
168
- >,
169
- validator: UInput
170
- ): ConvexBuilder<
171
- TDataModel,
172
- TFunctionType,
173
- TInitialContext,
174
- TCurrentContext,
175
- ToConvexArgsValidator<UInput>,
176
- TReturnsValidator
177
- > {
178
- const convexValidator = (
179
- isZodSchema(validator) ? toConvexValidator(validator) : validator
180
- ) as ToConvexArgsValidator<UInput>;
181
-
182
- return new ConvexBuilder({
183
- ...this.def,
184
- argsValidator: convexValidator,
185
- }) as any;
186
- }
187
-
188
- returns<UReturns extends ReturnsValidatorInput>(
189
- this: ConvexBuilder<
190
- TDataModel,
191
- FunctionType,
192
- TInitialContext,
193
- TCurrentContext,
194
- TArgsValidator,
195
- TReturnsValidator
196
- >,
197
- validator: UReturns
198
- ): ConvexBuilder<
199
- TDataModel,
200
- TFunctionType,
201
- TInitialContext,
202
- TCurrentContext,
203
- TArgsValidator,
204
- ToConvexReturnsValidator<UReturns>
205
- > {
206
- const convexValidator = (
207
- isZodSchema(validator) ? toConvexValidator(validator) : validator
208
- ) as ToConvexReturnsValidator<UReturns>;
209
-
210
- return new ConvexBuilder({
211
- ...this.def,
212
- returnsValidator: convexValidator,
213
- }) as any;
214
- }
215
-
216
- handler<
217
- TReturn extends InferHandlerReturn<
218
- TReturnsValidator,
219
- any
220
- > = InferHandlerReturn<TReturnsValidator, any>,
221
- >(
222
- this: ConvexBuilder<
223
- TDataModel,
224
- FunctionType,
225
- TInitialContext,
226
- TCurrentContext,
227
- TArgsValidator,
228
- TReturnsValidator
229
- >,
230
- handlerFn: (options: {
231
- context: TCurrentContext;
232
- input: InferredArgs<TArgsValidator>;
233
- }) => Promise<TReturn>
234
- ): ConvexBuilderWithHandler<
235
- TDataModel,
236
- TFunctionType & FunctionType,
237
- TCurrentContext,
238
- TArgsValidator,
239
- TReturnsValidator,
240
- InferHandlerReturn<TReturnsValidator, TReturn>
241
- > &
242
- CallableBuilder<
243
- TCurrentContext,
244
- TArgsValidator,
245
- InferHandlerReturn<TReturnsValidator, TReturn>
246
- > {
247
- if (this.def.handler) {
248
- throw new Error(
249
- "Handler already defined. Only one handler can be set per function chain."
250
- );
251
- }
252
-
253
- const rawHandler = async (
254
- transformedCtx: Context,
255
- baseArgs: InferredArgs<TArgsValidator>
256
- ) => {
257
- return handlerFn({
258
- context: transformedCtx as TCurrentContext,
259
- input: baseArgs,
260
- });
261
- };
262
-
263
- type InferredReturn = InferHandlerReturn<TReturnsValidator, TReturn>;
264
-
265
- return new ConvexBuilderWithHandler<
266
- TDataModel,
267
- TFunctionType & FunctionType,
268
- TCurrentContext,
269
- TArgsValidator,
270
- TReturnsValidator,
271
- InferredReturn
272
- >({
273
- ...this.def,
274
- handler: rawHandler as any,
275
- } as any) as any;
276
- }
277
101
  }
@@ -0,0 +1,262 @@
1
+ import type { GenericDataModel } from "convex/server";
2
+ import { ConvexBuilderWithHandler } from "./ConvexBuilderWithHandler";
3
+ import { InferredArgs } from "./types";
4
+ import { ExpectedReturnType } from "./types";
5
+ import { ConvexBuilderDef } from "./types";
6
+ import { CallableBuilder } from "./types";
7
+ import type { ConvexMiddleware, AnyConvexMiddleware } from "./middleware";
8
+ import type {
9
+ FunctionType,
10
+ Context,
11
+ EmptyObject,
12
+ ConvexArgsValidator,
13
+ ConvexReturnsValidator,
14
+ Visibility,
15
+ } from "./types";
16
+ import {
17
+ type ValidatorInput,
18
+ type ToConvexArgsValidator,
19
+ isZodSchema,
20
+ toConvexValidator,
21
+ type ReturnsValidatorInput,
22
+ type ToConvexReturnsValidator,
23
+ } from "./zod_support";
24
+
25
+ export class ConvexBuilderWithFunctionKind<
26
+ TDataModel extends GenericDataModel = GenericDataModel,
27
+ TFunctionType extends FunctionType = FunctionType,
28
+ TInitialContext extends Context = EmptyObject,
29
+ TCurrentContext extends Context = EmptyObject,
30
+ TArgsValidator extends ConvexArgsValidator | undefined = undefined,
31
+ TReturnsValidator extends ConvexReturnsValidator | undefined = undefined,
32
+ TVisibility extends Visibility = "public",
33
+ THasHandler extends boolean = false,
34
+ THandlerReturn = any,
35
+ > {
36
+ protected def: ConvexBuilderDef<
37
+ TFunctionType,
38
+ TArgsValidator,
39
+ TReturnsValidator,
40
+ TVisibility
41
+ >;
42
+
43
+ constructor(
44
+ def: ConvexBuilderDef<
45
+ TFunctionType,
46
+ TArgsValidator,
47
+ TReturnsValidator,
48
+ TVisibility
49
+ >
50
+ ) {
51
+ this.def = def;
52
+ }
53
+
54
+ $context<U extends Context>(): ConvexBuilderWithFunctionKind<
55
+ TDataModel,
56
+ TFunctionType,
57
+ U & EmptyObject,
58
+ U,
59
+ TArgsValidator,
60
+ TReturnsValidator,
61
+ TVisibility,
62
+ THasHandler,
63
+ THandlerReturn
64
+ > {
65
+ return new ConvexBuilderWithFunctionKind<
66
+ TDataModel,
67
+ TFunctionType,
68
+ U & EmptyObject,
69
+ U,
70
+ TArgsValidator,
71
+ TReturnsValidator,
72
+ TVisibility,
73
+ THasHandler,
74
+ THandlerReturn
75
+ >({
76
+ ...this.def,
77
+ middlewares: [],
78
+ });
79
+ }
80
+
81
+ middleware<UOutContext extends Context>(
82
+ middleware: ConvexMiddleware<TInitialContext, UOutContext>
83
+ ): ConvexMiddleware<TInitialContext, UOutContext>;
84
+ middleware<UInContext extends Context, UOutContext extends Context>(
85
+ middleware: ConvexMiddleware<UInContext, UOutContext>
86
+ ): ConvexMiddleware<UInContext, UOutContext>;
87
+ middleware<UInContext extends Context, UOutContext extends Context>(
88
+ middleware: ConvexMiddleware<UInContext, UOutContext>
89
+ ): ConvexMiddleware<UInContext, UOutContext> {
90
+ return middleware;
91
+ }
92
+
93
+ use<UOutContext extends Context>(
94
+ middleware: ConvexMiddleware<TCurrentContext, UOutContext>
95
+ ): ConvexBuilderWithFunctionKind<
96
+ TDataModel,
97
+ TFunctionType,
98
+ TInitialContext,
99
+ TCurrentContext & UOutContext,
100
+ TArgsValidator,
101
+ TReturnsValidator,
102
+ TVisibility,
103
+ THasHandler,
104
+ THandlerReturn
105
+ > {
106
+ return new ConvexBuilderWithFunctionKind<
107
+ TDataModel,
108
+ TFunctionType,
109
+ TInitialContext,
110
+ TCurrentContext & UOutContext,
111
+ TArgsValidator,
112
+ TReturnsValidator,
113
+ TVisibility,
114
+ THasHandler,
115
+ THandlerReturn
116
+ >({
117
+ ...this.def,
118
+ middlewares: [...this.def.middlewares, middleware as AnyConvexMiddleware],
119
+ });
120
+ }
121
+
122
+ input<UInput extends ValidatorInput>(
123
+ validator: UInput
124
+ ): ConvexBuilderWithFunctionKind<
125
+ TDataModel,
126
+ TFunctionType,
127
+ TInitialContext,
128
+ TCurrentContext,
129
+ ToConvexArgsValidator<UInput>,
130
+ TReturnsValidator,
131
+ TVisibility,
132
+ THasHandler,
133
+ THandlerReturn
134
+ > {
135
+ const convexValidator = isZodSchema(validator)
136
+ ? (toConvexValidator(validator) as ToConvexArgsValidator<UInput>)
137
+ : (validator as ToConvexArgsValidator<UInput>);
138
+
139
+ return new ConvexBuilderWithFunctionKind<
140
+ TDataModel,
141
+ TFunctionType,
142
+ TInitialContext,
143
+ TCurrentContext,
144
+ ToConvexArgsValidator<UInput>,
145
+ TReturnsValidator,
146
+ TVisibility,
147
+ THasHandler,
148
+ THandlerReturn
149
+ >({
150
+ ...this.def,
151
+ argsValidator: convexValidator,
152
+ });
153
+ }
154
+
155
+ returns<UReturns extends ReturnsValidatorInput>(
156
+ validator: UReturns
157
+ ): ConvexBuilderWithFunctionKind<
158
+ TDataModel,
159
+ TFunctionType,
160
+ TInitialContext,
161
+ TCurrentContext,
162
+ TArgsValidator,
163
+ ToConvexReturnsValidator<UReturns>,
164
+ TVisibility,
165
+ THasHandler,
166
+ THandlerReturn
167
+ > {
168
+ const convexValidator = isZodSchema(validator)
169
+ ? (toConvexValidator(validator) as ToConvexReturnsValidator<UReturns>)
170
+ : (validator as ToConvexReturnsValidator<UReturns>);
171
+
172
+ return new ConvexBuilderWithFunctionKind<
173
+ TDataModel,
174
+ TFunctionType,
175
+ TInitialContext,
176
+ TCurrentContext,
177
+ TArgsValidator,
178
+ ToConvexReturnsValidator<UReturns>,
179
+ TVisibility,
180
+ THasHandler,
181
+ THandlerReturn
182
+ >({
183
+ ...this.def,
184
+ returnsValidator: convexValidator,
185
+ });
186
+ }
187
+
188
+ handler<
189
+ TReturn extends [TReturnsValidator] extends [ConvexReturnsValidator]
190
+ ? ExpectedReturnType<TReturnsValidator>
191
+ : any = [TReturnsValidator] extends [ConvexReturnsValidator]
192
+ ? ExpectedReturnType<TReturnsValidator>
193
+ : any,
194
+ >(
195
+ handlerFn: (
196
+ context: TCurrentContext,
197
+ input: InferredArgs<TArgsValidator>
198
+ ) => Promise<TReturn>
199
+ ): ConvexBuilderWithHandler<
200
+ TDataModel,
201
+ TFunctionType,
202
+ TInitialContext,
203
+ TCurrentContext,
204
+ TArgsValidator,
205
+ TReturnsValidator,
206
+ TVisibility,
207
+ [TReturnsValidator] extends [ConvexReturnsValidator]
208
+ ? ExpectedReturnType<TReturnsValidator>
209
+ : TReturn
210
+ > &
211
+ CallableBuilder<
212
+ TCurrentContext,
213
+ TArgsValidator,
214
+ [TReturnsValidator] extends [ConvexReturnsValidator]
215
+ ? ExpectedReturnType<TReturnsValidator>
216
+ : TReturn
217
+ > {
218
+ if (this.def.handler) {
219
+ throw new Error(
220
+ "Handler already defined. Only one handler can be set per function chain."
221
+ );
222
+ }
223
+
224
+ // Store the raw handler function - it will be composed with all middlewares
225
+ // (including those added after .handler()) when .public() or .internal() is called
226
+ // The handler signature matches what Convex expects: (ctx, args) => Promise<return>
227
+ const rawHandler = async (
228
+ transformedCtx: Context,
229
+ baseArgs: InferredArgs<TArgsValidator>
230
+ ) => {
231
+ return handlerFn(transformedCtx as TCurrentContext, baseArgs);
232
+ };
233
+
234
+ type InferredReturn = [TReturnsValidator] extends [ConvexReturnsValidator]
235
+ ? ExpectedReturnType<TReturnsValidator>
236
+ : TReturn;
237
+
238
+ return new ConvexBuilderWithHandler<
239
+ TDataModel,
240
+ TFunctionType,
241
+ TInitialContext,
242
+ TCurrentContext,
243
+ TArgsValidator,
244
+ TReturnsValidator,
245
+ TVisibility,
246
+ InferredReturn
247
+ >({
248
+ ...this.def,
249
+ handler: rawHandler as any,
250
+ }) as ConvexBuilderWithHandler<
251
+ TDataModel,
252
+ TFunctionType,
253
+ TInitialContext,
254
+ TCurrentContext,
255
+ TArgsValidator,
256
+ TReturnsValidator,
257
+ TVisibility,
258
+ InferredReturn
259
+ > &
260
+ CallableBuilder<TCurrentContext, TArgsValidator, InferredReturn>;
261
+ }
262
+ }