import type { PropertyValidators, GenericValidator } from "convex/values"; import type { GenericActionCtx, GenericMutationCtx, GenericQueryCtx, GenericDataModel, } from "convex/server"; import type { AnyConvexMiddleware } from "./middleware.js"; export type Context = object; export type ConvexArgsValidator = PropertyValidators | GenericValidator; export type ConvexReturnsValidator = GenericValidator; type ValidatorType = T extends GenericValidator ? T["type"] : never; export type EmptyObject = Record; export type MaybeDefaultContext< TExisting extends Context, TFallback extends Context, > = TExisting extends EmptyObject ? TFallback : TExisting; type OptionalKeys> = { [K in keyof T]-?: T[K] extends GenericValidator ? T[K]["isOptional"] extends "optional" ? K : never : never; }[keyof T]; type RequiredKeys> = { [K in keyof T]-?: T[K] extends GenericValidator ? T[K]["isOptional"] extends "optional" ? never : K : never; }[keyof T]; type OptionalArgs> = { [K in OptionalKeys]?: T[K] extends GenericValidator ? ValidatorType | undefined : never; }; type RequiredArgs> = { [K in RequiredKeys]: ValidatorType; }; export type InferArgs = T extends GenericValidator ? T["type"] : RequiredArgs & OptionalArgs; export type InferReturns = ValidatorType; export type Promisable = T | PromiseLike; export type QueryCtx = GenericQueryCtx; export type MutationCtx = GenericMutationCtx; export type ActionCtx = GenericActionCtx; export type FunctionType = "query" | "mutation" | "action"; export type Visibility = "public" | "internal"; // Type for a callable ConvexBuilderWithHandler export type CallableBuilder< TCurrentContext extends Context, TArgsValidator extends ConvexArgsValidator | undefined, THandlerReturn, > = { ( context: TCurrentContext, args: InferredArgs ): Promise; }; export interface ConvexBuilderDef< TFunctionType extends FunctionType | undefined = undefined, TArgsValidator extends ConvexArgsValidator | undefined = undefined, TReturnsValidator extends ConvexReturnsValidator | undefined = undefined, > { functionType?: TFunctionType; middlewares: readonly AnyConvexMiddleware[]; argsValidator?: TArgsValidator; returnsValidator?: TReturnsValidator; handler?: (context: Context, input: any) => Promise; /** Optional hook to validate/transform args before the handler runs. Throw to reject. */ argsTransform?: (args: unknown) => unknown | Promise; /** Optional hook to validate/transform the return value after the handler runs. Throw to reject. */ returnsTransform?: (result: unknown) => unknown | Promise; } export type ExpectedReturnType< TReturnsValidator extends ConvexReturnsValidator | undefined, > = TReturnsValidator extends ConvexReturnsValidator ? InferReturns : any; export type InferredHandlerReturn< TReturnsValidator extends ConvexReturnsValidator | undefined, TReturn, > = [TReturnsValidator] extends [ConvexReturnsValidator] ? ExpectedReturnType : TReturn; export type InferredArgs = T extends ConvexArgsValidator ? InferArgs : EmptyObject; /** * Type-only keys a plugin can put on its validator types when the handler sees * something different from what the client sends or gets back. WithZod uses * them because Zod `.default()`s and `.transform()`s run between the two. * They're symbols so they never count as an argument or field: Convex's * `PropertyValidators` is a string-keyed record. Nothing exists at runtime. */ export declare const handlerArgsType: unique symbol; export declare const handlerReturnType: unique symbol; // The keys are required (on the type only), so their presence can be checked // directly and `unknown`/`any`/`undefined` handler types survive intact. /** What `.handler()` receives: the plugin's parsed args type, if it set one. */ export type HandlerArgs = T extends { readonly [handlerArgsType]: infer H } ? H : InferredArgs; /** What `.handler()` must return when `.returns()` is set. */ export type HandlerReturn< TReturnsValidator extends ConvexReturnsValidator | undefined, > = TReturnsValidator extends { readonly [handlerReturnType]: infer H } ? H : InferredHandlerReturn; type Simplify = T extends object ? { [K in keyof T]: T[K] } : T; // Strict equality (the usual generic-function trick), so `any` anywhere in // either type counts as a difference. Mutual assignability would call // `{ a: any }` and `{ a: string }` the same. A false here only costs the // ctrl+click overload, never correctness. type SameType = (() => G extends Simplify ? 1 : 2) extends () => G extends Simplify ? 1 : 2 ? true : false; /** * True when the handler sees the same types a direct caller does. `.handler()` * intersects the user's own function type into its result so ctrl+click on a * call lands on their code, but when a plugin's types differ (Zod transforms or * defaults) that extra signature would let calls through with the handler's * types instead of the caller's, so it's left off then. */ export type HandlerMatchesCaller< TArgsValidator extends ConvexArgsValidator | undefined, TReturnsValidator extends ConvexReturnsValidator | undefined, > = SameType, InferredArgs> extends true ? SameType< HandlerReturn, InferredHandlerReturn > : false;