kitcn
Version:
kitcn - React Query integration and CLI tools for Convex
121 lines (119 loc) • 3.09 kB
JavaScript
import { getFunctionName } from "convex/server";
//#region src/crpc/query-options.ts
/**
* Query options factory for Convex query function subscriptions.
* Requires `convexQueryClient.queryFn()` set as the default `queryFn` globally.
*/
function convexQuery(funcRef, args, meta, opts) {
const finalArgs = args ?? {};
const isSkip = finalArgs === "skip";
const funcName = getFunctionName(funcRef);
const [namespace, fnName] = funcName.split(":");
const authType = meta?.[namespace]?.[fnName]?.auth;
const skipUnauth = opts?.skipUnauth;
return {
queryKey: [
"convexQuery",
funcName,
isSkip ? "skip" : finalArgs
],
staleTime: Number.POSITIVE_INFINITY,
refetchInterval: false,
refetchOnMount: false,
refetchOnReconnect: false,
refetchOnWindowFocus: false,
...isSkip ? { enabled: false } : {},
meta: {
authType,
skipUnauth,
subscribe: true
}
};
}
/**
* Query options factory for Convex action functions.
* Actions are NOT reactive - they follow normal TanStack Query semantics.
*
* @example
* ```ts
* useQuery(convexAction(api.ai.generate, { prompt }))
* ```
*
* @example With additional options (use spread):
* ```ts
* useQuery({
* ...convexAction(api.files.process, { fileId }),
* staleTime: 60_000
* });
* ```
*/
function convexAction(funcRef, args, meta, opts) {
const finalArgs = args ?? {};
const isSkip = finalArgs === "skip";
const funcName = getFunctionName(funcRef);
const [namespace, fnName] = funcName.split(":");
const authType = meta?.[namespace]?.[fnName]?.auth;
const skipUnauth = opts?.skipUnauth;
return {
queryKey: [
"convexAction",
funcName,
isSkip ? {} : finalArgs
],
staleTime: Number.POSITIVE_INFINITY,
refetchInterval: false,
refetchOnMount: false,
refetchOnReconnect: false,
refetchOnWindowFocus: false,
...isSkip ? { enabled: false } : {},
meta: {
authType,
skipUnauth,
subscribe: false
}
};
}
/**
* Infinite query options factory for paginated Convex queries.
* Server-safe (non-hook) - can be used in RSC.
*
* Uses flat { cursor, limit } input like tRPC.
*/
function convexInfiniteQueryOptions(funcRef, args, opts = {}, meta) {
const { limit, skipUnauth, enabled, ...queryOptions } = opts;
const finalArgs = args === "skip" ? {} : args;
const isSkip = args === "skip";
const funcName = getFunctionName(funcRef);
const [namespace, fnName] = funcName.split(":");
const authType = (meta?.[namespace]?.[fnName])?.auth;
const firstPageArgs = {
...finalArgs,
cursor: null,
limit
};
const finalEnabled = enabled === false || isSkip ? false : void 0;
return {
queryKey: [
"convexQuery",
funcName,
firstPageArgs
],
staleTime: Number.POSITIVE_INFINITY,
refetchInterval: false,
refetchOnMount: false,
refetchOnReconnect: false,
refetchOnWindowFocus: false,
...queryOptions,
...finalEnabled === false ? { enabled: false } : {},
meta: {
authType,
skipUnauth,
subscribe: true,
queryName: funcName,
args: finalArgs,
limit
}
};
}
//#endregion
export { convexInfiniteQueryOptions as n, convexQuery as r, convexAction as t };