UNPKG

@nocobase/flow-engine

Version:

A standalone flow engine for NocoBase, managing workflows, models, and actions.

147 lines (145 loc) 5.24 kB
/** * 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 flowResource_exports = {}; __export(flowResource_exports, { FlowResource: () => FlowResource, ResourceError: () => ResourceError }); module.exports = __toCommonJS(flowResource_exports); var import_reactive = require("@formily/reactive"); var import_flowContext = require("../flowContext"); function toErrMessages(error) { var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k; if (typeof ((_a = error == null ? void 0 : error.response) == null ? void 0 : _a.data) === "string") { const tempElement = document.createElement("div"); tempElement.innerHTML = (_b = error == null ? void 0 : error.response) == null ? void 0 : _b.data; let message = tempElement.textContent || tempElement.innerText; if (message.includes("Error occurred while trying")) { message = "The application may be starting up. Please try again later."; return [{ code: "APP_WARNING", message }]; } if (message.includes("502 Bad Gateway")) { message = "The application may be starting up. Please try again later."; return [{ code: "APP_WARNING", message }]; } return [{ message }]; } if ((_d = (_c = error == null ? void 0 : error.response) == null ? void 0 : _c.data) == null ? void 0 : _d.error) { return [(_f = (_e = error == null ? void 0 : error.response) == null ? void 0 : _e.data) == null ? void 0 : _f.error]; } return ((_h = (_g = error == null ? void 0 : error.response) == null ? void 0 : _g.data) == null ? void 0 : _h.errors) || ((_j = (_i = error == null ? void 0 : error.response) == null ? void 0 : _i.data) == null ? void 0 : _j.messages) || ((_k = error == null ? void 0 : error.response) == null ? void 0 : _k.error) || [{ message: error.message || "Server error" }]; } __name(toErrMessages, "toErrMessages"); const _ResourceError = class _ResourceError extends Error { data; constructor(error) { const data = toErrMessages(error).shift(); super(data.message); this.name = "ResponseError"; } get code() { var _a; return ((_a = this.data) == null ? void 0 : _a.code) || "UNKNOWN_ERROR"; } get message() { var _a; return ((_a = this.data) == null ? void 0 : _a.message) || "An unknown error occurred"; } }; __name(_ResourceError, "ResourceError"); let ResourceError = _ResourceError; const _FlowResource = class _FlowResource { _data = import_reactive.observable.ref(null); _meta = import_reactive.observable.ref({}); _error = import_reactive.observable.ref(null); context; constructor(context) { this.context = new import_flowContext.FlowContext(); this.context.addDelegate(context); } getData() { return this._data.value; } hasData() { const data = this.getData(); if (Array.isArray(data)) { return data.length > 0; } else if (data && typeof data === "object") { return Object.keys(data).length > 0; } return false; } setData(value) { this._data.value = value; return this; } getMeta(metaKey) { return metaKey ? this._meta.value[metaKey] : this._meta.value; } setMeta(meta) { this._meta.value = { ...this._meta.value, ...meta }; return this; } get error() { return this._error.value; } getError() { return this._error.value; } setError(error) { this._error.value = error; return this; } clearError() { this._error.value = null; return this; } events = {}; on(event, callback) { (this.events[event] ||= []).push(callback); } once(event, callback) { const wrapper = /* @__PURE__ */ __name((...args) => { callback(...args); this.off(event, wrapper); }, "wrapper"); this.on(event, wrapper); } off(event, callback) { this.events[event] = (this.events[event] || []).filter((fn) => fn !== callback); } emit(event, ...args) { (this.events[event] || []).forEach((fn) => fn(...args)); } }; __name(_FlowResource, "FlowResource"); let FlowResource = _FlowResource; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { FlowResource, ResourceError });