UNPKG

kitcn

Version:

kitcn - React Query integration and CLI tools for Convex

74 lines (71 loc) 3.07 kB
import { getFunctionName, makeFunctionReference } from "convex/server"; //#region src/server/context-utils.ts const isQueryCtx = (ctx) => "db" in ctx; const isMutationCtx = (ctx) => "db" in ctx && "scheduler" in ctx; const isActionCtx = (ctx) => "runAction" in ctx; const isRunMutationCtx = (ctx) => "runMutation" in ctx; const isSchedulerCtx = (ctx) => "scheduler" in ctx && typeof ctx.scheduler === "object" && ctx.scheduler !== null; const requireQueryCtx = (ctx) => { if (!isQueryCtx(ctx)) throw new Error("Query context required"); return ctx; }; const requireMutationCtx = (ctx) => { if (!isMutationCtx(ctx)) throw new Error("Mutation context required"); return ctx; }; const requireActionCtx = (ctx) => { if (!isActionCtx(ctx)) { if (isSchedulerCtx(ctx)) throw new Error("Action context required. This ctx can schedule work but cannot call action procedures directly. Use requireSchedulerCtx(ctx) with caller.schedule.*."); throw new Error("Action context required"); } return ctx; }; const requireRunMutationCtx = (ctx) => { if (!isRunMutationCtx(ctx)) throw new Error("Mutation or action context required"); return ctx; }; const requireSchedulerCtx = (ctx) => { if (!isSchedulerCtx(ctx)) throw new Error("Mutation or action context with scheduler required"); return ctx; }; //#endregion //#region src/server/api-entry.ts function getGeneratedValue(root, path) { let current = root; for (let index = 0; index < path.length; index += 1) { const segment = path[index]; if (typeof current !== "object" || current === null) throw new Error(`[kitcn] Invalid generated path: ${path.join(".")}`); const directValue = current[segment]; if (directValue !== void 0) { current = directValue; continue; } let matched = false; for (let end = path.length - 1; end > index; end -= 1) { const collapsedSegment = path.slice(index, end + 1).join("/"); const collapsedValue = current[collapsedSegment]; if (collapsedValue === void 0) continue; current = collapsedValue; index = end; matched = true; break; } if (!matched) throw new Error(`[kitcn] Invalid generated path: ${path.join(".")}`); } return current; } /** * Build a generated API leaf from a Convex FunctionReference name. * Returns a plain FunctionReference-compatible object with attached cRPC metadata. */ function createApiLeaf(...args) { const [fnOrRoot, pathOrMeta, maybeMeta] = args; const meta = maybeMeta ?? pathOrMeta; const functionRef = makeFunctionReference(getFunctionName(Array.isArray(pathOrMeta) ? getGeneratedValue(fnOrRoot, pathOrMeta) : fnOrRoot)); return Object.assign(functionRef, meta, { functionRef }); } function createGeneratedFunctionReference(name) { return makeFunctionReference(name); } //#endregion export { isMutationCtx as a, isSchedulerCtx as c, requireQueryCtx as d, requireRunMutationCtx as f, isActionCtx as i, requireActionCtx as l, createGeneratedFunctionReference as n, isQueryCtx as o, requireSchedulerCtx as p, getGeneratedValue as r, isRunMutationCtx as s, createApiLeaf as t, requireMutationCtx as u };