@confect/core 10.0.0-next.15 → 10.0.0-next.18

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 (70) hide show
  1. package/CHANGELOG.md +246 -0
  2. package/dist/FunctionProvenance.d.ts +18 -18
  3. package/dist/FunctionProvenance.d.ts.map +1 -1
  4. package/dist/FunctionProvenance.js +1 -1
  5. package/dist/FunctionProvenance.js.map +1 -1
  6. package/dist/FunctionSpec.d.ts +54 -48
  7. package/dist/FunctionSpec.d.ts.map +1 -1
  8. package/dist/FunctionSpec.js +23 -5
  9. package/dist/FunctionSpec.js.map +1 -1
  10. package/dist/GroupPath.d.ts +1 -4
  11. package/dist/GroupPath.d.ts.map +1 -1
  12. package/dist/GroupPath.js.map +1 -1
  13. package/dist/GroupSpec.d.ts +16 -10
  14. package/dist/GroupSpec.d.ts.map +1 -1
  15. package/dist/GroupSpec.js +34 -11
  16. package/dist/GroupSpec.js.map +1 -1
  17. package/dist/Lazy.d.ts +2 -3
  18. package/dist/Lazy.d.ts.map +1 -1
  19. package/dist/Lazy.js +2 -3
  20. package/dist/Lazy.js.map +1 -1
  21. package/dist/MiddlewareSpec.d.ts +184 -0
  22. package/dist/MiddlewareSpec.d.ts.map +1 -0
  23. package/dist/MiddlewareSpec.js +55 -0
  24. package/dist/MiddlewareSpec.js.map +1 -0
  25. package/dist/Ref.d.ts +53 -37
  26. package/dist/Ref.d.ts.map +1 -1
  27. package/dist/Ref.js +53 -35
  28. package/dist/Ref.js.map +1 -1
  29. package/dist/Refs.d.ts +8 -3
  30. package/dist/Refs.d.ts.map +1 -1
  31. package/dist/Refs.js +3 -3
  32. package/dist/Refs.js.map +1 -1
  33. package/dist/SchemaToValidator.d.ts +99 -0
  34. package/dist/SchemaToValidator.d.ts.map +1 -0
  35. package/dist/SchemaToValidator.js +156 -0
  36. package/dist/SchemaToValidator.js.map +1 -0
  37. package/dist/Spec.d.ts +3 -2
  38. package/dist/Spec.d.ts.map +1 -1
  39. package/dist/Spec.js +1 -1
  40. package/dist/Spec.js.map +1 -1
  41. package/dist/Table.d.ts +74 -0
  42. package/dist/Table.d.ts.map +1 -0
  43. package/dist/Table.js +77 -0
  44. package/dist/Table.js.map +1 -0
  45. package/dist/Types.d.ts +8 -2
  46. package/dist/Types.d.ts.map +1 -1
  47. package/dist/Types.js.map +1 -1
  48. package/dist/index.d.ts +3 -1
  49. package/dist/index.d.ts.map +1 -1
  50. package/dist/index.js +4 -2
  51. package/package.json +9 -8
  52. package/src/FunctionProvenance.ts +8 -8
  53. package/src/FunctionSpec.ts +93 -14
  54. package/src/GroupPath.ts +1 -6
  55. package/src/GroupSpec.ts +125 -18
  56. package/src/Lazy.ts +2 -3
  57. package/src/MiddlewareSpec.ts +356 -0
  58. package/src/Ref.ts +310 -205
  59. package/src/Refs.ts +40 -16
  60. package/src/SchemaToValidator.ts +601 -0
  61. package/src/Spec.ts +4 -4
  62. package/src/Table.ts +426 -0
  63. package/src/Types.ts +9 -3
  64. package/src/index.ts +3 -1
  65. package/dist/Registry.d.ts +0 -24
  66. package/dist/Registry.d.ts.map +0 -1
  67. package/dist/Registry.js +0 -19
  68. package/dist/Registry.js.map +0 -1
  69. package/dist/tsconfig.src.tsbuildinfo +0 -1
  70. package/src/Registry.ts +0 -30
package/src/Ref.ts CHANGED
@@ -11,42 +11,114 @@ import * as Effect from "effect/Effect";
11
11
  import * as Match from "effect/Match";
12
12
  import * as Option from "effect/Option";
13
13
  import * as Schema from "effect/Schema";
14
+ import type * as FunctionProvenance from "./FunctionProvenance";
14
15
  import type * as FunctionSpec from "./FunctionSpec";
