@ruiapp/rapid-core 0.8.21 → 0.9.1

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.
@@ -0,0 +1,5 @@
1
+ import { RunMockHandlerOptions } from "../../../types";
2
+ import { ActionHandlerContext } from "../../../core/actionHandler";
3
+ import { RapidPlugin } from "../../../core/server";
4
+ export declare const code = "mock";
5
+ export declare function handler(plugin: RapidPlugin, ctx: ActionHandlerContext, options: RunMockHandlerOptions): Promise<void>;
package/dist/server.d.ts CHANGED
@@ -2,6 +2,7 @@ import { GetDataAccessorOptions, GetModelOptions, IDatabaseAccessor, IDatabaseCo
2
2
  import { ActionHandler, ActionHandlerContext, IPluginActionHandler } from "./core/actionHandler";
3
3
  import { IRpdServer, RapidPlugin } from "./core/server";
4
4
  import { Next } from "./core/routeContext";
5
+ import { RapidRequest } from "./core/request";
5
6
  import EntityManager from "./dataAccess/entityManager";
6
7
  import { Logger } from "./facilities/log/LogFacility";
7
8
  import { FacilityFactory } from "./core/facility";
@@ -55,7 +56,7 @@ export declare class RapidServer implements IRpdServer {
55
56
  queryDatabaseObject(sql: string, params?: unknown[] | Record<string, unknown>, client?: IDatabaseClient, dropErrorLog?: boolean): Promise<any[]>;
56
57
  tryQueryDatabaseObject(sql: string, params?: unknown[] | Record<string, unknown>, client?: IDatabaseClient, dropErrorLog?: boolean): Promise<any[]>;
57
58
  get middlewares(): any[];
58
- handleRequest(request: Request, next: Next): Promise<Response>;
59
+ handleRequest(rapidRequest: RapidRequest, next: Next): Promise<Response>;
59
60
  beforeRunRouteActions(handlerContext: ActionHandlerContext): Promise<void>;
60
61
  beforeCreateEntity(model: RpdDataModel, options: CreateEntityOptions): Promise<void>;
61
62
  beforeUpdateEntity(model: RpdDataModel, options: UpdateEntityByIdOptions, currentEntity: any): Promise<void>;
package/dist/types.d.ts CHANGED
@@ -52,6 +52,24 @@ export interface RunProxyHandlerOptions {
52
52
  /** Target url to proxy */
53
53
  target: string;
54
54
  }
55
+ export interface RunMockHandlerOptions {
56
+ /**
57
+ * 响应时间,毫秒
58
+ */
59
+ responseTime?: number;
60
+ /**
61
+ * 响应状态
62
+ */
63
+ responseStatus?: number;
64
+ /**
65
+ * 响应头
66
+ */
67
+ responseHeaders?: Record<string, string>;
68
+ /**
69
+ * 响应体
70
+ */
71
+ responseBody?: any;
72
+ }
55
73
  export interface GetDataAccessorOptions {
56
74
  namespace?: string;
57
75
  singularCode: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruiapp/rapid-core",
3
- "version": "0.8.21",
3
+ "version": "0.9.1",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -20,6 +20,7 @@ export class RapidRequest {
20
20
  #parsedCookies: Record<string, string>;
21
21
  method: string;
22
22
  url: URL;
23
+ ip?: string;
23
24
 
24
25
  constructor(server: IRpdServer, req: Request) {
25
26
  this.#logger = server.getLogger();
@@ -22,6 +22,7 @@ import EntityManager from "~/dataAccess/entityManager";
22
22
  import { Logger } from "~/facilities/log/LogFacility";
23
23
  import { FacilityFactory } from "./facility";
24
24
  import { CronJobConfiguration } from "~/types/cron-job-types";
25
+ import { RapidRequest } from "./request";
25
26
 
26
27
  export interface IRpdServer {
27
28
  config: RapidServerConfig;
@@ -50,7 +51,7 @@ export interface IRpdServer {
50
51
  registerEventHandler<K extends keyof RpdServerEventTypes>(eventName: K, listener: (...args: RpdServerEventTypes[K]) => void);
51
52
  registerEntityWatcher(entityWatcher: EntityWatcherType);
52
53
  emitEvent<TEventName extends keyof RpdServerEventTypes>(event: EmitServerEventOptions<TEventName>): void;
53
- handleRequest(request: Request, next: Next): Promise<Response>;
54
+ handleRequest(request: RapidRequest, next: Next): Promise<Response>;
54
55
  beforeRunRouteActions(handlerContext: ActionHandlerContext): Promise<void>;
55
56
  beforeCreateEntity(model: RpdDataModel, options: CreateEntityOptions): Promise<void>;
56
57
  beforeUpdateEntity(model: RpdDataModel, options: UpdateEntityByIdOptions, currentEntity: any): Promise<void>;
@@ -10,7 +10,8 @@ import {
10
10
  IRpdServer,
11
11
  RapidPlugin,
12
12
  } from "~/core/server";
13
- import * as httpProxy from "./actionHandlers/httpProxy";
13
+ import * as httpProxyActionHandler from "./actionHandlers/httpProxy";
14
+ import * as mockActionHandler from "./actionHandlers/mock";
14
15
 
15
16
  class RouteManager implements RapidPlugin {
16
17
  get code(): string {
@@ -34,7 +35,8 @@ class RouteManager implements RapidPlugin {
34
35
  }
35
36
 
36
37
  async registerActionHandlers(server: IRpdServer): Promise<any> {
37
- server.registerActionHandler(this, httpProxy);
38
+ server.registerActionHandler(this, httpProxyActionHandler);
39
+ server.registerActionHandler(this, mockActionHandler);
38
40
  }
39
41
 
40
42
  async registerEventHandlers(server: IRpdServer): Promise<any> {
@@ -0,0 +1,28 @@
1
+ import { RunMockHandlerOptions } from "~/types";
2
+ import { doProxy } from "~/proxy/mod";
3
+ import { ActionHandlerContext } from "~/core/actionHandler";
4
+ import { RapidPlugin } from "~/core/server";
5
+
6
+ export const code = "mock";
7
+
8
+ export async function handler(plugin: RapidPlugin, ctx: ActionHandlerContext, options: RunMockHandlerOptions) {
9
+ const { logger, routerContext } = ctx;
10
+ const { response } = routerContext;
11
+
12
+ const { responseTime, responseStatus, responseHeaders, responseBody } = options;
13
+
14
+ if (responseTime) {
15
+ await waitMilliseconds(responseTime);
16
+ }
17
+
18
+ for (const [key, value] of Object.entries(responseHeaders)) {
19
+ response.headers.set(key, value as string);
20
+ }
21
+
22
+ response.status = responseStatus || 200;
23
+ response.body = responseBody || "";
24
+ }
25
+
26
+ function waitMilliseconds(ms: number) {
27
+ return new Promise((resolve) => setTimeout(resolve, ms));
28
+ }
package/src/server.ts CHANGED
@@ -437,8 +437,7 @@ export class RapidServer implements IRpdServer {
437
437
  return this.#middlewares;
438
438
  }
439
439
 
440
- async handleRequest(request: Request, next: Next) {
441
- const rapidRequest = new RapidRequest(this, request);
440
+ async handleRequest(rapidRequest: RapidRequest, next: Next) {
442
441
  await rapidRequest.parseBody();
443
442
  const routeContext: RouteContext = new RouteContext(this, rapidRequest);
444
443
  const { response } = routeContext;
package/src/types.ts CHANGED
@@ -63,6 +63,28 @@ export interface RunProxyHandlerOptions {
63
63
  target: string;
64
64
  }
65
65
 
66
+ export interface RunMockHandlerOptions {
67
+ /**
68
+ * 响应时间,毫秒
69
+ */
70
+ responseTime?: number;
71
+
72
+ /**
73
+ * 响应状态
74
+ */
75
+ responseStatus?: number;
76
+
77
+ /**
78
+ * 响应头
79
+ */
80
+ responseHeaders?: Record<string, string>;
81
+
82
+ /**
83
+ * 响应体
84
+ */
85
+ responseBody?: any;
86
+ }
87
+
66
88
  export interface GetDataAccessorOptions {
67
89
  namespace?: string;
68
90
  singularCode: string;