@ruiapp/rapid-core 0.5.11 → 0.5.13

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 (185) hide show
  1. package/CHANGELOG.md +7 -7
  2. package/dist/facilities/cache/CacheFacilityTypes.d.ts +4 -2
  3. package/dist/facilities/cache/MemoryCache.d.ts +3 -1
  4. package/dist/index.d.ts +4 -0
  5. package/dist/index.js +131 -72
  6. package/dist/utilities/entityUtility.d.ts +1 -0
  7. package/dist/utilities/passwordUtility.d.ts +14 -0
  8. package/package.json +2 -2
  9. package/rollup.config.js +16 -16
  10. package/src/bootstrapApplicationConfig.ts +638 -638
  11. package/src/core/actionHandler.ts +22 -22
  12. package/src/core/eventManager.ts +20 -20
  13. package/src/core/facility.ts +7 -7
  14. package/src/core/http/formDataParser.ts +89 -89
  15. package/src/core/http-types.ts +4 -4
  16. package/src/core/pluginManager.ts +175 -175
  17. package/src/core/providers/runtimeProvider.ts +5 -5
  18. package/src/core/request.ts +95 -95
  19. package/src/core/response.ts +79 -79
  20. package/src/core/routeContext.ts +100 -100
  21. package/src/core/routesBuilder.ts +88 -88
  22. package/src/core/server.ts +145 -145
  23. package/src/dataAccess/columnTypeMapper.ts +22 -22
  24. package/src/dataAccess/dataAccessTypes.ts +163 -163
  25. package/src/dataAccess/dataAccessor.ts +135 -135
  26. package/src/dataAccess/entityManager.ts +1910 -1910
  27. package/src/dataAccess/entityMapper.ts +100 -100
  28. package/src/dataAccess/propertyMapper.ts +28 -28
  29. package/src/deno-std/assert/assert.ts +9 -9
  30. package/src/deno-std/assert/assertion_error.ts +7 -7
  31. package/src/deno-std/datetime/to_imf.ts +32 -32
  32. package/src/deno-std/encoding/base64.ts +141 -141
  33. package/src/deno-std/http/cookie.ts +372 -372
  34. package/src/facilities/cache/CacheFacilityTypes.ts +29 -27
  35. package/src/facilities/cache/CacheFactory.ts +31 -31
  36. package/src/facilities/cache/MemoryCache.ts +58 -42
  37. package/src/facilities/cache/MemoryCacheProvider.ts +15 -15
  38. package/src/facilities/log/LogFacility.ts +35 -35
  39. package/src/helpers/entityHelpers.ts +76 -76
  40. package/src/helpers/filterHelper.ts +148 -148
  41. package/src/helpers/inputHelper.ts +11 -11
  42. package/src/helpers/metaHelper.ts +104 -104
  43. package/src/helpers/runCollectionEntityActionHandler.ts +57 -57
  44. package/src/index.ts +67 -63
  45. package/src/plugins/auth/AuthPlugin.ts +93 -93
  46. package/src/plugins/auth/actionHandlers/changePassword.ts +60 -61
  47. package/src/plugins/auth/actionHandlers/createSession.ts +68 -68
  48. package/src/plugins/auth/actionHandlers/deleteSession.ts +18 -18
  49. package/src/plugins/auth/actionHandlers/getMyProfile.ts +35 -35
  50. package/src/plugins/auth/actionHandlers/index.ts +8 -8
  51. package/src/plugins/auth/actionHandlers/resetPassword.ts +44 -45
  52. package/src/plugins/auth/models/AccessToken.ts +56 -56
  53. package/src/plugins/auth/models/index.ts +3 -3
  54. package/src/plugins/auth/routes/changePassword.ts +15 -15
  55. package/src/plugins/auth/routes/getMyProfile.ts +15 -15
  56. package/src/plugins/auth/routes/index.ts +7 -7
  57. package/src/plugins/auth/routes/resetPassword.ts +15 -15
  58. package/src/plugins/auth/routes/signin.ts +15 -15
  59. package/src/plugins/auth/routes/signout.ts +15 -15
  60. package/src/plugins/auth/services/AuthService.ts +39 -39
  61. package/src/plugins/cronJob/CronJobPlugin.ts +112 -112
  62. package/src/plugins/cronJob/CronJobPluginTypes.ts +49 -49
  63. package/src/plugins/cronJob/actionHandlers/index.ts +4 -4
  64. package/src/plugins/cronJob/actionHandlers/runCronJob.ts +29 -29
  65. package/src/plugins/cronJob/routes/index.ts +3 -3
  66. package/src/plugins/cronJob/routes/runCronJob.ts +15 -15
  67. package/src/plugins/dataManage/DataManagePlugin.ts +163 -163
  68. package/src/plugins/dataManage/actionHandlers/addEntityRelations.ts +15 -15
  69. package/src/plugins/dataManage/actionHandlers/countCollectionEntities.ts +17 -17
  70. package/src/plugins/dataManage/actionHandlers/createCollectionEntitiesBatch.ts +81 -81
  71. package/src/plugins/dataManage/actionHandlers/createCollectionEntity.ts +20 -20
  72. package/src/plugins/dataManage/actionHandlers/deleteCollectionEntities.ts +45 -45
  73. package/src/plugins/dataManage/actionHandlers/deleteCollectionEntityById.ts +20 -20
  74. package/src/plugins/dataManage/actionHandlers/findCollectionEntities.ts +27 -27
  75. package/src/plugins/dataManage/actionHandlers/findCollectionEntityById.ts +30 -30
  76. package/src/plugins/dataManage/actionHandlers/queryDatabase.ts +22 -22
  77. package/src/plugins/dataManage/actionHandlers/removeEntityRelations.ts +15 -15
  78. package/src/plugins/dataManage/actionHandlers/updateCollectionEntityById.ts +38 -38
  79. package/src/plugins/entityAccessControl/EntityAccessControlPlugin.ts +146 -146
  80. package/src/plugins/fileManage/FileManagePlugin.ts +52 -52
  81. package/src/plugins/fileManage/actionHandlers/downloadDocument.ts +65 -65
  82. package/src/plugins/fileManage/actionHandlers/downloadFile.ts +44 -44
  83. package/src/plugins/fileManage/actionHandlers/uploadFile.ts +33 -33
  84. package/src/plugins/fileManage/routes/downloadDocument.ts +15 -15
  85. package/src/plugins/fileManage/routes/downloadFile.ts +15 -15
  86. package/src/plugins/fileManage/routes/index.ts +5 -5
  87. package/src/plugins/fileManage/routes/uploadFile.ts +15 -15
  88. package/src/plugins/license/LicensePlugin.ts +79 -79
  89. package/src/plugins/license/LicensePluginTypes.ts +95 -95
  90. package/src/plugins/license/LicenseService.ts +118 -118
  91. package/src/plugins/license/actionHandlers/getLicense.ts +18 -18
  92. package/src/plugins/license/actionHandlers/index.ts +4 -4
  93. package/src/plugins/license/helpers/certHelper.ts +21 -21
  94. package/src/plugins/license/helpers/cryptoHelper.ts +47 -47
  95. package/src/plugins/license/models/index.ts +1 -1
  96. package/src/plugins/license/routes/getLicense.ts +15 -15
  97. package/src/plugins/license/routes/index.ts +3 -3
  98. package/src/plugins/mail/MailPlugin.ts +74 -74
  99. package/src/plugins/mail/MailPluginTypes.ts +27 -27
  100. package/src/plugins/mail/MailService.ts +38 -38
  101. package/src/plugins/mail/actionHandlers/index.ts +3 -3
  102. package/src/plugins/mail/models/index.ts +1 -1
  103. package/src/plugins/mail/routes/index.ts +1 -1
  104. package/src/plugins/metaManage/MetaManagePlugin.ts +530 -530
  105. package/src/plugins/metaManage/actionHandlers/getMetaModelDetail.ts +10 -10
  106. package/src/plugins/metaManage/actionHandlers/listMetaModels.ts +9 -9
  107. package/src/plugins/metaManage/actionHandlers/listMetaRoutes.ts +9 -9
  108. package/src/plugins/notification/NotificationPlugin.ts +68 -68
  109. package/src/plugins/notification/NotificationPluginTypes.ts +13 -13
  110. package/src/plugins/notification/NotificationService.ts +25 -25
  111. package/src/plugins/notification/actionHandlers/index.ts +3 -3
  112. package/src/plugins/notification/models/Notification.ts +60 -60
  113. package/src/plugins/notification/models/index.ts +3 -3
  114. package/src/plugins/notification/routes/index.ts +1 -1
  115. package/src/plugins/routeManage/RouteManagePlugin.ts +62 -62
  116. package/src/plugins/routeManage/actionHandlers/httpProxy.ts +13 -13
  117. package/src/plugins/sequence/SequencePlugin.ts +146 -146
  118. package/src/plugins/sequence/SequencePluginTypes.ts +69 -69
  119. package/src/plugins/sequence/SequenceService.ts +92 -92
  120. package/src/plugins/sequence/actionHandlers/generateSn.ts +32 -32
  121. package/src/plugins/sequence/actionHandlers/index.ts +4 -4
  122. package/src/plugins/sequence/models/SequenceAutoIncrementRecord.ts +49 -49
  123. package/src/plugins/sequence/models/SequenceRule.ts +42 -42
  124. package/src/plugins/sequence/models/index.ts +4 -4
  125. package/src/plugins/sequence/routes/generateSn.ts +15 -15
  126. package/src/plugins/sequence/routes/index.ts +3 -3
  127. package/src/plugins/sequence/segment-utility.ts +11 -11
  128. package/src/plugins/sequence/segments/autoIncrement.ts +90 -90
  129. package/src/plugins/sequence/segments/dayOfMonth.ts +19 -19
  130. package/src/plugins/sequence/segments/index.ts +9 -9
  131. package/src/plugins/sequence/segments/literal.ts +16 -16
  132. package/src/plugins/sequence/segments/month.ts +19 -19
  133. package/src/plugins/sequence/segments/parameter.ts +20 -20
  134. package/src/plugins/sequence/segments/year.ts +19 -19
  135. package/src/plugins/serverOperation/ServerOperationPlugin.ts +91 -91
  136. package/src/plugins/serverOperation/ServerOperationPluginTypes.ts +15 -15
  137. package/src/plugins/serverOperation/actionHandlers/index.ts +4 -4
  138. package/src/plugins/serverOperation/actionHandlers/runServerOperation.ts +15 -15
  139. package/src/plugins/setting/SettingPlugin.ts +68 -68
  140. package/src/plugins/setting/SettingPluginTypes.ts +37 -37
  141. package/src/plugins/setting/SettingService.ts +213 -213
  142. package/src/plugins/setting/actionHandlers/getSystemSettingValues.ts +30 -30
  143. package/src/plugins/setting/actionHandlers/getUserSettingValues.ts +38 -38
  144. package/src/plugins/setting/actionHandlers/index.ts +6 -6
  145. package/src/plugins/setting/actionHandlers/setSystemSettingValues.ts +30 -30
  146. package/src/plugins/setting/models/SystemSettingGroupSetting.ts +57 -57
  147. package/src/plugins/setting/models/SystemSettingItem.ts +48 -48
  148. package/src/plugins/setting/models/SystemSettingItemSetting.ts +73 -73
  149. package/src/plugins/setting/models/UserSettingGroupSetting.ts +57 -57
  150. package/src/plugins/setting/models/UserSettingItem.ts +55 -55
  151. package/src/plugins/setting/models/UserSettingItemSetting.ts +73 -73
  152. package/src/plugins/setting/models/index.ts +8 -8
  153. package/src/plugins/setting/routes/getSystemSettingValues.ts +15 -15
  154. package/src/plugins/setting/routes/getUserSettingValues.ts +15 -15
  155. package/src/plugins/setting/routes/index.ts +5 -5
  156. package/src/plugins/setting/routes/setSystemSettingValues.ts +15 -15
  157. package/src/plugins/stateMachine/StateMachinePlugin.ts +196 -196
  158. package/src/plugins/stateMachine/StateMachinePluginTypes.ts +48 -48
  159. package/src/plugins/stateMachine/actionHandlers/index.ts +4 -4
  160. package/src/plugins/stateMachine/actionHandlers/sendStateMachineEvent.ts +54 -54
  161. package/src/plugins/stateMachine/models/StateMachine.ts +42 -42
  162. package/src/plugins/stateMachine/models/index.ts +3 -3
  163. package/src/plugins/stateMachine/routes/index.ts +3 -3
  164. package/src/plugins/stateMachine/routes/sendStateMachineEvent.ts +15 -15
  165. package/src/plugins/stateMachine/stateMachineHelper.ts +36 -36
  166. package/src/plugins/webhooks/WebhooksPlugin.ts +148 -148
  167. package/src/plugins/webhooks/pluginConfig.ts +75 -75
  168. package/src/polyfill.ts +5 -5
  169. package/src/proxy/mod.ts +38 -38
  170. package/src/proxy/types.ts +21 -21
  171. package/src/queryBuilder/index.ts +1 -1
  172. package/src/queryBuilder/queryBuilder.ts +668 -668
  173. package/src/server.ts +480 -480
  174. package/src/types.ts +722 -722
  175. package/src/utilities/accessControlUtility.ts +33 -33
  176. package/src/utilities/entityUtility.ts +18 -0
  177. package/src/utilities/errorUtility.ts +15 -15
  178. package/src/utilities/fsUtility.ts +61 -61
  179. package/src/utilities/httpUtility.ts +19 -19
  180. package/src/utilities/jwtUtility.ts +26 -26
  181. package/src/utilities/passwordUtility.ts +26 -0
  182. package/src/utilities/pathUtility.ts +14 -14
  183. package/src/utilities/timeUtility.ts +9 -9
  184. package/src/utilities/typeUtility.ts +15 -15
  185. package/tsconfig.json +19 -19