16
+ import * as Lazy from "./Lazy";
17
+ import * as MiddlewareSpec from "./MiddlewareSpec";
15
18
  import type * as RuntimeAndFunctionType from "./RuntimeAndFunctionType";
16
19
 
17
- export interface Ref<
18
- _RuntimeAndFunctionType extends RuntimeAndFunctionType.RuntimeAndFunctionType,
19
- _FunctionVisibility extends FunctionVisibility,
20
- _Args,
21
- _Returns,
22
- _Error = never,
20
+ export interface Base<
21
+ RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType,
22
+ FunctionVisibility_ extends FunctionVisibility,
23
+ Args_,
24
+ Returns_,
25
+ Error_ = never,
23
26
  > {
24
- readonly _RuntimeAndFunctionType?: _RuntimeAndFunctionType;
25
- readonly _FunctionVisibility?: _FunctionVisibility;
26
- readonly _Args?: _Args;
27
- readonly _Returns?: _Returns;
28
- readonly _Error?: _Error;
27
+ readonly "~RuntimeAndFunctionType": RuntimeAndFunctionType_;
28
+ readonly "~FunctionVisibility": FunctionVisibility_;
29
+ readonly "~Args": Args_;
30
+ readonly "~Returns": Returns_;
31
+ readonly "~Error": Error_;
29
32
  /** @internal */
30
- readonly functionSpec: FunctionSpec.AnyWithProps;
33
+ readonly convexFunctionName: string;
34
+ }
35
+
36
+ /**
37
+ * A reference to a single Convex function, as callers see it: the wire name
38
+ * plus the data needed to encode args, decode returns, and decode typed
39
+ * errors. A ref is one of two shapes, keyed by the spec's provenance.
40
+ */
41
+ export type Ref<
42
+ RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType,
43
+ FunctionVisibility_ extends FunctionVisibility,
44
+ Args_,
45
+ Returns_,
46
+ Error_ = never,
47
+ > =
48
+ | ConfectRef<
49
+ RuntimeAndFunctionType_,
50
+ FunctionVisibility_,
51
+ Args_,
52
+ Returns_,
53
+ Error_
54
+ >
55
+ | ConvexRef<
56
+ RuntimeAndFunctionType_,
57
+ FunctionVisibility_,
58
+ Args_,
59
+ Returns_,
60
+ Error_
61
+ >;
62
+
63
+ export interface ConfectRef<
64
+ RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType,
65
+ FunctionVisibility_ extends FunctionVisibility,
66
+ Args_,
67
+ Returns_,
68
+ Error_ = never,
69
+ > extends Base<
70
+ RuntimeAndFunctionType_,
71
+ FunctionVisibility_,
72
+ Args_,
73
+ Returns_,
74
+ Error_
75
+ > {
76
+ /** @internal */
77
+ readonly _tag: "Confect";
78
+ /** @internal */
79
+ readonly args: Schema.Codec<any, any>;
80
+ /** @internal */
81
+ readonly returns: Schema.Codec<any, any>;
82
+ /** @internal */
83
+ readonly kind: FunctionProvenance.ConfectKind;
31
84
  /** @internal */
32
- readonly functionNamespace: string;
85
+ readonly middlewareSpecs: ReadonlyArray<MiddlewareSpec.AnyMiddlewareSpec>;
86
+ /** @internal */
87
+ readonly error?: Schema.Codec<any, any>;
88
+ }
89
+
90
+ export interface ConvexRef<
91
+ RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType,
92
+ FunctionVisibility_ extends FunctionVisibility,
93
+ Args_,
94
+ Returns_,
95
+ Error_ = never,
96
+ > extends Base<
97
+ RuntimeAndFunctionType_,
98
+ FunctionVisibility_,
99
+ Args_,
100
+ Returns_,
101
+ Error_
102
+ > {
103
+ /** @internal */
104
+ readonly _tag: "Convex";
33
105
  }
34
106
 
35
- export interface Any extends Ref<any, any, any, any, any> {}
107
+ export type Any = Ref<any, any, any, any, any>;
36
108
 
37
- export interface AnyInternal extends Ref<any, "internal", any, any, any> {}
109
+ export type AnyInternal = Ref<any, "internal", any, any, any>;
38
110
 
39
- export interface AnyPublic extends Ref<any, "public", any, any, any> {}
111
+ export type AnyPublic = Ref<any, "public", any, any, any>;
40
112
 
