@confect/core 10.0.0-next.14 → 10.0.0-next.17

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/src/Ref.ts CHANGED
@@ -11,10 +11,13 @@ 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<
20
+ export interface Base<
18
21
  _RuntimeAndFunctionType extends RuntimeAndFunctionType.RuntimeAndFunctionType,
19
22
  _FunctionVisibility extends FunctionVisibility,
20
23
  _Args,
@@ -27,26 +30,95 @@ export interface Ref<
27
30
  readonly _Returns?: _Returns;
28
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";
31
78
  /** @internal */
32
- readonly functionNamespace: string;
79
+ readonly args: Schema.Codec<any, any>;
80
+ /** @internal */
81
+ readonly returns: Schema.Codec<any, any>;
82
+ /** @internal */
83
+ readonly kind: FunctionProvenance.ConfectKind;
84
+ /** @internal */
85
+ readonly middlewareSpecs: ReadonlyArray<MiddlewareSpec.AnyMiddlewareSpec>;
86
+ /** @internal */
87
+ readonly error?: Schema.Codec<any, any>;
33
88
  }
34
89
 
35
- export interface Any extends Ref<any, any, any, any, any> {}
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";
105
+ }
106
+
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,50 +127,50 @@ 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
- > {}
170
+ >;
99
171
 
100
172
  export type GetRuntimeAndFunctionType<Ref_> =
101
- Ref_ extends Ref<
173
+ Ref_ extends Base<
102
174
  infer RuntimeAndFunctionType_,
103
175
  infer _FunctionVisibility,
104
176
  infer _Args,
@@ -109,7 +181,7 @@ export type GetRuntimeAndFunctionType<Ref_> =
109
181
  : never;
110
182
 
111
183
  export type GetRuntime<Ref_> =
112
- Ref_ extends Ref<
184
+ Ref_ extends Base<
113
185
  infer RuntimeAndFunctionType_,
114
186
  infer _FunctionVisibility,
115
187
  infer _Args,
@@ -120,7 +192,7 @@ export type GetRuntime<Ref_> =
120
192
  : never;
121
193
 
122
194
  export type GetFunctionType<Ref_> =
123
- Ref_ extends Ref<
195
+ Ref_ extends Base<
124
196
  infer RuntimeAndFunctionType_,
125
197
  infer _FunctionVisibility,
126
198
  infer _Args,
@@ -131,7 +203,7 @@ export type GetFunctionType<Ref_> =
131
203
  : never;
132
204
 
133
205
  export type GetFunctionVisibility<Ref_> =
134
- Ref_ extends Ref<
206
+ Ref_ extends Base<
135
207
  infer _RuntimeAndFunctionType,
136
208
  infer FunctionVisibility_,
137
209
  infer _Args,
@@ -142,7 +214,7 @@ export type GetFunctionVisibility<Ref_> =
142
214
  : never;
143
215
 
144
216
  export type Args<Ref_> =
145
- Ref_ extends Ref<
217
+ Ref_ extends Base<
146
218
  infer _RuntimeAndFunctionType,
147
219
  infer _FunctionVisibility,
148
220
  infer Args_,
@@ -157,7 +229,7 @@ export type OptionalArgs<Ref_ extends Any> = keyof Args<Ref_> extends never
157
229
  : [args: Args<Ref_>];
158
230
 
159
231
  export type Returns<Ref_> =
160
- Ref_ extends Ref<
232
+ Ref_ extends Base<
161
233
  infer _RuntimeAndFunctionType,
162
234
  infer _FunctionVisibility,
163
235
  infer _Args,
@@ -168,7 +240,7 @@ export type Returns<Ref_> =
168
240
  : never;
169
241
 
170
242
  export type Error<Ref_> =
171
- Ref_ extends Ref<
243
+ Ref_ extends Base<
172
244
  infer _RuntimeAndFunctionType,
173
245
  infer _FunctionVisibility,
174
246
  infer _Args,
@@ -183,14 +255,55 @@ export type FunctionReference<Ref_ extends Any> = ConvexFunctionReference<
183
255
  GetFunctionVisibility<Ref_>
184
256
  >;
