@ruiapp/rapid-core 0.1.19 → 0.1.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/actionHandler.d.ts +2 -0
- package/dist/core/pluginManager.d.ts +3 -0
- package/dist/core/request.d.ts +2 -1
- package/dist/core/routeContext.d.ts +4 -1
- package/dist/core/server.d.ts +8 -2
- package/dist/dataAccess/dataAccessor.d.ts +2 -1
- package/dist/facilities/log/LogFacility.d.ts +33 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +288 -264
- package/dist/plugins/auth/AuthPlugin.d.ts +0 -9
- package/dist/plugins/dataManage/DataManagePlugin.d.ts +0 -10
- package/dist/plugins/entityAccessControl/EntityAccessControlPlugin.d.ts +17 -0
- package/dist/plugins/fileManage/FileManagePlugin.d.ts +0 -10
- package/dist/plugins/metaManage/MetaManagePlugin.d.ts +0 -8
- package/dist/plugins/routeManage/RouteManagePlugin.d.ts +0 -9
- package/dist/plugins/webhooks/WebhooksPlugin.d.ts +0 -6
- package/dist/server.d.ts +8 -3
- package/dist/types.d.ts +11 -0
- package/dist/utilities/accessControlUtility.d.ts +5 -0
- package/package.json +5 -2
- package/rollup.config.js +1 -18
- package/src/core/actionHandler.ts +2 -0
- package/src/core/pluginManager.ts +12 -0
- package/src/core/request.ts +6 -2
- package/src/core/routeContext.ts +6 -1
- package/src/core/routesBuilder.ts +16 -6
- package/src/core/server.ts +8 -2
- package/src/dataAccess/dataAccessor.ts +13 -9
- package/src/dataAccess/entityManager.ts +24 -24
- package/src/facilities/log/LogFacility.ts +36 -0
- package/src/helpers/inputHelper.ts +3 -3
- package/src/helpers/runCollectionEntityActionHandler.ts +4 -9
- package/src/index.ts +2 -1
- package/src/plugins/auth/AuthPlugin.ts +3 -31
- package/src/plugins/dataManage/DataManagePlugin.ts +0 -32
- package/src/plugins/dataManage/actionHandlers/addEntityRelations.ts +3 -7
- package/src/plugins/dataManage/actionHandlers/createCollectionEntitiesBatch.ts +3 -7
- package/src/plugins/dataManage/actionHandlers/createCollectionEntity.ts +3 -7
- package/src/plugins/dataManage/actionHandlers/deleteCollectionEntityById.ts +2 -2
- package/src/plugins/dataManage/actionHandlers/findCollectionEntities.ts +0 -1
- package/src/plugins/dataManage/actionHandlers/findCollectionEntityById.ts +2 -2
- package/src/plugins/dataManage/actionHandlers/queryDatabase.ts +5 -9
- package/src/plugins/dataManage/actionHandlers/removeEntityRelations.ts +2 -6
- package/src/plugins/dataManage/actionHandlers/updateCollectionEntityById.ts +3 -8
- package/src/plugins/entityAccessControl/EntityAccessControlPlugin.ts +107 -0
- package/src/plugins/fileManage/FileManagePlugin.ts +0 -31
- package/src/plugins/metaManage/MetaManagePlugin.ts +16 -39
- package/src/plugins/routeManage/RouteManagePlugin.ts +4 -30
- package/src/plugins/routeManage/actionHandlers/httpProxy.ts +2 -1
- package/src/plugins/webhooks/WebhooksPlugin.ts +26 -36
- package/src/queryBuilder/queryBuilder.ts +3 -3
- package/src/server.ts +53 -17
- package/src/types.ts +13 -0
- package/src/utilities/accessControlUtility.ts +33 -0
|
@@ -10,18 +10,9 @@ declare class AuthPlugin implements RapidPlugin {
|
|
|
10
10
|
get extendingAbilities(): RpdServerPluginExtendingAbilities[];
|
|
11
11
|
get configurableTargets(): RpdServerPluginConfigurableTargetOptions[];
|
|
12
12
|
get configurations(): RpdConfigurationItemOptions[];
|
|
13
|
-
initPlugin(server: IRpdServer): Promise<any>;
|
|
14
|
-
registerMiddlewares(server: IRpdServer): Promise<any>;
|
|
15
13
|
registerActionHandlers(server: IRpdServer): Promise<any>;
|
|
16
|
-
registerEventHandlers(server: IRpdServer): Promise<any>;
|
|
17
|
-
registerMessageHandlers(server: IRpdServer): Promise<any>;
|
|
18
|
-
registerTaskProcessors(server: IRpdServer): Promise<any>;
|
|
19
|
-
onLoadingApplication(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
20
14
|
configureModels(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
21
|
-
configureModelProperties(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
22
15
|
configureRoutes(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
23
|
-
onApplicationLoaded(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
24
|
-
onApplicationReady(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
25
16
|
onPrepareRouteContext(server: IRpdServer, routeContext: RouteContext): Promise<void>;
|
|
26
17
|
}
|
|
27
18
|
export default AuthPlugin;
|
|
@@ -11,17 +11,7 @@ declare class DataManager implements RapidPlugin {
|
|
|
11
11
|
get extendingAbilities(): RpdServerPluginExtendingAbilities[];
|
|
12
12
|
get configurableTargets(): RpdServerPluginConfigurableTargetOptions[];
|
|
13
13
|
get configurations(): RpdConfigurationItemOptions[];
|
|
14
|
-
initPlugin(server: IRpdServer): Promise<any>;
|
|
15
|
-
registerMiddlewares(server: IRpdServer): Promise<any>;
|
|
16
14
|
registerActionHandlers(server: IRpdServer): Promise<any>;
|
|
17
|
-
registerEventHandlers(server: IRpdServer): Promise<any>;
|
|
18
|
-
registerMessageHandlers(server: IRpdServer): Promise<any>;
|
|
19
|
-
registerTaskProcessors(server: IRpdServer): Promise<any>;
|
|
20
|
-
onLoadingApplication(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
21
|
-
configureModels(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
22
|
-
configureModelProperties(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
23
15
|
configureRoutes(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
24
|
-
onApplicationLoaded(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
25
|
-
onApplicationReady(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
26
16
|
}
|
|
27
17
|
export default DataManager;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { RpdApplicationConfig } from "../../types";
|
|
2
|
+
import { IRpdServer, RapidPlugin, RpdConfigurationItemOptions, RpdServerPluginConfigurableTargetOptions, RpdServerPluginExtendingAbilities } from "../../core/server";
|
|
3
|
+
import { ActionHandlerContext } from "../../core/actionHandler";
|
|
4
|
+
import { RouteContext } from "../../core/routeContext";
|
|
5
|
+
declare class EntityAccessControlPlugin implements RapidPlugin {
|
|
6
|
+
constructor();
|
|
7
|
+
get code(): string;
|
|
8
|
+
get description(): string;
|
|
9
|
+
get extendingAbilities(): RpdServerPluginExtendingAbilities[];
|
|
10
|
+
get configurableTargets(): RpdServerPluginConfigurableTargetOptions[];
|
|
11
|
+
get configurations(): RpdConfigurationItemOptions[];
|
|
12
|
+
onLoadingApplication(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
13
|
+
configureRoutes(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
14
|
+
onPrepareRouteContext(server: IRpdServer, routeContext: RouteContext): Promise<void>;
|
|
15
|
+
beforeRunRouteActions(server: IRpdServer, handlerContext: ActionHandlerContext): Promise<any>;
|
|
16
|
+
}
|
|
17
|
+
export default EntityAccessControlPlugin;
|
|
@@ -9,17 +9,7 @@ declare class FileManager implements RapidPlugin {
|
|
|
9
9
|
get extendingAbilities(): RpdServerPluginExtendingAbilities[];
|
|
10
10
|
get configurableTargets(): RpdServerPluginConfigurableTargetOptions[];
|
|
11
11
|
get configurations(): RpdConfigurationItemOptions[];
|
|
12
|
-
initPlugin(server: IRpdServer): Promise<any>;
|
|
13
|
-
registerMiddlewares(server: IRpdServer): Promise<any>;
|
|
14
12
|
registerActionHandlers(server: IRpdServer): Promise<any>;
|
|
15
|
-
registerEventHandlers(server: IRpdServer): Promise<any>;
|
|
16
|
-
registerMessageHandlers(server: IRpdServer): Promise<any>;
|
|
17
|
-
registerTaskProcessors(server: IRpdServer): Promise<any>;
|
|
18
|
-
onLoadingApplication(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
19
|
-
configureModels(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
20
|
-
configureModelProperties(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
21
13
|
configureRoutes(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
22
|
-
onApplicationLoaded(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
23
|
-
onApplicationReady(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
24
14
|
}
|
|
25
15
|
export default FileManager;
|
|
@@ -9,17 +9,9 @@ declare class MetaManager implements RapidPlugin {
|
|
|
9
9
|
get extendingAbilities(): RpdServerPluginExtendingAbilities[];
|
|
10
10
|
get configurableTargets(): RpdServerPluginConfigurableTargetOptions[];
|
|
11
11
|
get configurations(): RpdConfigurationItemOptions[];
|
|
12
|
-
initPlugin(server: IRpdServer): Promise<any>;
|
|
13
|
-
registerMiddlewares(server: IRpdServer): Promise<any>;
|
|
14
12
|
registerActionHandlers(server: IRpdServer): Promise<any>;
|
|
15
13
|
registerEventHandlers(server: IRpdServer): Promise<any>;
|
|
16
|
-
registerMessageHandlers(server: IRpdServer): Promise<any>;
|
|
17
|
-
registerTaskProcessors(server: IRpdServer): Promise<any>;
|
|
18
|
-
onLoadingApplication(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
19
14
|
configureModels(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
20
|
-
configureModelProperties(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
21
|
-
configureRoutes(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
22
15
|
onApplicationLoaded(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
23
|
-
onApplicationReady(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
24
16
|
}
|
|
25
17
|
export default MetaManager;
|
|
@@ -9,17 +9,8 @@ declare class RouteManager implements RapidPlugin {
|
|
|
9
9
|
get extendingAbilities(): RpdServerPluginExtendingAbilities[];
|
|
10
10
|
get configurableTargets(): RpdServerPluginConfigurableTargetOptions[];
|
|
11
11
|
get configurations(): RpdConfigurationItemOptions[];
|
|
12
|
-
initPlugin(server: IRpdServer): Promise<any>;
|
|
13
|
-
registerMiddlewares(server: IRpdServer): Promise<any>;
|
|
14
12
|
registerActionHandlers(server: IRpdServer): Promise<any>;
|
|
15
13
|
registerEventHandlers(server: IRpdServer): Promise<any>;
|
|
16
|
-
registerMessageHandlers(server: IRpdServer): Promise<any>;
|
|
17
|
-
registerTaskProcessors(server: IRpdServer): Promise<any>;
|
|
18
|
-
onLoadingApplication(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
19
|
-
configureModels(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
20
|
-
configureModelProperties(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
21
14
|
configureRoutes(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
22
|
-
onApplicationLoaded(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
23
|
-
onApplicationReady(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
24
15
|
}
|
|
25
16
|
export default RouteManager;
|
|
@@ -21,13 +21,7 @@ declare class WebhooksPlugin implements RapidPlugin {
|
|
|
21
21
|
get extendingAbilities(): RpdServerPluginExtendingAbilities[];
|
|
22
22
|
get configurableTargets(): RpdServerPluginConfigurableTargetOptions[];
|
|
23
23
|
get configurations(): RpdConfigurationItemOptions[];
|
|
24
|
-
initPlugin(server: IRpdServer): Promise<any>;
|
|
25
|
-
registerMiddlewares(server: IRpdServer): Promise<any>;
|
|
26
|
-
registerActionHandlers(server: IRpdServer): Promise<any>;
|
|
27
24
|
registerEventHandlers(server: IRpdServer): Promise<any>;
|
|
28
|
-
registerMessageHandlers(server: IRpdServer): Promise<any>;
|
|
29
|
-
registerTaskProcessors(server: IRpdServer): Promise<any>;
|
|
30
|
-
onLoadingApplication(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
31
25
|
configureModels(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
32
26
|
configureModelProperties(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
|
33
27
|
configureRoutes(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
|
package/dist/server.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import { GetDataAccessorOptions, GetModelOptions, IDatabaseAccessor, IDatabaseConfig, IQueryBuilder, IRpdDataAccessor, RpdApplicationConfig, RpdDataModel, RpdServerEventTypes, RapidServerConfig } from "./types";
|
|
2
|
-
import { ActionHandler, IPluginActionHandler } from "./core/actionHandler";
|
|
1
|
+
import { GetDataAccessorOptions, GetModelOptions, IDatabaseAccessor, IDatabaseConfig, IQueryBuilder, IRpdDataAccessor, RpdApplicationConfig, RpdDataModel, RpdServerEventTypes, RapidServerConfig, RpdDataModelProperty } from "./types";
|
|
2
|
+
import { ActionHandler, ActionHandlerContext, IPluginActionHandler } from "./core/actionHandler";
|
|
3
3
|
import { IRpdServer, RapidPlugin } from "./core/server";
|
|
4
4
|
import { Next } from "./core/routeContext";
|
|
5
5
|
import EntityManager from "./dataAccess/entityManager";
|
|
6
|
+
import { Logger } from "./facilities/log/LogFacility";
|
|
6
7
|
export interface InitServerOptions {
|
|
8
|
+
logger: Logger;
|
|
7
9
|
databaseAccessor: IDatabaseAccessor;
|
|
8
10
|
databaseConfig: IDatabaseConfig;
|
|
9
11
|
serverConfig: RapidServerConfig;
|
|
@@ -16,8 +18,10 @@ export declare class RapidServer implements IRpdServer {
|
|
|
16
18
|
config: RapidServerConfig;
|
|
17
19
|
databaseConfig: IDatabaseConfig;
|
|
18
20
|
constructor(options: InitServerOptions);
|
|
21
|
+
getLogger(): Logger;
|
|
19
22
|
getApplicationConfig(): RpdApplicationConfig;
|
|
20
23
|
appendApplicationConfig(config: Partial<RpdApplicationConfig>): void;
|
|
24
|
+
appendModelProperties(modelSingularCode: string, properties: RpdDataModelProperty[]): void;
|
|
21
25
|
registerActionHandler(plugin: RapidPlugin, options: IPluginActionHandler): void;
|
|
22
26
|
getActionHandlerByCode(code: string): ActionHandler;
|
|
23
27
|
registerMiddleware(middleware: any): void;
|
|
@@ -31,5 +35,6 @@ export declare class RapidServer implements IRpdServer {
|
|
|
31
35
|
queryDatabaseObject(sql: string, params?: unknown[] | Record<string, unknown>): Promise<any[]>;
|
|
32
36
|
tryQueryDatabaseObject(sql: string, params?: unknown[] | Record<string, unknown>): Promise<any[]>;
|
|
33
37
|
get middlewares(): any[];
|
|
34
|
-
handleRequest(request: Request, next: Next):
|
|
38
|
+
handleRequest(request: Request, next: Next): any;
|
|
39
|
+
beforeRunRouteActions(handlerContext: ActionHandlerContext): Promise<void>;
|
|
35
40
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -111,6 +111,17 @@ export interface RpdDataModel {
|
|
|
111
111
|
tableName: string;
|
|
112
112
|
properties: RpdDataModelProperty[];
|
|
113
113
|
extensions?: RpdDataModelExtension[];
|
|
114
|
+
permissionPolicies?: RpdDataModelPermissionPolicies;
|
|
115
|
+
}
|
|
116
|
+
export interface RpdDataModelPermissionPolicies {
|
|
117
|
+
find?: PermissionPolicy;
|
|
118
|
+
create?: PermissionPolicy;
|
|
119
|
+
update?: PermissionPolicy;
|
|
120
|
+
delete?: PermissionPolicy;
|
|
121
|
+
}
|
|
122
|
+
export interface PermissionPolicy {
|
|
123
|
+
any?: string[];
|
|
124
|
+
all?: string[];
|
|
114
125
|
}
|
|
115
126
|
export interface RpdDataModelProperty {
|
|
116
127
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ruiapp/rapid-core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.21",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"keywords": [],
|
|
@@ -12,8 +12,8 @@
|
|
|
12
12
|
"@types/node": "^20.11.16",
|
|
13
13
|
"rimraf": "^3.0.2",
|
|
14
14
|
"rollup": "^2.79.1",
|
|
15
|
+
"rollup-plugin-tsc-alias": "^1.1.2",
|
|
15
16
|
"rollup-plugin-typescript2": "^0.34.1",
|
|
16
|
-
"tsc-alias": "^1.8.8",
|
|
17
17
|
"typescript": "^4.8.4"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
@@ -23,6 +23,9 @@
|
|
|
23
23
|
"qs": "^6.11.0",
|
|
24
24
|
"uuid": "^9.0.1"
|
|
25
25
|
},
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"winston": "^3.11.0"
|
|
28
|
+
},
|
|
26
29
|
"scripts": {
|
|
27
30
|
"build": "rimraf dist && rollup --config",
|
|
28
31
|
"dev": "rollup --config",
|
package/rollup.config.js
CHANGED
|
@@ -1,22 +1,5 @@
|
|
|
1
|
-
import { exec } from "child_process";
|
|
2
1
|
import typescript from 'rollup-plugin-typescript2';
|
|
3
|
-
|
|
4
|
-
const tscAlias = () => {
|
|
5
|
-
return {
|
|
6
|
-
name: "tsAlias",
|
|
7
|
-
writeBundle: () => {
|
|
8
|
-
return new Promise((resolve, reject) => {
|
|
9
|
-
exec("tsc-alias", function callback(error, stdout, stderr) {
|
|
10
|
-
if (stderr || error) {
|
|
11
|
-
reject(stderr || error);
|
|
12
|
-
} else {
|
|
13
|
-
resolve(stdout);
|
|
14
|
-
}
|
|
15
|
-
});
|
|
16
|
-
});
|
|
17
|
-
},
|
|
18
|
-
};
|
|
19
|
-
};
|
|
2
|
+
import tscAlias from 'rollup-plugin-tsc-alias';
|
|
20
3
|
|
|
21
4
|
export default {
|
|
22
5
|
input: ["src/index.ts"],
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { RpdApplicationConfig } from "~/types";
|
|
2
2
|
import { IRpdServer, RapidPlugin } from "./server";
|
|
3
3
|
import { Next, RouteContext } from "./routeContext";
|
|
4
|
+
import { Logger } from "~/facilities/log/LogFacility";
|
|
4
5
|
|
|
5
6
|
export interface ActionHandlerContext {
|
|
7
|
+
logger: Logger;
|
|
6
8
|
routerContext: RouteContext;
|
|
7
9
|
next: Next;
|
|
8
10
|
server: IRpdServer;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { RpdApplicationConfig } from "~/types";
|
|
2
2
|
import { IRpdServer, RapidPlugin } from "./server";
|
|
3
3
|
import { RouteContext } from "./routeContext";
|
|
4
|
+
import { ActionHandlerContext } from "./actionHandler";
|
|
4
5
|
|
|
5
6
|
class PluginManager {
|
|
6
7
|
#server: IRpdServer;
|
|
@@ -147,6 +148,17 @@ class PluginManager {
|
|
|
147
148
|
}
|
|
148
149
|
}
|
|
149
150
|
}
|
|
151
|
+
|
|
152
|
+
/** 在接收到HTTP请求,执行 actions 前调用。 */
|
|
153
|
+
async beforeRunRouteActions(
|
|
154
|
+
handlerContext: ActionHandlerContext,
|
|
155
|
+
) {
|
|
156
|
+
for (const plugin of this.#plugins) {
|
|
157
|
+
if (plugin.beforeRunRouteActions) {
|
|
158
|
+
await plugin.beforeRunRouteActions(this.#server, handlerContext);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
150
162
|
}
|
|
151
163
|
|
|
152
164
|
export default PluginManager;
|
package/src/core/request.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import qs from "qs";
|
|
2
2
|
import { parseFormDataBody } from "./http/formDataParser";
|
|
3
3
|
import { getCookies } from "~/deno-std/http/cookie";
|
|
4
|
+
import { IRpdServer } from "./server";
|
|
5
|
+
import { Logger } from "~/facilities/log/LogFacility";
|
|
4
6
|
|
|
5
7
|
export const GlobalRequest = global.Request;
|
|
6
8
|
|
|
@@ -10,6 +12,7 @@ export interface RapidRequestBody {
|
|
|
10
12
|
}
|
|
11
13
|
|
|
12
14
|
export class RapidRequest {
|
|
15
|
+
#logger: Logger;
|
|
13
16
|
#raw: Request;
|
|
14
17
|
#bodyParsed: boolean;
|
|
15
18
|
#body: RapidRequestBody;
|
|
@@ -18,7 +21,8 @@ export class RapidRequest {
|
|
|
18
21
|
method: string;
|
|
19
22
|
url: URL;
|
|
20
23
|
|
|
21
|
-
constructor(req: Request) {
|
|
24
|
+
constructor(server: IRpdServer, req: Request) {
|
|
25
|
+
this.#logger = server.getLogger();
|
|
22
26
|
this.#raw = req;
|
|
23
27
|
this.method = req.method;
|
|
24
28
|
this.url = new URL(req.url);
|
|
@@ -27,7 +31,7 @@ export class RapidRequest {
|
|
|
27
31
|
|
|
28
32
|
async parseBody(): Promise<void> {
|
|
29
33
|
if (this.#bodyParsed) {
|
|
30
|
-
|
|
34
|
+
this.#logger.warn("Request body has been parsed. 'parseBody()' method should not be called more than once.");
|
|
31
35
|
return;
|
|
32
36
|
}
|
|
33
37
|
|
package/src/core/routeContext.ts
CHANGED
|
@@ -2,18 +2,23 @@ import { isArray, isObject } from "lodash";
|
|
|
2
2
|
import { RapidRequest } from "./request";
|
|
3
3
|
import { RapidResponse } from "./response";
|
|
4
4
|
import { HttpStatus, ResponseData } from "./http-types";
|
|
5
|
+
import { IRpdServer } from "./server";
|
|
6
|
+
import { Logger } from "~/facilities/log/LogFacility";
|
|
5
7
|
|
|
6
8
|
export type Next = () => Promise<void>;
|
|
7
9
|
|
|
8
10
|
export class RouteContext {
|
|
11
|
+
#logger: Logger;
|
|
9
12
|
readonly request: RapidRequest;
|
|
10
13
|
readonly response: RapidResponse;
|
|
11
14
|
readonly state: Record<string, any>;
|
|
12
15
|
method: string;
|
|
13
16
|
path: string;
|
|
14
17
|
params: Record<string, string>;
|
|
18
|
+
routeConfig: any;
|
|
15
19
|
|
|
16
|
-
constructor(request: RapidRequest) {
|
|
20
|
+
constructor(server: IRpdServer, request: RapidRequest) {
|
|
21
|
+
this.#logger = server.getLogger();
|
|
17
22
|
this.request = request;
|
|
18
23
|
this.state = {};
|
|
19
24
|
this.response = new RapidResponse();
|
|
@@ -5,11 +5,13 @@ import { IRpdServer } from "~/core/server";
|
|
|
5
5
|
import { RpdApplicationConfig } from "~/types";
|
|
6
6
|
import { isNullOrUndefined } from "~/utilities/typeUtility";
|
|
7
7
|
import { Next, RouteContext } from "./routeContext";
|
|
8
|
+
import { cloneDeep } from "lodash";
|
|
8
9
|
|
|
9
10
|
export async function buildRoutes(
|
|
10
11
|
server: IRpdServer,
|
|
11
12
|
applicationConfig: RpdApplicationConfig,
|
|
12
13
|
) {
|
|
14
|
+
const logger = server.getLogger();
|
|
13
15
|
const router = new Router();
|
|
14
16
|
|
|
15
17
|
let baseUrl = server.config.baseUrl;
|
|
@@ -31,6 +33,7 @@ export async function buildRoutes(
|
|
|
31
33
|
(router as any)[routeConfig.method.toLowerCase()](
|
|
32
34
|
routePath,
|
|
33
35
|
async (routerContext: RouteContext, next: Next) => {
|
|
36
|
+
routerContext.routeConfig = cloneDeep(routeConfig);
|
|
34
37
|
const { request, params } = routerContext;
|
|
35
38
|
|
|
36
39
|
let search = request.url.search;
|
|
@@ -53,10 +56,14 @@ export async function buildRoutes(
|
|
|
53
56
|
|
|
54
57
|
// Normalize input value
|
|
55
58
|
|
|
56
|
-
|
|
57
|
-
|
|
59
|
+
logger.debug("Processing rapid request.", {
|
|
60
|
+
method: requestMethod,
|
|
61
|
+
url: request.url.toString(),
|
|
62
|
+
input
|
|
63
|
+
});
|
|
58
64
|
|
|
59
65
|
let handlerContext: ActionHandlerContext = {
|
|
66
|
+
logger,
|
|
60
67
|
routerContext,
|
|
61
68
|
next,
|
|
62
69
|
server,
|
|
@@ -64,13 +71,16 @@ export async function buildRoutes(
|
|
|
64
71
|
input,
|
|
65
72
|
};
|
|
66
73
|
|
|
67
|
-
|
|
68
|
-
|
|
74
|
+
await server.beforeRunRouteActions(handlerContext);
|
|
75
|
+
|
|
76
|
+
for (const actionConfig of routeConfig.actions) {
|
|
77
|
+
const actionCode = actionConfig.code;
|
|
78
|
+
const handler = server.getActionHandlerByCode(actionCode);
|
|
69
79
|
if (!handler) {
|
|
70
|
-
throw new Error("Unknown handler: " +
|
|
80
|
+
throw new Error("Unknown handler: " + actionCode);
|
|
71
81
|
}
|
|
72
82
|
|
|
73
|
-
const result = handler(handlerContext,
|
|
83
|
+
const result = handler(handlerContext, actionConfig.config);
|
|
74
84
|
if (result instanceof Promise) {
|
|
75
85
|
await result;
|
|
76
86
|
}
|
package/src/core/server.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
import { GetDataAccessorOptions, GetModelOptions, IDatabaseConfig, IQueryBuilder, IRpdDataAccessor, RapidServerConfig, RpdApplicationConfig, RpdDataModel, RpdServerEventTypes } from "~/types";
|
|
2
|
-
import { IPluginActionHandler, ActionHandler } from "./actionHandler";
|
|
1
|
+
import { GetDataAccessorOptions, GetModelOptions, IDatabaseConfig, IQueryBuilder, IRpdDataAccessor, RapidServerConfig, RpdApplicationConfig, RpdDataModel, RpdDataModelProperty, RpdServerEventTypes } from "~/types";
|
|
2
|
+
import { IPluginActionHandler, ActionHandler, ActionHandlerContext } from "./actionHandler";
|
|
3
3
|
import { Next, RouteContext } from "./routeContext";
|
|
4
4
|
import EntityManager from "~/dataAccess/entityManager";
|
|
5
|
+
import { Logger } from "~/facilities/log/LogFacility";
|
|
5
6
|
|
|
6
7
|
export interface IRpdServer {
|
|
7
8
|
config: RapidServerConfig;
|
|
8
9
|
databaseConfig: IDatabaseConfig;
|
|
9
10
|
queryBuilder: IQueryBuilder;
|
|
11
|
+
getLogger(): Logger;
|
|
10
12
|
queryDatabaseObject: (
|
|
11
13
|
sql: string,
|
|
12
14
|
params?: unknown[] | Record<string, unknown>,
|
|
@@ -27,6 +29,7 @@ export interface IRpdServer {
|
|
|
27
29
|
getEntityManager<TEntity = any>(singularCode: string): EntityManager<TEntity>;
|
|
28
30
|
getApplicationConfig(): RpdApplicationConfig;
|
|
29
31
|
appendApplicationConfig(config: Partial<RpdApplicationConfig>);
|
|
32
|
+
appendModelProperties(modelSingularCode: string, properties: RpdDataModelProperty[]);
|
|
30
33
|
getModel(options: GetModelOptions): RpdDataModel | undefined;
|
|
31
34
|
registerEventHandler<K extends keyof RpdServerEventTypes>(
|
|
32
35
|
eventName: K,
|
|
@@ -38,6 +41,7 @@ export interface IRpdServer {
|
|
|
38
41
|
payload: RpdServerEventTypes[K][1],
|
|
39
42
|
): void;
|
|
40
43
|
handleRequest(request: Request, next: Next): Promise<Response>;
|
|
44
|
+
beforeRunRouteActions(handlerContext: ActionHandlerContext): Promise<void>;
|
|
41
45
|
}
|
|
42
46
|
|
|
43
47
|
|
|
@@ -127,4 +131,6 @@ export interface RapidPlugin {
|
|
|
127
131
|
onApplicationReady?: (server: IRpdServer, applicationConfig: RpdApplicationConfig) => Promise<any>;
|
|
128
132
|
/** 在接收到HTTP请求,准备路由上下文时调用。 */
|
|
129
133
|
onPrepareRouteContext?: (server: IRpdServer, routeContext: RouteContext) => Promise<any>;
|
|
134
|
+
/** 在接收到HTTP请求,执行 actions 前调用。 */
|
|
135
|
+
beforeRunRouteActions?: (server: IRpdServer, handlerContext: ActionHandlerContext) => Promise<any>;
|
|
130
136
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import * as _ from "lodash";
|
|
2
1
|
import {
|
|
3
2
|
CountEntityOptions,
|
|
4
3
|
FindEntityOptions,
|
|
@@ -9,6 +8,9 @@ import {
|
|
|
9
8
|
IDatabaseAccessor,
|
|
10
9
|
} from "~/types";
|
|
11
10
|
import QueryBuilder from "~/queryBuilder/queryBuilder";
|
|
11
|
+
import { first, set } from "lodash";
|
|
12
|
+
import { IRpdServer } from "~/core/server";
|
|
13
|
+
import { Logger } from "~/facilities/log/LogFacility";
|
|
12
14
|
|
|
13
15
|
export interface IDataAccessorOptions {
|
|
14
16
|
model: RpdDataModel;
|
|
@@ -16,11 +18,13 @@ export interface IDataAccessorOptions {
|
|
|
16
18
|
}
|
|
17
19
|
|
|
18
20
|
export default class DataAccessor<T = any> implements IRpdDataAccessor<T> {
|
|
21
|
+
#logger: Logger;
|
|
19
22
|
#model: RpdDataModel;
|
|
20
23
|
#queryBuilder: QueryBuilder;
|
|
21
24
|
#databaseAccessor: IDatabaseAccessor;
|
|
22
25
|
|
|
23
|
-
constructor(databaseAccessor: IDatabaseAccessor, options: IDataAccessorOptions) {
|
|
26
|
+
constructor(server: IRpdServer, databaseAccessor: IDatabaseAccessor, options: IDataAccessorOptions) {
|
|
27
|
+
this.#logger = server.getLogger();
|
|
24
28
|
this.#databaseAccessor = databaseAccessor;
|
|
25
29
|
this.#queryBuilder = options.queryBuilder;
|
|
26
30
|
this.#model = options.model;
|
|
@@ -36,7 +40,7 @@ export default class DataAccessor<T = any> implements IRpdDataAccessor<T> {
|
|
|
36
40
|
};
|
|
37
41
|
const query = this.#queryBuilder.insert(this.#model, options);
|
|
38
42
|
const result = await this.#databaseAccessor.queryDatabaseObject(query.command, query.params);
|
|
39
|
-
return
|
|
43
|
+
return first(result);
|
|
40
44
|
}
|
|
41
45
|
|
|
42
46
|
async updateById(id: any, entity: Partial<T>): Promise<{ count: number }> {
|
|
@@ -52,19 +56,19 @@ export default class DataAccessor<T = any> implements IRpdDataAccessor<T> {
|
|
|
52
56
|
};
|
|
53
57
|
const query = this.#queryBuilder.update(this.#model, options);
|
|
54
58
|
const result = await this.#databaseAccessor.queryDatabaseObject(query.command, query.params);
|
|
55
|
-
return
|
|
59
|
+
return first(result);
|
|
56
60
|
}
|
|
57
61
|
|
|
58
62
|
async find(options: FindEntityOptions): Promise<T[]> {
|
|
59
|
-
|
|
63
|
+
this.#logger.debug(`Finding '${this.#model.singularCode}' entity.`, { options });
|
|
60
64
|
const query = this.#queryBuilder.select(this.#model, options);
|
|
61
65
|
return await this.#databaseAccessor.queryDatabaseObject(query.command, query.params);
|
|
62
66
|
}
|
|
63
67
|
|
|
64
68
|
async findOne(options: FindEntityOptions): Promise<T> {
|
|
65
|
-
|
|
69
|
+
set(options, "pagination.limit", 1);
|
|
66
70
|
const list = await this.find(options);
|
|
67
|
-
return
|
|
71
|
+
return first(list);
|
|
68
72
|
}
|
|
69
73
|
|
|
70
74
|
async findById(id: any): Promise<T | null> {
|
|
@@ -79,14 +83,14 @@ export default class DataAccessor<T = any> implements IRpdDataAccessor<T> {
|
|
|
79
83
|
};
|
|
80
84
|
const query = this.#queryBuilder.select(this.#model, options);
|
|
81
85
|
const result = await this.#databaseAccessor.queryDatabaseObject(query.command, query.params);
|
|
82
|
-
return
|
|
86
|
+
return first(result);
|
|
83
87
|
}
|
|
84
88
|
|
|
85
89
|
async count(options: CountEntityOptions): Promise<any> {
|
|
86
90
|
const query = this.#queryBuilder.count(this.#model, options);
|
|
87
91
|
const result = await this.#databaseAccessor.queryDatabaseObject(query.command, query.params);
|
|
88
92
|
|
|
89
|
-
const row =
|
|
93
|
+
const row = first(result);
|
|
90
94
|
if (row) {
|
|
91
95
|
return row;
|
|
92
96
|
}
|