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 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"; export type CallableBuilder = { (context: TCurrentContext, args: InferredArgs): Promise; }; export interface ConvexBuilderDef { 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 ? InferReturns : any; export type InferredHandlerReturn = [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; /** 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 { readonly [handlerReturnType]: infer H; } ? H : InferredHandlerReturn; type Simplify = T extends object ? { [K in keyof T]: T[K]; } : T; 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 = SameType, InferredArgs> extends true ? SameType, InferredHandlerReturn> : false; export {}; //# sourceMappingURL=types.d.ts.map