185
257
 
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
- >;
258
+ export type FromFunctionSpec<
259
+ FunctionSpec_ extends FunctionSpec.AnyWithProps,
260
+ MiddlewareError = never,
261
+ > = FromFunctionSpecHelper<
262
+ FunctionSpec_,
263
+ FunctionSpec.GetRuntimeAndFunctionType<FunctionSpec_>,
264
+ FunctionSpec.GetFunctionVisibility<FunctionSpec_>,
265
+ FunctionSpec.Args<FunctionSpec_>,
266
+ FunctionSpec.Returns<FunctionSpec_>,
267
+ FunctionSpec.Error<FunctionSpec_> | MiddlewareError
268
+ >;
269
+
270
+ type FromFunctionSpecHelper<
271
+ FunctionSpec_ extends FunctionSpec.AnyWithProps,
272
+ RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType,
273
+ FunctionVisibility_ extends FunctionVisibility,
274
+ Args_,
275
+ Returns_,
276
+ Error_,
277
+ > =
278
+ FunctionSpec_ extends FunctionSpec.WithFunctionProvenance<
279
+ FunctionSpec_,
280
+ FunctionProvenance.AnyConvex
281
+ >
282
+ ? ConvexRef<
283
+ RuntimeAndFunctionType_,
284
+ FunctionVisibility_,
285
+ Args_,
286
+ Returns_,
287
+ Error_
288
+ >
289
+ : FunctionSpec_ extends FunctionSpec.WithFunctionProvenance<
290
+ FunctionSpec_,
291
+ FunctionProvenance.AnyConfect
292
+ >
293
+ ? ConfectRef<
294
+ RuntimeAndFunctionType_,
295
+ FunctionVisibility_,
296
+ Args_,
297
+ Returns_,
298
+ Error_
299
+ >
300
+ : Ref<
301
+ RuntimeAndFunctionType_,
302
+ FunctionVisibility_,
303
+ Args_,
304
+ Returns_,
305
+ Error_
306
+ >;
194
307
 
195
308
  export const make = <FunctionSpec_ extends FunctionSpec.AnyWithProps>(
196
309
  /**
@@ -198,36 +311,70 @@ export const make = <FunctionSpec_ extends FunctionSpec.AnyWithProps>(
198
311
  * colon. For example, for `myGroupDir/myGroupMod:myFunc` this would be
199
312
  * `myGroupDir/myGroupMod`.
200
313
  */
201
- functionNamespace: string,
314
+ convexFunctionNamespace: string,
202
315
  functionSpec: FunctionSpec_,
203
- ): FromFunctionSpec<FunctionSpec_> => ({ functionSpec, functionNamespace });
316
+ groupMiddlewareSpecs: ReadonlyArray<MiddlewareSpec.AnyMiddlewareSpec> = [],
317
+ ): FromFunctionSpec<FunctionSpec_> => {
318
+ const convexFunctionName = `${convexFunctionNamespace}:${functionSpec.name}`;
319
+
320
+ return Match.value(functionSpec.functionProvenance).pipe(
321
+ Match.tag("Convex", (): Any => ({
322
+ _tag: "Convex",
323
+ convexFunctionName,
324
+ })),
325
+ Match.tag("Confect", (provenance): Any => {
326
+ const ref = {
327
+ _tag: "Confect" as const,
328
+ convexFunctionName,
329
+ kind: provenance.kind,
330
+ middlewareSpecs: [
331
+ ...groupMiddlewareSpecs,
332
+ ...functionSpec.middlewareSpecs,
333
+ ],
334
+ };
335
+
336
+ Lazy.defineProperty(ref, "args", () => provenance.args);
337
+ Lazy.defineProperty(ref, "returns", () => provenance.returns);
338
+ if ("error" in provenance) {
339
+ Lazy.defineProperty(ref, "error", () => provenance.error);
340
+ }
341
+
342
+ return ref as unknown as Any;
343
+ }),
344
+ Match.exhaustive,
345
+ ) as FromFunctionSpec<FunctionSpec_>;
346
+ };
204
347
 
205
348
  export const getConvexFunctionName = (ref: Any): string =>
