convex
Version:
Client for the Convex Cloud
37 lines (34 loc) • 1.53 kB
text/typescript
declare const Convex: {
syscall: (op: string, jsonArgs: string) => string;
asyncSyscall: (op: string, jsonArgs: string) => Promise<string>;
};
/**
* Perform a syscall, taking in a JSON-encodable object as an argument, serializing with
* JSON.stringify, calling into Rust, and then parsing the response as a JSON-encodable
* value. If one of your arguments is a Convex value, you must call `convexToJson` on it
* before passing it to this function, and if the return value has a Convex value, you're
* also responsible for calling `jsonToConvex`: This layer only deals in JSON.
*/
export function performSyscall(op: string, arg: Record<string, any>): any {
if (typeof Convex === "undefined" || Convex.syscall === undefined) {
throw new Error(
"The Convex database and auth objects are being used outside of a Convex backend. " +
"Did you mean to use `useQuery` or `useMutation` to call a Convex function?"
);
}
const resultStr = Convex.syscall(op, JSON.stringify(arg));
return JSON.parse(resultStr);
}
export async function performAsyncSyscall(
op: string,
arg: Record<string, any>
): Promise<any> {
if (typeof Convex === "undefined" || Convex.syscall === undefined) {
throw new Error(
"The Convex database and auth objects are being used outside of a Convex backend. " +
"Did you mean to use `useQuery` or `useMutation` to call a Convex function?"
);
}
const resultStr = await Convex.asyncSyscall(op, JSON.stringify(arg));
return JSON.parse(resultStr);
}