@@ -1,22 +1,22 @@
1
- import { RpdApplicationConfig } from "~/types";
2
- import { IRpdServer, RapidPlugin } from "./server";
3
- import { Next, RouteContext } from "./routeContext";
4
- import { Logger } from "~/facilities/log/LogFacility";
5
-
6
- export interface ActionHandlerContext {
7
- logger: Logger;
8
- routerContext: RouteContext;
9
- next: Next;
10
- server: IRpdServer;
11
- applicationConfig: RpdApplicationConfig;
12
- input?: any;
13
- output?: any;
14
- status?: Response["status"];
15
- }
16
-
17
- export type ActionHandler = (ctx: ActionHandlerContext, options: any) => void | Promise<void>;
18
-
19
- export interface IPluginActionHandler {
20
- code: string;
21
- handler: (plugin: RapidPlugin, ctx: ActionHandlerContext, options: any) => void | Promise<void>;
22
- }
1
+ import { RpdApplicationConfig } from "~/types";
2
+ import { IRpdServer, RapidPlugin } from "./server";
3
+ import { Next, RouteContext } from "./routeContext";
4
+ import { Logger } from "~/facilities/log/LogFacility";
5
+
6
+ export interface ActionHandlerContext {
7
+ logger: Logger;
8
+ routerContext: RouteContext;
9
+ next: Next;
10
+ server: IRpdServer;
11
+ applicationConfig: RpdApplicationConfig;
12
+ input?: any;
13
+ output?: any;
14
+ status?: Response["status"];
15
+ }
16
+
17
+ export type ActionHandler = (ctx: ActionHandlerContext, options: any) => void | Promise<void>;
18
+
19
+ export interface IPluginActionHandler {
20
+ code: string;
21
+ handler: (plugin: RapidPlugin, ctx: ActionHandlerContext, options: any) => void | Promise<void>;
22
+ }
@@ -1,20 +1,20 @@
1
- import { EventEmitter } from "events";
2
-
3
- export default class EventManager<EventTypes extends Record<string, any[]>> {
4
- #eventEmitter: EventEmitter;
5
-
6
- constructor() {
7
- this.#eventEmitter = new EventEmitter();
8
- }
9
-
10
- on<K extends keyof EventTypes>(eventName: K, listener: (...args: EventTypes[K]) => void) {
11
- this.#eventEmitter.on(eventName as string, listener);
12
- }
13
-
14
- async emit<K extends keyof EventTypes>(eventName: K, ...args: EventTypes[K]) {
15
- const listeners = this.#eventEmitter.listeners(eventName as string);
16
- for (const listener of listeners) {
17
- await listener(...args);
18
- }
19
- }
20
- }
1
+ import { EventEmitter } from "events";
2
+
3
+ export default class EventManager<EventTypes extends Record<string, any[]>> {
4
+ #eventEmitter: EventEmitter;
5
+
6
+ constructor() {
7
+ this.#eventEmitter = new EventEmitter();
8
+ }
9
+
10
+ on<K extends keyof EventTypes>(eventName: K, listener: (...args: EventTypes[K]) => void) {
11
+ this.#eventEmitter.on(eventName as string, listener);
12
+ }
13
+
14
+ async emit<K extends keyof EventTypes>(eventName: K, ...args: EventTypes[K]) {
15
+ const listeners = this.#eventEmitter.listeners(eventName as string);
16
+ for (const listener of listeners) {
17
+ await listener(...args);
18
+ }
19
+ }
20
+ }
@@ -1,7 +1,7 @@
1
- import { IRpdServer } from "./server";
2
-
3
- export interface FacilityFactory<TFacility = any, TCreateFacilityOptions = any> {
4
- name: string;
5
-
6
- createFacility: (server: IRpdServer, options?: TCreateFacilityOptions) => Promise<TFacility>;
7
- }
1
+ import { IRpdServer } from "./server";
2
+
3
+ export interface FacilityFactory<TFacility = any, TCreateFacilityOptions = any> {
4
+ name: string;
5
+
6
+ createFacility: (server: IRpdServer, options?: TCreateFacilityOptions) => Promise<TFacility>;
7
+ }
@@ -1,89 +1,89 @@
1
- import type { RapidRequest } from "../request";
2
-
3
- export type BodyData = Record<string, string | File | (string | File)[]>;
4
- export type ParseBodyOptions = {
5
- /**
6
- * Parse all fields with multiple values should be parsed as an array.
7
- * @default false
8
- * @example
9
- * ```ts
10
- * const data = new FormData()
11
- * data.append('file', 'aaa')
12
- * data.append('file', 'bbb')
13
- * data.append('message', 'hello')
14
- * ```
15
- *
16
- * If `all` is `false`:
17
- * parseBody should return `{ file: 'bbb', message: 'hello' }`
18
- *
19
- * If `all` is `true`:
20
- * parseBody should return `{ file: ['aaa', 'bbb'], message: 'hello' }`
21
- */
22
- all?: boolean;
23
- };
24
-
25
- export const parseFormDataBody = async <T extends BodyData = BodyData>(request: Request, options: ParseBodyOptions = { all: false }): Promise<T> => {
26
- const contentType = request.headers.get("Content-Type");
27
-
28
- if (isFormDataContent(contentType)) {
29
- return parseFormData<T>(request, options);
30
- }
31
-
32
- return {} as T;
33
- };
34
-
35
- function isFormDataContent(contentType: string | null): boolean {
36
- if (contentType === null) {
37
- return false;
38
- }
39
-
40
- return contentType.startsWith("multipart/form-data") || contentType.startsWith("application/x-www-form-urlencoded");
41
- }
42
-
43
- async function parseFormData<T extends BodyData = BodyData>(request: Request, options: ParseBodyOptions): Promise<T> {
44
- const formData = await (request as Request).formData();
45
-
46
- if (formData) {
47
- return convertFormDataToBodyData<T>(formData, options);
48
- }
49
-
50
- return {} as T;
51
- }
52
-
53
- function convertFormDataToBodyData<T extends BodyData = BodyData>(formData: FormData, options: ParseBodyOptions): T {
54
- const form: BodyData = {};
55
-
56
- formData.forEach((value, key) => {
57
- const shouldParseAllValues = options.all || key.endsWith("[]");
58
-
59
- if (!shouldParseAllValues) {
60
- form[key] = value;
61
- } else {
62
- handleParsingAllValues(form, key, value);
63
- }
64
- });
65
-
66
- return form as T;
67
- }
68
-
69
- const handleParsingAllValues = (form: BodyData, key: string, value: FormDataEntryValue): void => {
70
- if (form[key] && isArrayField(form[key])) {
71
- appendToExistingArray(form[key] as (string | File)[], value);
72
- } else if (form[key]) {
73
- convertToNewArray(form, key, value);
74
- } else {
75
- form[key] = value;
76
- }
77
- };
78
-
79
- function isArrayField(field: unknown): field is (string | File)[] {
80
- return Array.isArray(field);
81
- }
82
-
83
- const appendToExistingArray = (arr: (string | File)[], value: FormDataEntryValue): void => {
84
- arr.push(value);
85
- };
86
-
87
- const convertToNewArray = (form: BodyData, key: string, value: FormDataEntryValue): void => {
88
- form[key] = [form[key] as string | File, value];
89
- };
1
+ import type { RapidRequest } from "../request";
2
+
3
+ export type BodyData = Record<string, string | File | (string | File)[]>;
4
+ export type ParseBodyOptions = {
5
+ /**
6
+ * Parse all fields with multiple values should be parsed as an array.
7
+ * @default false
8
+ * @example
9
+ * ```ts
10
+ * const data = new FormData()
11
+ * data.append('file', 'aaa')
12
+ * data.append('file', 'bbb')
13
+ * data.append('message', 'hello')
14
+ * ```
15
+ *
16
+ * If `all` is `false`:
17
+ * parseBody should return `{ file: 'bbb', message: 'hello' }`
18
+ *
19
+ * If `all` is `true`:
20
+ * parseBody should return `{ file: ['aaa', 'bbb'], message: 'hello' }`
21
+ */
22
+ all?: boolean;
23
+ };
24
+
25
+ export const parseFormDataBody = async <T extends BodyData = BodyData>(request: Request, options: ParseBodyOptions = { all: false }): Promise<T> => {
26
+ const contentType = request.headers.get("Content-Type");
27
+
28
+ if (isFormDataContent(contentType)) {
29
+ return parseFormData<T>(request, options);
30
+ }
31
+
32
+ return {} as T;
33
+ };
34
+
35
+ function isFormDataContent(contentType: string | null): boolean {
36
+ if (contentType === null) {
37
+ return false;
38
+ }
39
+
40
+ return contentType.startsWith("multipart/form-data") || contentType.startsWith("application/x-www-form-urlencoded");
41
+ }
42
+
43
+ async function parseFormData<T extends BodyData = BodyData>(request: Request, options: ParseBodyOptions): Promise<T> {
44
+ const formData = await (request as Request).formData();
45
+
46
+ if (formData) {
47
+ return convertFormDataToBodyData<T>(formData, options);
48
+ }
49
+
50
+ return {} as T;
51
+ }
52
+
53
+ function convertFormDataToBodyData<T extends BodyData = BodyData>(formData: FormData, options: ParseBodyOptions): T {
54
+ const form: BodyData = {};
55
+
56
+ formData.forEach((value, key) => {
57
+ const shouldParseAllValues = options.all || key.endsWith("[]");
58
+
59
+ if (!shouldParseAllValues) {
60
+ form[key] = value;
61
+ } else {
62
+ handleParsingAllValues(form, key, value);
63
+ }
64
+ });
65
+
66
+ return form as T;
67
+ }
68
+
69
+ const handleParsingAllValues = (form: BodyData, key: string, value: FormDataEntryValue): void => {
70
+ if (form[key] && isArrayField(form[key])) {
71
+ appendToExistingArray(form[key] as (string | File)[], value);
72
+ } else if (form[key]) {
73
+ convertToNewArray(form, key, value);
74
+ } else {
75
+ form[key] = value;
76
+ }
77
+ };
78
+
79
+ function isArrayField(field: unknown): field is (string | File)[] {
80
+ return Array.isArray(field);
81
+ }
82
+
83
+ const appendToExistingArray = (arr: (string | File)[], value: FormDataEntryValue): void => {
84
+ arr.push(value);
85
+ };
86
+
87
+ const convertToNewArray = (form: BodyData, key: string, value: FormDataEntryValue): void => {
88
+ form[key] = [form[key] as string | File, value];
89
+ };
@@ -1,4 +1,4 @@
1
- export type HttpStatus = number;
2
- export type ResponseData = string | ArrayBuffer | ReadableStream;
3
-
4
- export type HeadersDefinition = [string, string][] | Record<string, string>;
1
+ export type HttpStatus = number;
2
+ export type ResponseData = string | ArrayBuffer | ReadableStream;
3
+
4
+ export type HeadersDefinition = [string, string][] | Record<string, string>;
@@ -1,175 +1,175 @@
1
- import { CreateEntityOptions, RpdApplicationConfig, RpdDataModel, UpdateEntityByIdOptions } from "~/types";
2
- import { IRpdServer, RapidPlugin } from "./server";
3
- import { RouteContext } from "./routeContext";
4
- import { ActionHandlerContext } from "./actionHandler";
5
-
6
- class PluginManager {
7
- #server: IRpdServer;
8
- #plugins: RapidPlugin[];
9
-
10
- constructor(server: IRpdServer) {
11
- this.#server = server;
12
- this.#plugins = [];
13
- }
14
-
15
- async loadPlugins(plugins: RapidPlugin[]) {
16
- for (const plugin of plugins) {
17
- this.#plugins.push(plugin);
18
- }
19
- }
20
-
21
- /** 初始化插件时调用。 */
22
- async initPlugins() {
23
- for (const plugin of this.#plugins) {
24
- if (plugin.initPlugin) {
25
- await plugin.initPlugin(this.#server);
26
- }
27
- }
28
- }
29
-
30
- /** 注册中间件 */
31
- async registerMiddlewares() {
32
- for (const plugin of this.#plugins) {
33
- if (plugin.registerMiddlewares) {
34
- await plugin.registerMiddlewares(this.#server);
35
- }
36
- }
37
- }
38
-
39
- /** 注册接口动作处理程序 */
40
- async registerActionHandlers() {
41
- for (const plugin of this.#plugins) {
42
- if (plugin.registerActionHandlers) {
43
- await plugin.registerActionHandlers(this.#server);
44
- }
45
- }
46
- }
47
-
48
- /** 注册事件处理程序 */
49
- async registerEventHandlers() {
50
- for (const plugin of this.#plugins) {
51
- if (plugin.registerEventHandlers) {
52
- await plugin.registerEventHandlers(this.#server);
53
- }
54
- }
55
- }
56
-
57
- /** 注册消息处理程序 */
58
- async registerMessageHandlers() {
59
- for (const plugin of this.#plugins) {
60
- if (plugin.registerMessageHandlers) {
61
- await plugin.registerMessageHandlers(this.#server);
62
- }
63
- }
64
- }
65
-
66
- /** 注册任务处理程序 */
67
- async registerTaskProcessors() {
68
- for (const plugin of this.#plugins) {
69
- if (plugin.registerTaskProcessors) {
70
- await plugin.registerTaskProcessors(this.#server);
71
- }
72
- }
73
- }
74
-
75
- /** 在加载应用前调用。 */
76
- async onLoadingApplication(applicationConfig: RpdApplicationConfig) {
77
- for (const plugin of this.#plugins) {
78
- if (plugin.onLoadingApplication) {
79
- await plugin.onLoadingApplication(this.#server, applicationConfig);
80
- }
81
- }
82
- }
83
-
84
- /** 配置数据模型 */
85
- async configureModels(applicationConfig: RpdApplicationConfig) {
86
- for (const plugin of this.#plugins) {
87
- if (plugin.configureModels) {
88
- await plugin.configureModels(this.#server, applicationConfig);
89
- }
90
- }
91
- }
92
-
93
- /** 配置模型属性 */
94
- async configureModelProperties(applicationConfig: RpdApplicationConfig) {
95
- for (const plugin of this.#plugins) {
96
- if (plugin.configureModelProperties) {
97
- await plugin.configureModelProperties(this.#server, applicationConfig);
98
- }
99
- }
100
- }
101
-
102
- /** 配置服务 */
103
- async configureServices(applicationConfig: RpdApplicationConfig) {
104
- for (const plugin of this.#plugins) {
105
- if (plugin.configureServices) {
106
- await plugin.configureServices(this.#server, applicationConfig);
107
- }
108
- }
109
- }
110
-
111
- /** 配置路由 */
112
- async configureRoutes(applicationConfig: RpdApplicationConfig) {
113
- for (const plugin of this.#plugins) {
114
- if (plugin.configureRoutes) {
115
- await plugin.configureRoutes(this.#server, applicationConfig);
116
- }
117
- }
118
- }
119
-
120
- /** 在应用配置加载完成后调用。此时插件可以进行一些数据的初始化工作。 */
121
- async onApplicationLoaded(applicationConfig: RpdApplicationConfig) {
122
- for (const plugin of this.#plugins) {
123
- if (plugin.onApplicationLoaded) {
124
- await plugin.onApplicationLoaded(this.#server, applicationConfig);
125
- }
126
- }
127
- }
128
-
129
- /** 在应用准备完成后调用。此时服务器已经可以处理网络请求。 */
130
- async onApplicationReady(applicationConfig: RpdApplicationConfig) {
131
- for (const plugin of this.#plugins) {
132
- if (plugin.onApplicationReady) {
133
- await plugin.onApplicationReady(this.#server, applicationConfig);
134
- }
135
- }
136
- }
137
-
138
- /** 在接收到HTTP请求,准备路由上下文时调用。 */
139
- async onPrepareRouteContext(routeContext: RouteContext) {
140
- for (const plugin of this.#plugins) {
141
- if (plugin.onPrepareRouteContext) {
142
- await plugin.onPrepareRouteContext(this.#server, routeContext);
143
- }
144
- }
145
- }
146
-
147
- /** 在接收到HTTP请求,执行 actions 前调用。 */
148
- async beforeRunRouteActions(handlerContext: ActionHandlerContext) {
149
- for (const plugin of this.#plugins) {
150
- if (plugin.beforeRunRouteActions) {
151
- await plugin.beforeRunRouteActions(this.#server, handlerContext);
152
- }
153
- }
154
- }
155
-
156
- /** 在创建实体前调用。 */
157
- async beforeCreateEntity(model: RpdDataModel, options: CreateEntityOptions) {
158
- for (const plugin of this.#plugins) {
159
- if (plugin.beforeCreateEntity) {
160
- await plugin.beforeCreateEntity(this.#server, model, options);
161
- }
162
- }
163
- }
164
-
165
- /** 在更新实体前调用。 */
166
- async beforeUpdateEntity(model: RpdDataModel, options: UpdateEntityByIdOptions, currentEntity: any) {
167
- for (const plugin of this.#plugins) {
168
- if (plugin.beforeUpdateEntity) {
169
- await plugin.beforeUpdateEntity(this.#server, model, options, currentEntity);
170
- }
171
- }
172
- }
173
- }
174
-
175
- export default PluginManager;
1
+ import { CreateEntityOptions, RpdApplicationConfig, RpdDataModel, UpdateEntityByIdOptions } from "~/types";
2
+ import { IRpdServer, RapidPlugin } from "./server";
3
+ import { RouteContext } from "./routeContext";
4
+ import { ActionHandlerContext } from "./actionHandler";
5
+
6
+ class PluginManager {
7
+ #server: IRpdServer;
8
+ #plugins: RapidPlugin[];
9
+
10
+ constructor(server: IRpdServer) {
11
+ this.#server = server;
12
+ this.#plugins = [];
13
+ }
14
+
15
+ async loadPlugins(plugins: RapidPlugin[]) {
16
+ for (const plugin of plugins) {
17
+ this.#plugins.push(plugin);
18
+ }
19
+ }
20
+
21
+ /** 初始化插件时调用。 */
22
+ async initPlugins() {
23
+ for (const plugin of this.#plugins) {
24
+ if (plugin.initPlugin) {
25
+ await plugin.initPlugin(this.#server);
26
+ }
27
+ }
28
+ }
29
+
30
+ /** 注册中间件 */
31
+ async registerMiddlewares() {
32
+ for (const plugin of this.#plugins) {
33
+ if (plugin.registerMiddlewares) {
34
+ await plugin.registerMiddlewares(this.#server);
35
+ }
36
+ }
37
+ }
38
+
39
+ /** 注册接口动作处理程序 */
40
+ async registerActionHandlers() {
41
+ for (const plugin of this.#plugins) {
42
+ if (plugin.registerActionHandlers) {
43
+ await plugin.registerActionHandlers(this.#server);
44
+ }
45
+ }
46
+ }
47
+
48
+ /** 注册事件处理程序 */
49
+ async registerEventHandlers() {
50
+ for (const plugin of this.#plugins) {
51
+ if (plugin.registerEventHandlers) {
52
+ await plugin.registerEventHandlers(this.#server);
53
+ }
54
+ }
55
+ }
56
+
57
+ /** 注册消息处理程序 */
58
+ async registerMessageHandlers() {
59
+ for (const plugin of this.#plugins) {
60
+ if (plugin.registerMessageHandlers) {
61
+ await plugin.registerMessageHandlers(this.#server);
62
+ }
63
+ }
64
+ }
65
+
66
+ /** 注册任务处理程序 */
67
+ async registerTaskProcessors() {
68
+ for (const plugin of this.#plugins) {
69
+ if (plugin.registerTaskProcessors) {
70
+ await plugin.registerTaskProcessors(this.#server);
71
+ }
72
+ }
73
+ }
74
+
75
+ /** 在加载应用前调用。 */
76
+ async onLoadingApplication(applicationConfig: RpdApplicationConfig) {
77
+ for (const plugin of this.#plugins) {
78
+ if (plugin.onLoadingApplication) {
79
+ await plugin.onLoadingApplication(this.#server, applicationConfig);
80
+ }
81
+ }
82
+ }
83
+
84
+ /** 配置数据模型 */
85
+ async configureModels(applicationConfig: RpdApplicationConfig) {
86
+ for (const plugin of this.#plugins) {
87
+ if (plugin.configureModels) {
88
+ await plugin.configureModels(this.#server, applicationConfig);
89
+ }
90
+ }
91
+ }
92
+
93
+ /** 配置模型属性 */
94
+ async configureModelProperties(applicationConfig: RpdApplicationConfig) {
95
+ for (const plugin of this.#plugins) {
96
+ if (plugin.configureModelProperties) {
97
+ await plugin.configureModelProperties(this.#server, applicationConfig);
98
+ }
99
+ }
100
+ }
101
+
102
+ /** 配置服务 */
103
+ async configureServices(applicationConfig: RpdApplicationConfig) {
104
+ for (const plugin of this.#plugins) {
105
+ if (plugin.configureServices) {
106
+ await plugin.configureServices(this.#server, applicationConfig);
107
+ }
108
+ }
109
+ }
110
+
111
+ /** 配置路由 */
112
+ async configureRoutes(applicationConfig: RpdApplicationConfig) {
113
+ for (const plugin of this.#plugins) {
114
+ if (plugin.configureRoutes) {
115
+ await plugin.configureRoutes(this.#server, applicationConfig);
116
+ }
117
+ }
118
+ }
119
+
120
+ /** 在应用配置加载完成后调用。此时插件可以进行一些数据的初始化工作。 */
121
+ async onApplicationLoaded(applicationConfig: RpdApplicationConfig) {
122
+ for (const plugin of this.#plugins) {
123
+ if (plugin.onApplicationLoaded) {
124
+ await plugin.onApplicationLoaded(this.#server, applicationConfig);
125
+ }
126
+ }
127
+ }
128
+
129
+ /** 在应用准备完成后调用。此时服务器已经可以处理网络请求。 */
130
+ async onApplicationReady(applicationConfig: RpdApplicationConfig) {
131
+ for (const plugin of this.#plugins) {
132
+ if (plugin.onApplicationReady) {
133
+ await plugin.onApplicationReady(this.#server, applicationConfig);
134
+ }
135
+ }
136
+ }
137
+
138
+ /** 在接收到HTTP请求,准备路由上下文时调用。 */
139
+ async onPrepareRouteContext(routeContext: RouteContext) {
140
+ for (const plugin of this.#plugins) {
141
+ if (plugin.onPrepareRouteContext) {
142
+ await plugin.onPrepareRouteContext(this.#server, routeContext);
143
+ }
144
+ }
145
+ }
146
+
147
+ /** 在接收到HTTP请求,执行 actions 前调用。 */
148
+ async beforeRunRouteActions(handlerContext: ActionHandlerContext) {
149
+ for (const plugin of this.#plugins) {
150
+ if (plugin.beforeRunRouteActions) {
151
+ await plugin.beforeRunRouteActions(this.#server, handlerContext);
152
+ }
153
+ }
154
+ }
155
+
156
+ /** 在创建实体前调用。 */
157
+ async beforeCreateEntity(model: RpdDataModel, options: CreateEntityOptions) {
158
+ for (const plugin of this.#plugins) {
159
+ if (plugin.beforeCreateEntity) {
160
+ await plugin.beforeCreateEntity(this.#server, model, options);
161
+ }
162
+ }
163
+ }
164
+
165
+ /** 在更新实体前调用。 */
166
+ async beforeUpdateEntity(model: RpdDataModel, options: UpdateEntityByIdOptions, currentEntity: any) {
167
+ for (const plugin of this.#plugins) {
168
+ if (plugin.beforeUpdateEntity) {
169
+ await plugin.beforeUpdateEntity(this.#server, model, options, currentEntity);
170
+ }
171
+ }
172
+ }
173
+ }
174
+
175
+ export default PluginManager;
@@ -1,5 +1,5 @@
1
- export interface IRuntimeProvider {
2
- copyFile(fromPath: string, toPath: string): Promise<void>;
3
-
4
- removeFile(path: string): Promise<void>;
5
- }
1
+ export interface IRuntimeProvider {
2
+ copyFile(fromPath: string, toPath: string): Promise<void>;
3
+
4
+ removeFile(path: string): Promise<void>;
5
+ }