convex
Version:
Client for the Convex Cloud
62 lines (61 loc) • 2.02 kB
JavaScript
;
import { convexToJson, jsonToConvex } from "../../values/index.js";
import { version } from "../../index.js";
import { performAsyncSyscall } from "./syscall.js";
import { parseArgs } from "../../common/index.js";
import { functionName } from "../../server/api.js";
import { extractReferencePath, isFunctionHandle } from "../components/index.js";
function syscallArgs(requestId, functionReference, args) {
const address = getFunctionAddress(functionReference);
return {
...address,
args: convexToJson(parseArgs(args)),
version,
requestId
};
}
export function getFunctionAddress(functionReference) {
let functionAddress;
if (typeof functionReference === "string") {
if (isFunctionHandle(functionReference)) {
functionAddress = { functionHandle: functionReference };
} else {
functionAddress = { name: functionReference };
}
} else if (functionReference[functionName]) {
functionAddress = { name: functionReference[functionName] };
} else {
const referencePath = extractReferencePath(functionReference);
if (!referencePath) {
throw new Error(`${functionReference} is not a functionReference`);
}
functionAddress = { reference: referencePath };
}
return functionAddress;
}
export function setupActionCalls(requestId) {
return {
runQuery: async (query, args) => {
const result = await performAsyncSyscall(
"1.0/actions/query",
syscallArgs(requestId, query, args)
);
return jsonToConvex(result);
},
runMutation: async (mutation, args) => {
const result = await performAsyncSyscall(
"1.0/actions/mutation",
syscallArgs(requestId, mutation, args)
);
return jsonToConvex(result);
},
runAction: async (action, args) => {
const result = await performAsyncSyscall(
"1.0/actions/action",
syscallArgs(requestId, action, args)
);
return jsonToConvex(result);
}
};
}
//# sourceMappingURL=actions_impl.js.map