convex
Version:
Client for the Convex Cloud
85 lines (84 loc) • 2.58 kB
JavaScript
;
import { functionName } from "./functionName.js";
import { getFunctionAddress } from "./components/paths.js";
export function getFunctionName(functionReference) {
const address = getFunctionAddress(functionReference);
if (address.name === void 0) {
if (address.functionHandle !== void 0) {
throw new Error(
`Expected function reference like "api.file.func" or "internal.file.func", but received function handle ${address.functionHandle}`
);
} else if (address.reference !== void 0) {
throw new Error(
`Expected function reference in the current component like "api.file.func" or "internal.file.func", but received reference ${address.reference}`
);
}
throw new Error(
`Expected function reference like "api.file.func" or "internal.file.func", but received ${JSON.stringify(address)}`
);
}
if (typeof functionReference === "string") return functionReference;
const name = functionReference[functionName];
if (!name) {
throw new Error(`${functionReference} is not a functionReference`);
}
return name;
}
export function makeFunctionReference(name) {
return { [functionName]: name };
}
function createApi(pathParts = []) {
const handler = {
get(_, prop) {
if (typeof prop === "string") {
const newParts = [...pathParts, prop];
return createApi(newParts);
} else if (prop === functionName) {
if (pathParts.length < 2) {
const found = ["api", ...pathParts].join(".");
throw new Error(
`API path is expected to be of the form \`api.moduleName.functionName\`. Found: \`${found}\``
);
}
const path = pathParts.slice(0, -1).join("/");
const exportName = pathParts[pathParts.length - 1];
if (exportName === "default") {
return path;
} else {
return path + ":" + exportName;
}
} else if (prop === Symbol.toStringTag) {
return "FunctionReference";
} else {
return void 0;
}
}
};
return new Proxy({}, handler);
}
export function filterApi(api) {
return api;
}
export function justInternal(api) {
return api;
}
export function justPublic(api) {
return api;
}
export function justQueries(api) {
return api;
}
export function justMutations(api) {
return api;
}
export function justActions(api) {
return api;
}
export function justPaginatedQueries(api) {
return api;
}
export function justSchedulable(api) {
return api;
}
export const anyApi = createApi();
//# sourceMappingURL=api.js.map