@nocobase/flow-engine
Version:
A standalone flow engine for NocoBase, managing workflows, models, and actions.
151 lines (149 loc) • 6.76 kB
JavaScript
/**
* This file is part of the NocoBase (R) project.
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
* Authors: NocoBase Team.
*
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
* For more information, please refer to: https://www.nocobase.com/agreement.
*/
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
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 variablesParams_exports = {};
__export(variablesParams_exports, {
buildRecordMeta: () => buildRecordMeta,
collectContextParamsForTemplate: () => collectContextParamsForTemplate,
createCurrentRecordMetaFactory: () => createCurrentRecordMetaFactory,
createRecordMetaFactory: () => createRecordMetaFactory,
extractUsedVariableNames: () => extractUsedVariableNames,
extractUsedVariablePaths: () => extractUsedVariablePaths,
inferParentRecordRef: () => inferParentRecordRef,
inferRecordRef: () => inferRecordRef
});
module.exports = __toCommonJS(variablesParams_exports);
var import_createCollectionContextMeta = require("./createCollectionContextMeta");
var import_serverContextParams = require("../utils/serverContextParams");
var import_client = require("@nocobase/utils/client");
function isObject(value) {
return typeof value === "object" && value !== null;
}
__name(isObject, "isObject");
function getResource(ctx) {
const r = ctx["resource"];
return isObject(r) ? r : void 0;
}
__name(getResource, "getResource");
function getCollection(ctx) {
const c = ctx["collection"];
if (isObject(c)) {
const mc = c;
return { name: mc.name, dataSourceKey: mc.dataSourceKey };
}
return void 0;
}
__name(getCollection, "getCollection");
function inferRecordRef(ctx) {
var _a, _b, _c;
const resource = getResource(ctx);
const collectionObj = getCollection(ctx);
const resourceName = (collectionObj == null ? void 0 : collectionObj.name) || ((_a = resource == null ? void 0 : resource.getResourceName) == null ? void 0 : _a.call(resource));
const dataSourceKey = (collectionObj == null ? void 0 : collectionObj.dataSourceKey) || ((_b = resource == null ? void 0 : resource.getDataSourceKey) == null ? void 0 : _b.call(resource));
const filterByTk = (_c = resource == null ? void 0 : resource.getFilterByTk) == null ? void 0 : _c.call(resource);
if (!resourceName || typeof filterByTk === "undefined" || filterByTk === null) return void 0;
const parts = String(resourceName).split(".");
const collection = parts[parts.length - 1];
return { collection, dataSourceKey, filterByTk };
}
__name(inferRecordRef, "inferRecordRef");
function inferParentRecordRef(ctx) {
var _a, _b, _c, _d;
const resource = getResource(ctx);
const dataSourceKey = ((_a = getCollection(ctx)) == null ? void 0 : _a.dataSourceKey) || ((_b = resource == null ? void 0 : resource.getDataSourceKey) == null ? void 0 : _b.call(resource));
const rn = (_c = resource == null ? void 0 : resource.getResourceName) == null ? void 0 : _c.call(resource);
const sourceId = (_d = resource == null ? void 0 : resource.getSourceId) == null ? void 0 : _d.call(resource);
if (!rn || typeof sourceId === "undefined" || sourceId === null) return void 0;
const parts = String(rn).split(".");
if (parts.length < 2) return void 0;
return { collection: parts[0], dataSourceKey, filterByTk: sourceId };
}
__name(inferParentRecordRef, "inferParentRecordRef");
async function buildRecordMeta(collectionAccessor, title, paramsBuilder) {
const base = await (0, import_createCollectionContextMeta.createCollectionContextMeta)(collectionAccessor, title)();
if (!base) return null;
return {
...base,
buildVariablesParams: /* @__PURE__ */ __name((ctx) => paramsBuilder ? paramsBuilder(ctx) : inferRecordRef(ctx), "buildVariablesParams")
};
}
__name(buildRecordMeta, "buildRecordMeta");
function createRecordMetaFactory(collectionAccessor, title, paramsBuilder) {
const factory = /* @__PURE__ */ __name(async () => buildRecordMeta(collectionAccessor, title, paramsBuilder), "factory");
factory.title = title;
return factory;
}
__name(createRecordMetaFactory, "createRecordMetaFactory");
function createCurrentRecordMetaFactory(ctx, collectionAccessor) {
var _a;
const title = ((_a = ctx == null ? void 0 : ctx.t) == null ? void 0 : _a.call(ctx, "Current record")) || "Current record";
return createRecordMetaFactory(collectionAccessor, title, (c) => inferRecordRef(c));
}
__name(createCurrentRecordMetaFactory, "createCurrentRecordMetaFactory");
const extractUsedVariableNames = import_client.extractUsedVariableNames;
const extractUsedVariablePaths = import_client.extractUsedVariablePaths;
async function collectContextParamsForTemplate(ctx, template) {
var _a;
try {
const usage = (0, import_client.extractUsedVariablePaths)(template) || {};
const varNames = Object.keys(usage);
if (!varNames.length) return void 0;
const input = {};
for (const key of varNames) {
const opt = (_a = ctx.getPropertyOptions) == null ? void 0 : _a.call(ctx, key);
const meta = opt == null ? void 0 : opt.meta;
if (!meta) continue;
let built;
try {
const resolvedMeta = typeof meta === "function" ? await meta() : meta;
const builder = resolvedMeta == null ? void 0 : resolvedMeta.buildVariablesParams;
if (typeof builder === "function") {
built = await builder(ctx);
}
} catch (err) {
continue;
}
if (built) {
input[key] = built;
}
}
return (0, import_serverContextParams.buildServerContextParams)(ctx, input);
} catch (_) {
return void 0;
}
}
__name(collectContextParamsForTemplate, "collectContextParamsForTemplate");
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
buildRecordMeta,
collectContextParamsForTemplate,
createCurrentRecordMetaFactory,
createRecordMetaFactory,
extractUsedVariableNames,
extractUsedVariablePaths,
inferParentRecordRef,
inferRecordRef
});