41
- export interface AnyQuery extends Ref<
113
+ export type AnyQuery = Ref<
42
114
  RuntimeAndFunctionType.AnyQuery,
43
115
  FunctionVisibility,
44
116
  any,
45
117
  any,
46
118
  any
47
- > {}
119
+ >;
48
120
 
49
- export interface AnyPublicPaginatedQuery extends Ref<
121
+ export type AnyPublicPaginatedQuery = Ref<
50
122
  RuntimeAndFunctionType.AnyQuery,
51
123
  "public",
52
124
  {
@@ -55,142 +127,139 @@ export interface AnyPublicPaginatedQuery extends Ref<
55
127
  },
56
128
  PaginationResult<any>,
57
129
  any
58
- > {}
130
+ >;
59
131
 
60
- export interface AnyMutation extends Ref<
132
+ export type AnyMutation = Ref<
61
133
  RuntimeAndFunctionType.AnyMutation,
62
134
  FunctionVisibility,
63
135
  any,
64
136
  any,
65
137
  any
66
- > {}
138
+ >;
67
139
 
68
- export interface AnyAction extends Ref<
140
+ export type AnyAction = Ref<
69
141
  RuntimeAndFunctionType.AnyAction,
70
142
  FunctionVisibility,
71
143
  any,
72
144
  any,
73
145
  any
74
- > {}
146
+ >;
75
147
 
76
- export interface AnyPublicQuery extends Ref<
148
+ export type AnyPublicQuery = Ref<
77
149
  RuntimeAndFunctionType.AnyQuery,
78
150
  "public",
79
151
  any,
80
152
  any,
81
153
  any
82
- > {}
154
+ >;
83
155
 
84
- export interface AnyPublicMutation extends Ref<
156
+ export type AnyPublicMutation = Ref<
85
157
  RuntimeAndFunctionType.AnyMutation,
86
158
  "public",
87
159
  any,
88
160
  any,
89
161
  any
90
- > {}
162
+ >;
91
163
 
92
- export interface AnyPublicAction extends Ref<
164
+ export type AnyPublicAction = Ref<
93
165
  RuntimeAndFunctionType.AnyAction,
94
166
  "public",
95
167
  any,
96
168
  any,
97
169
  any
98
- > {}
99
-
100
- export type GetRuntimeAndFunctionType<Ref_> =
101
- Ref_ extends Ref<
102
- infer RuntimeAndFunctionType_,
103
- infer _FunctionVisibility,
104
- infer _Args,
105
- infer _Returns,
106
- infer _Error
107
- >
108
- ? RuntimeAndFunctionType_
109
- : never;
110
-
111
- export type GetRuntime<Ref_> =
112
- Ref_ extends Ref<
113
- infer RuntimeAndFunctionType_,
114
- infer _FunctionVisibility,
115
- infer _Args,
116
- infer _Returns,
117
- infer _Error
118
- >
119
- ? RuntimeAndFunctionType.GetRuntime<RuntimeAndFunctionType_>
120
- : never;
121
-
122
- export type GetFunctionType<Ref_> =
123
- Ref_ extends Ref<
124
- infer RuntimeAndFunctionType_,
125
- infer _FunctionVisibility,
126
- infer _Args,
127
- infer _Returns,
128
- infer _Error
129
- >
130
- ? RuntimeAndFunctionType.GetFunctionType<RuntimeAndFunctionType_>
131
- : never;
132
-
133
- export type GetFunctionVisibility<Ref_> =
134
- Ref_ extends Ref<
135
- infer _RuntimeAndFunctionType,
136
- infer FunctionVisibility_,
137
- infer _Args,
138
- infer _Returns,
139
- infer _Error
140
- >
141
- ? FunctionVisibility_
142
- : never;
143
-
144
- export type Args<Ref_> =
145
- Ref_ extends Ref<
146
- infer _RuntimeAndFunctionType,
147
- infer _FunctionVisibility,
148
- infer Args_,
149
- infer _Returns,
150
- infer _Error
151
- >
152
- ? Args_
153
- : never;
170
+ >;
171
+
172
+ export type GetRuntimeAndFunctionType<Ref_> = Ref_ extends {
173
+ readonly "~RuntimeAndFunctionType": infer RuntimeAndFunctionType_ extends
174
+ RuntimeAndFunctionType.RuntimeAndFunctionType;
175
+ }
176
+ ? RuntimeAndFunctionType_
177
+ : never;
178
+
179
+ export type GetRuntime<Ref_> = RuntimeAndFunctionType.GetRuntime<
180
+ GetRuntimeAndFunctionType<Ref_>
181
+ >;
182
+
183
+ export type GetFunctionType<Ref_> = RuntimeAndFunctionType.GetFunctionType<
184
+ GetRuntimeAndFunctionType<Ref_>
185
+ >;
186
+
187
+ export type GetFunctionVisibility<Ref_> = Ref_ extends {
188
+ readonly "~FunctionVisibility": infer FunctionVisibility_;
189
+ }
190
+ ? FunctionVisibility_
191
+ : never;
192
+
193
+ export type Args<Ref_> = Ref_ extends { readonly "~Args": infer Args_ }
194
+ ? Args_
195
+ : never;
154
196
 
