@tachybase/module-event-source 0.23.41 → 0.23.47
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client/{customEventSources → custom-event-sources}/CustomEventSourcePane.schema.d.ts +8 -4
- package/dist/client/{customEventSources/customEventSourcePane.collection.d.ts → custom-event-sources/collectionCustomEventSources.d.ts} +1 -1
- package/dist/client/index.d.ts +3 -0
- package/dist/client/index.js +3 -3
- package/dist/client/triggers/APPEventTrigger.d.ts +15 -0
- package/dist/client/triggers/CustomActionTrigger.d.ts +25 -0
- package/dist/client/triggers/DatabaseEventTrigger.d.ts +35 -0
- package/dist/client/triggers/ResourceEventTrigger.d.ts +24 -0
- package/dist/client/triggers/index.d.ts +19 -0
- package/dist/client/webhook/collections/dispatchers.d.ts +3 -0
- package/dist/client/webhook/components/TypeContainer.d.ts +1 -0
- package/dist/externalVersion.js +10 -10
- package/dist/locale/en-US.json +22 -1
- package/dist/locale/zh-CN.json +39 -1
- package/dist/server/collections/webhooks.js +13 -0
- package/dist/server/constants.d.ts +2 -0
- package/dist/server/constants.js +30 -0
- package/dist/server/migrations/20250210192452-refactor.d.ts +6 -0
- package/dist/server/migrations/20250210192452-refactor.js +45 -0
- package/dist/server/model/EventSourceModel.d.ts +16 -0
- package/dist/server/model/EventSourceModel.js +29 -0
- package/dist/server/trigger/AppEventTrigger.d.ts +10 -0
- package/dist/server/trigger/AppEventTrigger.js +93 -0
- package/dist/server/trigger/CustionActionTrigger.d.ts +13 -0
- package/dist/server/trigger/CustionActionTrigger.js +76 -0
- package/dist/server/trigger/DatabaseEventTrigger.d.ts +10 -0
- package/dist/server/trigger/DatabaseEventTrigger.js +104 -0
- package/dist/server/trigger/ResourceEventTrigger.d.ts +14 -0
- package/dist/server/trigger/ResourceEventTrigger.js +149 -0
- package/dist/server/trigger/Trigger.d.ts +21 -0
- package/dist/server/trigger/Trigger.js +71 -0
- package/dist/server/types.d.ts +6 -0
- package/dist/server/types.js +15 -0
- package/dist/server/webhooks/Plugin.d.ts +5 -0
- package/dist/server/webhooks/Plugin.js +114 -64
- package/dist/server/webhooks/webhooks.js +2 -1
- package/package.json +12 -12
- package/dist/server/services/ActionAPIService.d.ts +0 -4
- package/dist/server/services/ActionAPIService.js +0 -122
- package/dist/server/services/ApplicationEventService.d.ts +0 -5
- package/dist/server/services/ApplicationEventService.js +0 -124
- package/dist/server/services/BeforeResourceService.d.ts +0 -5
- package/dist/server/services/BeforeResourceService.js +0 -135
- package/dist/server/services/DatabaseEventService.d.ts +0 -5
- package/dist/server/services/DatabaseEventService.js +0 -135
- package/dist/server/services/ResourceService.d.ts +0 -5
- package/dist/server/services/ResourceService.js +0 -114
- /package/dist/client/{customEventSources → custom-event-sources}/CustomEventSourcePane.d.ts +0 -0
- /package/dist/client/{customEventSources → custom-event-sources}/useSyncCustomEventSource.d.ts +0 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var AppEventTrigger_exports = {};
|
|
19
|
+
__export(AppEventTrigger_exports, {
|
|
20
|
+
AppEventTrigger: () => AppEventTrigger
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(AppEventTrigger_exports);
|
|
23
|
+
var import_module_workflow = require("@tachybase/module-workflow");
|
|
24
|
+
var import_eval_simulate = require("../utils/eval-simulate");
|
|
25
|
+
var import_Trigger = require("./Trigger");
|
|
26
|
+
class AppEventTrigger extends import_Trigger.EventSourceTrigger {
|
|
27
|
+
constructor() {
|
|
28
|
+
super(...arguments);
|
|
29
|
+
this.eventMap = /* @__PURE__ */ new Map();
|
|
30
|
+
}
|
|
31
|
+
load(model) {
|
|
32
|
+
if (!model.options) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const {
|
|
36
|
+
options: { eventName },
|
|
37
|
+
workflowKey
|
|
38
|
+
} = model;
|
|
39
|
+
this.app.logger.info("Add application event listener", { meta: { eventName, workflowKey } });
|
|
40
|
+
const callback = this.getAppEvent(model).bind(this);
|
|
41
|
+
this.app.on(eventName, callback);
|
|
42
|
+
this.eventMap.set(model.id, callback);
|
|
43
|
+
}
|
|
44
|
+
afterCreate(model) {
|
|
45
|
+
this.load(model);
|
|
46
|
+
}
|
|
47
|
+
afterUpdate(model) {
|
|
48
|
+
if (model.enabled && !this.workSet.has(model.id)) {
|
|
49
|
+
this.load(model);
|
|
50
|
+
} else if (!model.enabled && this.workSet.has(model.id)) {
|
|
51
|
+
this.app.db.off(model.options.eventName, this.eventMap.get(model.id));
|
|
52
|
+
this.eventMap.delete(model.id);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
afterDestroy(model) {
|
|
56
|
+
const callback = this.eventMap.get(model.id);
|
|
57
|
+
if (!callback) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
this.app.db.off(model.options.eventName, callback);
|
|
61
|
+
this.eventMap.delete(model.id);
|
|
62
|
+
}
|
|
63
|
+
getAppEvent(model) {
|
|
64
|
+
const { code, workflowKey } = model;
|
|
65
|
+
return async () => {
|
|
66
|
+
const webhookCtx = {
|
|
67
|
+
body: ""
|
|
68
|
+
};
|
|
69
|
+
try {
|
|
70
|
+
await (0, import_eval_simulate.evalSimulate)(code, {
|
|
71
|
+
ctx: webhookCtx,
|
|
72
|
+
lib: {
|
|
73
|
+
JSON,
|
|
74
|
+
Math
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
} catch (err) {
|
|
78
|
+
this.app.logger.error(err);
|
|
79
|
+
}
|
|
80
|
+
if (!workflowKey) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const pluginWorkflow = this.app.getPlugin(import_module_workflow.PluginWorkflow);
|
|
84
|
+
const wfRepo = this.app.db.getRepository("workflows");
|
|
85
|
+
const wf = await wfRepo.findOne({ filter: { key: workflowKey, enabled: true } });
|
|
86
|
+
await pluginWorkflow.trigger(wf, { data: webhookCtx.body }, {});
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
91
|
+
0 && (module.exports = {
|
|
92
|
+
AppEventTrigger
|
|
93
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { EventSourceModel } from '../model/EventSourceModel';
|
|
2
|
+
import { EventSourceTrigger } from './Trigger';
|
|
3
|
+
type IAPITriggerConfig = {
|
|
4
|
+
code: string;
|
|
5
|
+
workflowKey: string;
|
|
6
|
+
};
|
|
7
|
+
export declare class CustomActionTrigger extends EventSourceTrigger {
|
|
8
|
+
eventMap: Map<number, IAPITriggerConfig>;
|
|
9
|
+
load(model: EventSourceModel): void;
|
|
10
|
+
afterCreate(model: EventSourceModel): void;
|
|
11
|
+
afterUpdate(model: EventSourceModel): void;
|
|
12
|
+
}
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var CustionActionTrigger_exports = {};
|
|
19
|
+
__export(CustionActionTrigger_exports, {
|
|
20
|
+
CustomActionTrigger: () => CustomActionTrigger
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(CustionActionTrigger_exports);
|
|
23
|
+
var import_webhooks = require("../webhooks/webhooks");
|
|
24
|
+
var import_Trigger = require("./Trigger");
|
|
25
|
+
class CustomActionTrigger extends import_Trigger.EventSourceTrigger {
|
|
26
|
+
constructor() {
|
|
27
|
+
super(...arguments);
|
|
28
|
+
this.eventMap = /* @__PURE__ */ new Map();
|
|
29
|
+
}
|
|
30
|
+
load(model) {
|
|
31
|
+
if (!model.options) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
const app = this.app;
|
|
35
|
+
const {
|
|
36
|
+
options: { actionName, resourceName },
|
|
37
|
+
id,
|
|
38
|
+
code,
|
|
39
|
+
workflowKey
|
|
40
|
+
} = model;
|
|
41
|
+
this.eventMap.set(id, { code, workflowKey });
|
|
42
|
+
if (!app.resourcer.isDefined(resourceName)) {
|
|
43
|
+
app.resourcer.define({ name: resourceName });
|
|
44
|
+
}
|
|
45
|
+
if (app.resourcer.getResource(resourceName).actions.has(actionName)) {
|
|
46
|
+
app.logger.warn(`${resourceName}:${actionName} action handler exists`);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
app.logger.info(`Add ${resourceName}:${actionName} action handler`);
|
|
50
|
+
app.resourcer.getResource(resourceName).addAction(actionName, async (ctx) => {
|
|
51
|
+
if (this.realTimeRefresh && !this.workSet.has(id)) {
|
|
52
|
+
ctx.throw(404, "Not found");
|
|
53
|
+
}
|
|
54
|
+
const { code: code2, workflowKey: workflowKey2 } = this.eventMap.get(id);
|
|
55
|
+
const body = await new import_webhooks.WebhookController().action(ctx, { code: code2 });
|
|
56
|
+
await new import_webhooks.WebhookController().triggerWorkflow(ctx, { workflowKey: workflowKey2 }, body);
|
|
57
|
+
});
|
|
58
|
+
app.acl.allow(resourceName, actionName, "loggedIn");
|
|
59
|
+
}
|
|
60
|
+
afterCreate(model) {
|
|
61
|
+
this.load(model);
|
|
62
|
+
}
|
|
63
|
+
// TODO 很难修改和删除,目前实时刷新的时候间接修改
|
|
64
|
+
afterUpdate(model) {
|
|
65
|
+
const { enabled, code, workflowKey, id } = model;
|
|
66
|
+
if (enabled && !this.workSet.has(id)) {
|
|
67
|
+
this.load(model);
|
|
68
|
+
} else {
|
|
69
|
+
this.eventMap.set(id, { code, workflowKey });
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
74
|
+
0 && (module.exports = {
|
|
75
|
+
CustomActionTrigger
|
|
76
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { EventSourceModel } from '../model/EventSourceModel';
|
|
2
|
+
import { EventSourceTrigger } from './Trigger';
|
|
3
|
+
export declare class DatabaseEventTrigger extends EventSourceTrigger {
|
|
4
|
+
eventMap: Map<number, (...args: any[]) => void>;
|
|
5
|
+
load(model: EventSourceModel): void;
|
|
6
|
+
getDbEvent(model: EventSourceModel): (model: any, options: any) => Promise<void>;
|
|
7
|
+
afterCreate(model: EventSourceModel): void;
|
|
8
|
+
afterUpdate(model: EventSourceModel): void;
|
|
9
|
+
afterDestroy(model: EventSourceModel): void;
|
|
10
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var DatabaseEventTrigger_exports = {};
|
|
19
|
+
__export(DatabaseEventTrigger_exports, {
|
|
20
|
+
DatabaseEventTrigger: () => DatabaseEventTrigger
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(DatabaseEventTrigger_exports);
|
|
23
|
+
var import_module_workflow = require("@tachybase/module-workflow");
|
|
24
|
+
var import_eval_simulate = require("../utils/eval-simulate");
|
|
25
|
+
var import_Trigger = require("./Trigger");
|
|
26
|
+
class DatabaseEventTrigger extends import_Trigger.EventSourceTrigger {
|
|
27
|
+
constructor() {
|
|
28
|
+
super(...arguments);
|
|
29
|
+
this.eventMap = /* @__PURE__ */ new Map();
|
|
30
|
+
}
|
|
31
|
+
load(model) {
|
|
32
|
+
if (!model.options) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const {
|
|
36
|
+
options: { eventName },
|
|
37
|
+
workflowKey,
|
|
38
|
+
code
|
|
39
|
+
} = model;
|
|
40
|
+
this.app.logger.info("Add database event listener", { meta: { eventName, workflowKey } });
|
|
41
|
+
const callback = this.getDbEvent(model).bind(this);
|
|
42
|
+
this.app.db.on(eventName, callback);
|
|
43
|
+
this.eventMap.set(model.id, callback);
|
|
44
|
+
}
|
|
45
|
+
getDbEvent(model) {
|
|
46
|
+
const { code, workflowKey } = model;
|
|
47
|
+
return async (model2, options) => {
|
|
48
|
+
var _a;
|
|
49
|
+
const webhookCtx = {
|
|
50
|
+
body: "",
|
|
51
|
+
model: model2,
|
|
52
|
+
options
|
|
53
|
+
};
|
|
54
|
+
try {
|
|
55
|
+
await (0, import_eval_simulate.evalSimulate)(code, {
|
|
56
|
+
ctx: webhookCtx,
|
|
57
|
+
lib: {
|
|
58
|
+
JSON,
|
|
59
|
+
Math
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
} catch (err) {
|
|
63
|
+
this.app.logger.error(err);
|
|
64
|
+
}
|
|
65
|
+
if (!workflowKey) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const pluginWorkflow = this.app.getPlugin(import_module_workflow.PluginWorkflow);
|
|
69
|
+
const wfRepo = this.app.db.getRepository("workflows");
|
|
70
|
+
const wf = await wfRepo.findOne({ filter: { key: workflowKey, enabled: true } });
|
|
71
|
+
const result = await pluginWorkflow.trigger(
|
|
72
|
+
wf,
|
|
73
|
+
{ data: webhookCtx.body },
|
|
74
|
+
{ dbModel: model2, dbOptions: options, ...options }
|
|
75
|
+
);
|
|
76
|
+
if ((result == null ? void 0 : result.lastSavedJob.status) === import_module_workflow.JOB_STATUS.ERROR) {
|
|
77
|
+
throw new Error((_a = result.lastSavedJob) == null ? void 0 : _a.result);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
afterCreate(model) {
|
|
82
|
+
this.load(model);
|
|
83
|
+
}
|
|
84
|
+
afterUpdate(model) {
|
|
85
|
+
if (model.enabled && !this.workSet.has(model.id)) {
|
|
86
|
+
this.load(model);
|
|
87
|
+
} else if (!model.enabled && this.workSet.has(model.id)) {
|
|
88
|
+
this.app.db.off(model.options.eventName, this.eventMap.get(model.id));
|
|
89
|
+
this.eventMap.delete(model.id);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
afterDestroy(model) {
|
|
93
|
+
const callback = this.eventMap.get(model.id);
|
|
94
|
+
if (!callback) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
this.app.db.off(model.options.eventName, callback);
|
|
98
|
+
this.eventMap.delete(model.id);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
102
|
+
0 && (module.exports = {
|
|
103
|
+
DatabaseEventTrigger
|
|
104
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { EventSourceModel } from '../model/EventSourceModel';
|
|
2
|
+
import { EventSourceTrigger } from './Trigger';
|
|
3
|
+
export declare class ResourceEventTrigger extends EventSourceTrigger {
|
|
4
|
+
private beforeList;
|
|
5
|
+
private afterList;
|
|
6
|
+
load(model: EventSourceModel): void;
|
|
7
|
+
afterAllLoad(): void;
|
|
8
|
+
private getMatchList;
|
|
9
|
+
afterCreate(model: EventSourceModel): void;
|
|
10
|
+
afterUpdate(model: EventSourceModel): void;
|
|
11
|
+
private afterUpdateList;
|
|
12
|
+
afterDestroy(model: EventSourceModel): void;
|
|
13
|
+
private afterDestroyList;
|
|
14
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var ResourceEventTrigger_exports = {};
|
|
19
|
+
__export(ResourceEventTrigger_exports, {
|
|
20
|
+
ResourceEventTrigger: () => ResourceEventTrigger
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(ResourceEventTrigger_exports);
|
|
23
|
+
var import_module_workflow = require("@tachybase/module-workflow");
|
|
24
|
+
var import_webhooks = require("../webhooks/webhooks");
|
|
25
|
+
var import_Trigger = require("./Trigger");
|
|
26
|
+
class ResourceEventTrigger extends import_Trigger.EventSourceTrigger {
|
|
27
|
+
constructor() {
|
|
28
|
+
super(...arguments);
|
|
29
|
+
// 优先级越小越靠前
|
|
30
|
+
this.beforeList = [];
|
|
31
|
+
this.afterList = [];
|
|
32
|
+
}
|
|
33
|
+
load(model) {
|
|
34
|
+
if (!model.options) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const { type } = model;
|
|
38
|
+
const prefix = type.substring(0, type.indexOf("Resource"));
|
|
39
|
+
if (prefix === "before") {
|
|
40
|
+
this.beforeList.push(model.toJSON());
|
|
41
|
+
} else {
|
|
42
|
+
this.afterList.push(model.toJSON());
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
afterAllLoad() {
|
|
46
|
+
this.app.resourcer.use(
|
|
47
|
+
async (ctx, next) => {
|
|
48
|
+
const { resourceName, actionName } = ctx.action;
|
|
49
|
+
if (!this.beforeList.length && !this.afterList.length) {
|
|
50
|
+
return next();
|
|
51
|
+
}
|
|
52
|
+
const matchBefore = this.getMatchList(this.beforeList, resourceName, actionName);
|
|
53
|
+
const matchAfter = this.getMatchList(this.afterList, resourceName, actionName);
|
|
54
|
+
if (!matchBefore.length && !matchAfter.length) {
|
|
55
|
+
return next();
|
|
56
|
+
}
|
|
57
|
+
for (const model of matchBefore) {
|
|
58
|
+
const body = await new import_webhooks.WebhookController().action(ctx, model);
|
|
59
|
+
const result = await new import_webhooks.WebhookController().triggerWorkflow(ctx, model, body);
|
|
60
|
+
if (result && result.lastSavedJob.status === import_module_workflow.JOB_STATUS.ERROR) {
|
|
61
|
+
ctx.throw(400, result.lastSavedJob.result);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
await next();
|
|
65
|
+
for (const model of matchAfter) {
|
|
66
|
+
const body = await new import_webhooks.WebhookController().action(ctx, model);
|
|
67
|
+
await new import_webhooks.WebhookController().triggerWorkflow(ctx, model, body);
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
{ tag: "event-source-resource" }
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
getMatchList(list, resourceName, actionName) {
|
|
74
|
+
let targetResource = resourceName || "";
|
|
75
|
+
const matchList = [];
|
|
76
|
+
list.sort((a, b) => {
|
|
77
|
+
let diffSort = a.options.sort - b.options.sort;
|
|
78
|
+
if (diffSort !== 0) {
|
|
79
|
+
return diffSort;
|
|
80
|
+
}
|
|
81
|
+
return a.id - b.id;
|
|
82
|
+
});
|
|
83
|
+
for (const item of list) {
|
|
84
|
+
targetResource = item.options.resourceName;
|
|
85
|
+
if (item.options.triggerOnAssociation) {
|
|
86
|
+
const parts = resourceName.split(".");
|
|
87
|
+
if (parts.length === 2) {
|
|
88
|
+
const collection = this.app.db.getCollection(resourceName);
|
|
89
|
+
targetResource = collection == null ? void 0 : collection.name;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (targetResource === resourceName && item.options.actionName === actionName) {
|
|
93
|
+
matchList.push(item);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return matchList;
|
|
97
|
+
}
|
|
98
|
+
afterCreate(model) {
|
|
99
|
+
const { type } = model;
|
|
100
|
+
const prefix = type.substring(0, type.indexOf("Resource"));
|
|
101
|
+
if (prefix === "before") {
|
|
102
|
+
this.beforeList.push(model);
|
|
103
|
+
} else {
|
|
104
|
+
this.afterList.push(model);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
afterUpdate(model) {
|
|
108
|
+
const { type } = model;
|
|
109
|
+
const prefix = type.substring(0, type.indexOf("Resource"));
|
|
110
|
+
if (prefix === "before") {
|
|
111
|
+
this.afterUpdateList(this.beforeList, model);
|
|
112
|
+
} else {
|
|
113
|
+
this.afterUpdateList(this.afterList, model);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
afterUpdateList(list, model) {
|
|
117
|
+
const index = list.findIndex((item) => item.id === model.id);
|
|
118
|
+
if (!model.enabled) {
|
|
119
|
+
if (index !== -1) {
|
|
120
|
+
list.splice(index, 1);
|
|
121
|
+
}
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (index !== -1) {
|
|
125
|
+
list[index] = model.toJSON();
|
|
126
|
+
} else {
|
|
127
|
+
list.push(model.toJSON());
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
afterDestroy(model) {
|
|
131
|
+
const { type } = model;
|
|
132
|
+
const prefix = type.substring(0, type.indexOf("Resource"));
|
|
133
|
+
if (prefix === "before") {
|
|
134
|
+
this.afterDestroyList(this.beforeList, model);
|
|
135
|
+
} else {
|
|
136
|
+
this.afterDestroyList(this.afterList, model);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
afterDestroyList(list, model) {
|
|
140
|
+
const index = list.findIndex((item) => item.id === model.id);
|
|
141
|
+
if (index !== -1) {
|
|
142
|
+
list.splice(index, 1);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
147
|
+
0 && (module.exports = {
|
|
148
|
+
ResourceEventTrigger
|
|
149
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import Application from '@tachybase/server';
|
|
2
|
+
import { EventSourceModel } from '../model/EventSourceModel';
|
|
3
|
+
import { IEventSourceTrigger } from '../types';
|
|
4
|
+
export declare class EventSourceTrigger implements IEventSourceTrigger {
|
|
5
|
+
protected app: Application;
|
|
6
|
+
protected realTimeRefresh: boolean;
|
|
7
|
+
protected workSet: Set<number>;
|
|
8
|
+
protected effectConfigMap: Map<number, any>;
|
|
9
|
+
getRealTimeRefresh(): boolean;
|
|
10
|
+
constructor(app: Application, realTimeRefresh?: boolean);
|
|
11
|
+
load(model: EventSourceModel): void;
|
|
12
|
+
afterAllLoad(): void;
|
|
13
|
+
workSetAdd(id: number): void;
|
|
14
|
+
workSetDelete(id: number): void;
|
|
15
|
+
effectConfigSet(id: number, config: any): void;
|
|
16
|
+
getEffect(model: EventSourceModel): boolean;
|
|
17
|
+
afterCreate(model: EventSourceModel): void;
|
|
18
|
+
afterUpdate(model: EventSourceModel): void;
|
|
19
|
+
afterDestroy(model: EventSourceModel): void;
|
|
20
|
+
getEffectConfig(id: number): string;
|
|
21
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var Trigger_exports = {};
|
|
19
|
+
__export(Trigger_exports, {
|
|
20
|
+
EventSourceTrigger: () => EventSourceTrigger
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(Trigger_exports);
|
|
23
|
+
class EventSourceTrigger {
|
|
24
|
+
constructor(app, realTimeRefresh = false) {
|
|
25
|
+
this.realTimeRefresh = false;
|
|
26
|
+
this.workSet = /* @__PURE__ */ new Set();
|
|
27
|
+
this.effectConfigMap = /* @__PURE__ */ new Map();
|
|
28
|
+
this.app = app;
|
|
29
|
+
this.realTimeRefresh = realTimeRefresh;
|
|
30
|
+
}
|
|
31
|
+
getRealTimeRefresh() {
|
|
32
|
+
return this.realTimeRefresh;
|
|
33
|
+
}
|
|
34
|
+
// 加载到中间件|事件(app,db,API)|API
|
|
35
|
+
load(model) {
|
|
36
|
+
}
|
|
37
|
+
// 加载到中间件|事件(app,db,API)|API
|
|
38
|
+
afterAllLoad() {
|
|
39
|
+
}
|
|
40
|
+
workSetAdd(id) {
|
|
41
|
+
this.workSet.add(id);
|
|
42
|
+
}
|
|
43
|
+
workSetDelete(id) {
|
|
44
|
+
this.workSet.delete(id);
|
|
45
|
+
}
|
|
46
|
+
effectConfigSet(id, config) {
|
|
47
|
+
this.effectConfigMap.set(id, config);
|
|
48
|
+
}
|
|
49
|
+
// 判断是否生效(中间件中执行)
|
|
50
|
+
getEffect(model) {
|
|
51
|
+
return this.workSet.has(model.id);
|
|
52
|
+
}
|
|
53
|
+
afterCreate(model) {
|
|
54
|
+
}
|
|
55
|
+
afterUpdate(model) {
|
|
56
|
+
}
|
|
57
|
+
afterDestroy(model) {
|
|
58
|
+
}
|
|
59
|
+
getEffectConfig(id) {
|
|
60
|
+
const model = this.effectConfigMap.get(id);
|
|
61
|
+
if (!model) {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
const { enabled, type, options, workflowKey } = model;
|
|
65
|
+
return JSON.stringify({ enabled, type, options, workflowKey }, null, 2);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
69
|
+
0 && (module.exports = {
|
|
70
|
+
EventSourceTrigger
|
|
71
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
+
}
|
|
11
|
+
return to;
|
|
12
|
+
};
|
|
13
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
14
|
+
var types_exports = {};
|
|
15
|
+
module.exports = __toCommonJS(types_exports);
|
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import { Plugin } from '@tachybase/server';
|
|
2
|
+
import { Registry } from '@tachybase/utils';
|
|
3
|
+
import { EventSourceTrigger } from '../trigger/Trigger';
|
|
2
4
|
export declare class PluginWebhook extends Plugin {
|
|
5
|
+
triggers: Registry<EventSourceTrigger>;
|
|
6
|
+
beforeLoad(): Promise<void>;
|
|
3
7
|
load(): Promise<void>;
|
|
8
|
+
loadEventSources(): Promise<void>;
|
|
4
9
|
}
|