convex
Version:
Client for the Convex Cloud
101 lines (98 loc) • 4.24 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var runTestFunction_exports = {};
__export(runTestFunction_exports, {
INLINE_QUERY_DESCRIPTION: () => INLINE_QUERY_DESCRIPTION,
RUN_ONEOFF_QUERY_SOURCE_DESCRIPTION: () => RUN_ONEOFF_QUERY_SOURCE_DESCRIPTION,
inlineQueryToQuerySource: () => inlineQueryToQuerySource,
runTestFunctionQuery: () => runTestFunctionQuery
});
module.exports = __toCommonJS(runTestFunction_exports);
var import_utils = require("./utils/utils.js");
const QUERY_MODULE_PREAMBLE = 'import { query, internalQuery } from "convex:/_system/repl/wrappers.js";';
const RUN_ONEOFF_QUERY_SOURCE_DESCRIPTION = 'JavaScript module source for a single file (testQuery.js) that exports a default readonly query, for example: export default query({ handler: async (ctx) => ({ count: (await ctx.db.query("messages").take(10)).length }) });';
const INLINE_QUERY_DESCRIPTION = "JavaScript to evaluate as a readonly query, for example: 'await ctx.db.query(\"messages\").take(5)'. Simple expressions are returned automatically. For multi-statement queries, use an explicit return. Full `export default query(...)` modules are also supported. This is a one-shot query and cannot be combined with `--watch`. Use `--component` to target a mounted component. For more examples, see `npx convex docs`.";
function inlineQueryToQuerySource(inlineQuery) {
const trimmedQuery = inlineQuery.trim();
if (looksLikeQueryModuleSource(trimmedQuery)) {
return injectQueryModulePreamble(trimmedQuery);
}
const queryBody = inlineQueryBody(trimmedQuery);
return `${QUERY_MODULE_PREAMBLE}
export default query({
handler: async (ctx) => {
${indent(queryBody, 4)}
},
});`;
}
async function runTestFunctionQuery(ctx, args) {
const fetchDeployment = (0, import_utils.deploymentFetch)(ctx, {
deploymentUrl: args.deploymentUrl,
adminKey: args.adminKey
});
const response = await fetchDeployment("/api/run_test_function", {
method: "POST",
body: JSON.stringify({
adminKey: args.adminKey,
args: {},
bundle: {
path: "testQuery.js",
source: args.querySource
},
format: "convex_encoded_json",
...args.componentId !== void 0 ? { componentId: args.componentId } : {}
})
});
const result = await response.json();
if (typeof result !== "object" || result === null || !("status" in result) || result.status !== "success") {
return { kind: "applicationFailure", payload: result };
}
const ok = result;
return {
kind: "success",
value: ok.value,
logLines: ok.logLines ?? []
};
}
function looksLikeQueryModuleSource(querySource) {
if (!querySource.includes("export default")) return false;
return /\b(?:query|internalQuery)\s*\(/.test(querySource);
}
function injectQueryModulePreamble(querySource) {
if (querySource.includes("convex:/_system/repl/wrappers.js"))
return querySource;
return `${QUERY_MODULE_PREAMBLE}
${querySource}`;
}
function inlineQueryBody(inlineQuery) {
const trimmed = inlineQuery.trim();
if (!isExpression(trimmed)) return trimmed;
return `return (${trimmed.replace(/;$/, "")});`;
}
function isExpression(inlineQuery) {
if (inlineQuery.includes("\n")) return false;
return !/^(const|let|var|if|for|while|switch|try|throw|return)\b/.test(
inlineQuery
);
}
function indent(text, spaces) {
const prefix = " ".repeat(spaces);
return text.split("\n").map((line) => `${prefix}${line}`).join("\n");
}
//# sourceMappingURL=runTestFunction.js.map