155
197
  export type OptionalArgs<Ref_ extends Any> = keyof Args<Ref_> extends never
156
198
  ? [args?: Args<Ref_>]
157
199
  : [args: Args<Ref_>];
158
200
 
159
- export type Returns<Ref_> =
160
- Ref_ extends Ref<
161
- infer _RuntimeAndFunctionType,
162
- infer _FunctionVisibility,
163
- infer _Args,
164
- infer Returns_,
165
- infer _Error
166
- >
167
- ? Returns_
168
- : never;
169
-
170
- export type Error<Ref_> =
171
- Ref_ extends Ref<
172
- infer _RuntimeAndFunctionType,
173
- infer _FunctionVisibility,
174
- infer _Args,
175
- infer _Returns,
176
- infer Error_
177
- >
178
- ? Error_
179
- : never;
201
+ export type Returns<Ref_> = Ref_ extends { readonly "~Returns": infer Returns_ }
202
+ ? Returns_
203
+ : never;
204
+
205
+ export type Error<Ref_> = Ref_ extends { readonly "~Error": infer Error_ }
206
+ ? Error_
207
+ : never;
180
208
 
181
209
  export type FunctionReference<Ref_ extends Any> = ConvexFunctionReference<
182
210
  GetFunctionType<Ref_>,
183
211
  GetFunctionVisibility<Ref_>
184
212
  >;
185
213
 
186
- export type FromFunctionSpec<FunctionSpec_ extends FunctionSpec.AnyWithProps> =
187
- Ref<
188
- FunctionSpec.GetRuntimeAndFunctionType<FunctionSpec_>,
189
- FunctionSpec.GetFunctionVisibility<FunctionSpec_>,
190
- FunctionSpec.Args<FunctionSpec_>,
191
- FunctionSpec.Returns<FunctionSpec_>,
192
- FunctionSpec.Error<FunctionSpec_>
193
- >;
214
+ export type FromFunctionSpec<
215
+ FunctionSpec_ extends FunctionSpec.AnyWithProps,
216
+ MiddlewareError = never,
217
+ > = FromFunctionSpecHelper<
218
+ FunctionSpec_,
219
+ FunctionSpec.GetRuntimeAndFunctionType<FunctionSpec_>,
220
+ FunctionSpec.GetFunctionVisibility<FunctionSpec_>,
221
+ FunctionSpec.Args<FunctionSpec_>,
222
+ FunctionSpec.Returns<FunctionSpec_>,
223
+ FunctionSpec.Error<FunctionSpec_> | MiddlewareError
224
+ >;
225
+
226
+ type FromFunctionSpecHelper<
227
+ FunctionSpec_ extends FunctionSpec.AnyWithProps,
228
+ RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType,
229
+ FunctionVisibility_ extends FunctionVisibility,
230
+ Args_,
231
+ Returns_,
232
+ Error_,
233
+ > =
234
+ FunctionSpec_ extends FunctionSpec.WithFunctionProvenance<
235
+ FunctionSpec_,
236
+ FunctionProvenance.AnyConvex
237
+ >
238
+ ? ConvexRef<
239
+ RuntimeAndFunctionType_,
240
+ FunctionVisibility_,
241
+ Args_,
242
+ Returns_,
243
+ Error_
244
+ >
245
+ : FunctionSpec_ extends FunctionSpec.WithFunctionProvenance<
246
+ FunctionSpec_,
247
+ FunctionProvenance.AnyConfect
248
+ >
249
+ ? ConfectRef<
250
+ RuntimeAndFunctionType_,
251
+ FunctionVisibility_,
252
+ Args_,
253
+ Returns_,
254
+ Error_
255
+ >
256
+ : Ref<
257
+ RuntimeAndFunctionType_,
258
+ FunctionVisibility_,
259
+ Args_,
260
+ Returns_,
261
+ Error_
262
+ >;
194
263
 
