import { EmptyObject, DefaultFunctionArgs, FunctionVisibility, RegisteredAction, RegisteredMutation, RegisteredQuery } from "./registration.js"; import { Expand, UnionToIntersection } from "../type_utils.js"; import { PaginationOptions, PaginationResult } from "./pagination.js"; /** * The type of a Convex function. * * @public */ export type FunctionType = "query" | "mutation" | "action"; /** * A reference to a registered Convex function. * * You can create a {@link FunctionReference} using the generated `api` utility: * ```js * import { api } from "../convex/_generated/api"; * * const reference = api.myModule.myFunction; * ``` * * If you aren't using code generation, you can create references using * {@link anyApi}: * ```js * import { anyApi } from "convex/server"; * * const reference = anyApi.myModule.myFunction; * ``` * * Function references can be used to invoke functions from the client. For * example, in React you can pass references to the {@link react.useQuery} hook: * ```js * const result = useQuery(api.myModule.myFunction); * ``` * * @typeParam Type - The type of the function ("query", "mutation", or "action"). * @typeParam Visibility - The visibility of the function ("public" or "internal"). * @typeParam Args - The arguments to this function. This is an object mapping * argument names to their types. * @typeParam ReturnType - The return type of this function. * @public */ export type FunctionReference = { _type: Type; _visibility: Visibility; _args: Args; _returnType: ReturnType; _componentPath: ComponentPath; }; /** * Get the name of a function from a {@link FunctionReference}. * * The name is a string like "myDir/myModule:myFunction". If the exported name * of the function is `"default"`, the function name is omitted * (e.g. "myDir/myModule"). * * @param functionReference - A {@link FunctionReference} to get the name of. * @returns A string of the function's name. * * @public */ export declare function getFunctionName(functionReference: AnyFunctionReference): string; /** * FunctionReferences generally come from generated code, but in custom clients * it may be useful to be able to build one manually. * * Real function references are empty objects at runtime, but the same interface * can be implemented with an object for tests and clients which don't use * code generation. * * @param name - The identifier of the function. E.g. `path/to/file:functionName` * @public */ export declare function makeFunctionReference(name: string): FunctionReference; /** * Given an export from a module, convert it to a {@link FunctionReference} * if it is a Convex function. */ export type FunctionReferenceFromExport = Export extends RegisteredQuery ? FunctionReference<"query", Visibility, Args, ConvertReturnType> : Export extends RegisteredMutation ? FunctionReference<"mutation", Visibility, Args, ConvertReturnType> : Export extends RegisteredAction ? FunctionReference<"action", Visibility, Args, ConvertReturnType> : never; /** * Given a module, convert all the Convex functions into * {@link FunctionReference}s and remove the other exports. * * BE CAREFUL WHEN EDITING THIS! * * This is written carefully to preserve jumping to function definitions using * cmd+click. If you edit it, please test that cmd+click still works. */ type FunctionReferencesInModule> = { -readonly [ExportName in keyof Module as Module[ExportName]["isConvexFunction"] extends true ? ExportName : never]: FunctionReferenceFromExport; }; /** * Given a path to a module and it's type, generate an API type for this module. * * This is a nested object according to the module's path. */ type ApiForModule = ModulePath extends `${infer First}/${infer Second}` ? { [_ in First]: ApiForModule; } : { [_ in ModulePath]: FunctionReferencesInModule; }; /** * Given the types of all modules in the `convex/` directory, construct the type * of `api`. * * `api` is a utility for constructing {@link FunctionReference}s. * * @typeParam AllModules - A type mapping module paths (like `"dir/myModule"`) to * the types of the modules. * @public */ export type ApiFromModules> = FilterApi, FunctionReference>; type ApiFromModulesAllowEmptyNodes> = ExpandModulesAndDirs; }[keyof AllModules]>>; type FilterKeysInApi = API extends Predicate ? key : API extends FunctionReference ? never : FilterApi extends Record ? never : key; /** * @public * * Filter a Convex deployment api object for functions which meet criteria, * for example all public queries. */ export type FilterApi = Expand<{ [mod in keyof API as FilterKeysInApi]: API[mod] extends Predicate ? API[mod] : FilterApi; }>; /** * Given an api of type API and a FunctionReference subtype, return an api object * containing only the function references that match. * * ```ts * const q = filterApi>(api) * ``` * * @public */ export declare function filterApi(api: API): FilterApi; /** @public */ export declare function justInternal(api: API): FilterApi>; /** @public */ export declare function justPublic(api: API): FilterApi>; /** @public */ export declare function justQueries(api: API): FilterApi>; /** @public */ export declare function justMutations(api: API): FilterApi>; /** @public */ export declare function justActions(api: API): FilterApi>; /** @public */ export declare function justPaginatedQueries(api: API): FilterApi>>; /** @public */ export declare function justSchedulable(api: API): FilterApi>; /** * Like {@link Expand}, this simplifies how TypeScript displays object types. * The differences are: * 1. This version is recursive. * 2. This stops recursing when it hits a {@link FunctionReference}. */ type ExpandModulesAndDirs = ObjectType extends AnyFunctionReference ? ObjectType : { [Key in keyof ObjectType]: ExpandModulesAndDirs; }; /** * A {@link FunctionReference} of any type and any visibility with any * arguments and any return type. * * @public */ export type AnyFunctionReference = FunctionReference; type AnyModuleDirOrFunc = { [key: string]: AnyModuleDirOrFunc; } & AnyFunctionReference; /** * The type that Convex api objects extend. If you were writing an api from * scratch it should extend this type. * * @public */ export type AnyApi = Record>; /** * Recursive partial API, useful for defining a subset of an API when mocking * or building custom api objects. * * @public */ export type PartialApi = { [mod in keyof API]?: API[mod] extends FunctionReference ? API[mod] : PartialApi; }; /** * A utility for constructing {@link FunctionReference}s in projects that * are not using code generation. * * You can create a reference to a function like: * ```js * const reference = anyApi.myModule.myFunction; * ``` * * This supports accessing any path regardless of what directories and modules * are in your project. All function references are typed as * {@link AnyFunctionReference}. * * * If you're using code generation, use `api` from `convex/_generated/api` * instead. It will be more type-safe and produce better auto-complete * in your editor. * * @public */ export declare const anyApi: AnyApi; /** * Given a {@link FunctionReference}, get the return type of the function. * * This is represented as an object mapping argument names to values. * @public */ export type FunctionArgs = FuncRef["_args"]; /** * A tuple type of the (maybe optional) arguments to `FuncRef`. * * This type is used to make methods involving arguments type safe while allowing * skipping the arguments for functions that don't require arguments. * * @public */ export type OptionalRestArgs = FuncRef["_args"] extends EmptyObject ? [args?: EmptyObject] : [args: FuncRef["_args"]]; /** * A tuple type of the (maybe optional) arguments to `FuncRef`, followed by an options * object of type `Options`. * * This type is used to make methods like `useQuery` type-safe while allowing * 1. Skipping arguments for functions that don't require arguments. * 2. Skipping the options object. * @public */ export type ArgsAndOptions = FuncRef["_args"] extends EmptyObject ? [args?: EmptyObject, options?: Options] : [args: FuncRef["_args"], options?: Options]; /** * Given a {@link FunctionReference}, get the return type of the function. * * @public */ export type FunctionReturnType = FuncRef["_returnType"]; type UndefinedToNull = T extends void ? null : T; type NullToUndefinedOrNull = T extends null ? T | undefined | void : T; /** * Convert the return type of a function to it's client-facing format. * * This means: * - Converting `undefined` and `void` to `null` * - Removing all `Promise` wrappers */ export type ConvertReturnType = UndefinedToNull>; export type ValidatorTypeToReturnType = Promise> | NullToUndefinedOrNull; export {}; //# sourceMappingURL=api.d.ts.map