@ruiapp/rapid-core 0.1.4 → 0.1.5

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 (111) hide show
  1. package/dist/core/httpHandler.d.ts +3 -3
  2. package/dist/core/pluginManager.d.ts +31 -26
  3. package/dist/core/server.d.ts +21 -21
  4. package/dist/index.d.ts +6 -0
  5. package/dist/index.js +3367 -3311
  6. package/dist/plugins/{authManager → auth}/httpHandlers/createSession.d.ts +2 -2
  7. package/dist/plugins/auth/httpHandlers/deleteSession.d.ts +4 -0
  8. package/dist/plugins/auth/httpHandlers/getMyProfile.d.ts +4 -0
  9. package/dist/plugins/auth/mod.d.ts +25 -0
  10. package/dist/plugins/dataManage/httpHandlers/addEntityRelations.d.ts +5 -0
  11. package/dist/plugins/dataManage/httpHandlers/countCollectionEntities.d.ts +5 -0
  12. package/dist/plugins/dataManage/httpHandlers/createCollectionEntitiesBatch.d.ts +5 -0
  13. package/dist/plugins/dataManage/httpHandlers/createCollectionEntity.d.ts +5 -0
  14. package/dist/plugins/dataManage/httpHandlers/deleteCollectionEntityById.d.ts +5 -0
  15. package/dist/plugins/dataManage/httpHandlers/findCollectionEntities.d.ts +5 -0
  16. package/dist/plugins/dataManage/httpHandlers/findCollectionEntityById.d.ts +5 -0
  17. package/dist/plugins/dataManage/httpHandlers/queryDatabase.d.ts +5 -0
  18. package/dist/plugins/dataManage/httpHandlers/removeEntityRelations.d.ts +5 -0
  19. package/dist/plugins/dataManage/httpHandlers/updateCollectionEntityById.d.ts +5 -0
  20. package/dist/plugins/dataManage/mod.d.ts +27 -0
  21. package/dist/plugins/fileManage/httpHandlers/downloadDocument.d.ts +4 -0
  22. package/dist/plugins/fileManage/httpHandlers/downloadFile.d.ts +4 -0
  23. package/dist/plugins/fileManage/httpHandlers/uploadFile.d.ts +4 -0
  24. package/dist/plugins/fileManage/mod.d.ts +25 -0
  25. package/dist/plugins/metaManage/httpHandlers/getMetaModelDetail.d.ts +4 -0
  26. package/dist/plugins/metaManage/httpHandlers/listMetaModels.d.ts +4 -0
  27. package/dist/plugins/metaManage/httpHandlers/listMetaRoutes.d.ts +4 -0
  28. package/dist/plugins/metaManage/mod.d.ts +25 -0
  29. package/dist/plugins/routeManage/httpHandlers/httpProxy.d.ts +5 -0
  30. package/dist/plugins/routeManage/mod.d.ts +25 -0
  31. package/dist/plugins/webhooks/mod.d.ts +25 -11
  32. package/dist/server.d.ts +5 -4
  33. package/dist/types.d.ts +7 -9
  34. package/package.json +1 -1
  35. package/src/core/httpHandler.ts +3 -3
  36. package/src/core/pluginManager.ts +107 -108
  37. package/src/core/server.ts +21 -39
  38. package/src/dataAccess/dataAccessor.ts +1 -1
  39. package/src/index.ts +7 -0
  40. package/src/plugins/{authManager → auth}/httpHandlers/createSession.ts +2 -2
  41. package/src/plugins/{authManager → auth}/httpHandlers/deleteSession.ts +2 -2
  42. package/src/plugins/{authManager → auth}/httpHandlers/getMyProfile.ts +2 -2
  43. package/src/plugins/auth/mod.ts +80 -0
  44. package/src/plugins/{dataManager → dataManage}/httpHandlers/addEntityRelations.ts +3 -2
  45. package/src/plugins/{dataManager → dataManage}/httpHandlers/countCollectionEntities.ts +3 -2
  46. package/src/plugins/{dataManager → dataManage}/httpHandlers/createCollectionEntitiesBatch.ts +3 -2
  47. package/src/plugins/{dataManager → dataManage}/httpHandlers/createCollectionEntity.ts +3 -2
  48. package/src/plugins/{dataManager → dataManage}/httpHandlers/deleteCollectionEntityById.ts +5 -4
  49. package/src/plugins/{dataManager → dataManage}/httpHandlers/findCollectionEntities.ts +3 -2
  50. package/src/plugins/{dataManager → dataManage}/httpHandlers/findCollectionEntityById.ts +3 -2
  51. package/src/plugins/{dataManager → dataManage}/httpHandlers/queryDatabase.ts +3 -2
  52. package/src/plugins/{dataManager → dataManage}/httpHandlers/removeEntityRelations.ts +3 -2
  53. package/src/plugins/{dataManager → dataManage}/httpHandlers/updateCollectionEntityById.ts +3 -2
  54. package/src/plugins/dataManage/mod.ts +187 -0
  55. package/src/plugins/{fileManager → fileManage}/httpHandlers/downloadDocument.ts +2 -2
  56. package/src/plugins/{fileManager → fileManage}/httpHandlers/downloadFile.ts +2 -2
  57. package/src/plugins/{fileManager → fileManage}/httpHandlers/uploadFile.ts +3 -3
  58. package/src/plugins/fileManage/mod.ts +77 -0
  59. package/src/plugins/{metaManager → metaManage}/httpHandlers/getMetaModelDetail.ts +2 -2
  60. package/src/plugins/{metaManager → metaManage}/httpHandlers/listMetaModels.ts +2 -2
  61. package/src/plugins/{routeManager → metaManage}/httpHandlers/listMetaRoutes.ts +2 -2
  62. package/src/plugins/{metaManager → metaManage}/mod.ts +86 -53
  63. package/src/plugins/{routeManager → routeManage}/httpHandlers/httpProxy.ts +3 -2
  64. package/src/plugins/routeManage/mod.ts +92 -0
  65. package/src/plugins/webhooks/mod.ts +134 -94
  66. package/src/server.ts +19 -12
  67. package/src/types.ts +8 -11
  68. package/dist/core/plugin.d.ts +0 -6
  69. package/dist/plugins/authManager/httpHandlers/deleteSession.d.ts +0 -4
  70. package/dist/plugins/authManager/httpHandlers/getMyProfile.d.ts +0 -4
  71. package/dist/plugins/authManager/mod.d.ts +0 -16
  72. package/dist/plugins/dataManager/httpHandlers/addEntityRelations.d.ts +0 -4
  73. package/dist/plugins/dataManager/httpHandlers/countCollectionEntities.d.ts +0 -4
  74. package/dist/plugins/dataManager/httpHandlers/createCollectionEntitiesBatch.d.ts +0 -4
  75. package/dist/plugins/dataManager/httpHandlers/createCollectionEntity.d.ts +0 -4
  76. package/dist/plugins/dataManager/httpHandlers/deleteCollectionEntityById.d.ts +0 -4
  77. package/dist/plugins/dataManager/httpHandlers/findCollectionEntities.d.ts +0 -4
  78. package/dist/plugins/dataManager/httpHandlers/findCollectionEntityById.d.ts +0 -4
  79. package/dist/plugins/dataManager/httpHandlers/queryDatabase.d.ts +0 -4
  80. package/dist/plugins/dataManager/httpHandlers/removeEntityRelations.d.ts +0 -4
  81. package/dist/plugins/dataManager/httpHandlers/updateCollectionEntityById.d.ts +0 -4
  82. package/dist/plugins/dataManager/mod.d.ts +0 -16
  83. package/dist/plugins/fileManager/httpHandlers/downloadDocument.d.ts +0 -4
  84. package/dist/plugins/fileManager/httpHandlers/downloadFile.d.ts +0 -4
  85. package/dist/plugins/fileManager/httpHandlers/uploadFile.d.ts +0 -4
  86. package/dist/plugins/fileManager/mod.d.ts +0 -15
  87. package/dist/plugins/metaManager/httpHandlers/getMetaModelDetail.d.ts +0 -4
  88. package/dist/plugins/metaManager/httpHandlers/listMetaModels.d.ts +0 -4
  89. package/dist/plugins/metaManager/mod.d.ts +0 -15
  90. package/dist/plugins/routeManager/httpHandlers/httpProxy.d.ts +0 -4
  91. package/dist/plugins/routeManager/httpHandlers/listMetaRoutes.d.ts +0 -4
  92. package/dist/plugins/routeManager/mod.d.ts +0 -15
  93. package/src/core/plugin.ts +0 -13
  94. package/src/plugins/authManager/mod.ts +0 -56
  95. package/src/plugins/dataManager/mod.ts +0 -154
  96. package/src/plugins/fileManager/mod.ts +0 -48
  97. package/src/plugins/routeManager/mod.ts +0 -97
  98. /package/dist/plugins/{authManager → auth}/httpHandlers/index.d.ts +0 -0
  99. /package/dist/plugins/{authManager → auth}/models/AccessToken.d.ts +0 -0
  100. /package/dist/plugins/{authManager → auth}/models/index.d.ts +0 -0
  101. /package/dist/plugins/{authManager → auth}/routes/getMyProfile.d.ts +0 -0
  102. /package/dist/plugins/{authManager → auth}/routes/index.d.ts +0 -0
  103. /package/dist/plugins/{authManager → auth}/routes/signin.d.ts +0 -0
  104. /package/dist/plugins/{authManager → auth}/routes/signout.d.ts +0 -0
  105. /package/src/plugins/{authManager → auth}/httpHandlers/index.ts +0 -0
  106. /package/src/plugins/{authManager → auth}/models/AccessToken.ts +0 -0
  107. /package/src/plugins/{authManager → auth}/models/index.ts +0 -0
  108. /package/src/plugins/{authManager → auth}/routes/getMyProfile.ts +0 -0
  109. /package/src/plugins/{authManager → auth}/routes/index.ts +0 -0
  110. /package/src/plugins/{authManager → auth}/routes/signin.ts +0 -0
  111. /package/src/plugins/{authManager → auth}/routes/signout.ts +0 -0