206
- `${ref.functionNamespace}:${ref.functionSpec.name}`;
349
+ ref.convexFunctionName;
207
350
 
208
351
  const functionReferenceCache = new Map<string, FunctionReference<Any>>();
209
352
 
210
353
  export const getFunctionReference = <Ref_ extends Any>(
211
354
  ref: Ref_,
212
355
  ): FunctionReference<Ref_> => {
213
- const functionName = getConvexFunctionName(ref);
356
+ const convexFunctionName = getConvexFunctionName(ref);
214
357
 
215
- const cached = functionReferenceCache.get(functionName);
358
+ const cached = functionReferenceCache.get(convexFunctionName);
216
359
  if (cached !== undefined) {
217
360
  return cached as FunctionReference<Ref_>;
218
361
  }
219
362
 
220
- const functionReference = makeFunctionReference(functionName);
221
- functionReferenceCache.set(functionName, functionReference);
363
+ const functionReference = makeFunctionReference(convexFunctionName);
364
+ functionReferenceCache.set(convexFunctionName, functionReference);
222
365
 
223
366
  return functionReference as FunctionReference<Ref_>;
224
367
  };
225
368
 
226
369
  export const hasErrorSchema = (ref: Any): boolean =>
227
- Match.value(ref.functionSpec.functionProvenance).pipe(
370
+ Match.value(ref).pipe(
228
371
  Match.tag(
229
372
  "Confect",
230
- (confectFunctionProvenance) => "error" in confectFunctionProvenance,
373
+ (confectRef) =>
374
+ "error" in confectRef ||
375
+ confectRef.middlewareSpecs.some(
376
+ (middlewareSpec) => "error" in middlewareSpec,
377
+ ),
231
378
  ),
232
379
  Match.tag("Convex", () => false),
233
380
  Match.exhaustive,
@@ -237,9 +384,9 @@ export const encodeArgs = <Ref_ extends Any>(
237
384
  ref: Ref_,
238
385
  args: Args<Ref_>,
239
386
  ): Effect.Effect<unknown, Schema.SchemaError> =>
240
- Match.value(ref.functionSpec.functionProvenance).pipe(
241
- Match.tag("Confect", (confectFunctionProvenance) =>
242
- Schema.encodeEffect(confectFunctionProvenance.args)(args),
387
+ Match.value<Any>(ref).pipe(
388
+ Match.tag("Confect", (confectRef) =>
389
+ Schema.encodeEffect(confectRef.args)(args),
243
390
  ),
244
391
  Match.tag("Convex", () => Effect.succeed(args)),
245
392
  Match.exhaustive,
@@ -249,11 +396,11 @@ export const decodeReturns = <Ref_ extends Any>(
249
396
  ref: Ref_,
250
397
  returns: unknown,
251
398
  ): Effect.Effect<Returns<Ref_>, Schema.SchemaError> =>
252
- Match.value(ref.functionSpec.functionProvenance).pipe(
253
- Match.tag("Confect", (confectFunctionProvenance) =>
254
- Schema.decodeUnknownEffect(confectFunctionProvenance.returns)(returns),
399
+ Match.value<Any>(ref).pipe(
400
+ Match.tag("Confect", (confectRef) =>
401
+ Schema.decodeUnknownEffect(confectRef.returns)(returns),
255
402
  ),
256
- Match.tag("Convex", () => Effect.succeed(returns)),
403
+ Match.tag("Convex", () => Effect.succeed(returns as Returns<Ref_>)),
257
404
  Match.exhaustive,
258
405
  );
259
406
 
@@ -261,9 +408,9 @@ export const encodeArgsSync = <Ref_ extends Any>(
261
408
  ref: Ref_,
262
409
  args: Args<Ref_>,
263
410
  ): unknown =>
264
- Match.value(ref.functionSpec.functionProvenance).pipe(
265
- Match.tag("Confect", (confectFunctionProvenance) =>
266
- Schema.encodeSync(confectFunctionProvenance.args)(args),
411
+ Match.value<Any>(ref).pipe(
412
+ Match.tag("Confect", (confectRef) =>
413
+ Schema.encodeSync(confectRef.args)(args),
267
414
  ),
268
415
  Match.tag("Convex", () => args),
269
416
  Match.exhaustive,
@@ -273,9 +420,9 @@ export const decodeArgsSync = <Ref_ extends Any>(
273
420
  ref: Ref_,
274
421
  encodedArgs: unknown,
275
422
  ): Args<Ref_> =>
276
- Match.value(ref.functionSpec.functionProvenance).pipe(
277
- Match.tag("Confect", (confectFunctionProvenance) =>
278
- Schema.decodeUnknownSync(confectFunctionProvenance.args)(encodedArgs),
423
+ Match.value<Any>(ref).pipe(
424
+ Match.tag("Confect", (confectRef) =>
425
+ Schema.decodeUnknownSync(confectRef.args)(encodedArgs),
279
426
  ),
280
427
  Match.tag("Convex", () => encodedArgs),
281
428
  Match.exhaustive,
@@ -285,9 +432,9 @@ export const encodeReturnsSync = <Ref_ extends Any>(
285
432
  ref: Ref_,
286
433
  returns: Returns<Ref_>,
287
434
  ): unknown =>
288
- Match.value(ref.functionSpec.functionProvenance).pipe(
289
- Match.tag("Confect", (confectFunctionProvenance) =>
290
- Schema.encodeSync(confectFunctionProvenance.returns)(returns),
435
+ Match.value<Any>(ref).pipe(
436
+ Match.tag("Confect", (confectRef) =>
437
+ Schema.encodeSync(confectRef.returns)(returns),
291
438
  ),
292
439
  Match.tag("Convex", () => returns),
293
440
  Match.exhaustive,
@@ -297,11 +444,9 @@ export const decodeReturnsSync = <Ref_ extends Any>(
297
444
  ref: Ref_,
298
445
  encodedReturns: unknown,
299
446
  ): Returns<Ref_> =>
300
- Match.value(ref.functionSpec.functionProvenance).pipe(
301
- Match.tag("Confect", (confectFunctionProvenance) =>
302
- Schema.decodeUnknownSync(confectFunctionProvenance.returns)(
303
- encodedReturns,
304
- ),
447
+ Match.value<Any>(ref).pipe(
448
+ Match.tag("Confect", (confectRef) =>
449
+ Schema.decodeUnknownSync(confectRef.returns)(encodedReturns),
305
450
  ),
306
451
  Match.tag("Convex", () => encodedReturns),
307
452
  Match.exhaustive,
@@ -336,10 +481,31 @@ export const decodeErrorOrElse =
336
481
  return mapUnknownError(error);
337
482
  };
338
483
 
484
+ const errorSchemaOf = (ref: Any): Option.Option<Schema.Codec<any, any>> =>
485
+ Match.value(ref).pipe(
486
+ Match.tag("Confect", (confectRef) => {
487
+ const schemas = [
488
+ ...("error" in confectRef && confectRef.error !== undefined
489
+ ? [confectRef.error]
490
+ : []),
491
+ ...MiddlewareSpec.errorSchemas(confectRef.middlewareSpecs),
492
+ ];
493
+ return schemas.length === 0
494
+ ? Option.none<Schema.Codec<any, any>>()
495
+ : Option.some(
496
+ schemas.length === 1 ? schemas[0]! : Schema.Union(schemas),
497
+ );
498
+ }),
499
+ Match.tag("Convex", () => Option.none<Schema.Codec<any, any>>()),
500
+ Match.exhaustive,
501
+ );
502
+
339
503
  /**
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
504
+ * Decode `encodedError` against the ref's error schema — the function's
505
+ * declared `error` schema unioned with its covering middlewares' error
506
+ * schemas. Returns `None` if the ref declares no typed error at all (Confect
507
+ * ref without an `error` schema and without failing middleware, or a
508
+ * Convex-provenance ref)—by definition there's nothing to decode the value
343
509
  * into, and the caller is responsible for deciding what to do (typically:
344
510
  * surface the original value as a defect).
345
511
  */
@@ -347,20 +513,14 @@ export const decodeError = <Ref_ extends Any>(
347
513
  ref: Ref_,
348
514
  encodedError: unknown,
349
515
  ): 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
- );
516
+ Option.match(errorSchemaOf(ref), {
517
+ onNone: () => Effect.succeed(Option.none<Error<Ref_>>()),
518
+ onSome: (schema) =>
519
+ Effect.map(
520
+ Schema.decodeUnknownEffect(schema)(encodedError),
521
+ Option.some,
522
+ ) as Effect.Effect<Option.Option<Error<Ref_>>, Schema.SchemaError>,
523
+ });
364
524
 
365
525
  /**
366
526
  * Synchronous counterpart to `decodeError`. Returns `None` when the value is
@@ -381,16 +541,12 @@ export const decodeErrorOption = <Ref_ extends Any>(
381
541
  ref: Ref_,
382
542
  encodedError: unknown,
383
543
  ): 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,
544
+ Option.flatMap(
545
+ errorSchemaOf(ref),
546
+ (schema) =>
547
+ Schema.decodeUnknownOption(schema)(encodedError) as Option.Option<
548
+ Error<Ref_>
549
+ >,
394
550
  );
395
551
 
396
552
  const missingPaginatedProvenanceError = (ref: Any) =>
@@ -406,6 +562,17 @@ const missingPaginatedProvenanceError = (ref: Any) =>
406
562
  " })",
407
563
  );
408
564
 
565
+ const paginatedKind = (
566
+ ref: ConfectRef<any, any, any, any, any>,
567
+ ): FunctionProvenance.Paginated =>
568
+ Match.value(ref.kind).pipe(
569
+ Match.tag("Paginated", (kind) => kind),
570
+ Match.tag("Standard", () => {
571
+ throw missingPaginatedProvenanceError(ref);
572
+ }),
573
+ Match.exhaustive,
574
+ );
575
+
409
576
  /**
410
577
  * Encode the args of a paginated query ref via its user-args schema —
411
578
  * `paginationOpts` is excluded, since the pagination protocol fields are
@@ -419,15 +586,10 @@ export const encodePaginatedQueryArgsSync = <
419
586
  ref: Ref_,
420
587
  args: Omit<Args<Ref_>, "paginationOpts">,
421
588
  ): 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
- }),
589
+ Match.value<Any>(ref).pipe(
590
+ Match.tag("Confect", (confectRef) =>
591
+ Schema.encodeUnknownSync(paginatedKind(confectRef).userArgs)(args),
592
+ ),
431
593
  Match.tag("Convex", () => args),
432
594
  Match.exhaustive,
433
595
  );
@@ -441,15 +603,10 @@ export const decodePaginationPageSync = <Ref_ extends AnyPublicPaginatedQuery>(
441
603
  ref: Ref_,
442
604
  encodedPage: unknown,
443
605
  ): 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
- }),
606
+ Match.value<Any>(ref).pipe(
607
+ Match.tag("Confect", (confectRef) =>
608
+ Schema.decodeUnknownSync(paginatedKind(confectRef).page)(encodedPage),
609
+ ),
453
610
  Match.tag("Convex", () => encodedPage),
454
611
  Match.exhaustive,
455
612
  ) as Returns<Ref_>["page"];
@@ -473,7 +630,6 @@ export const runWithCodec = <Ref_ extends Any, E = never>(
473
630
  ): Effect.Effect<Returns<Ref_>, E | Error<Ref_> | Schema.SchemaError> =>
474
631
  Effect.gen(function* () {
475
632
  const functionReference = getFunctionReference(ref);
476
- const functionProvenance = ref.functionSpec.functionProvenance;
477
633
  const invoke = (
478
634
  encodedArgs: unknown,
479
635
  ): Effect.Effect<unknown, Error<Ref_> | E> =>
@@ -492,19 +648,8 @@ export const runWithCodec = <Ref_ extends Any, E = never>(
492
648
  throw error;
493
649
  },
494
650
  });
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
- );
651
+
652
+ const encodedArgs = yield* encodeArgs(ref, args);
653
+ const encodedReturns = yield* invoke(encodedArgs);
654
+ return yield* decodeReturns(ref, encodedReturns);
510
655
  });