@ruiapp/rapid-core 0.9.6 → 0.9.7

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,12 @@
1
+ import { ActionHandlerContext } from "../actionHandler";
2
+ declare const _default: {
3
+ namespace: string;
4
+ name: string;
5
+ code: string;
6
+ type: "RESTful";
7
+ method: "GET";
8
+ endpoint: string;
9
+ handler: typeof handler;
10
+ };
11
+ export default _default;
12
+ export declare function handler(ctx: ActionHandlerContext): Promise<void>;
@@ -0,0 +1,10 @@
1
+ declare const _default: {
2
+ namespace: string;
3
+ name: string;
4
+ code: string;
5
+ type: "RESTful";
6
+ method: "GET";
7
+ endpoint: string;
8
+ handler: typeof import("./healthz").handler;
9
+ }[];
10
+ export default _default;
package/dist/index.js CHANGED
@@ -1032,18 +1032,38 @@ async function buildRoutes(server, applicationConfig) {
1032
1032
  input,
1033
1033
  };
1034
1034
  await server.beforeRunRouteActions(handlerContext);
1035
- for (const actionConfig of routeConfig.actions) {
1036
- const actionCode = actionConfig.code;
1037
- const handler = server.getActionHandlerByCode(actionCode);
1038
- if (!handler) {
1039
- throw new Error("Unknown handler: " + actionCode);
1035
+ let handler = routeConfig.handler;
1036
+ if (handler) {
1037
+ if (lodash.isString(handler)) {
1038
+ handler = new Function(`return (${routeConfig.handler})`);
1040
1039
  }
1041
- await server.beforeRunActionHandler(handlerContext, actionConfig);
1042
- const result = handler(handlerContext, actionConfig.config);
1043
- if (result instanceof Promise) {
1044
- await result;
1040
+ if (lodash.isFunction(handler)) {
1041
+ const result = handler(handlerContext);
1042
+ if (result instanceof Promise) {
1043
+ await result;
1044
+ }
1045
+ }
1046
+ else {
1047
+ throw new Error(`Invalid handler for route ${routeConfig.code}: ${routeConfig.handler}`);
1045
1048
  }
1046
1049
  }
1050
+ else if (routeConfig.actions) {
1051
+ for (const actionConfig of routeConfig.actions) {
1052
+ const actionCode = actionConfig.code;
1053
+ const handler = server.getActionHandlerByCode(actionCode);
1054
+ if (!handler) {
1055
+ throw new Error("Unknown handler: " + actionCode);
1056
+ }
1057
+ await server.beforeRunActionHandler(handlerContext, actionConfig);
1058
+ const result = handler(handlerContext, actionConfig.config);
1059
+ if (result instanceof Promise) {
1060
+ await result;
1061
+ }
1062
+ }
1063
+ }
1064
+ else {
1065
+ throw new Error(`No handler or actions defined for route ${routeConfig.code}`);
1066
+ }
1047
1067
  if (!isNullOrUndefined(handlerContext.output)) {
1048
1068
  routerContext.json(handlerContext.output, handlerContext.status);
1049
1069
  }
@@ -3963,6 +3983,21 @@ class EntityManager {
3963
3983
  }
3964
3984
  }
3965
3985
 
3986
+ var healthz = {
3987
+ namespace: "sys",
3988
+ name: "sys.healthz",
3989
+ code: "sys.healthz",
3990
+ type: "RESTful",
3991
+ method: "GET",
3992
+ endpoint: "/healthz",
3993
+ handler: handler$x,
3994
+ };
3995
+ async function handler$x(ctx) {
3996
+ ctx.output = {};
3997
+ }
3998
+
3999
+ var coreRoutes = [healthz];
4000
+
3966
4001
  class RapidServer {
3967
4002
  #logger;
3968
4003
  #facilityFactories;
@@ -4237,7 +4272,12 @@ class RapidServer {
4237
4272
  this.#entityBeforeResponseEventEmitters.on(entityWatcher.modelSingularCode, entityWatcher.handler);
4238
4273
  }
4239
4274
  }
4275
+ this.#applicationConfig = lodash.cloneDeep(this.#bootstrapApplicationConfig);
4276
+ this.appendApplicationConfig({
4277
+ routes: coreRoutes,
4278
+ });
4240
4279
  await this.configureApplication();
4280
+ this.#buildedRoutes = await buildRoutes(this, this.#applicationConfig);
4241
4281
  if (!this.#disableCronJobs) {
4242
4282
  await pluginManager.registerCronJobs();
4243
4283
  }
@@ -4245,7 +4285,6 @@ class RapidServer {
4245
4285
  await pluginManager.onApplicationReady(this.#applicationConfig);
4246
4286
  }
4247
4287
  async configureApplication() {
4248
- this.#applicationConfig = lodash.cloneDeep(this.#bootstrapApplicationConfig);
4249
4288
  const pluginManager = this.#pluginManager;
4250
4289
  await pluginManager.onLoadingApplication(this.#applicationConfig);
4251
4290
  await pluginManager.configureModels(this.#applicationConfig);
@@ -4254,7 +4293,6 @@ class RapidServer {
4254
4293
  await pluginManager.configureRoutes(this.#applicationConfig);
4255
4294
  // TODO: check application configuration.
4256
4295
  await pluginManager.onApplicationLoaded(this.#applicationConfig);
4257
- this.#buildedRoutes = await buildRoutes(this, this.#applicationConfig);
4258
4296
  }
4259
4297
  registerFacilityFactory(factory) {
4260
4298
  this.#facilityFactories.set(factory.name, factory);
package/dist/types.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { ActionHandlerContext } from "./core/actionHandler";
1
2
  import { RouteContext } from "./core/routeContext";
2
3
  import { IRpdServer, RapidPlugin } from "./core/server";
3
4
  import { ColumnSelectOptions, CountRowOptions, FindRowOptions, RowFilterOptions } from "./dataAccess/dataAccessTypes";
@@ -483,6 +484,7 @@ export interface RpdRoute {
483
484
  method: RpdHttpMethod;
484
485
  endpoint: string;
485
486
  actions?: RpdRouteActionConfig[];
487
+ handler?: string | ((ctx: ActionHandlerContext) => Promise<void>);
486
488
  }
487
489
  export type RpdHttpMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
488
490
  export interface RpdRouteActionConfig {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruiapp/rapid-core",
3
- "version": "0.9.6",
3
+ "version": "0.9.7",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -0,0 +1,20 @@
1
+ import { RpdRoute } from "~/types";
2
+ import { ActionHandlerContext } from "../actionHandler";
3
+
4
+ export default {
5
+ namespace: "sys",
6
+ name: "sys.healthz",
7
+ code: "sys.healthz",
8
+ type: "RESTful",
9
+ method: "GET",
10
+ endpoint: "/healthz",
11
+ handler,
12
+ } satisfies RpdRoute;
13
+
14
+ export async function handler(ctx: ActionHandlerContext) {
15
+ const { server, input, routerContext: routeContext, logger } = ctx;
16
+ const { response } = routeContext;
17
+ const {} = input;
18
+
19
+ ctx.output = {};
20
+ }
@@ -0,0 +1,3 @@
1
+ import healthz from "./healthz";
2
+
3
+ export default [healthz];
@@ -5,7 +5,7 @@ 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
+ import { cloneDeep, isFunction, isString } from "lodash";
9
9
 
10
10
  export async function buildRoutes(server: IRpdServer, applicationConfig: RpdApplicationConfig) {
11
11
  const logger = server.getLogger();
@@ -67,19 +67,37 @@ export async function buildRoutes(server: IRpdServer, applicationConfig: RpdAppl
67
67
 
68
68
  await server.beforeRunRouteActions(handlerContext);
69
69
 
70
- for (const actionConfig of routeConfig.actions) {
71
- const actionCode = actionConfig.code;
72
- const handler = server.getActionHandlerByCode(actionCode);
73
- if (!handler) {
74
- throw new Error("Unknown handler: " + actionCode);
70
+ let handler = routeConfig.handler as any;
71
+ if (handler) {
72
+ if (isString(handler)) {
73
+ handler = new Function(`return (${routeConfig.handler})`) as any;
75
74
  }
76
75
 
77
- await server.beforeRunActionHandler(handlerContext, actionConfig);
78
-
79
- const result = handler(handlerContext, actionConfig.config);
80
- if (result instanceof Promise) {
81
- await result;
76
+ if (isFunction(handler)) {
77
+ const result = handler(handlerContext);
78
+ if (result instanceof Promise) {
79
+ await result;
80
+ }
81
+ } else {
82
+ throw new Error(`Invalid handler for route ${routeConfig.code}: ${routeConfig.handler}`);
83
+ }
84
+ } else if (routeConfig.actions) {
85
+ for (const actionConfig of routeConfig.actions) {
86
+ const actionCode = actionConfig.code;
87
+ const handler = server.getActionHandlerByCode(actionCode);
88
+ if (!handler) {
89
+ throw new Error("Unknown handler: " + actionCode);
90
+ }
91
+
92
+ await server.beforeRunActionHandler(handlerContext, actionConfig);
93
+
94
+ const result = handler(handlerContext, actionConfig.config);
95
+ if (result instanceof Promise) {
96
+ await result;
97
+ }
82
98
  }
99
+ } else {
100
+ throw new Error(`No handler or actions defined for route ${routeConfig.code}`);
83
101
  }
84
102
 
85
103
  if (!isNullOrUndefined(handlerContext.output)) {
package/src/server.ts CHANGED
@@ -35,6 +35,7 @@ import { bind, cloneDeep, find, forEach, isString, merge, omit } from "lodash";
35
35
  import { Logger } from "./facilities/log/LogFacility";
36
36
  import { FacilityFactory } from "./core/facility";
37
37
  import { CronJobConfiguration } from "./types/cron-job-types";
38
+ import coreRoutes from "./core/routes";
38
39
 
39
40
  export interface InitServerOptions {
40
41
  logger: Logger;
@@ -367,7 +368,12 @@ export class RapidServer implements IRpdServer {
367
368
  }
368
369
  }
369
370
 
371
+ this.#applicationConfig = cloneDeep(this.#bootstrapApplicationConfig) as RpdApplicationConfig;
372
+ this.appendApplicationConfig({
373
+ routes: coreRoutes,
374
+ });
370
375
  await this.configureApplication();
376
+ this.#buildedRoutes = await buildRoutes(this, this.#applicationConfig);
371
377
 
372
378
  if (!this.#disableCronJobs) {
373
379
  await pluginManager.registerCronJobs();
@@ -378,8 +384,6 @@ export class RapidServer implements IRpdServer {
378
384
  }
379
385
 
380
386
  async configureApplication() {
381
- this.#applicationConfig = cloneDeep(this.#bootstrapApplicationConfig) as RpdApplicationConfig;
382
-
383
387
  const pluginManager = this.#pluginManager;
384
388
  await pluginManager.onLoadingApplication(this.#applicationConfig);
385
389
  await pluginManager.configureModels(this.#applicationConfig);
@@ -390,8 +394,6 @@ export class RapidServer implements IRpdServer {
390
394
  // TODO: check application configuration.
391
395
 
392
396
  await pluginManager.onApplicationLoaded(this.#applicationConfig);
393
-
394
- this.#buildedRoutes = await buildRoutes(this, this.#applicationConfig);
395
397
  }
396
398
 
397
399
  registerFacilityFactory(factory: FacilityFactory) {
package/src/types.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { ActionHandlerContext } from "./core/actionHandler";
1
2
  import { RouteContext } from "./core/routeContext";
2
3
  import { IRpdServer, RapidPlugin } from "./core/server";
3
4
  import { ColumnSelectOptions, CountRowOptions, FindRowOptions, RowFilterOptions } from "./dataAccess/dataAccessTypes";
@@ -586,6 +587,7 @@ export interface RpdRoute {
586
587
  method: RpdHttpMethod;
587
588
  endpoint: string;
588
589
  actions?: RpdRouteActionConfig[];
590
+ handler?: string | ((ctx: ActionHandlerContext) => Promise<void>);
589
591
  }
590
592
 
591
593
  export type RpdHttpMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH";