/** * Allows you to persist state server-side, associated with a sessionId stored * on the client (in localStorage, e.g.). * * You can define your function to take in a sessionId parameter of type * SessionId, which is just a branded string to help avoid errors. * The validator is vSessionId, or you can spread the argument in for your * function like this: * ```ts * const myMutation = mutation({ * args: { * arg1: v.number(), // whatever other args you want * ...SessionIdArg, * }, * handler: async (ctx, args) => { * // args.sessionId is a SessionId * }) * }); * ``` * * Then, on the client side, you can use {@link useSessionMutation} to call * your function with the sessionId automatically passed in, like: * ```ts * const myMutation = useSessionMutation(api.myModule.myMutation); * ... * await myMutation({ arg1: 123 }); * ``` * * To codify the sessionId parameter, you can use the customFunction module to * create a custom mutation or query, like: * ```ts * export const sessionMutation = customMutation(mutation, { * args: { ...SessionIdArg }, * input: (ctx, { sessionId }) => { * const anonUser = await getAnonymousUser(ctx, sessionId); * return { ctx: { anonUser }, args: {} }; * }, * }); * ``` * * Then you can define functions like: * ```ts * export const myMutation = sessionMutation({ * args: { arg1: v.number() }, // whatever other args you want * handler: async (ctx, args) => { * // ctx.anonUser exists and has a type from getAnonymousUser. * // args is { arg1: number } * }) * }); * ``` * * See the associated [Stack post](https://stack.convex.dev/track-sessions-without-cookies) * for more information. */ import type { FunctionArgs, FunctionReference, FunctionReturnType, GenericActionCtx, GenericDataModel } from "convex/server"; import type { Validator } from "convex/values"; import type { BetterOmit, EmptyObject } from "../index.js"; export type SessionId = string & { __SessionId: true; }; export declare const vSessionId: Validator; export declare const SessionIdArg: { sessionId: Validator; }; type SessionFunction = FunctionReference; type SessionArgsArray> = keyof FunctionArgs extends "sessionId" ? [args?: EmptyObject] : [args: BetterOmit, "sessionId">]; export interface RunSessionFunctions { /** * Run the Convex query with the given name and arguments. * * Consider using an {@link internalQuery} to prevent users from calling the * query directly. * * @param query - A {@link FunctionReference} for the query to run. * @param args - The arguments to the query function. * @returns A promise of the query's result. */ runSessionQuery>(query: Query, ...args: SessionArgsArray): Promise>; /** * Run the Convex mutation with the given name and arguments. * * Consider using an {@link internalMutation} to prevent users from calling * the mutation directly. * * @param mutation - A {@link FunctionReference} for the mutation to run. * @param args - The arguments to the mutation function. * @returns A promise of the mutation's result. */ runSessionMutation>(mutation: Mutation, ...args: SessionArgsArray): Promise>; /** * Run the Convex action with the given name and arguments. * * Consider using an {@link internalAction} to prevent users from calling the * action directly. * * @param action - A {@link FunctionReference} for the action to run. * @param args - The arguments to the action function. * @returns A promise of the action's result. */ runSessionAction>(action: Action, ...args: SessionArgsArray): Promise>; } export declare function runSessionFunctions(ctx: GenericActionCtx, sessionId: SessionId): RunSessionFunctions; export {}; //# sourceMappingURL=sessions.d.ts.map