195
264
  export const make = <FunctionSpec_ extends FunctionSpec.AnyWithProps>(
196
265
  /**
@@ -198,36 +267,74 @@ export const make = <FunctionSpec_ extends FunctionSpec.AnyWithProps>(
198
267
  * colon. For example, for `myGroupDir/myGroupMod:myFunc` this would be
199
268
  * `myGroupDir/myGroupMod`.
200
269
  */
201
- functionNamespace: string,
270
+ convexFunctionNamespace: string,
202
271
  functionSpec: FunctionSpec_,
203
- ): FromFunctionSpec<FunctionSpec_> => ({ functionSpec, functionNamespace });
272
+ groupMiddlewareSpecs: ReadonlyArray<MiddlewareSpec.AnyMiddlewareSpec> = [],
273
+ ): FromFunctionSpec<FunctionSpec_> => {
274
+ const convexFunctionName = `${convexFunctionNamespace}:${functionSpec.name}`;
275
+
276
+ return Match.value(functionSpec.functionProvenance).pipe(
277
+ Match.tag(
278
+ "Convex",
279
+ (): Any =>
280
+ ({
281
+ _tag: "Convex",
282
+ convexFunctionName,
283
+ }) as Any,
284
+ ),
285
+ Match.tag("Confect", (provenance): Any => {
286
+ const ref = {
287
+ _tag: "Confect" as const,
288
+ convexFunctionName,
289
+ kind: provenance.kind,
290
+ middlewareSpecs: [
291
+ ...groupMiddlewareSpecs,
292
+ ...functionSpec.middlewareSpecs,
293
+ ],
294
+ };
295
+
296
+ Lazy.defineProperty(ref, "args", () => provenance.args);
297
+ Lazy.defineProperty(ref, "returns", () => provenance.returns);
298
+ if ("error" in provenance) {
299
+ Lazy.defineProperty(ref, "error", () => provenance.error);
300
+ }
301
+
302
+ return ref as unknown as Any;
303
+ }),
304
+ Match.exhaustive,
305
+ ) as FromFunctionSpec<FunctionSpec_>;
306
+ };
204
307
 
205
308
  export const getConvexFunctionName = (ref: Any): string =>
206
- `${ref.functionNamespace}:${ref.functionSpec.name}`;
309
+ ref.convexFunctionName;
207
310
 
208
311
  const functionReferenceCache = new Map<string, FunctionReference<Any>>();
209
312
 
210
313
  export const getFunctionReference = <Ref_ extends Any>(
211
314
  ref: Ref_,
212
315
  ): FunctionReference<Ref_> => {
213
- const functionName = getConvexFunctionName(ref);
316
+ const convexFunctionName = getConvexFunctionName(ref);
214
317
 
215
- const cached = functionReferenceCache.get(functionName);
318
+ const cached = functionReferenceCache.get(convexFunctionName);
216
319
  if (cached !== undefined) {
217
320
  return cached as FunctionReference<Ref_>;
218
321
  }
219
322
 
220
- const functionReference = makeFunctionReference(functionName);
221
- functionReferenceCache.set(functionName, functionReference);
323
+ const functionReference = makeFunctionReference(convexFunctionName);
324
+ functionReferenceCache.set(convexFunctionName, functionReference);
222
325
 
223
326
  return functionReference as FunctionReference<Ref_>;
224
327
  };
225
328
 
226
329
  export const hasErrorSchema = (ref: Any): boolean =>
