@ruiapp/rapid-core 0.1.4 → 0.1.6

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.
Files changed (121) hide show
  1. package/dist/bootstrapApplicationConfig.d.ts +1 -1
  2. package/dist/core/actionHandler.d.ts +17 -0
  3. package/dist/core/pluginManager.d.ts +31 -26
  4. package/dist/core/server.d.ts +24 -24
  5. package/dist/helpers/runCollectionEntityActionHandler.d.ts +5 -0
  6. package/dist/index.d.ts +8 -0
  7. package/dist/index.js +3335 -3279
  8. package/dist/plugins/auth/actionHandlers/createSession.d.ts +8 -0
  9. package/dist/plugins/auth/actionHandlers/deleteSession.d.ts +4 -0
  10. package/dist/plugins/auth/actionHandlers/getMyProfile.d.ts +4 -0
  11. package/dist/plugins/auth/mod.d.ts +25 -0
  12. package/dist/plugins/{authManager → auth}/routes/getMyProfile.d.ts +1 -1
  13. package/dist/plugins/{authManager → auth}/routes/signin.d.ts +1 -1
  14. package/dist/plugins/{authManager → auth}/routes/signout.d.ts +1 -1
  15. package/dist/plugins/dataManage/actionHandlers/addEntityRelations.d.ts +5 -0
  16. package/dist/plugins/dataManage/actionHandlers/countCollectionEntities.d.ts +5 -0
  17. package/dist/plugins/dataManage/actionHandlers/createCollectionEntitiesBatch.d.ts +5 -0
  18. package/dist/plugins/dataManage/actionHandlers/createCollectionEntity.d.ts +5 -0
  19. package/dist/plugins/dataManage/actionHandlers/deleteCollectionEntityById.d.ts +5 -0
  20. package/dist/plugins/dataManage/actionHandlers/findCollectionEntities.d.ts +5 -0
  21. package/dist/plugins/dataManage/actionHandlers/findCollectionEntityById.d.ts +5 -0
  22. package/dist/plugins/dataManage/actionHandlers/queryDatabase.d.ts +5 -0
  23. package/dist/plugins/dataManage/actionHandlers/removeEntityRelations.d.ts +5 -0
  24. package/dist/plugins/dataManage/actionHandlers/updateCollectionEntityById.d.ts +5 -0
  25. package/dist/plugins/dataManage/mod.d.ts +27 -0
  26. package/dist/plugins/fileManage/actionHandlers/downloadDocument.d.ts +4 -0
  27. package/dist/plugins/fileManage/actionHandlers/downloadFile.d.ts +4 -0
  28. package/dist/plugins/fileManage/actionHandlers/uploadFile.d.ts +4 -0
  29. package/dist/plugins/fileManage/mod.d.ts +25 -0
  30. package/dist/plugins/metaManage/actionHandlers/getMetaModelDetail.d.ts +4 -0
  31. package/dist/plugins/metaManage/actionHandlers/listMetaModels.d.ts +4 -0
  32. package/dist/plugins/metaManage/actionHandlers/listMetaRoutes.d.ts +4 -0
  33. package/dist/plugins/metaManage/mod.d.ts +25 -0
  34. package/dist/plugins/routeManage/actionHandlers/httpProxy.d.ts +5 -0
  35. package/dist/plugins/routeManage/mod.d.ts +25 -0
  36. package/dist/plugins/webhooks/mod.d.ts +25 -11
  37. package/dist/server.d.ts +7 -6
  38. package/dist/types.d.ts +10 -12
  39. package/package.json +1 -1
  40. package/src/bootstrapApplicationConfig.ts +6 -6
  41. package/src/core/actionHandler.ts +23 -0
  42. package/src/core/pluginManager.ts +107 -108
  43. package/src/core/routesBuilder.ts +4 -4
  44. package/src/core/server.ts +26 -44
  45. package/src/dataAccess/dataAccessor.ts +1 -1
  46. package/src/helpers/{runCollectionEntityHttpHandler.ts → runCollectionEntityActionHandler.ts} +5 -5
  47. package/src/index.ts +9 -0
  48. package/src/plugins/{authManager/httpHandlers → auth/actionHandlers}/createSession.ts +4 -4
  49. package/src/plugins/{authManager/httpHandlers → auth/actionHandlers}/deleteSession.ts +4 -4
  50. package/src/plugins/{authManager/httpHandlers → auth/actionHandlers}/getMyProfile.ts +4 -4
  51. package/src/plugins/{authManager/httpHandlers → auth/actionHandlers}/index.ts +2 -2
  52. package/src/plugins/auth/mod.ts +80 -0
  53. package/src/plugins/{authManager → auth}/routes/getMyProfile.ts +1 -1
  54. package/src/plugins/{authManager → auth}/routes/signin.ts +1 -1
  55. package/src/plugins/{authManager → auth}/routes/signout.ts +1 -1
  56. package/src/plugins/{dataManager/httpHandlers → dataManage/actionHandlers}/addEntityRelations.ts +6 -5
  57. package/src/plugins/dataManage/actionHandlers/countCollectionEntities.ts +23 -0
  58. package/src/plugins/{dataManager/httpHandlers → dataManage/actionHandlers}/createCollectionEntitiesBatch.ts +6 -5
  59. package/src/plugins/{dataManager/httpHandlers → dataManage/actionHandlers}/createCollectionEntity.ts +6 -5
  60. package/src/plugins/{dataManager/httpHandlers → dataManage/actionHandlers}/deleteCollectionEntityById.ts +8 -7
  61. package/src/plugins/{dataManager/httpHandlers → dataManage/actionHandlers}/findCollectionEntities.ts +8 -7
  62. package/src/plugins/{dataManager/httpHandlers → dataManage/actionHandlers}/findCollectionEntityById.ts +6 -5
  63. package/src/plugins/{dataManager/httpHandlers → dataManage/actionHandlers}/queryDatabase.ts +5 -4
  64. package/src/plugins/{dataManager/httpHandlers → dataManage/actionHandlers}/removeEntityRelations.ts +6 -5
  65. package/src/plugins/{dataManager/httpHandlers → dataManage/actionHandlers}/updateCollectionEntityById.ts +6 -5
  66. package/src/plugins/dataManage/mod.ts +187 -0
  67. package/src/plugins/{fileManager/httpHandlers → fileManage/actionHandlers}/downloadDocument.ts +4 -4
  68. package/src/plugins/{fileManager/httpHandlers → fileManage/actionHandlers}/downloadFile.ts +4 -4
  69. package/src/plugins/{fileManager/httpHandlers → fileManage/actionHandlers}/uploadFile.ts +5 -5
  70. package/src/plugins/fileManage/mod.ts +77 -0
  71. package/src/plugins/{metaManager/httpHandlers → metaManage/actionHandlers}/getMetaModelDetail.ts +4 -4
  72. package/src/plugins/{metaManager/httpHandlers → metaManage/actionHandlers}/listMetaModels.ts +4 -4
  73. package/src/plugins/{routeManager/httpHandlers → metaManage/actionHandlers}/listMetaRoutes.ts +4 -4
  74. package/src/plugins/{metaManager → metaManage}/mod.ts +88 -55
  75. package/src/plugins/{routeManager/httpHandlers → routeManage/actionHandlers}/httpProxy.ts +5 -4
  76. package/src/plugins/routeManage/mod.ts +92 -0
  77. package/src/plugins/webhooks/mod.ts +134 -94
  78. package/src/server.ts +28 -21
  79. package/src/types.ts +11 -14
  80. package/dist/core/httpHandler.d.ts +0 -18
  81. package/dist/core/plugin.d.ts +0 -6
  82. package/dist/helpers/runCollectionEntityHttpHandler.d.ts +0 -5
  83. package/dist/plugins/authManager/httpHandlers/createSession.d.ts +0 -8
  84. package/dist/plugins/authManager/httpHandlers/deleteSession.d.ts +0 -4
  85. package/dist/plugins/authManager/httpHandlers/getMyProfile.d.ts +0 -4
  86. package/dist/plugins/authManager/mod.d.ts +0 -16
  87. package/dist/plugins/dataManager/httpHandlers/addEntityRelations.d.ts +0 -4
  88. package/dist/plugins/dataManager/httpHandlers/countCollectionEntities.d.ts +0 -4
  89. package/dist/plugins/dataManager/httpHandlers/createCollectionEntitiesBatch.d.ts +0 -4
  90. package/dist/plugins/dataManager/httpHandlers/createCollectionEntity.d.ts +0 -4
  91. package/dist/plugins/dataManager/httpHandlers/deleteCollectionEntityById.d.ts +0 -4
  92. package/dist/plugins/dataManager/httpHandlers/findCollectionEntities.d.ts +0 -4
  93. package/dist/plugins/dataManager/httpHandlers/findCollectionEntityById.d.ts +0 -4
  94. package/dist/plugins/dataManager/httpHandlers/queryDatabase.d.ts +0 -4
  95. package/dist/plugins/dataManager/httpHandlers/removeEntityRelations.d.ts +0 -4
  96. package/dist/plugins/dataManager/httpHandlers/updateCollectionEntityById.d.ts +0 -4
  97. package/dist/plugins/dataManager/mod.d.ts +0 -16
  98. package/dist/plugins/fileManager/httpHandlers/downloadDocument.d.ts +0 -4
  99. package/dist/plugins/fileManager/httpHandlers/downloadFile.d.ts +0 -4
  100. package/dist/plugins/fileManager/httpHandlers/uploadFile.d.ts +0 -4
  101. package/dist/plugins/fileManager/mod.d.ts +0 -15
  102. package/dist/plugins/metaManager/httpHandlers/getMetaModelDetail.d.ts +0 -4
  103. package/dist/plugins/metaManager/httpHandlers/listMetaModels.d.ts +0 -4
  104. package/dist/plugins/metaManager/mod.d.ts +0 -15
  105. package/dist/plugins/routeManager/httpHandlers/httpProxy.d.ts +0 -4
  106. package/dist/plugins/routeManager/httpHandlers/listMetaRoutes.d.ts +0 -4
  107. package/dist/plugins/routeManager/mod.d.ts +0 -15
  108. package/src/core/httpHandler.ts +0 -29
  109. package/src/core/plugin.ts +0 -13
  110. package/src/plugins/authManager/mod.ts +0 -56
  111. package/src/plugins/dataManager/httpHandlers/countCollectionEntities.ts +0 -22
  112. package/src/plugins/dataManager/mod.ts +0 -154
  113. package/src/plugins/fileManager/mod.ts +0 -48
  114. package/src/plugins/routeManager/mod.ts +0 -97
  115. /package/dist/plugins/{authManager/httpHandlers → auth/actionHandlers}/index.d.ts +0 -0
  116. /package/dist/plugins/{authManager → auth}/models/AccessToken.d.ts +0 -0
  117. /package/dist/plugins/{authManager → auth}/models/index.d.ts +0 -0
  118. /package/dist/plugins/{authManager → auth}/routes/index.d.ts +0 -0
  119. /package/src/plugins/{authManager → auth}/models/AccessToken.ts +0 -0
  120. /package/src/plugins/{authManager → auth}/models/index.ts +0 -0
  121. /package/src/plugins/{authManager → auth}/routes/index.ts +0 -0
