Skip to content

Commit 2ac52d0

Browse files
sshaderConvex, Inc.
authored andcommitted
Add comments + export registration.ts utility types (#27148)
The goal is to export enough to be able to reconstruct the `MutationBuilder` type declaration to support `convex-helpers` customFunctions. I also added to the comments on our utility types. GitOrigin-RevId: de0a11f4adbcf9615cc1a21c37be199b891a4736
1 parent f99a7d6 commit 2ac52d0

File tree

2 files changed

+120
-40
lines changed

2 files changed

+120
-40
lines changed

npm-packages/convex/src/server/index.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,22 +100,26 @@ export type { IndexRange, IndexRangeBuilder } from "./index_range_builder.js";
100100
export * from "./pagination.js";
101101
export type { OrderedQuery, Query, QueryInitializer } from "./query.js";
102102
export type {
103-
ActionBuilder,
104103
ArgsArray,
105104
DefaultFunctionArgs,
106-
HttpActionBuilder,
107105
FunctionVisibility,
106+
ActionBuilder,
107+
MutationBuilder,
108+
QueryBuilder,
109+
HttpActionBuilder,
108110
GenericActionCtx,
109111
GenericMutationCtx,
110112
GenericQueryCtx,
111-
MutationBuilder,
112-
PublicHttpAction,
113113
RegisteredAction,
114114
RegisteredMutation,
115115
RegisteredQuery,
116-
QueryBuilder,
116+
PublicHttpAction,
117117
UnvalidatedFunction,
118118
ValidatedFunction,
119+
ReturnValueForOptionalValidator,
120+
ArgsArrayForOptionalValidator,
121+
ArgsArrayToObject,
122+
DefaultArgsForOptionalValidator,
119123
} from "./registration.js";
120124
export * from "./search_filter_builder.js";
121125
export * from "./storage.js";

npm-packages/convex/src/server/registration.ts

Lines changed: 111 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,9 @@ export type EmptyObject = Record<string, never>;
253253
* Convert an {@link ArgsArray} into a single object type.
254254
*
255255
* Empty arguments arrays are converted to {@link EmptyObject}.
256+
* @public
256257
*/
257-
type ArgsArrayToObject<Args extends ArgsArray> =
258+
export type ArgsArrayToObject<Args extends ArgsArray> =
258259
Args extends OneArgArray<infer ArgsObject> ? ArgsObject : EmptyObject;
259260

260261
/**
@@ -382,6 +383,9 @@ export type PublicHttpAction = {
382383
};
383384

384385
/**
386+
* @deprecated -- See the type definition for `MutationBuilder` or similar for
387+
* the types used for defining Convex functions.
388+
*
385389
* The definition of a Convex query, mutation, or action function without
386390
* argument validation.
387391
*
@@ -414,6 +418,9 @@ export type UnvalidatedFunction<Ctx, Args extends ArgsArray, Returns> =
414418
};
415419

416420
/**
421+
* @deprecated -- See the type definition for `MutationBuilder` or similar for
422+
* the types used for defining Convex functions.
423+
*
417424
* The definition of a Convex query, mutation, or action function with argument
418425
* validation.
419426
*
@@ -477,48 +484,48 @@ export interface ValidatedFunction<
477484
handler: (ctx: Ctx, args: ObjectType<ArgsValidator>) => Returns;
478485
}
479486

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+
*/
504515

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<
510517
ReturnsValidator extends Validator<any, any, any> | void,
511518
> = [ReturnsValidator] extends [Validator<any, any, any>]
512519
? ValidatorTypeToReturnType<Infer<ReturnsValidator>>
513520
: any;
514521

515-
type ArgsArrayForOptionalValidator<
522+
export type ArgsArrayForOptionalValidator<
516523
ArgsValidator extends PropertyValidators | void,
517524
> = [ArgsValidator] extends [PropertyValidators]
518525
? OneArgArray<ObjectType<ArgsValidator>>
519526
: ArgsArray;
520527

521-
type DefaultArgsForOptionalValidator<
528+
export type DefaultArgsForOptionalValidator<
522529
ArgsValidator extends PropertyValidators | void,
523530
> = [ArgsValidator] extends [PropertyValidators]
524531
? [ObjectType<ArgsValidator>]
@@ -545,15 +552,36 @@ export type MutationBuilder<
545552
| {
546553
args?: ArgsValidator;
547554
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+
*/
548567
handler: (
549568
ctx: GenericMutationCtx<DataModel>,
550569
...args: OneOrZeroArgs
551570
) => ReturnValue;
552571
}
553572
| {
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+
*/
557585
(
558586
ctx: GenericMutationCtx<DataModel>,
559587
...args: OneOrZeroArgs
@@ -587,12 +615,36 @@ export type QueryBuilder<
587615
| {
588616
args?: ArgsValidator;
589617
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+
*/
590630
handler: (
591631
ctx: GenericQueryCtx<DataModel>,
592632
...args: OneOrZeroArgs
593633
) => ReturnValue;
594634
}
595635
| {
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+
*/
596648
(
597649
ctx: GenericQueryCtx<DataModel>,
598650
...args: OneOrZeroArgs
@@ -622,12 +674,36 @@ export type ActionBuilder<
622674
| {
623675
args?: ArgsValidator;
624676
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+
*/
625689
handler: (
626690
ctx: GenericActionCtx<DataModel>,
627691
...args: OneOrZeroArgs
628692
) => ReturnValue;
629693
}
630694
| {
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+
*/
631707
(
632708
ctx: GenericActionCtx<DataModel>,
633709
...args: OneOrZeroArgs

0 commit comments

Comments
 (0)