227
- Match.value(ref.functionSpec.functionProvenance).pipe(
330
+ Match.value(ref).pipe(
228
331
  Match.tag(
229
332
  "Confect",
230
- (confectFunctionProvenance) => "error" in confectFunctionProvenance,
333
+ (confectRef) =>
334
+ "error" in confectRef ||
335
+ confectRef.middlewareSpecs.some(
336
+ (middlewareSpec) => "error" in middlewareSpec,
337
+ ),
231
338
  ),
232
339
  Match.tag("Convex", () => false),
233
340
  Match.exhaustive,
@@ -237,9 +344,9 @@ export const encodeArgs = <Ref_ extends Any>(
237
344
  ref: Ref_,
238
345
  args: Args<Ref_>,
239
346
  ): Effect.Effect<unknown, Schema.SchemaError> =>
240
- Match.value(ref.functionSpec.functionProvenance).pipe(
241
- Match.tag("Confect", (confectFunctionProvenance) =>
242
- Schema.encodeEffect(confectFunctionProvenance.args)(args),
347
+ Match.value<Any>(ref).pipe(
348
+ Match.tag("Confect", (confectRef) =>
349
+ Schema.encodeEffect(confectRef.args)(args),
243
350
  ),
244
351
  Match.tag("Convex", () => Effect.succeed(args)),
245
352
  Match.exhaustive,
@@ -249,11 +356,11 @@ export const decodeReturns = <Ref_ extends Any>(
249
356
  ref: Ref_,
250
357
  returns: unknown,
251
358
  ): Effect.Effect<Returns<Ref_>, Schema.SchemaError> =>
252
- Match.value(ref.functionSpec.functionProvenance).pipe(
253
- Match.tag("Confect", (confectFunctionProvenance) =>
254
- Schema.decodeUnknownEffect(confectFunctionProvenance.returns)(returns),
359
+ Match.value<Any>(ref).pipe(
360
+ Match.tag("Confect", (confectRef) =>
361
+ Schema.decodeUnknownEffect(confectRef.returns)(returns),
255
362
  ),
256
- Match.tag("Convex", () => Effect.succeed(returns)),
363
+ Match.tag("Convex", () => Effect.succeed(returns as Returns<Ref_>)),
257
364
  Match.exhaustive,
258
365
  );
259
366
 
@@ -261,9 +368,9 @@ export const encodeArgsSync = <Ref_ extends Any>(
261
368
  ref: Ref_,
262
369
  args: Args<Ref_>,
263
370
  ): unknown =>
264
- Match.value(ref.functionSpec.functionProvenance).pipe(
265
- Match.tag("Confect", (confectFunctionProvenance) =>
266
- Schema.encodeSync(confectFunctionProvenance.args)(args),
371
+ Match.value<Any>(ref).pipe(
372
+ Match.tag("Confect", (confectRef) =>
373
+ Schema.encodeSync(confectRef.args)(args),
267
374
  ),
268
375
  Match.tag("Convex", () => args),
269
376
  Match.exhaustive,
@@ -273,9 +380,9 @@ export const decodeArgsSync = <Ref_ extends Any>(
273
380
  ref: Ref_,
274
381
  encodedArgs: unknown,
275
382
  ): Args<Ref_> =>
276
- Match.value(ref.functionSpec.functionProvenance).pipe(
277
- Match.tag("Confect", (confectFunctionProvenance) =>
278
- Schema.decodeUnknownSync(confectFunctionProvenance.args)(encodedArgs),
383
+ Match.value<Any>(ref).pipe(
384
+ Match.tag("Confect", (confectRef) =>
385
+ Schema.decodeUnknownSync(confectRef.args)(encodedArgs),
279
386
  ),
280
387
  Match.tag("Convex", () => encodedArgs),
281
388
  Match.exhaustive,
@@ -285,9 +392,9 @@ export const encodeReturnsSync = <Ref_ extends Any>(
285
392
  ref: Ref_,
286
393
  returns: Returns<Ref_>,
287
394
  ): unknown =>
288
- Match.value(ref.functionSpec.functionProvenance).pipe(
289
- Match.tag("Confect", (confectFunctionProvenance) =>
290
- Schema.encodeSync(confectFunctionProvenance.returns)(returns),
395
+ Match.value<Any>(ref).pipe(
396
+ Match.tag("Confect", (confectRef) =>
397
+ Schema.encodeSync(confectRef.returns)(returns),
291
398
  ),
292
399
  Match.tag("Convex", () => returns),
293
400
  Match.exhaustive,
@@ -297,11 +404,9 @@ export const decodeReturnsSync = <Ref_ extends Any>(
297
404
  ref: Ref_,
298
405
  encodedReturns: unknown,
299
406
  ): Returns<Ref_> =>
300
- Match.value(ref.functionSpec.functionProvenance).pipe(
301
- Match.tag("Confect", (confectFunctionProvenance) =>
302
- Schema.decodeUnknownSync(confectFunctionProvenance.returns)(
303
- encodedReturns,
304
- ),
407
+ Match.value<Any>(ref).pipe(
408
+ Match.tag("Confect", (confectRef) =>
409
+ Schema.decodeUnknownSync(confectRef.returns)(encodedReturns),
305
410
  ),
306
411
  Match.tag("Convex", () => encodedReturns),
307
412
  Match.exhaustive,
@@ -336,10 +441,31 @@ export const decodeErrorOrElse =
336
441
  return mapUnknownError(error);
337
442
  };
338
443
 
444
+ const errorSchemaOf = (ref: Any): Option.Option<Schema.Codec<any, any>> =>
445
+ Match.value(ref).pipe(
446
+ Match.tag("Confect", (confectRef) => {
447
+ const schemas = [
448
+ ...("error" in confectRef && confectRef.error !== undefined
449
+ ? [confectRef.error]
450
+ : []),
451
+ ...MiddlewareSpec.errorSchemas(confectRef.middlewareSpecs),
452
+ ];
453
+ return schemas.length === 0
454
+ ? Option.none<Schema.Codec<any, any>>()
455
+ : Option.some(
456
+ schemas.length === 1 ? schemas[0]! : Schema.Union(schemas),
457
+ );
458
+ }),
459
+ Match.tag("Convex", () => Option.none<Schema.Codec<any, any>>()),
460
+ Match.exhaustive,
461
+ );
462
+
339
463
  /**
340
- * Decode `encodedError` against the ref's error schema. Returns `None` if the
341
- * ref doesn't declare a typed error (Confect ref without an `error` schema, or
342
- * a Convex-provenance ref)—by definition there's nothing to decode the value
464
+ * Decode `encodedError` against the ref's error schema — the function's
465
+ * declared `error` schema unioned with its covering middlewares' error
466
+ * schemas. Returns `None` if the ref declares no typed error at all (Confect
467
+ * ref without an `error` schema and without failing middleware, or a
468
+ * Convex-provenance ref)—by definition there's nothing to decode the value
343
469
  * into, and the caller is responsible for deciding what to do (typically:
344
470
  * surface the original value as a defect).
345
471
  */
@@ -347,20 +473,14 @@ export const decodeError = <Ref_ extends Any>(
347
473
  ref: Ref_,
348
474
  encodedError: unknown,
349
475
  ): Effect.Effect<Option.Option<Error<Ref_>>, Schema.SchemaError> =>
350
- Match.value(ref.functionSpec.functionProvenance).pipe(
351
- Match.tag("Confect", (confectFunctionProvenance) =>
352
- "error" in confectFunctionProvenance
353
- ? Effect.map(
354
- Schema.decodeUnknownEffect(confectFunctionProvenance.error)(
355
- encodedError,
356
- ),
357
- Option.some,
358
- )
359
- : Effect.succeed(Option.none<Error<Ref_>>()),
360
- ),
361
- Match.tag("Convex", () => Effect.succeed(Option.none<Error<Ref_>>())),
362
- Match.exhaustive,
363
- );
476
+ Option.match(errorSchemaOf(ref), {
477
+ onNone: () => Effect.succeed(Option.none<Error<Ref_>>()),
478
+ onSome: (schema) =>
479
+ Effect.map(
480
+ Schema.decodeUnknownEffect(schema)(encodedError),
481
+ Option.some,
482
+ ) as Effect.Effect<Option.Option<Error<Ref_>>, Schema.SchemaError>,
483
+ });
364
484
 
365
485
  /**
366
486
  * Synchronous counterpart to `decodeError`. Returns `None` when the value is
@@ -381,16 +501,12 @@ export const decodeErrorOption = <Ref_ extends Any>(
381
501
  ref: Ref_,
382
502
  encodedError: unknown,
383
503
  ): Option.Option<Error<Ref_>> =>
384
- Match.value(ref.functionSpec.functionProvenance).pipe(
385
- Match.tag("Confect", (confectFunctionProvenance) =>
386
- "error" in confectFunctionProvenance
387
- ? (Schema.decodeUnknownOption(confectFunctionProvenance.error)(
388
- encodedError,
389
- ) as Option.Option<Error<Ref_>>)
390
- : Option.none<Error<Ref_>>(),
391
- ),
392
- Match.tag("Convex", () => Option.none<Error<Ref_>>()),
393
- Match.exhaustive,
504
+ Option.flatMap(
505
+ errorSchemaOf(ref),
506
+ (schema) =>
507
+ Schema.decodeUnknownOption(schema)(encodedError) as Option.Option<
508
+ Error<Ref_>
509
+ >,
394
510
  );
395
511
 
396
512
  const missingPaginatedProvenanceError = (ref: Any) =>
@@ -406,6 +522,17 @@ const missingPaginatedProvenanceError = (ref: Any) =>
406
522
  " })",
407
523
  );
408
524
 
525
+ const paginatedKind = (
526
+ ref: ConfectRef<any, any, any, any, any>,
527
+ ): FunctionProvenance.Paginated =>
528
+ Match.value(ref.kind).pipe(
529
+ Match.tag("Paginated", (kind) => kind),
530
+ Match.tag("Standard", () => {
531
+ throw missingPaginatedProvenanceError(ref);
532
+ }),
533
+ Match.exhaustive,
534
+ );
535
+
409
536
  /**
410
537
  * Encode the args of a paginated query ref via its user-args schema —
411
538
  * `paginationOpts` is excluded, since the pagination protocol fields are
@@ -419,15 +546,10 @@ export const encodePaginatedQueryArgsSync = <
419
546
  ref: Ref_,
420
547
  args: Omit<Args<Ref_>, "paginationOpts">,
421
548
  ): unknown =>
422
- Match.value(ref.functionSpec.functionProvenance).pipe(
423
- Match.tag("Confect", (confectFunctionProvenance) => {
424
- if (confectFunctionProvenance.kind._tag !== "Paginated") {
425
- throw missingPaginatedProvenanceError(ref);
426
- }
427
- return Schema.encodeUnknownSync(confectFunctionProvenance.kind.userArgs)(
428
- args,
429
- );
430
- }),
549
+ Match.value<Any>(ref).pipe(
550
+ Match.tag("Confect", (confectRef) =>
551
+ Schema.encodeUnknownSync(paginatedKind(confectRef).userArgs)(args),
552
+ ),
431
553
  Match.tag("Convex", () => args),
432
554
  Match.exhaustive,
433
555
  );
@@ -441,15 +563,10 @@ export const decodePaginationPageSync = <Ref_ extends AnyPublicPaginatedQuery>(
441
563
  ref: Ref_,
442
564
  encodedPage: unknown,
443
565
  ): Returns<Ref_>["page"] =>
444
- Match.value(ref.functionSpec.functionProvenance).pipe(
445
- Match.tag("Confect", (confectFunctionProvenance) => {
446
- if (confectFunctionProvenance.kind._tag !== "Paginated") {
447
- throw missingPaginatedProvenanceError(ref);
448
- }
449
- return Schema.decodeUnknownSync(confectFunctionProvenance.kind.page)(
450
- encodedPage,
451
- );
452
- }),
566
+ Match.value<Any>(ref).pipe(
567
+ Match.tag("Confect", (confectRef) =>
568
+ Schema.decodeUnknownSync(paginatedKind(confectRef).page)(encodedPage),
569
+ ),
453
570
  Match.tag("Convex", () => encodedPage),
454
571
  Match.exhaustive,
455
572
  ) as Returns<Ref_>["page"];
@@ -473,7 +590,6 @@ export const runWithCodec = <Ref_ extends Any, E = never>(
473
590
  ): Effect.Effect<Returns<Ref_>, E | Error<Ref_> | Schema.SchemaError> =>
474
591
  Effect.gen(function* () {
475
592
  const functionReference = getFunctionReference(ref);
476
- const functionProvenance = ref.functionSpec.functionProvenance;
477
593
  const invoke = (
478
594
  encodedArgs: unknown,
479
595
  ): Effect.Effect<unknown, Error<Ref_> | E> =>
@@ -492,19 +608,8 @@ export const runWithCodec = <Ref_ extends Any, E = never>(
492
608
  throw error;
493
609
  },
494
610
  });
495
- return yield* Match.value(functionProvenance).pipe(
496
- Match.tag("Confect", (confectFunctionProvenance) =>
497
- Effect.gen(function* () {
498
- const encodedArgs = yield* Schema.encodeEffect(
499
- confectFunctionProvenance.args,
500
- )(args);
501
- const encodedReturns = yield* invoke(encodedArgs);
502
- return yield* Schema.decodeUnknownEffect(
503
- confectFunctionProvenance.returns,
504
- )(encodedReturns);
505
- }),
506
- ),
507
- Match.tag("Convex", () => invoke(args)),
508
- Match.exhaustive,
509
- );
611
+
612
+ const encodedArgs = yield* encodeArgs(ref, args);
613
+ const encodedReturns = yield* invoke(encodedArgs);
614
+ return yield* decodeReturns(ref, encodedReturns);
510
615
  });