@@ -5,7 +5,6 @@
5
5
  import * as _ from "lodash";
6
6
  import { findEntities } from "~/dataAccess/entityManager";
7
7
  import {
8
- IPluginInstance,
9
8
  IQueryBuilder,
10
9
  QuoteTableOptions,
11
10
  RpdApplicationConfig,
@@ -16,50 +15,103 @@ import {
16
15
  RpdEntityDeleteEventPayload,
17
16
  RpdEntityUpdateEventPayload,
18
17
  } from "~/types";
19
- import { IRpdServer, RpdConfigurationItemOptions, RpdServerPluginConfigurableTargetOptions, RpdServerPluginExtendingAbilities } from "~/core/server";
18
+ import { IRpdServer, RapidPlugin, RpdConfigurationItemOptions, RpdServerPluginConfigurableTargetOptions, RpdServerPluginExtendingAbilities } from "~/core/server";
20
19
 
21
- import * as listMetaModels from "./httpHandlers/listMetaModels";
22
- import * as getMetaModelDetail from "./httpHandlers/getMetaModelDetail";
20
+ import * as listMetaModels from "./actionHandlers/listMetaModels";
21
+ import * as listMetaRoutes from "./actionHandlers/listMetaRoutes";
22
+ import * as getMetaModelDetail from "./actionHandlers/getMetaModelDetail";
23
23
  import { isRelationProperty } from "~/utilities/rapidUtility";
24
24
 
25
- export const code = "metaManager";
26
- export const description = "metaManager";
27
- export const extendingAbilities: RpdServerPluginExtendingAbilities[] = [];
28
- export const configurableTargets: RpdServerPluginConfigurableTargetOptions[] = [];
29
- export const configurations: RpdConfigurationItemOptions[] = [];
25
+ class MetaManager implements RapidPlugin {
26
+ get code(): string {
27
+ return "metaManager";
28
+ }
30
29
 
31
- let _plugin: IPluginInstance;
30
+ get description(): string {
31
+ return null;
32
+ }
32
33
 
33
- export async function initPlugin(plugin: IPluginInstance, server: IRpdServer) {
34
- _plugin = plugin;
35
- }
34
+ get extendingAbilities(): RpdServerPluginExtendingAbilities[] {
35
+ return [];
36
+ }
36
37
 
37
- export async function registerHttpHandlers(server: IRpdServer) {
38
- server.registerHttpHandler(_plugin, listMetaModels);
39
- server.registerHttpHandler(_plugin, getMetaModelDetail);
40
- }
38
+ get configurableTargets(): RpdServerPluginConfigurableTargetOptions[] {
39
+ return [];
40
+ }
41
+
42
+ get configurations(): RpdConfigurationItemOptions[] {
43
+ return [];
44
+ }
45
+
46
+ async initPlugin(server: IRpdServer): Promise<any> {
47
+ }
48
+
49
+ async registerMiddlewares(server: IRpdServer): Promise<any> {
50
+ }
41
51
 
42
- export async function registerEventHandlers(server: IRpdServer) {
43
- server.registerEventHandler(
44
- "entity.create",
45
- handleEntityCreateEvent.bind(null, server),
46
- );
47
- server.registerEventHandler(
48
- "entity.update",
49
- handleEntityUpdateEvent.bind(null, server),
50
- );
51
- server.registerEventHandler(
52
- "entity.delete",
53
- handleEntityDeleteEvent.bind(null, server),
54
- );
52
+ async registerActionHandlers(server: IRpdServer): Promise<any> {
53
+ server.registerActionHandler(this, listMetaModels);
54
+ server.registerActionHandler(this, listMetaRoutes);
55
+ server.registerActionHandler(this, getMetaModelDetail);
56
+ }
57
+
58
+ async registerEventHandlers(server: IRpdServer): Promise<any> {
59
+ server.registerEventHandler(
60
+ "entity.create",
61
+ handleEntityCreateEvent.bind(this, server),
62
+ );
63
+ server.registerEventHandler(
64
+ "entity.update",
65
+ handleEntityUpdateEvent.bind(this, server),
66
+ );
67
+ server.registerEventHandler(
68
+ "entity.delete",
69
+ handleEntityDeleteEvent.bind(this, server),
70
+ );
71
+ }
72
+
73
+ async registerMessageHandlers(server: IRpdServer): Promise<any> {
74
+ }
75
+
76
+ async registerTaskProcessors(server: IRpdServer): Promise<any> {
77
+ }
78
+
79
+ async onLoadingApplication(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
80
+ }
81
+
82
+ async configureModels(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
83
+ try {
84
+ const models: RpdDataModel[] = await listCollections(server, applicationConfig);
85
+ server.appendApplicationConfig({ models });
86
+ } catch (ex) {
87
+ console.warn("Failed to loading existing meta of models.", ex);
88
+ }
89
+ }
90
+
91
+ async configureModelProperties(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
92
+ }
93
+
94
+ async configureRoutes(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
95
+ }
96
+
97
+ async onApplicationLoaded(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
98
+ console.log("metaManager.onApplicationLoaded");
99
+ await syncDatabaseSchema(server, applicationConfig);
100
+ }
101
+
102
+ async onApplicationReady(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
103
+ }
55
104
  }
56
105
 
106
+ export default MetaManager;
107
+
108
+
57
109
  async function handleEntityCreateEvent(
58
110
  server: IRpdServer,
59
- sender: IPluginInstance,
111
+ sender: RapidPlugin,
60
112
  payload: RpdEntityCreateEventPayload,
61
113
  ) {
62
- if (sender === _plugin) {
114
+ if (sender === this) {
63
115
  return;
64
116
  }
65
117
 
@@ -79,10 +131,10 @@ async function handleEntityCreateEvent(
79
131
 
80
132
  async function handleEntityUpdateEvent(
81
133
  server: IRpdServer,
82
- sender: IPluginInstance,
134
+ sender: RapidPlugin,
83
135
  payload: RpdEntityUpdateEventPayload,
84
136
  ) {
85
- if (sender === _plugin) {
137
+ if (sender === this) {
86
138
  return;
87
139
  }
88
140
 
@@ -102,10 +154,10 @@ async function handleEntityUpdateEvent(
102
154
 
103
155
  async function handleEntityDeleteEvent(
104
156
  server: IRpdServer,
105
- sender: IPluginInstance,
157
+ sender: RapidPlugin,
106
158
  payload: RpdEntityDeleteEventPayload,
107
159
  ) {
108
- if (sender === _plugin) {
160
+ if (sender === this) {
109
161
  return;
110
162
  }
111
163
 
@@ -150,18 +202,6 @@ async function handleEntityDeleteEvent(
150
202
  }
151
203
  }
152
204
 
153
- export async function configureModels(
154
- server: IRpdServer,
155
- applicationConfig: RpdApplicationConfig,
156
- ) {
157
- try {
158
- const models: RpdDataModel[] = await listCollections(server, applicationConfig);
159
- server.appendApplicationConfig({ models });
160
- } catch (ex) {
161
- console.warn("Failed to loading existing meta of models.", ex);
162
- }
163
- }
164
-
165
205
  function listCollections(
166
206
  server: IRpdServer,
167
207
  applicationConfig: RpdApplicationConfig,
@@ -177,13 +217,6 @@ function listCollections(
177
217
  });
178
218
  }
179
219
 
180
- export async function onApplicationLoaded(
181
- server: IRpdServer,
182
- applicationConfig: RpdApplicationConfig,
183
- ) {
184
- console.log("metaManager.onApplicationLoaded");
185
- await syncDatabaseSchema(server, applicationConfig);
186
- }
187
220
 
188
221
  type TableInformation = {
189
222
  table_schema: string;
@@ -1,12 +1,13 @@
1
- import { IPluginInstance, RunProxyHandlerOptions } from "~/types";
1
+ import { RunProxyHandlerOptions } from "~/types";
2
2
  import { doProxy } from "~/proxy/mod";
3
- import { HttpHandlerContext } from "~/core/httpHandler";
3
+ import { ActionHandlerContext } from "~/core/actionHandler";
4
+ import { RapidPlugin } from "~/core/server";
4
5
 
5
6
  export const code = "httpProxy";
6
7
 
7
8
  export async function handler(
8
- plugin: IPluginInstance,
9
- ctx: HttpHandlerContext,
9
+ plugin: RapidPlugin,
10
+ ctx: ActionHandlerContext,
10
11
  options: RunProxyHandlerOptions,
11
12
  ) {
12
13
  console.debug(`Running ${code} handler...`);
@@ -0,0 +1,92 @@
1
+ /**
2
+ * Route manager plugin
3
+ */
4
+
5
+ import {
6
+ RpdApplicationConfig,
7
+ } from "~/types";
8
+ import { RpdServerPluginExtendingAbilities, RpdServerPluginConfigurableTargetOptions, RpdConfigurationItemOptions, IRpdServer, RapidPlugin } from "~/core/server";
9
+ import { findEntities } from "../../dataAccess/entityManager";
10
+ import * as httpProxy from "./actionHandlers/httpProxy";
11
+
12
+
13
+ class RouteManager implements RapidPlugin {
14
+ get code(): string {
15
+ return "routeManager";
16
+ }
17
+
18
+ get description(): string {
19
+ return null;
20
+ }
21
+
22
+ get extendingAbilities(): RpdServerPluginExtendingAbilities[] {
23
+ return [];
24
+ }
25
+
26
+ get configurableTargets(): RpdServerPluginConfigurableTargetOptions[] {
27
+ return [];
28
+ }
29
+
30
+ get configurations(): RpdConfigurationItemOptions[] {
31
+ return [];
32
+ }
33
+
34
+ async initPlugin(server: IRpdServer): Promise<any> {
35
+ }
36
+
37
+ async registerMiddlewares(server: IRpdServer): Promise<any> {
38
+ }
39
+
40
+ async registerActionHandlers(server: IRpdServer): Promise<any> {
41
+ server.registerActionHandler(this, httpProxy);
42
+ }
43
+
44
+ async registerEventHandlers(server: IRpdServer): Promise<any> {
45
+ // TODO: Should rebuild routes after route configurations changed.
46
+ // server.registerEventHandler("entity.create", handleEntityEvent.bind(null, server))
47
+ // server.registerEventHandler("entity.update", handleEntityEvent.bind(null, server))
48
+ // server.registerEventHandler("entity.delete", handleEntityEvent.bind(null, server))
49
+ }
50
+
51
+ async registerMessageHandlers(server: IRpdServer): Promise<any> {
52
+ }
53
+
54
+ async registerTaskProcessors(server: IRpdServer): Promise<any> {
55
+ }
56
+
57
+ async onLoadingApplication(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
58
+ }
59
+
60
+ async configureModels(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
61
+ }
62
+
63
+ async configureModelProperties(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
64
+ }
65
+
66
+ async configureRoutes(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
67
+ try {
68
+ const dataAccessor = server.getDataAccessor({
69
+ namespace: "meta",
70
+ singularCode: "route",
71
+ });
72
+
73
+ const routes = await findEntities(server, dataAccessor, {
74
+ orderBy: [
75
+ { field: "endpoint" },
76
+ ],
77
+ });
78
+ applicationConfig.routes.push(...routes);
79
+ } catch (ex) {
80
+ console.warn("Failed to loading existing meta of routes.", ex.message);
81
+ }
82
+ }
83
+
84
+ async onApplicationLoaded(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
85
+ console.log("[routeManager.onApplicationLoaded] onApplicationLoaded");
86
+ }
87
+
88
+ async onApplicationReady(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
89
+ }
90
+ }
91
+
92
+ export default RouteManager;
@@ -4,23 +4,17 @@
4
4
 
5
5
  import * as _ from "lodash";
6
6
  import {
7
- IPluginInstance,
8
7
  RpdApplicationConfig,
9
8
  RpdEntityCreateEventPayload,
10
9
  RpdEntityDeleteEventPayload,
11
10
  RpdEntityUpdateEventPayload,
12
11
  RpdServerEventTypes,
13
12
  } from "~/types";
14
- import { RpdServerPluginExtendingAbilities, RpdServerPluginConfigurableTargetOptions, RpdConfigurationItemOptions, IRpdServer } from "~/core/server";
13
+ import { RpdServerPluginExtendingAbilities, RpdServerPluginConfigurableTargetOptions, RpdConfigurationItemOptions, IRpdServer, RapidPlugin } from "~/core/server";
15
14
  import { fetchWithTimeout } from "~/utilities/httpUtility";
16
15
  import { findEntities } from "~/dataAccess/entityManager";
17
16
  import pluginConfig from "./pluginConfig";
18
17
 
19
- export const code = "webhooks";
20
- export const description = "webhooks";
21
- export const extendingAbilities: RpdServerPluginExtendingAbilities[] = [];
22
- export const configurableTargets: RpdServerPluginConfigurableTargetOptions[] = [];
23
- export const configurations: RpdConfigurationItemOptions[] = [];
24
18
 
25
19
  export interface Webhook {
26
20
  name: string;
@@ -33,112 +27,158 @@ export interface Webhook {
33
27
  enabled: boolean;
34
28
  }
35
29
 
36
- let _plugin: IPluginInstance;
37
- let webhooks: Webhook[];
38
30
 
39
- export async function initPlugin(plugin: IPluginInstance, server: IRpdServer) {
40
- _plugin = plugin;
41
- }
42
-
43
- export async function configureModels(
31
+ function listWebhooks(
44
32
  server: IRpdServer,
45
- applicationConfig: RpdApplicationConfig,
46
33
  ) {
47
- server.appendApplicationConfig({
48
- models: pluginConfig.models,
34
+ const dataAccessor = server.getDataAccessor({
35
+ namespace: "sys",
36
+ singularCode: "webhook",
37
+ });
38
+
39
+ return findEntities(server, dataAccessor, {
40
+ filters: [
41
+ {
42
+ field: "enabled",
43
+ operator: "eq",
44
+ value: true,
45
+ },
46
+ ],
49
47
  });
50
48
  }
51
49
 
52
- export async function registerEventHandlers(server: IRpdServer) {
53
- const events: (keyof RpdServerEventTypes)[] = [
54
- "entity.create",
55
- "entity.update",
56
- "entity.delete",
57
- ];
58
- for (const event of events) {
59
- server.registerEventHandler(
60
- event,
61
- handleEntityEvent.bind(null, server, event),
62
- );
50
+
51
+ class WebhooksPlugin implements RapidPlugin {
52
+ #webhooks: Webhook[];
53
+
54
+ constructor() {
55
+ this.#webhooks = [];
63
56
  }
64
- }
65
57
 
66
- async function handleEntityEvent(
67
- server: IRpdServer,
68
- event: keyof RpdServerEventTypes,
69
- sender: IPluginInstance,
70
- payload:
71
- | RpdEntityCreateEventPayload
72
- | RpdEntityUpdateEventPayload
73
- | RpdEntityDeleteEventPayload,
74
- ) {
75
- if (sender === _plugin) {
76
- return;
58
+ get code(): string {
59
+ return "webhooks";
77
60
  }
78
61
 
79
- if (payload.namespace === "sys" && payload.modelSingularCode === "webhook") {
80
- webhooks = await listWebhooks(server);
81
- return;
62
+ get description(): string {
63
+ return null;
82
64
  }
83
65
 
84
- // We will not trigger webhooks if entity changed is in "meta" or "sys" namespaces.
85
- if (payload.namespace === "meta" || payload.namespace === "sys") {
86
- return;
66
+ get extendingAbilities(): RpdServerPluginExtendingAbilities[] {
67
+ return [];
87
68
  }
88
69
 
89
- for (const webhook of webhooks) {
90
- if (_.indexOf(webhook.events, event) === -1) {
91
- continue;
92
- }
70
+ get configurableTargets(): RpdServerPluginConfigurableTargetOptions[] {
71
+ return [];
72
+ }
93
73
 
94
- if (
95
- webhook.namespace != payload.namespace ||
96
- webhook.modelSingularCode !== payload.modelSingularCode
97
- ) {
98
- continue;
99
- }
74
+ get configurations(): RpdConfigurationItemOptions[] {
75
+ return [];
76
+ }
77
+
78
+ async initPlugin(server: IRpdServer): Promise<any> {
79
+ }
80
+
81
+ async registerMiddlewares(server: IRpdServer): Promise<any> {
82
+ }
83
+
84
+ async registerActionHandlers(server: IRpdServer): Promise<any> {
85
+ }
100
86
 
101
- console.debug(`Triggering webhook. ${webhook.url}`);
102
- // TODO: It's better to trigger webhook through message queue.
103
- try {
104
- await fetchWithTimeout(webhook.url, {
105
- method: "post",
106
- headers: {
107
- "Content-Type": "application/json",
108
- "x-webhook-secret": webhook.secret || "",
109
- },
110
- body: JSON.stringify({ event, payload }),
111
- });
112
- } catch (err) {
113
- console.warn(new Error("Failed to call webhook. " + err.message));
114
- console.warn(err);
87
+ async registerEventHandlers(server: IRpdServer): Promise<any> {
88
+ const events: (keyof RpdServerEventTypes)[] = [
89
+ "entity.create",
90
+ "entity.update",
91
+ "entity.delete",
92
+ ];
93
+ for (const event of events) {
94
+ server.registerEventHandler(
95
+ event,
96
+ this.handleEntityEvent.bind(this, server, event),
97
+ );
115
98
  }
116
99
  }
117
- }
118
100
 
119
- export async function onApplicationLoaded(
120
- server: IRpdServer,
121
- applicationConfig: RpdApplicationConfig,
122
- ) {
123
- console.log("[webhooks.onApplicationLoaded] loading webhooks");
124
- webhooks = await listWebhooks(server);
125
- }
101
+ async registerMessageHandlers(server: IRpdServer): Promise<any> {
102
+ }
126
103
 
127
- function listWebhooks(
128
- server: IRpdServer,
129
- ) {
130
- const dataAccessor = server.getDataAccessor({
131
- namespace: "sys",
132
- singularCode: "webhook",
133
- });
104
+ async registerTaskProcessors(server: IRpdServer): Promise<any> {
105
+ }
134
106
 
135
- return findEntities(server, dataAccessor, {
136
- filters: [
137
- {
138
- field: "enabled",
139
- operator: "eq",
140
- value: true,
141
- },
142
- ],
143
- });
107
+ async onLoadingApplication(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
108
+ }
109
+
110
+ async configureModels(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
111
+ server.appendApplicationConfig({
112
+ models: pluginConfig.models,
113
+ });
114
+ }
115
+
116
+ async configureModelProperties(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
117
+ }
118
+
119
+ async configureRoutes(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
120
+ }
121
+
122
+ async onApplicationLoaded(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
123
+ console.log("[webhooks.onApplicationLoaded] loading webhooks");
124
+ this.#webhooks = await listWebhooks(server);
125
+ }
126
+
127
+ async onApplicationReady(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
128
+ }
129
+
130
+ async handleEntityEvent(
131
+ server: IRpdServer,
132
+ event: keyof RpdServerEventTypes,
133
+ sender: RapidPlugin,
134
+ payload:
135
+ | RpdEntityCreateEventPayload
136
+ | RpdEntityUpdateEventPayload
137
+ | RpdEntityDeleteEventPayload,
138
+ ) {
139
+ if (sender === this) {
140
+ return;
141
+ }
142
+
143
+ if (payload.namespace === "sys" && payload.modelSingularCode === "webhook") {
144
+ this.#webhooks = await listWebhooks(server);
145
+ return;
146
+ }
147
+
148
+ // We will not trigger webhooks if entity changed is in "meta" or "sys" namespaces.
149
+ if (payload.namespace === "meta" || payload.namespace === "sys") {
150
+ return;
151
+ }
152
+
153
+ for (const webhook of this.#webhooks) {
154
+ if (_.indexOf(webhook.events, event) === -1) {
155
+ continue;
156
+ }
157
+
158
+ if (
159
+ webhook.namespace != payload.namespace ||
160
+ webhook.modelSingularCode !== payload.modelSingularCode
161
+ ) {
162
+ continue;
163
+ }
164
+
165
+ console.debug(`Triggering webhook. ${webhook.url}`);
166
+ // TODO: It's better to trigger webhook through message queue.
167
+ try {
168
+ await fetchWithTimeout(webhook.url, {
169
+ method: "post",
170
+ headers: {
171
+ "Content-Type": "application/json",
172
+ "x-webhook-secret": webhook.secret || "",
173
+ },
174
+ body: JSON.stringify({ event, payload }),
175
+ });
176
+ } catch (err) {
177
+ console.warn(new Error("Failed to call webhook. " + err.message));
178
+ console.warn(err);
179
+ }
180
+ }
181
+ }
144
182
  }
183
+
184
+ export default WebhooksPlugin;