@ruiapp/rapid-core 0.1.20 → 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/request.d.ts +2 -1
- package/dist/core/routeContext.d.ts +3 -1
- package/dist/core/server.d.ts +2 -0
- package/dist/dataAccess/dataAccessor.d.ts +2 -1
- package/dist/facilities/log/LogFacility.d.ts +33 -0
- package/dist/index.js +99 -171
- package/dist/plugins/auth/AuthPlugin.d.ts +0 -9
- package/dist/plugins/dataManage/DataManagePlugin.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 +4 -1
- package/package.json +4 -1
- package/src/core/actionHandler.ts +2 -0
- package/src/core/request.ts +6 -2
- package/src/core/routeContext.ts +5 -1
- package/src/core/routesBuilder.ts +7 -2
- package/src/core/server.ts +2 -0
- package/src/dataAccess/dataAccessor.ts +6 -2
- package/src/facilities/log/LogFacility.ts +36 -0
- package/src/helpers/runCollectionEntityActionHandler.ts +3 -7
- package/src/plugins/auth/AuthPlugin.ts +3 -30
- package/src/plugins/dataManage/DataManagePlugin.ts +0 -31
- 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/findCollectionEntityById.ts +2 -2
- package/src/plugins/dataManage/actionHandlers/queryDatabase.ts +3 -7
- package/src/plugins/dataManage/actionHandlers/removeEntityRelations.ts +2 -6
- package/src/plugins/dataManage/actionHandlers/updateCollectionEntityById.ts +3 -8
- package/src/plugins/entityAccessControl/EntityAccessControlPlugin.ts +3 -0
- package/src/plugins/metaManage/MetaManagePlugin.ts +10 -33
- package/src/plugins/routeManage/RouteManagePlugin.ts +4 -30
- package/src/plugins/routeManage/actionHandlers/httpProxy.ts +2 -1
- package/src/plugins/webhooks/WebhooksPlugin.ts +24 -34
- package/src/server.ts +22 -8
package/dist/index.js
CHANGED
|
@@ -27,10 +27,12 @@ function fixBigIntJSONSerialize() {
|
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
class DataAccessor {
|
|
30
|
+
#logger;
|
|
30
31
|
#model;
|
|
31
32
|
#queryBuilder;
|
|
32
33
|
#databaseAccessor;
|
|
33
|
-
constructor(databaseAccessor, options) {
|
|
34
|
+
constructor(server, databaseAccessor, options) {
|
|
35
|
+
this.#logger = server.getLogger();
|
|
34
36
|
this.#databaseAccessor = databaseAccessor;
|
|
35
37
|
this.#queryBuilder = options.queryBuilder;
|
|
36
38
|
this.#model = options.model;
|
|
@@ -62,7 +64,7 @@ class DataAccessor {
|
|
|
62
64
|
return lodash.first(result);
|
|
63
65
|
}
|
|
64
66
|
async find(options) {
|
|
65
|
-
|
|
67
|
+
this.#logger.debug(`Finding '${this.#model.singularCode}' entity.`, { options });
|
|
66
68
|
const query = this.#queryBuilder.select(this.#model, options);
|
|
67
69
|
return await this.#databaseAccessor.queryDatabaseObject(query.command, query.params);
|
|
68
70
|
}
|
|
@@ -569,6 +571,7 @@ function isNullOrUndefined(val) {
|
|
|
569
571
|
}
|
|
570
572
|
|
|
571
573
|
async function buildRoutes(server, applicationConfig) {
|
|
574
|
+
const logger = server.getLogger();
|
|
572
575
|
const router = new Router__default["default"]();
|
|
573
576
|
let baseUrl = server.config.baseUrl;
|
|
574
577
|
if (baseUrl) {
|
|
@@ -602,9 +605,13 @@ async function buildRoutes(server, applicationConfig) {
|
|
|
602
605
|
}
|
|
603
606
|
}
|
|
604
607
|
// Normalize input value
|
|
605
|
-
|
|
606
|
-
|
|
608
|
+
logger.debug("Processing rapid request.", {
|
|
609
|
+
method: requestMethod,
|
|
610
|
+
url: request.url.toString(),
|
|
611
|
+
input
|
|
612
|
+
});
|
|
607
613
|
let handlerContext = {
|
|
614
|
+
logger,
|
|
608
615
|
routerContext,
|
|
609
616
|
next,
|
|
610
617
|
server,
|
|
@@ -690,6 +697,7 @@ class RapidResponse {
|
|
|
690
697
|
}
|
|
691
698
|
|
|
692
699
|
class RouteContext {
|
|
700
|
+
#logger;
|
|
693
701
|
request;
|
|
694
702
|
response;
|
|
695
703
|
state;
|
|
@@ -697,7 +705,8 @@ class RouteContext {
|
|
|
697
705
|
path;
|
|
698
706
|
params;
|
|
699
707
|
routeConfig;
|
|
700
|
-
constructor(request) {
|
|
708
|
+
constructor(server, request) {
|
|
709
|
+
this.#logger = server.getLogger();
|
|
701
710
|
this.request = request;
|
|
702
711
|
this.state = {};
|
|
703
712
|
this.response = new RapidResponse();
|
|
@@ -1009,6 +1018,7 @@ function setCookie(headers, cookie) {
|
|
|
1009
1018
|
|
|
1010
1019
|
const GlobalRequest = global.Request;
|
|
1011
1020
|
class RapidRequest {
|
|
1021
|
+
#logger;
|
|
1012
1022
|
#raw;
|
|
1013
1023
|
#bodyParsed;
|
|
1014
1024
|
#body;
|
|
@@ -1016,7 +1026,8 @@ class RapidRequest {
|
|
|
1016
1026
|
#parsedCookies;
|
|
1017
1027
|
method;
|
|
1018
1028
|
url;
|
|
1019
|
-
constructor(req) {
|
|
1029
|
+
constructor(server, req) {
|
|
1030
|
+
this.#logger = server.getLogger();
|
|
1020
1031
|
this.#raw = req;
|
|
1021
1032
|
this.method = req.method;
|
|
1022
1033
|
this.url = new URL(req.url);
|
|
@@ -1024,7 +1035,7 @@ class RapidRequest {
|
|
|
1024
1035
|
}
|
|
1025
1036
|
async parseBody() {
|
|
1026
1037
|
if (this.#bodyParsed) {
|
|
1027
|
-
|
|
1038
|
+
this.#logger.warn("Request body has been parsed. 'parseBody()' method should not be called more than once.");
|
|
1028
1039
|
return;
|
|
1029
1040
|
}
|
|
1030
1041
|
const requestMethod = this.method;
|
|
@@ -2362,6 +2373,7 @@ class EntityManager {
|
|
|
2362
2373
|
}
|
|
2363
2374
|
|
|
2364
2375
|
class RapidServer {
|
|
2376
|
+
#logger;
|
|
2365
2377
|
#pluginManager;
|
|
2366
2378
|
#plugins;
|
|
2367
2379
|
#eventManager;
|
|
@@ -2377,6 +2389,7 @@ class RapidServer {
|
|
|
2377
2389
|
databaseConfig;
|
|
2378
2390
|
#buildedRoutes;
|
|
2379
2391
|
constructor(options) {
|
|
2392
|
+
this.#logger = options.logger;
|
|
2380
2393
|
this.#pluginManager = new PluginManager(this);
|
|
2381
2394
|
this.#eventManager = new EventManager();
|
|
2382
2395
|
this.#middlewares = [];
|
|
@@ -2393,6 +2406,9 @@ class RapidServer {
|
|
|
2393
2406
|
this.config = options.serverConfig;
|
|
2394
2407
|
this.#plugins = options.plugins || [];
|
|
2395
2408
|
}
|
|
2409
|
+
getLogger() {
|
|
2410
|
+
return this.#logger;
|
|
2411
|
+
}
|
|
2396
2412
|
getApplicationConfig() {
|
|
2397
2413
|
return this.#applicationConfig;
|
|
2398
2414
|
}
|
|
@@ -2469,7 +2485,7 @@ class RapidServer {
|
|
|
2469
2485
|
if (!model) {
|
|
2470
2486
|
throw new Error(`Data model ${namespace}.${singularCode} not found.`);
|
|
2471
2487
|
}
|
|
2472
|
-
dataAccessor = new DataAccessor(this.#databaseAccessor, {
|
|
2488
|
+
dataAccessor = new DataAccessor(this, this.#databaseAccessor, {
|
|
2473
2489
|
model,
|
|
2474
2490
|
queryBuilder: this.queryBuilder,
|
|
2475
2491
|
});
|
|
@@ -2497,7 +2513,7 @@ class RapidServer {
|
|
|
2497
2513
|
return this;
|
|
2498
2514
|
}
|
|
2499
2515
|
async emitEvent(eventName, sender, payload) {
|
|
2500
|
-
|
|
2516
|
+
this.#logger.debug(`Emitting '${eventName}' event.`, { eventName, payload });
|
|
2501
2517
|
await this.#eventManager.emit(eventName, sender, payload);
|
|
2502
2518
|
// TODO: should move this logic into metaManager
|
|
2503
2519
|
// if (
|
|
@@ -2509,7 +2525,7 @@ class RapidServer {
|
|
|
2509
2525
|
// }
|
|
2510
2526
|
}
|
|
2511
2527
|
async start() {
|
|
2512
|
-
|
|
2528
|
+
this.#logger.info("Starting rapid server...");
|
|
2513
2529
|
const pluginManager = this.#pluginManager;
|
|
2514
2530
|
await pluginManager.loadPlugins(this.#plugins);
|
|
2515
2531
|
await pluginManager.initPlugins();
|
|
@@ -2519,7 +2535,7 @@ class RapidServer {
|
|
|
2519
2535
|
await pluginManager.registerMessageHandlers();
|
|
2520
2536
|
await pluginManager.registerTaskProcessors();
|
|
2521
2537
|
await this.configureApplication();
|
|
2522
|
-
|
|
2538
|
+
this.#logger.info(`Rapid server ready.`);
|
|
2523
2539
|
await pluginManager.onApplicationReady(this.#applicationConfig);
|
|
2524
2540
|
}
|
|
2525
2541
|
async configureApplication() {
|
|
@@ -2534,14 +2550,20 @@ class RapidServer {
|
|
|
2534
2550
|
this.#buildedRoutes = await buildRoutes(this, this.#applicationConfig);
|
|
2535
2551
|
}
|
|
2536
2552
|
async queryDatabaseObject(sql, params) {
|
|
2537
|
-
|
|
2553
|
+
try {
|
|
2554
|
+
return await this.#databaseAccessor.queryDatabaseObject(sql, params);
|
|
2555
|
+
}
|
|
2556
|
+
catch (err) {
|
|
2557
|
+
this.#logger.error("Failed to query database object.", { errorMessage: err.message, sql, params });
|
|
2558
|
+
throw err;
|
|
2559
|
+
}
|
|
2538
2560
|
}
|
|
2539
2561
|
async tryQueryDatabaseObject(sql, params) {
|
|
2540
2562
|
try {
|
|
2541
2563
|
return await this.queryDatabaseObject(sql, params);
|
|
2542
2564
|
}
|
|
2543
2565
|
catch (err) {
|
|
2544
|
-
|
|
2566
|
+
this.#logger.error("Failed to query database object.", { errorMessage: err.message, sql, params });
|
|
2545
2567
|
}
|
|
2546
2568
|
return [];
|
|
2547
2569
|
}
|
|
@@ -2549,9 +2571,9 @@ class RapidServer {
|
|
|
2549
2571
|
return this.#middlewares;
|
|
2550
2572
|
}
|
|
2551
2573
|
async handleRequest(request, next) {
|
|
2552
|
-
const rapidRequest = new RapidRequest(request);
|
|
2574
|
+
const rapidRequest = new RapidRequest(this, request);
|
|
2553
2575
|
await rapidRequest.parseBody();
|
|
2554
|
-
const routeContext = new RouteContext(rapidRequest);
|
|
2576
|
+
const routeContext = new RouteContext(this, rapidRequest);
|
|
2555
2577
|
await this.#pluginManager.onPrepareRouteContext(routeContext);
|
|
2556
2578
|
await this.#buildedRoutes(routeContext, next);
|
|
2557
2579
|
return routeContext.response.getResponse();
|
|
@@ -2764,10 +2786,6 @@ class MetaManager {
|
|
|
2764
2786
|
get configurations() {
|
|
2765
2787
|
return [];
|
|
2766
2788
|
}
|
|
2767
|
-
async initPlugin(server) {
|
|
2768
|
-
}
|
|
2769
|
-
async registerMiddlewares(server) {
|
|
2770
|
-
}
|
|
2771
2789
|
async registerActionHandlers(server) {
|
|
2772
2790
|
server.registerActionHandler(this, listMetaModels);
|
|
2773
2791
|
server.registerActionHandler(this, listMetaRoutes);
|
|
@@ -2778,31 +2796,20 @@ class MetaManager {
|
|
|
2778
2796
|
server.registerEventHandler("entity.update", handleEntityUpdateEvent.bind(this, server));
|
|
2779
2797
|
server.registerEventHandler("entity.delete", handleEntityDeleteEvent.bind(this, server));
|
|
2780
2798
|
}
|
|
2781
|
-
async registerMessageHandlers(server) {
|
|
2782
|
-
}
|
|
2783
|
-
async registerTaskProcessors(server) {
|
|
2784
|
-
}
|
|
2785
|
-
async onLoadingApplication(server, applicationConfig) {
|
|
2786
|
-
}
|
|
2787
2799
|
async configureModels(server, applicationConfig) {
|
|
2800
|
+
const logger = server.getLogger();
|
|
2788
2801
|
try {
|
|
2802
|
+
logger.info("Loading meta of models...");
|
|
2789
2803
|
const models = await listCollections(server, applicationConfig);
|
|
2790
2804
|
server.appendApplicationConfig({ models });
|
|
2791
2805
|
}
|
|
2792
|
-
catch (
|
|
2793
|
-
|
|
2806
|
+
catch (error) {
|
|
2807
|
+
logger.crit("Failed to load meta of models.", { error });
|
|
2794
2808
|
}
|
|
2795
2809
|
}
|
|
2796
|
-
async configureModelProperties(server, applicationConfig) {
|
|
2797
|
-
}
|
|
2798
|
-
async configureRoutes(server, applicationConfig) {
|
|
2799
|
-
}
|
|
2800
2810
|
async onApplicationLoaded(server, applicationConfig) {
|
|
2801
|
-
console.log("metaManager.onApplicationLoaded");
|
|
2802
2811
|
await syncDatabaseSchema(server, applicationConfig);
|
|
2803
2812
|
}
|
|
2804
|
-
async onApplicationReady(server, applicationConfig) {
|
|
2805
|
-
}
|
|
2806
2813
|
}
|
|
2807
2814
|
async function handleEntityCreateEvent(server, sender, payload) {
|
|
2808
2815
|
if (sender === this) {
|
|
@@ -2862,12 +2869,13 @@ function listCollections(server, applicationConfig) {
|
|
|
2862
2869
|
});
|
|
2863
2870
|
}
|
|
2864
2871
|
async function syncDatabaseSchema(server, applicationConfig) {
|
|
2865
|
-
|
|
2872
|
+
const logger = server.getLogger();
|
|
2873
|
+
logger.info("Synchronizing database schema...");
|
|
2866
2874
|
const sqlQueryTableInformations = `SELECT table_schema, table_name FROM information_schema.tables`;
|
|
2867
2875
|
const tablesInDb = await server.queryDatabaseObject(sqlQueryTableInformations);
|
|
2868
2876
|
const { queryBuilder } = server;
|
|
2869
2877
|
for (const model of applicationConfig.models) {
|
|
2870
|
-
|
|
2878
|
+
logger.debug(`Checking data table for '${model.namespace}.${model.singularCode}'...`);
|
|
2871
2879
|
const expectedTableSchema = model.schema || server.databaseConfig.dbDefaultSchema;
|
|
2872
2880
|
const expectedTableName = model.tableName;
|
|
2873
2881
|
const tableInDb = lodash.find(tablesInDb, { table_schema: expectedTableSchema, table_name: expectedTableName });
|
|
@@ -2879,14 +2887,14 @@ async function syncDatabaseSchema(server, applicationConfig) {
|
|
|
2879
2887
|
FROM information_schema.columns;`;
|
|
2880
2888
|
const columnsInDb = await server.queryDatabaseObject(sqlQueryColumnInformations, []);
|
|
2881
2889
|
for (const model of applicationConfig.models) {
|
|
2882
|
-
|
|
2890
|
+
logger.debug(`Checking data columns for '${model.namespace}.${model.singularCode}'...`);
|
|
2883
2891
|
for (const property of model.properties) {
|
|
2884
2892
|
let columnDDL;
|
|
2885
2893
|
if (isRelationProperty(property)) {
|
|
2886
2894
|
if (property.relation === "one") {
|
|
2887
2895
|
const targetModel = applicationConfig.models.find(item => item.singularCode === property.targetSingularCode);
|
|
2888
2896
|
if (!targetModel) {
|
|
2889
|
-
|
|
2897
|
+
logger.warn(`Cannot find target model with singular code "${property.targetSingularCode}".`);
|
|
2890
2898
|
}
|
|
2891
2899
|
const columnInDb = lodash.find(columnsInDb, {
|
|
2892
2900
|
table_schema: model.schema || "public",
|
|
@@ -2919,7 +2927,7 @@ async function syncDatabaseSchema(server, applicationConfig) {
|
|
|
2919
2927
|
else {
|
|
2920
2928
|
const targetModel = applicationConfig.models.find(item => item.singularCode === property.targetSingularCode);
|
|
2921
2929
|
if (!targetModel) {
|
|
2922
|
-
|
|
2930
|
+
logger.warn(`Cannot find target model with singular code "${property.targetSingularCode}".`);
|
|
2923
2931
|
continue;
|
|
2924
2932
|
}
|
|
2925
2933
|
const columnInDb = lodash.find(columnsInDb, {
|
|
@@ -3010,7 +3018,6 @@ function generateCreateColumnDDL(queryBuilder, options) {
|
|
|
3010
3018
|
else {
|
|
3011
3019
|
const columnType = pgPropertyTypeColumnMap[options.type];
|
|
3012
3020
|
if (!columnType) {
|
|
3013
|
-
console.log('options', options);
|
|
3014
3021
|
throw new Error(`Property type "${options.type}" is not supported.`);
|
|
3015
3022
|
}
|
|
3016
3023
|
columnDDL += ` ${columnType}`;
|
|
@@ -3057,13 +3064,10 @@ function doNotMergeArray(objValue, srcValue) {
|
|
|
3057
3064
|
}
|
|
3058
3065
|
|
|
3059
3066
|
async function runCollectionEntityActionHandler(ctx, options, code, handleEntityAction) {
|
|
3060
|
-
const { server, input } = ctx;
|
|
3067
|
+
const { logger, server, input } = ctx;
|
|
3061
3068
|
const { defaultInput, fixedInput } = options;
|
|
3062
|
-
console.debug(`Running ${code} handler...`);
|
|
3063
|
-
console.debug(`defaultInput: ${JSON.stringify(defaultInput)}`);
|
|
3064
3069
|
const mergedInput = mergeInput(defaultInput, input, fixedInput);
|
|
3065
|
-
|
|
3066
|
-
console.debug(`mergedInput: ${JSON.stringify(mergedInput)}`);
|
|
3070
|
+
logger.debug(`Running ${code} handler...`, { defaultInput, fixedInput, mergedInput });
|
|
3067
3071
|
const entityManager = server.getEntityManager(options.singularCode);
|
|
3068
3072
|
const result = handleEntityAction(entityManager, mergedInput);
|
|
3069
3073
|
if (result instanceof Promise) {
|
|
@@ -3135,8 +3139,8 @@ var findCollectionEntities = /*#__PURE__*/Object.freeze({
|
|
|
3135
3139
|
|
|
3136
3140
|
const code$g = "findCollectionEntityById";
|
|
3137
3141
|
async function handler$g(plugin, ctx, options) {
|
|
3138
|
-
|
|
3139
|
-
|
|
3142
|
+
const { logger, server, input } = ctx;
|
|
3143
|
+
logger.debug(`Running ${code$g} handler...`, { input });
|
|
3140
3144
|
const { id } = input;
|
|
3141
3145
|
const entityManager = server.getEntityManager(options.singularCode);
|
|
3142
3146
|
const entity = await entityManager.findById(id);
|
|
@@ -3168,13 +3172,10 @@ var countCollectionEntities = /*#__PURE__*/Object.freeze({
|
|
|
3168
3172
|
|
|
3169
3173
|
const code$e = "createCollectionEntity";
|
|
3170
3174
|
async function handler$e(plugin, ctx, options) {
|
|
3171
|
-
const { server, input } = ctx;
|
|
3175
|
+
const { logger, server, input } = ctx;
|
|
3172
3176
|
const { defaultInput, fixedInput } = options;
|
|
3173
|
-
console.debug(`Running ${code$e} handler...`);
|
|
3174
|
-
console.debug(`defaultInput: ${JSON.stringify(defaultInput)}`);
|
|
3175
3177
|
const mergedInput = mergeInput(defaultInput, input, fixedInput);
|
|
3176
|
-
|
|
3177
|
-
console.debug(`mergedInput: ${JSON.stringify(mergedInput)}`);
|
|
3178
|
+
logger.debug(`Running ${code$e} handler...`, { defaultInput, fixedInput, mergedInput });
|
|
3178
3179
|
const userId = ctx.routerContext.state?.userId;
|
|
3179
3180
|
if (userId) {
|
|
3180
3181
|
input.createdBy = userId;
|
|
@@ -3194,19 +3195,16 @@ var createCollectionEntity = /*#__PURE__*/Object.freeze({
|
|
|
3194
3195
|
|
|
3195
3196
|
const code$d = "createCollectionEntitiesBatch";
|
|
3196
3197
|
async function handler$d(plugin, ctx, options) {
|
|
3197
|
-
const { server, input } = ctx;
|
|
3198
|
+
const { logger, server, input } = ctx;
|
|
3198
3199
|
const { defaultInput, fixedInput } = options;
|
|
3199
|
-
|
|
3200
|
+
logger.debug(`Running ${code$d} handler...`, { defaultInput, fixedInput, input });
|
|
3200
3201
|
const { entities } = input;
|
|
3201
3202
|
if (!lodash.isArray(entities)) {
|
|
3202
3203
|
throw new Error("input.entities should be an array.");
|
|
3203
3204
|
}
|
|
3204
|
-
console.debug(`defaultInput: ${JSON.stringify(defaultInput)}`);
|
|
3205
|
-
console.debug(`fixedInput: ${JSON.stringify(fixedInput)}`);
|
|
3206
3205
|
const output = [];
|
|
3207
3206
|
for (const entity of entities) {
|
|
3208
3207
|
const mergedEntity = mergeInput(defaultInput?.entity || {}, entity, fixedInput?.entity);
|
|
3209
|
-
console.debug(`mergedEntity: ${JSON.stringify(mergedEntity)}`);
|
|
3210
3208
|
const userId = ctx.routerContext.state?.userId;
|
|
3211
3209
|
if (userId) {
|
|
3212
3210
|
mergedEntity.createdBy = userId;
|
|
@@ -3228,13 +3226,10 @@ var createCollectionEntitiesBatch = /*#__PURE__*/Object.freeze({
|
|
|
3228
3226
|
|
|
3229
3227
|
const code$c = "updateCollectionEntityById";
|
|
3230
3228
|
async function handler$c(plugin, ctx, options) {
|
|
3231
|
-
const { server, input } = ctx;
|
|
3229
|
+
const { logger, server, input } = ctx;
|
|
3232
3230
|
const { defaultInput, fixedInput } = options;
|
|
3233
|
-
console.debug(`Running ${code$c} handler...`);
|
|
3234
|
-
console.debug(`defaultInput: ${JSON.stringify(defaultInput)}`);
|
|
3235
3231
|
const mergedInput = mergeInput(defaultInput, input, fixedInput);
|
|
3236
|
-
|
|
3237
|
-
console.debug(`mergedInput: ${JSON.stringify(mergedInput)}`);
|
|
3232
|
+
logger.debug(`Running ${code$c} handler...`, { defaultInput, fixedInput, mergedInput });
|
|
3238
3233
|
const entityManager = server.getEntityManager(options.singularCode);
|
|
3239
3234
|
const output = await entityManager.updateEntityById({ id: mergedInput.id, entityToSave: mergedInput }, plugin);
|
|
3240
3235
|
ctx.output = output;
|
|
@@ -3248,8 +3243,8 @@ var updateCollectionEntityById = /*#__PURE__*/Object.freeze({
|
|
|
3248
3243
|
|
|
3249
3244
|
const code$b = "deleteCollectionEntityById";
|
|
3250
3245
|
async function handler$b(plugin, ctx, options) {
|
|
3251
|
-
|
|
3252
|
-
|
|
3246
|
+
const { logger, server, input } = ctx;
|
|
3247
|
+
logger.debug(`Running ${code$b} handler...`);
|
|
3253
3248
|
const entityManager = server.getEntityManager(options.singularCode);
|
|
3254
3249
|
await entityManager.deleteById(input.id, plugin);
|
|
3255
3250
|
ctx.status = 200;
|
|
@@ -3264,13 +3259,10 @@ var deleteCollectionEntityById = /*#__PURE__*/Object.freeze({
|
|
|
3264
3259
|
|
|
3265
3260
|
const code$a = "addEntityRelations";
|
|
3266
3261
|
async function handler$a(plugin, ctx, options) {
|
|
3267
|
-
const { server, input } = ctx;
|
|
3262
|
+
const { logger, server, input } = ctx;
|
|
3268
3263
|
const { defaultInput, fixedInput } = options;
|
|
3269
|
-
console.debug(`Running ${code$a} handler...`);
|
|
3270
|
-
console.debug(`defaultInput: ${JSON.stringify(defaultInput)}`);
|
|
3271
3264
|
const mergedInput = mergeInput(defaultInput, input, fixedInput);
|
|
3272
|
-
|
|
3273
|
-
console.debug(`mergedInput: ${JSON.stringify(mergedInput)}`);
|
|
3265
|
+
logger.debug(`Running ${code$a} handler...`, { defaultInput, fixedInput, mergedInput });
|
|
3274
3266
|
const entityManager = server.getEntityManager(options.singularCode);
|
|
3275
3267
|
await entityManager.addRelations(mergedInput, plugin);
|
|
3276
3268
|
ctx.output = {};
|
|
@@ -3284,13 +3276,10 @@ var addEntityRelations = /*#__PURE__*/Object.freeze({
|
|
|
3284
3276
|
|
|
3285
3277
|
const code$9 = "removeEntityRelations";
|
|
3286
3278
|
async function handler$9(plugin, ctx, options) {
|
|
3287
|
-
const { server, input } = ctx;
|
|
3279
|
+
const { logger, server, input } = ctx;
|
|
3288
3280
|
const { defaultInput, fixedInput } = options;
|
|
3289
|
-
console.debug(`Running ${code$9} handler...`);
|
|
3290
|
-
console.debug(`defaultInput: ${JSON.stringify(defaultInput)}`);
|
|
3291
3281
|
const mergedInput = mergeInput(defaultInput, input, fixedInput);
|
|
3292
|
-
|
|
3293
|
-
console.debug(`mergedInput: ${JSON.stringify(mergedInput)}`);
|
|
3282
|
+
logger.debug(`Running ${code$9} handler...`, { defaultInput, fixedInput, mergedInput });
|
|
3294
3283
|
const entityManager = server.getEntityManager(options.singularCode);
|
|
3295
3284
|
await entityManager.removeRelations(mergedInput, plugin);
|
|
3296
3285
|
ctx.output = {};
|
|
@@ -3304,13 +3293,10 @@ var removeEntityRelations = /*#__PURE__*/Object.freeze({
|
|
|
3304
3293
|
|
|
3305
3294
|
const code$8 = "queryDatabase";
|
|
3306
3295
|
async function handler$8(plugin, ctx, options) {
|
|
3307
|
-
const { server, input } = ctx;
|
|
3296
|
+
const { logger, server, input } = ctx;
|
|
3308
3297
|
const { sql, querySingle, defaultInput, fixedInput } = options;
|
|
3309
|
-
console.debug(`Running ${code$8} handler...`);
|
|
3310
|
-
console.debug(`defaultInput: ${JSON.stringify(defaultInput)}`);
|
|
3311
3298
|
const mergedInput = mergeInput(defaultInput, input, fixedInput);
|
|
3312
|
-
|
|
3313
|
-
console.debug(`mergedInput: ${JSON.stringify(mergedInput)}`);
|
|
3299
|
+
logger.debug(`Running ${code$8} handler...`, { defaultInput, fixedInput, mergedInput });
|
|
3314
3300
|
const result = await server.queryDatabaseObject(sql, mergedInput);
|
|
3315
3301
|
if (querySingle) {
|
|
3316
3302
|
ctx.output = lodash.first(result);
|
|
@@ -3403,10 +3389,6 @@ class DataManager {
|
|
|
3403
3389
|
get configurations() {
|
|
3404
3390
|
return [];
|
|
3405
3391
|
}
|
|
3406
|
-
async initPlugin(server) {
|
|
3407
|
-
}
|
|
3408
|
-
async registerMiddlewares(server) {
|
|
3409
|
-
}
|
|
3410
3392
|
async registerActionHandlers(server) {
|
|
3411
3393
|
server.registerActionHandler(this, findCollectionEntities);
|
|
3412
3394
|
server.registerActionHandler(this, findCollectionEntityById);
|
|
@@ -3419,18 +3401,6 @@ class DataManager {
|
|
|
3419
3401
|
server.registerActionHandler(this, deleteCollectionEntityById);
|
|
3420
3402
|
server.registerActionHandler(this, queryDatabase);
|
|
3421
3403
|
}
|
|
3422
|
-
async registerEventHandlers(server) {
|
|
3423
|
-
}
|
|
3424
|
-
async registerMessageHandlers(server) {
|
|
3425
|
-
}
|
|
3426
|
-
async registerTaskProcessors(server) {
|
|
3427
|
-
}
|
|
3428
|
-
async onLoadingApplication(server, applicationConfig) {
|
|
3429
|
-
}
|
|
3430
|
-
async configureModels(server, applicationConfig) {
|
|
3431
|
-
}
|
|
3432
|
-
async configureModelProperties(server, applicationConfig) {
|
|
3433
|
-
}
|
|
3434
3404
|
async configureRoutes(server, applicationConfig) {
|
|
3435
3405
|
const { models } = applicationConfig;
|
|
3436
3406
|
const routes = [];
|
|
@@ -3458,11 +3428,6 @@ class DataManager {
|
|
|
3458
3428
|
});
|
|
3459
3429
|
server.appendApplicationConfig({ routes });
|
|
3460
3430
|
}
|
|
3461
|
-
async onApplicationLoaded(server, applicationConfig) {
|
|
3462
|
-
console.log("[dataManager.onApplicationLoaded]");
|
|
3463
|
-
}
|
|
3464
|
-
async onApplicationReady(server, applicationConfig) {
|
|
3465
|
-
}
|
|
3466
3431
|
}
|
|
3467
3432
|
|
|
3468
3433
|
async function fetchWithTimeout(url, reqInit, timeout) {
|
|
@@ -3517,7 +3482,8 @@ async function sendSourceResponse(proxyCtx, targetRes) {
|
|
|
3517
3482
|
|
|
3518
3483
|
const code$7 = "httpProxy";
|
|
3519
3484
|
async function handler$7(plugin, ctx, options) {
|
|
3520
|
-
|
|
3485
|
+
const { logger } = ctx;
|
|
3486
|
+
logger.debug(`Running ${code$7} handler...`);
|
|
3521
3487
|
await doProxy(ctx.routerContext, options);
|
|
3522
3488
|
}
|
|
3523
3489
|
|
|
@@ -3546,10 +3512,6 @@ class RouteManager {
|
|
|
3546
3512
|
get configurations() {
|
|
3547
3513
|
return [];
|
|
3548
3514
|
}
|
|
3549
|
-
async initPlugin(server) {
|
|
3550
|
-
}
|
|
3551
|
-
async registerMiddlewares(server) {
|
|
3552
|
-
}
|
|
3553
3515
|
async registerActionHandlers(server) {
|
|
3554
3516
|
server.registerActionHandler(this, httpProxy);
|
|
3555
3517
|
}
|
|
@@ -3559,18 +3521,10 @@ class RouteManager {
|
|
|
3559
3521
|
// server.registerEventHandler("entity.update", handleEntityEvent.bind(null, server))
|
|
3560
3522
|
// server.registerEventHandler("entity.delete", handleEntityEvent.bind(null, server))
|
|
3561
3523
|
}
|
|
3562
|
-
async registerMessageHandlers(server) {
|
|
3563
|
-
}
|
|
3564
|
-
async registerTaskProcessors(server) {
|
|
3565
|
-
}
|
|
3566
|
-
async onLoadingApplication(server, applicationConfig) {
|
|
3567
|
-
}
|
|
3568
|
-
async configureModels(server, applicationConfig) {
|
|
3569
|
-
}
|
|
3570
|
-
async configureModelProperties(server, applicationConfig) {
|
|
3571
|
-
}
|
|
3572
3524
|
async configureRoutes(server, applicationConfig) {
|
|
3525
|
+
const logger = server.getLogger();
|
|
3573
3526
|
try {
|
|
3527
|
+
logger.info("Loading meta of routes...");
|
|
3574
3528
|
const entityManager = server.getEntityManager("route");
|
|
3575
3529
|
const routes = await entityManager.findEntities({
|
|
3576
3530
|
orderBy: [
|
|
@@ -3579,15 +3533,10 @@ class RouteManager {
|
|
|
3579
3533
|
});
|
|
3580
3534
|
applicationConfig.routes.push(...routes);
|
|
3581
3535
|
}
|
|
3582
|
-
catch (
|
|
3583
|
-
|
|
3536
|
+
catch (error) {
|
|
3537
|
+
logger.crit("Failed to load meta of routes.", { error });
|
|
3584
3538
|
}
|
|
3585
3539
|
}
|
|
3586
|
-
async onApplicationLoaded(server, applicationConfig) {
|
|
3587
|
-
console.log("[routeManager.onApplicationLoaded] onApplicationLoaded");
|
|
3588
|
-
}
|
|
3589
|
-
async onApplicationReady(server, applicationConfig) {
|
|
3590
|
-
}
|
|
3591
3540
|
}
|
|
3592
3541
|
|
|
3593
3542
|
var pluginConfig = {
|
|
@@ -3667,16 +3616,23 @@ var pluginConfig = {
|
|
|
3667
3616
|
* Webhooks plugin
|
|
3668
3617
|
*/
|
|
3669
3618
|
function listWebhooks(server) {
|
|
3670
|
-
const
|
|
3671
|
-
|
|
3672
|
-
|
|
3673
|
-
|
|
3674
|
-
|
|
3675
|
-
|
|
3676
|
-
|
|
3677
|
-
|
|
3678
|
-
|
|
3679
|
-
|
|
3619
|
+
const logger = server.getLogger();
|
|
3620
|
+
logger.info("Loading meta of webhooks...");
|
|
3621
|
+
try {
|
|
3622
|
+
const entityManager = server.getEntityManager("webhook");
|
|
3623
|
+
return entityManager.findEntities({
|
|
3624
|
+
filters: [
|
|
3625
|
+
{
|
|
3626
|
+
field: "enabled",
|
|
3627
|
+
operator: "eq",
|
|
3628
|
+
value: true,
|
|
3629
|
+
},
|
|
3630
|
+
],
|
|
3631
|
+
});
|
|
3632
|
+
}
|
|
3633
|
+
catch (error) {
|
|
3634
|
+
logger.crit("Failed to load meta of webhooks.", { error });
|
|
3635
|
+
}
|
|
3680
3636
|
}
|
|
3681
3637
|
class WebhooksPlugin {
|
|
3682
3638
|
#webhooks;
|
|
@@ -3698,12 +3654,6 @@ class WebhooksPlugin {
|
|
|
3698
3654
|
get configurations() {
|
|
3699
3655
|
return [];
|
|
3700
3656
|
}
|
|
3701
|
-
async initPlugin(server) {
|
|
3702
|
-
}
|
|
3703
|
-
async registerMiddlewares(server) {
|
|
3704
|
-
}
|
|
3705
|
-
async registerActionHandlers(server) {
|
|
3706
|
-
}
|
|
3707
3657
|
async registerEventHandlers(server) {
|
|
3708
3658
|
const events = [
|
|
3709
3659
|
"entity.create",
|
|
@@ -3714,12 +3664,6 @@ class WebhooksPlugin {
|
|
|
3714
3664
|
server.registerEventHandler(event, this.handleEntityEvent.bind(this, server, event));
|
|
3715
3665
|
}
|
|
3716
3666
|
}
|
|
3717
|
-
async registerMessageHandlers(server) {
|
|
3718
|
-
}
|
|
3719
|
-
async registerTaskProcessors(server) {
|
|
3720
|
-
}
|
|
3721
|
-
async onLoadingApplication(server, applicationConfig) {
|
|
3722
|
-
}
|
|
3723
3667
|
async configureModels(server, applicationConfig) {
|
|
3724
3668
|
server.appendApplicationConfig({
|
|
3725
3669
|
models: pluginConfig.models,
|
|
@@ -3730,7 +3674,6 @@ class WebhooksPlugin {
|
|
|
3730
3674
|
async configureRoutes(server, applicationConfig) {
|
|
3731
3675
|
}
|
|
3732
3676
|
async onApplicationLoaded(server, applicationConfig) {
|
|
3733
|
-
console.log("[webhooks.onApplicationLoaded] loading webhooks");
|
|
3734
3677
|
this.#webhooks = await listWebhooks(server);
|
|
3735
3678
|
}
|
|
3736
3679
|
async onApplicationReady(server, applicationConfig) {
|
|
@@ -3747,6 +3690,7 @@ class WebhooksPlugin {
|
|
|
3747
3690
|
if (payload.namespace === "meta" || payload.namespace === "sys") {
|
|
3748
3691
|
return;
|
|
3749
3692
|
}
|
|
3693
|
+
const logger = server.getLogger();
|
|
3750
3694
|
for (const webhook of this.#webhooks) {
|
|
3751
3695
|
if (lodash.indexOf(webhook.events, event) === -1) {
|
|
3752
3696
|
continue;
|
|
@@ -3755,8 +3699,9 @@ class WebhooksPlugin {
|
|
|
3755
3699
|
webhook.modelSingularCode !== payload.modelSingularCode) {
|
|
3756
3700
|
continue;
|
|
3757
3701
|
}
|
|
3758
|
-
|
|
3702
|
+
logger.debug(`Triggering webhook. ${webhook.url}`);
|
|
3759
3703
|
// TODO: It's better to trigger webhook through message queue.
|
|
3704
|
+
const requestBody = { event, payload };
|
|
3760
3705
|
try {
|
|
3761
3706
|
await fetchWithTimeout(webhook.url, {
|
|
3762
3707
|
method: "post",
|
|
@@ -3764,12 +3709,11 @@ class WebhooksPlugin {
|
|
|
3764
3709
|
"Content-Type": "application/json",
|
|
3765
3710
|
"x-webhook-secret": webhook.secret || "",
|
|
3766
3711
|
},
|
|
3767
|
-
body: JSON.stringify(
|
|
3712
|
+
body: JSON.stringify(requestBody),
|
|
3768
3713
|
});
|
|
3769
3714
|
}
|
|
3770
|
-
catch (
|
|
3771
|
-
|
|
3772
|
-
console.warn(err);
|
|
3715
|
+
catch (error) {
|
|
3716
|
+
logger.error("Failed to call webhook.", { error, webhookUrl: webhook.url, requestBody });
|
|
3773
3717
|
}
|
|
3774
3718
|
}
|
|
3775
3719
|
}
|
|
@@ -4004,36 +3948,17 @@ class AuthPlugin {
|
|
|
4004
3948
|
get configurations() {
|
|
4005
3949
|
return [];
|
|
4006
3950
|
}
|
|
4007
|
-
async initPlugin(server) {
|
|
4008
|
-
}
|
|
4009
|
-
async registerMiddlewares(server) {
|
|
4010
|
-
}
|
|
4011
3951
|
async registerActionHandlers(server) {
|
|
4012
3952
|
for (const actionHandler of pluginActionHandlers$1) {
|
|
4013
3953
|
server.registerActionHandler(this, actionHandler);
|
|
4014
3954
|
}
|
|
4015
3955
|
}
|
|
4016
|
-
async registerEventHandlers(server) {
|
|
4017
|
-
}
|
|
4018
|
-
async registerMessageHandlers(server) {
|
|
4019
|
-
}
|
|
4020
|
-
async registerTaskProcessors(server) {
|
|
4021
|
-
}
|
|
4022
|
-
async onLoadingApplication(server, applicationConfig) {
|
|
4023
|
-
}
|
|
4024
3956
|
async configureModels(server, applicationConfig) {
|
|
4025
3957
|
server.appendApplicationConfig({ models: pluginModels });
|
|
4026
3958
|
}
|
|
4027
|
-
async configureModelProperties(server, applicationConfig) {
|
|
4028
|
-
}
|
|
4029
3959
|
async configureRoutes(server, applicationConfig) {
|
|
4030
3960
|
server.appendApplicationConfig({ routes: pluginRoutes$1 });
|
|
4031
3961
|
}
|
|
4032
|
-
async onApplicationLoaded(server, applicationConfig) {
|
|
4033
|
-
console.log("authManager.onApplicationLoaded");
|
|
4034
|
-
}
|
|
4035
|
-
async onApplicationReady(server, applicationConfig) {
|
|
4036
|
-
}
|
|
4037
3962
|
async onPrepareRouteContext(server, routeContext) {
|
|
4038
3963
|
const request = routeContext.request;
|
|
4039
3964
|
let token;
|
|
@@ -4056,8 +3981,9 @@ class AuthPlugin {
|
|
|
4056
3981
|
routeContext.state.userId = tokenPayload.aud;
|
|
4057
3982
|
routeContext.state.userLogin = tokenPayload.act;
|
|
4058
3983
|
}
|
|
4059
|
-
catch (
|
|
4060
|
-
|
|
3984
|
+
catch (error) {
|
|
3985
|
+
const logger = server.getLogger();
|
|
3986
|
+
logger.debug("Verify JWT failed.", { error });
|
|
4061
3987
|
}
|
|
4062
3988
|
}
|
|
4063
3989
|
}
|
|
@@ -4467,6 +4393,8 @@ class EntityAccessControlPlugin {
|
|
|
4467
4393
|
server.appendModelProperties("model", properties);
|
|
4468
4394
|
}
|
|
4469
4395
|
async configureRoutes(server, applicationConfig) {
|
|
4396
|
+
const logger = server.getLogger();
|
|
4397
|
+
logger.info("Configuring entity access checking policies...");
|
|
4470
4398
|
const model = lodash.find(applicationConfig.models, (item) => item.singularCode === "model");
|
|
4471
4399
|
if (!model) {
|
|
4472
4400
|
return;
|