@@ -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 "./httpHandlers/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 registerHttpHandlers(server: IRpdServer): Promise<any> {
41
+ server.registerHttpHandler(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 registerHttpHandlers(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;
package/src/server.ts CHANGED
@@ -5,7 +5,6 @@ import {
5
5
  GetModelOptions,
6
6
  IDatabaseAccessor,
7
7
  IDatabaseConfig,
8
- IPluginInstance,
9
8
  IQueryBuilder,
10
9
  IRpdDataAccessor,
11
10
  RpdApplicationConfig,
@@ -15,10 +14,10 @@ import {
15
14
  } from "./types";
16
15
 
17
16
  import QueryBuilder from "./queryBuilder/queryBuilder";
18
- import * as pluginManager from "./core/pluginManager";
17
+ import PluginManager from "./core/pluginManager";
19
18
  import EventManager from "./core/eventManager";
20
19
  import { HttpRequestHandler, IPluginHttpHandler } from "./core/httpHandler";
21
- import { IRpdServer } from "./core/server";
20
+ import { IRpdServer, RapidPlugin } from "./core/server";
22
21
  import { buildRoutes } from "./core/routesBuilder";
23
22
  import { Next, RouteContext } from "./core/routeContext";
24
23
  import { RapidRequest } from "./core/request";
@@ -29,9 +28,12 @@ export interface InitServerOptions {
29
28
  databaseConfig: IDatabaseConfig;
30
29
  serverConfig: RapidServerConfig;
31
30
  applicationConfig?: RpdApplicationConfig;
31
+ plugins?: RapidPlugin[];
32
32
  }
33
33
 
34
34
  export class RapidServer implements IRpdServer {
35
+ #pluginManager: PluginManager;
36
+ #plugins: RapidPlugin[];
35
37
  #eventManager: EventManager;
36
38
  #middlewares: any[];
37
39
  #bootstrapApplicationConfig: RpdApplicationConfig;
@@ -44,6 +46,7 @@ export class RapidServer implements IRpdServer {
44
46
  #buildedRoutes: (ctx: any, next: any) => any;
45
47
 
46
48
  constructor(options: InitServerOptions) {
49
+ this.#pluginManager = new PluginManager(this);
47
50
  this.#eventManager = new EventManager();
48
51
  this.#middlewares = [];
49
52
  this.#bootstrapApplicationConfig = options.applicationConfig || bootstrapApplicationConfig;
@@ -57,6 +60,8 @@ export class RapidServer implements IRpdServer {
57
60
  });
58
61
  this.databaseConfig = options.databaseConfig;
59
62
  this.config = options.serverConfig;
63
+
64
+ this.#plugins = options.plugins || [];
60
65
  }
61
66
 
62
67
  getApplicationConfig() {
@@ -99,7 +104,7 @@ export class RapidServer implements IRpdServer {
99
104
  }
100
105
 
101
106
  registerHttpHandler(
102
- plugin: IPluginInstance,
107
+ plugin: RapidPlugin,
103
108
  options: IPluginHttpHandler,
104
109
  ) {
105
110
  const handler = _.bind(options.handler, null, plugin);
@@ -149,7 +154,7 @@ export class RapidServer implements IRpdServer {
149
154
 
150
155
  async emitEvent<K extends keyof RpdServerEventTypes>(
151
156
  eventName: K,
152
- sender: IPluginInstance,
157
+ sender: RapidPlugin,
153
158
  payload: RpdServerEventTypes[K][1],
154
159
  ) {
155
160
  console.log(`Emit event "${eventName}"`, payload);
@@ -167,15 +172,16 @@ export class RapidServer implements IRpdServer {
167
172
 
168
173
  async start() {
169
174
  console.log("Starting rapid server...");
170
- await pluginManager.loadPlugins();
175
+ const pluginManager = this.#pluginManager;
176
+ await pluginManager.loadPlugins(this.#plugins);
171
177
 
172
- await pluginManager.initPlugins(this);
178
+ await pluginManager.initPlugins();
173
179
 
174
- await pluginManager.registerMiddlewares(this);
175
- await pluginManager.registerHttpHandlers(this);
176
- await pluginManager.registerEventHandlers(this);
177
- await pluginManager.registerMessageHandlers(this);
178
- await pluginManager.registerTaskProcessors(this);
180
+ await pluginManager.registerMiddlewares();
181
+ await pluginManager.registerHttpHandlers();
182
+ await pluginManager.registerEventHandlers();
183
+ await pluginManager.registerMessageHandlers();
184
+ await pluginManager.registerTaskProcessors();
179
185
 
180
186
  await this.configureApplication();
181
187
 
@@ -189,6 +195,7 @@ export class RapidServer implements IRpdServer {
189
195
  this.#bootstrapApplicationConfig,
190
196
  ) as RpdApplicationConfig;
191
197
 
198
+ const pluginManager = this.#pluginManager;
192
199
  await pluginManager.onLoadingApplication(this, this.#applicationConfig);
193
200
  await pluginManager.configureModels(this, this.#applicationConfig);
194
201
  await pluginManager.configureModelProperties(this, this.#applicationConfig);
package/src/types.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { RapidPlugin } from "./core/server";
2
+
1
3
  export type RapidServerConfig = {
2
4
  baseUrl?: string;
3
5
  sessionCookieName: string;
@@ -60,11 +62,11 @@ export interface GetModelOptions {
60
62
  }
61
63
 
62
64
  export type RpdServerEventTypes = {
63
- "entity.create": [IPluginInstance, RpdEntityCreateEventPayload];
64
- "entity.update": [IPluginInstance, RpdEntityUpdateEventPayload];
65
- "entity.delete": [IPluginInstance, RpdEntityDeleteEventPayload];
66
- "entity.addRelations": [IPluginInstance, RpdEntityAddRelationsEventPayload];
67
- "entity.removeRelations": [IPluginInstance, RpdEntityRemoveRelationsEventPayload];
65
+ "entity.create": [RapidPlugin, RpdEntityCreateEventPayload];
66
+ "entity.update": [RapidPlugin, RpdEntityUpdateEventPayload];
67
+ "entity.delete": [RapidPlugin, RpdEntityDeleteEventPayload];
68
+ "entity.addRelations": [RapidPlugin, RpdEntityAddRelationsEventPayload];
69
+ "entity.removeRelations": [RapidPlugin, RpdEntityRemoveRelationsEventPayload];
68
70
  };
69
71
 
70
72
  export interface RpdEntityCreateEventPayload {
@@ -284,13 +286,8 @@ export interface RpdDataModelExtension {
284
286
  config: any;
285
287
  }
286
288
 
287
-
288
- export interface IPluginInstance {
289
- getName(): string;
290
- }
291
-
292
289
  export type EventHandler<T = any> = (
293
- sender: IPluginInstance,
290
+ sender: RapidPlugin,
294
291
  payload: T,
295
292
  ) => void;
296
293
 
@@ -1,6 +0,0 @@
1
- import { IPluginInstance } from "~/types";
2
- export default class Plugin implements IPluginInstance {
3
- #private;
4
- constructor(name: string);
5
- getName(): string;
6
- }
@@ -1,4 +0,0 @@
1
- import { HttpHandlerContext } from "~/core/httpHandler";
2
- import { IPluginInstance } from "~/types";
3
- export declare const code = "deleteSession";
4
- export declare function handler(plugin: IPluginInstance, ctx: HttpHandlerContext, options: any): Promise<void>;
@@ -1,4 +0,0 @@
1
- import { IPluginInstance } from "~/types";
2
- import { HttpHandlerContext } from "~/core/httpHandler";
3
- export declare const code = "getMyProfile";
4
- export declare function handler(plugin: IPluginInstance, ctx: HttpHandlerContext, options: any): Promise<void>;
@@ -1,16 +0,0 @@
1
- /**
2
- * Meta manager plugin
3
- */
4
- import { IPluginInstance, RpdApplicationConfig } from "~/types";
5
- import { IRpdServer, RpdConfigurationItemOptions, RpdServerPluginConfigurableTargetOptions, RpdServerPluginExtendingAbilities } from "~/core/server";
6
- export declare const code = "authManager";
7
- export declare const description = "authManager";
8
- export declare const extendingAbilities: RpdServerPluginExtendingAbilities[];
9
- export declare const configurableTargets: RpdServerPluginConfigurableTargetOptions[];
10
- export declare const configurations: RpdConfigurationItemOptions[];
11
- export declare function initPlugin(plugin: IPluginInstance, server: IRpdServer): Promise<void>;
12
- export declare function registerHttpHandlers(server: IRpdServer): Promise<void>;
13
- export declare function registerEventHandlers(server: IRpdServer): Promise<void>;
14
- export declare function configureModels(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<void>;
15
- export declare function configureRoutes(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<void>;
16
- export declare function onApplicationLoaded(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<void>;
@@ -1,4 +0,0 @@
1
- import { IPluginInstance, RunEntityHttpHandlerOptions } from "~/types";
2
- import { HttpHandlerContext } from "~/core/httpHandler";
3
- export declare const code = "addEntityRelations";
4
- export declare function handler(plugin: IPluginInstance, ctx: HttpHandlerContext, options: RunEntityHttpHandlerOptions): Promise<void>;
@@ -1,4 +0,0 @@
1
- import { IPluginInstance, RunEntityHttpHandlerOptions } from "~/types";
2
- import { HttpHandlerContext } from "~/core/httpHandler";
3
- export declare const code = "countCollectionEntities";
4
- export declare function handler(plugin: IPluginInstance, ctx: HttpHandlerContext, options: RunEntityHttpHandlerOptions): Promise<void>;
@@ -1,4 +0,0 @@
1
- import { IPluginInstance, RunEntityHttpHandlerOptions } from "~/types";
2
- import { HttpHandlerContext } from "~/core/httpHandler";
3
- export declare const code = "createCollectionEntitiesBatch";
4
- export declare function handler(plugin: IPluginInstance, ctx: HttpHandlerContext, options: RunEntityHttpHandlerOptions): Promise<void>;
@@ -1,4 +0,0 @@
1
- import { IPluginInstance, RunEntityHttpHandlerOptions } from "~/types";
2
- import { HttpHandlerContext } from "~/core/httpHandler";
3
- export declare const code = "createCollectionEntity";
4
- export declare function handler(plugin: IPluginInstance, ctx: HttpHandlerContext, options: RunEntityHttpHandlerOptions): Promise<void>;
@@ -1,4 +0,0 @@
1
- import { IPluginInstance, RunEntityHttpHandlerOptions } from "~/types";
2
- import { HttpHandlerContext } from "~/core/httpHandler";
3
- export declare const code = "deleteCollectionEntityById";
4
- export declare function handler(plugin: IPluginInstance, ctx: HttpHandlerContext, options: RunEntityHttpHandlerOptions): Promise<void>;
@@ -1,4 +0,0 @@
1
- import { IPluginInstance, RunEntityHttpHandlerOptions } from "~/types";
2
- import { HttpHandlerContext } from "~/core/httpHandler";
3
- export declare const code = "findCollectionEntities";
4
- export declare function handler(plugin: IPluginInstance, ctx: HttpHandlerContext, options: RunEntityHttpHandlerOptions): Promise<void>;
@@ -1,4 +0,0 @@
1
- import { IPluginInstance, RunEntityHttpHandlerOptions } from "~/types";
2
- import { HttpHandlerContext } from "~/core/httpHandler";
3
- export declare const code = "findCollectionEntityById";
4
- export declare function handler(plugin: IPluginInstance, ctx: HttpHandlerContext, options: RunEntityHttpHandlerOptions): Promise<void>;
@@ -1,4 +0,0 @@
1
- import { IPluginInstance, RunQueryDatabaseHandlerOptions } from "~/types";
2
- import { HttpHandlerContext } from "~/core/httpHandler";
3
- export declare const code = "queryDatabase";
4
- export declare function handler(plugin: IPluginInstance, ctx: HttpHandlerContext, options: RunQueryDatabaseHandlerOptions): Promise<void>;
@@ -1,4 +0,0 @@
1
- import { IPluginInstance, RunEntityHttpHandlerOptions } from "~/types";
2
- import { HttpHandlerContext } from "~/core/httpHandler";
3
- export declare const code = "removeEntityRelations";
4
- export declare function handler(plugin: IPluginInstance, ctx: HttpHandlerContext, options: RunEntityHttpHandlerOptions): Promise<void>;
@@ -1,4 +0,0 @@
1
- import { IPluginInstance, RunEntityHttpHandlerOptions } from "~/types";
2
- import { HttpHandlerContext } from "~/core/httpHandler";
3
- export declare const code = "updateCollectionEntityById";
4
- export declare function handler(plugin: IPluginInstance, ctx: HttpHandlerContext, options: RunEntityHttpHandlerOptions): Promise<void>;
@@ -1,16 +0,0 @@
1
- /**
2
- * Support manage data in database.
3
- * This plugin provide:
4
- * - routes for manage data in database.
5
- */
6
- import { IPluginInstance, RpdApplicationConfig } from "~/types";
7
- import { RpdServerPluginExtendingAbilities, RpdServerPluginConfigurableTargetOptions, RpdConfigurationItemOptions, IRpdServer } from "~/core/server";
8
- export declare const code = "dataManager";
9
- export declare const description = "\u5BF9\u6570\u636E\u8FDB\u884C\u7BA1\u7406\uFF0C\u63D0\u4F9B\u589E\u5220\u6539\u67E5\u7B49\u63A5\u53E3\u3002";
10
- export declare const extendingAbilities: RpdServerPluginExtendingAbilities[];
11
- export declare const configurableTargets: RpdServerPluginConfigurableTargetOptions[];
12
- export declare const configurations: RpdConfigurationItemOptions[];
13
- export declare function initPlugin(plugin: IPluginInstance, server: IRpdServer): Promise<void>;
14
- export declare function registerHttpHandlers(server: IRpdServer): Promise<void>;
15
- export declare function configureRoutes(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<void>;
16
- export declare function onApplicationLoaded(server: IRpdServer, application: RpdApplicationConfig): Promise<void>;
@@ -1,4 +0,0 @@
1
- import { IPluginInstance } from "~/types";
2
- import { HttpHandlerContext } from "~/core/httpHandler";
3
- export declare const code = "downloadDocument";
4
- export declare function handler(plugin: IPluginInstance, ctx: HttpHandlerContext, options: any): Promise<void>;
@@ -1,4 +0,0 @@
1
- import { IPluginInstance } from "~/types";
2
- import { HttpHandlerContext } from "~/core/httpHandler";
3
- export declare const code = "downloadFile";
4
- export declare function handler(plugin: IPluginInstance, ctx: HttpHandlerContext, options: any): Promise<void>;
@@ -1,4 +0,0 @@
1
- import { IPluginInstance } from "~/types";
2
- import { HttpHandlerContext } from "~/core/httpHandler";
3
- export declare const code = "uploadFile";
4
- export declare function handler(plugin: IPluginInstance, ctx: HttpHandlerContext, options: any): Promise<void>;
@@ -1,15 +0,0 @@
1
- /**
2
- * File manager plugin
3
- */
4
- import { IPluginInstance, RpdApplicationConfig } from "~/types";
5
- import { IRpdServer, RpdConfigurationItemOptions, RpdServerPluginConfigurableTargetOptions, RpdServerPluginExtendingAbilities } from "~/core/server";
6
- export declare const code = "fileManager";
7
- export declare const description = "fileManager";
8
- export declare const extendingAbilities: RpdServerPluginExtendingAbilities[];
9
- export declare const configurableTargets: RpdServerPluginConfigurableTargetOptions[];
10
- export declare const configurations: RpdConfigurationItemOptions[];
11
- export declare function initPlugin(plugin: IPluginInstance, server: IRpdServer): Promise<void>;
12
- export declare function registerHttpHandlers(server: IRpdServer): Promise<void>;
13
- export declare function registerEventHandlers(server: IRpdServer): Promise<void>;
14
- export declare function configureModels(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<void>;
15
- export declare function onApplicationLoaded(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<void>;
@@ -1,4 +0,0 @@
1
- import { HttpHandlerContext } from "~/core/httpHandler";
2
- import { IPluginInstance } from "~/types";
3
- export declare const code = "getMetaModelDetail";
4
- export declare function handler(plugin: IPluginInstance, ctx: HttpHandlerContext, options: any): Promise<void>;