@@ -253,8 +253,9 @@ export type EmptyObject = Record<string, never>;
253
253
* Convert an {@link ArgsArray} into a single object type.
254
254
*
255
255
* Empty arguments arrays are converted to {@link EmptyObject}.
256
+ * @public
256
257
*/
257
- type ArgsArrayToObject < Args extends ArgsArray > =
258
+ export type ArgsArrayToObject < Args extends ArgsArray > =
258
259
Args extends OneArgArray < infer ArgsObject > ? ArgsObject : EmptyObject ;
259
260
260
261
/**
@@ -382,6 +383,9 @@ export type PublicHttpAction = {
382
383
} ;
383
384
384
385
/**
386
+ * @deprecated -- See the type definition for `MutationBuilder` or similar for
387
+ * the types used for defining Convex functions.
388
+ *
385
389
* The definition of a Convex query, mutation, or action function without
386
390
* argument validation.
387
391
*
@@ -414,6 +418,9 @@ export type UnvalidatedFunction<Ctx, Args extends ArgsArray, Returns> =
414
418
} ;
415
419
416
420
/**
421
+ * @deprecated -- See the type definition for `MutationBuilder` or similar for
422
+ * the types used for defining Convex functions.
423
+ *
417
424
* The definition of a Convex query, mutation, or action function with argument
418
425
* validation.
419
426
*
@@ -477,48 +484,48 @@ export interface ValidatedFunction<
477
484
handler : ( ctx : Ctx , args : ObjectType < ArgsValidator > ) => Returns ;
478
485
}
479
486
480
- export interface ArgsAndReturnsValidatedFunction <
481
- Ctx ,
482
- ArgsValidator extends PropertyValidators ,
483
- ReturnsValidator extends Validator < any , any , any > ,
484
- > {
485
- args : ArgsValidator ;
486
- returns : ReturnsValidator ;
487
- /**
488
- * The implementation of this function.
489
- *
490
- * This is a function that takes in the appropriate context and arguments
491
- * and produces some result.
492
- *
493
- * @param ctx - The context object. This is one of {@link QueryCtx},
494
- * {@link MutationCtx}, or {@link ActionCtx} depending on the function type.
495
- * @param args - The arguments object for this function. This will match
496
- * the type defined by the argument validator.
497
- * @returns
498
- */
499
- handler : (
500
- ctx : Ctx ,
501
- args : ObjectType < ArgsValidator > ,
502
- ) => ValidatorTypeToReturnType < ReturnsValidator [ "type" ] > ;
503
- }
487
+ /**
488
+ * There are multiple syntaxes for defining a Convex function:
489
+ * ```
490
+ * - query(async (ctx, args) => {...})
491
+ * - query({ handler: async (ctx, args) => {...} })
492
+ * - query({ args: { a: v.string }, handler: async (ctx, args) => {...} } })
493
+ * - query({ args: { a: v.string }, returns: v.string(), handler: async (ctx, args) => {...} } })
494
+ *```
495
+ *
496
+ * In each of these, we want to correctly infer the type for the arguments and
497
+ * return value, preferring the type derived from a validator if it's provided.
498
+ *
499
+ * To avoid having a separate overload for each, which would show up in error messages,
500
+ * we use the type params -- ArgsValidator, ReturnsValidator, ReturnValue, OneOrZeroArgs.
501
+ *
502
+ * The type for ReturnValue and OneOrZeroArgs are constrained by the type or ArgsValidator and
503
+ * ReturnsValidator if they're present, and inferred from any explicit type annotations to the
504
+ * arguments or return value of the function.
505
+ *
506
+ * Below are a few utility types to get the appropriate type constraints based on
507
+ * an optional validator.
508
+ *
509
+ * Additional tricks:
510
+ * - We use Validator | void instead of Validator | undefined because the latter does
511
+ * not work with `strictNullChecks` since it's equivalent to just `Validator`.
512
+ * - We use a tuple type of length 1 to avoid distribution over the union
513
+ * https://github.com/microsoft/TypeScript/issues/29368#issuecomment-453529532
514
+ */
504
515
505
- // These types use a tuple of length 1 to avoid distribution over the union
506
- // https://github.com/microsoft/TypeScript/issues/29368#issuecomment-453529532
507
- // We also use `void` instead of `undefined` so that this works with `strictNullChecks: false`
508
- // which would treat `T | undefined` as equivalent to `T`.
509
- type ReturnValueForOptionalValidator <
516
+ export type ReturnValueForOptionalValidator <
510
517
ReturnsValidator extends Validator < any , any , any > | void ,
511
518
> = [ ReturnsValidator ] extends [ Validator < any , any , any > ]
512
519
? ValidatorTypeToReturnType < Infer < ReturnsValidator > >
513
520
: any ;
514
521
515
- type ArgsArrayForOptionalValidator <
522
+ export type ArgsArrayForOptionalValidator <
516
523
ArgsValidator extends PropertyValidators | void ,
517
524
> = [ ArgsValidator ] extends [ PropertyValidators ]
518
525
? OneArgArray < ObjectType < ArgsValidator > >
519
526
: ArgsArray ;
520
527
521
- type DefaultArgsForOptionalValidator <
528
+ export type DefaultArgsForOptionalValidator <
522
529
ArgsValidator extends PropertyValidators | void ,
523
530
> = [ ArgsValidator ] extends [ PropertyValidators ]
524
531
? [ ObjectType < ArgsValidator > ]
@@ -545,15 +552,36 @@ export type MutationBuilder<
545
552
| {
546
553
args ?: ArgsValidator ;
547
554
returns ?: ReturnsValidator ;
555
+ /**
556
+ * The implementation of this function.
557
+ *
558
+ * This is a function that takes in the appropriate context and arguments
559
+ * and produces some result.
560
+ *
561
+ * @param ctx - The context object. This is one of {@link QueryCtx},
562
+ * {@link MutationCtx}, or {@link ActionCtx} depending on the function type.
563
+ * @param args - The arguments object for this function. This will match
564
+ * the type defined by the argument validator if provided.
565
+ * @returns
566
+ */
548
567
handler : (
549
568
ctx : GenericMutationCtx < DataModel > ,
550
569
...args : OneOrZeroArgs
551
570
) => ReturnValue ;
552
571
}
553
572
| {
554
- // args and returns could be allowed here but this doesn't work at runtime today
555
- //args?: ArgsValidator;
556
- //returns?: ReturnsValidator;
573
+ /**
574
+ * The implementation of this function.
575
+ *
576
+ * This is a function that takes in the appropriate context and arguments
577
+ * and produces some result.
578
+ *
579
+ * @param ctx - The context object. This is one of {@link QueryCtx},
580
+ * {@link MutationCtx}, or {@link ActionCtx} depending on the function type.
581
+ * @param args - The arguments object for this function. This will match
582
+ * the type defined by the argument validator if provided.
583
+ * @returns
584
+ */
557
585
(
558
586
ctx : GenericMutationCtx < DataModel > ,
559
587
...args : OneOrZeroArgs
@@ -587,12 +615,36 @@ export type QueryBuilder<
587
615
| {
588
616
args ?: ArgsValidator ;
589
617
returns ?: ReturnsValidator ;
618
+ /**
619
+ * The implementation of this function.
620
+ *
621
+ * This is a function that takes in the appropriate context and arguments
622
+ * and produces some result.
623
+ *
624
+ * @param ctx - The context object. This is one of {@link QueryCtx},
625
+ * {@link MutationCtx}, or {@link ActionCtx} depending on the function type.
626
+ * @param args - The arguments object for this function. This will match
627
+ * the type defined by the argument validator if provided.
628
+ * @returns
629
+ */
590
630
handler : (
591
631
ctx : GenericQueryCtx < DataModel > ,
592
632
...args : OneOrZeroArgs
593
633
) => ReturnValue ;
594
634
}
595
635
| {
636
+ /**
637
+ * The implementation of this function.
638
+ *
639
+ * This is a function that takes in the appropriate context and arguments
640
+ * and produces some result.
641
+ *
642
+ * @param ctx - The context object. This is one of {@link QueryCtx},
643
+ * {@link MutationCtx}, or {@link ActionCtx} depending on the function type.
644
+ * @param args - The arguments object for this function. This will match
645
+ * the type defined by the argument validator if provided.
646
+ * @returns
647
+ */
596
648
(
597
649
ctx : GenericQueryCtx < DataModel > ,
598
650
...args : OneOrZeroArgs
@@ -622,12 +674,36 @@ export type ActionBuilder<
622
674
| {
623
675
args ?: ArgsValidator ;
624
676
returns ?: ReturnsValidator ;
677
+ /**
678
+ * The implementation of this function.
679
+ *
680
+ * This is a function that takes in the appropriate context and arguments
681
+ * and produces some result.
682
+ *
683
+ * @param ctx - The context object. This is one of {@link QueryCtx},
684
+ * {@link MutationCtx}, or {@link ActionCtx} depending on the function type.
685
+ * @param args - The arguments object for this function. This will match
686
+ * the type defined by the argument validator if provided.
687
+ * @returns
688
+ */
625
689
handler : (
626
690
ctx : GenericActionCtx < DataModel > ,
627
691
...args : OneOrZeroArgs
628
692
) => ReturnValue ;
629
693
}
630
694
| {
695
+ /**
696
+ * The implementation of this function.
697
+ *
698
+ * This is a function that takes in the appropriate context and arguments
699
+ * and produces some result.
700
+ *
701
+ * @param ctx - The context object. This is one of {@link QueryCtx},
702
+ * {@link MutationCtx}, or {@link ActionCtx} depending on the function type.
703
+ * @param args - The arguments object for this function. This will match
704
+ * the type defined by the argument validator if provided.
705
+ * @returns
706
+ */
631
707
(
632
708
ctx : GenericActionCtx < DataModel > ,
633
709
...args : OneOrZeroArgs
0 commit comments