@ticatec/common-express-server 0.0.8 → 0.1.2
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/README.md +491 -0
- package/lib/AppConf.d.ts +31 -0
- package/lib/AppConf.js +57 -0
- package/lib/AppConf.js.map +1 -0
- package/lib/BaseServer.d.ts +72 -26
- package/lib/BaseServer.js +52 -24
- package/lib/BaseServer.js.map +1 -1
- package/lib/CommonRouterHelper.d.ts +45 -22
- package/lib/CommonRouterHelper.js +52 -22
- package/lib/CommonRouterHelper.js.map +1 -1
- package/lib/CommonRoutes.d.ts +21 -3
- package/lib/CommonRoutes.js +16 -1
- package/lib/CommonRoutes.js.map +1 -1
- package/lib/LoggedUser.d.ts +11 -15
- package/lib/common/AdminBaseController.d.ts +14 -10
- package/lib/common/AdminBaseController.js +11 -8
- package/lib/common/AdminBaseController.js.map +1 -1
- package/lib/common/AdminSearchController.d.ts +6 -1
- package/lib/common/AdminSearchController.js +7 -2
- package/lib/common/AdminSearchController.js.map +1 -1
- package/lib/common/BaseController.d.ts +18 -2
- package/lib/common/BaseController.js +18 -3
- package/lib/common/BaseController.js.map +1 -1
- package/lib/common/CommonController.d.ts +49 -25
- package/lib/common/CommonController.js +42 -23
- package/lib/common/CommonController.js.map +1 -1
- package/lib/common/TenantBaseController.d.ts +16 -11
- package/lib/common/TenantBaseController.js +14 -13
- package/lib/common/TenantBaseController.js.map +1 -1
- package/lib/common/TenantSearchController.d.ts +6 -1
- package/lib/common/TenantSearchController.js +7 -2
- package/lib/common/TenantSearchController.js.map +1 -1
- package/lib/index.d.ts +14 -0
- package/lib/index.js +30 -0
- package/lib/index.js.map +1 -0
- package/package.json +54 -14
- package/.idea/common-express-server.iml +0 -9
- package/.idea/inspectionProfiles/Project_Default.xml +0 -11
- package/.idea/misc.xml +0 -5
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
- package/tsconfig.json +0 -13
package/lib/BaseServer.d.ts
CHANGED
|
@@ -1,79 +1,125 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
1
|
import { Express } from 'express';
|
|
3
2
|
import CommonRouterHelper from "./CommonRouterHelper";
|
|
4
3
|
import http from "http";
|
|
5
4
|
import { Logger } from "log4js";
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Function signature for module loader
|
|
7
|
+
*/
|
|
8
|
+
export type moduleLoader = () => Promise<any>;
|
|
9
|
+
/**
|
|
10
|
+
* Abstract base server class providing common functionality for Express servers
|
|
11
|
+
* @template T The type of CommonRouterHelper this server uses
|
|
12
|
+
*/
|
|
13
|
+
export default abstract class BaseServer<T extends CommonRouterHelper> {
|
|
14
|
+
/** Logger instance for this server */
|
|
8
15
|
protected logger: Logger;
|
|
9
|
-
|
|
16
|
+
/** Router helper instance */
|
|
17
|
+
protected helper: T;
|
|
18
|
+
/** Context root path for the server */
|
|
10
19
|
protected contextRoot: string;
|
|
20
|
+
/**
|
|
21
|
+
* Protected constructor for base server
|
|
22
|
+
* @protected
|
|
23
|
+
*/
|
|
11
24
|
protected constructor();
|
|
12
25
|
/**
|
|
13
|
-
*
|
|
26
|
+
* Gets the router helper instance for this server
|
|
27
|
+
* @returns RouterHelper instance
|
|
14
28
|
* @protected
|
|
29
|
+
* @abstract
|
|
15
30
|
*/
|
|
16
|
-
protected abstract getHelper():
|
|
31
|
+
protected abstract getHelper(): T;
|
|
17
32
|
/**
|
|
18
|
-
*
|
|
33
|
+
* Loads configuration file
|
|
34
|
+
* @returns Promise that resolves when configuration is loaded
|
|
19
35
|
* @protected
|
|
36
|
+
* @abstract
|
|
20
37
|
*/
|
|
21
38
|
protected abstract loadConfigFile(): Promise<void>;
|
|
22
39
|
/**
|
|
23
|
-
*
|
|
24
|
-
* @param port
|
|
25
|
-
* @param fileName
|
|
40
|
+
* Writes the listening port to check.dat file
|
|
41
|
+
* @param port The port number to write
|
|
42
|
+
* @param fileName The file name to write to (default: './check.dat')
|
|
26
43
|
* @protected
|
|
27
44
|
*/
|
|
28
45
|
protected writeCheckFile(port: number, fileName?: string): void;
|
|
46
|
+
/**
|
|
47
|
+
* Starts up the server
|
|
48
|
+
* @returns Promise that resolves when server startup is complete
|
|
49
|
+
*/
|
|
29
50
|
startup(): Promise<void>;
|
|
30
51
|
/**
|
|
31
|
-
* web
|
|
32
|
-
* @param server
|
|
52
|
+
* Interceptor function called after web server is created
|
|
53
|
+
* @param server The HTTP server instance
|
|
54
|
+
* @returns Promise that resolves when post-creation setup is complete
|
|
33
55
|
* @protected
|
|
34
56
|
*/
|
|
35
57
|
protected postServerCreated(server: http.Server): Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
* Gets web configuration
|
|
60
|
+
* @returns Web configuration object
|
|
61
|
+
* @protected
|
|
62
|
+
* @abstract
|
|
63
|
+
*/
|
|
36
64
|
protected abstract getWebConf(): any;
|
|
37
65
|
/**
|
|
38
|
-
*
|
|
66
|
+
* Interceptor function called before startup
|
|
67
|
+
* @returns Promise that resolves when pre-startup setup is complete
|
|
39
68
|
* @protected
|
|
40
69
|
*/
|
|
41
70
|
protected beforeStart(): Promise<void>;
|
|
71
|
+
/**
|
|
72
|
+
* Gets the health check endpoint path
|
|
73
|
+
* @returns The health check path
|
|
74
|
+
* @protected
|
|
75
|
+
*/
|
|
42
76
|
protected getHealthCheckPath(): string;
|
|
43
77
|
/**
|
|
44
|
-
*
|
|
45
|
-
* @param app
|
|
78
|
+
* Adds health check endpoint to the Express app
|
|
79
|
+
* @param app Express application instance
|
|
46
80
|
* @protected
|
|
47
81
|
*/
|
|
48
82
|
protected addHealthCheck(app: Express): void;
|
|
83
|
+
/**
|
|
84
|
+
* Starts the web server with given configuration
|
|
85
|
+
* @param webConf Web server configuration
|
|
86
|
+
* @returns Promise that resolves to the HTTP server instance
|
|
87
|
+
* @protected
|
|
88
|
+
*/
|
|
49
89
|
protected startWebServer(webConf: any): Promise<unknown>;
|
|
50
90
|
/**
|
|
51
|
-
*
|
|
91
|
+
* Binds static site resources
|
|
92
|
+
* @param app Express application instance
|
|
93
|
+
* @returns Promise that resolves when static binding is complete
|
|
52
94
|
* @protected
|
|
53
95
|
*/
|
|
54
96
|
protected bindStaticSite(app: Express): Promise<void>;
|
|
55
97
|
/**
|
|
56
|
-
*
|
|
57
|
-
* @param app
|
|
98
|
+
* Initializes Express application
|
|
99
|
+
* @param app Express application instance
|
|
58
100
|
* @protected
|
|
59
101
|
*/
|
|
60
102
|
protected setupExpress(app: Express): void;
|
|
61
103
|
/**
|
|
62
|
-
*
|
|
63
|
-
* @param app
|
|
64
|
-
* @param path
|
|
65
|
-
* @param loader
|
|
104
|
+
* Binds a route to the Express application
|
|
105
|
+
* @param app Express application instance
|
|
106
|
+
* @param path The route path
|
|
107
|
+
* @param loader Module loader function that returns the route class
|
|
108
|
+
* @returns Promise that resolves when route binding is complete
|
|
66
109
|
* @protected
|
|
67
110
|
*/
|
|
68
111
|
protected bindRoutes(app: Express, path: string, loader: moduleLoader): Promise<void>;
|
|
69
112
|
/**
|
|
70
|
-
*
|
|
113
|
+
* Sets up routes for the application
|
|
114
|
+
* @param app Express application instance
|
|
115
|
+
* @returns Promise that resolves when all routes are set up
|
|
71
116
|
* @protected
|
|
117
|
+
* @abstract
|
|
72
118
|
*/
|
|
73
119
|
protected abstract setupRoutes(app: Express): Promise<void>;
|
|
74
120
|
/**
|
|
75
|
-
*
|
|
76
|
-
* @param server
|
|
121
|
+
* Static method to start a server instance
|
|
122
|
+
* @param server The server instance to start
|
|
77
123
|
*/
|
|
78
|
-
static startup(server: BaseServer): void;
|
|
124
|
+
static startup(server: BaseServer<any>): void;
|
|
79
125
|
}
|
package/lib/BaseServer.js
CHANGED
|
@@ -6,24 +6,36 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
const express_1 = __importDefault(require("express"));
|
|
7
7
|
const express_exception_1 = require("@ticatec/express-exception");
|
|
8
8
|
const fs_1 = __importDefault(require("fs"));
|
|
9
|
+
/**
|
|
10
|
+
* Abstract base server class providing common functionality for Express servers
|
|
11
|
+
* @template T The type of CommonRouterHelper this server uses
|
|
12
|
+
*/
|
|
9
13
|
class BaseServer {
|
|
14
|
+
/**
|
|
15
|
+
* Protected constructor for base server
|
|
16
|
+
* @protected
|
|
17
|
+
*/
|
|
10
18
|
constructor() {
|
|
11
19
|
}
|
|
12
20
|
/**
|
|
13
|
-
*
|
|
14
|
-
* @param port
|
|
15
|
-
* @param fileName
|
|
21
|
+
* Writes the listening port to check.dat file
|
|
22
|
+
* @param port The port number to write
|
|
23
|
+
* @param fileName The file name to write to (default: './check.dat')
|
|
16
24
|
* @protected
|
|
17
25
|
*/
|
|
18
26
|
writeCheckFile(port, fileName = './check.dat') {
|
|
19
27
|
try {
|
|
20
|
-
this.logger.debug('
|
|
28
|
+
this.logger.debug('Port', port);
|
|
21
29
|
fs_1.default.writeFileSync(fileName, `${port}`);
|
|
22
30
|
}
|
|
23
31
|
catch (err) {
|
|
24
|
-
this.logger.error('
|
|
32
|
+
this.logger.error('Error writing port file', err);
|
|
25
33
|
}
|
|
26
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* Starts up the server
|
|
37
|
+
* @returns Promise that resolves when server startup is complete
|
|
38
|
+
*/
|
|
27
39
|
async startup() {
|
|
28
40
|
await this.loadConfigFile();
|
|
29
41
|
try {
|
|
@@ -35,37 +47,50 @@ class BaseServer {
|
|
|
35
47
|
await this.startWebServer(webConf);
|
|
36
48
|
}
|
|
37
49
|
catch (err) {
|
|
38
|
-
this.logger.error('
|
|
50
|
+
this.logger.error('Startup failed, reason:', err);
|
|
39
51
|
}
|
|
40
52
|
}
|
|
41
53
|
/**
|
|
42
|
-
* web
|
|
43
|
-
* @param server
|
|
54
|
+
* Interceptor function called after web server is created
|
|
55
|
+
* @param server The HTTP server instance
|
|
56
|
+
* @returns Promise that resolves when post-creation setup is complete
|
|
44
57
|
* @protected
|
|
45
58
|
*/
|
|
46
59
|
async postServerCreated(server) {
|
|
47
60
|
}
|
|
48
61
|
/**
|
|
49
|
-
*
|
|
62
|
+
* Interceptor function called before startup
|
|
63
|
+
* @returns Promise that resolves when pre-startup setup is complete
|
|
50
64
|
* @protected
|
|
51
65
|
*/
|
|
52
66
|
async beforeStart() {
|
|
53
67
|
}
|
|
68
|
+
/**
|
|
69
|
+
* Gets the health check endpoint path
|
|
70
|
+
* @returns The health check path
|
|
71
|
+
* @protected
|
|
72
|
+
*/
|
|
54
73
|
getHealthCheckPath() {
|
|
55
74
|
return '/health-check';
|
|
56
75
|
}
|
|
57
76
|
/**
|
|
58
|
-
*
|
|
59
|
-
* @param app
|
|
77
|
+
* Adds health check endpoint to the Express app
|
|
78
|
+
* @param app Express application instance
|
|
60
79
|
* @protected
|
|
61
80
|
*/
|
|
62
81
|
addHealthCheck(app) {
|
|
63
82
|
let path = this.getHealthCheckPath();
|
|
64
|
-
this.logger.debug('
|
|
83
|
+
this.logger.debug('Loading system health check', path);
|
|
65
84
|
if (path) {
|
|
66
85
|
app.get(path, (req, res) => { res.send(''); });
|
|
67
86
|
}
|
|
68
87
|
}
|
|
88
|
+
/**
|
|
89
|
+
* Starts the web server with given configuration
|
|
90
|
+
* @param webConf Web server configuration
|
|
91
|
+
* @returns Promise that resolves to the HTTP server instance
|
|
92
|
+
* @protected
|
|
93
|
+
*/
|
|
69
94
|
async startWebServer(webConf) {
|
|
70
95
|
let app = (0, express_1.default)();
|
|
71
96
|
app.disable("x-powered-by");
|
|
@@ -76,35 +101,38 @@ class BaseServer {
|
|
|
76
101
|
await this.setupRoutes(app);
|
|
77
102
|
app.use(this.helper.actionNotFound());
|
|
78
103
|
app.use((err, req, res, next) => {
|
|
79
|
-
this.logger.debug("
|
|
104
|
+
this.logger.debug("application error: ", err);
|
|
80
105
|
(0, express_exception_1.handleError)(err, req, res);
|
|
81
106
|
});
|
|
82
107
|
return new Promise(resolve => {
|
|
83
108
|
let server = app.listen(webConf.port, webConf.ip, () => {
|
|
84
|
-
this.logger.info(`Web
|
|
109
|
+
this.logger.info(`Web service started successfully, listening on IP: ${webConf.ip}, port: ${webConf.port}`);
|
|
85
110
|
this.postServerCreated(server);
|
|
86
111
|
resolve(server);
|
|
87
112
|
});
|
|
88
113
|
});
|
|
89
114
|
}
|
|
90
115
|
/**
|
|
91
|
-
*
|
|
116
|
+
* Binds static site resources
|
|
117
|
+
* @param app Express application instance
|
|
118
|
+
* @returns Promise that resolves when static binding is complete
|
|
92
119
|
* @protected
|
|
93
120
|
*/
|
|
94
121
|
async bindStaticSite(app) {
|
|
95
122
|
}
|
|
96
123
|
/**
|
|
97
|
-
*
|
|
98
|
-
* @param app
|
|
124
|
+
* Initializes Express application
|
|
125
|
+
* @param app Express application instance
|
|
99
126
|
* @protected
|
|
100
127
|
*/
|
|
101
128
|
setupExpress(app) {
|
|
102
129
|
}
|
|
103
130
|
/**
|
|
104
|
-
*
|
|
105
|
-
* @param app
|
|
106
|
-
* @param path
|
|
107
|
-
* @param loader
|
|
131
|
+
* Binds a route to the Express application
|
|
132
|
+
* @param app Express application instance
|
|
133
|
+
* @param path The route path
|
|
134
|
+
* @param loader Module loader function that returns the route class
|
|
135
|
+
* @returns Promise that resolves when route binding is complete
|
|
108
136
|
* @protected
|
|
109
137
|
*/
|
|
110
138
|
async bindRoutes(app, path, loader) {
|
|
@@ -113,13 +141,13 @@ class BaseServer {
|
|
|
113
141
|
router.bindRouter(app, `${this.contextRoot}${path}`);
|
|
114
142
|
}
|
|
115
143
|
/**
|
|
116
|
-
*
|
|
117
|
-
* @param server
|
|
144
|
+
* Static method to start a server instance
|
|
145
|
+
* @param server The server instance to start
|
|
118
146
|
*/
|
|
119
147
|
static startup(server) {
|
|
120
148
|
server.startup().then(() => {
|
|
121
149
|
}).catch(ex => {
|
|
122
|
-
console.error('
|
|
150
|
+
console.error('Server startup error', ex);
|
|
123
151
|
});
|
|
124
152
|
}
|
|
125
153
|
}
|
package/lib/BaseServer.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BaseServer.js","sourceRoot":"src/","sources":["BaseServer.ts"],"names":[],"mappings":";;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"BaseServer.js","sourceRoot":"src/","sources":["BaseServer.ts"],"names":[],"mappings":";;;;;AAAA,sDAA0E;AAE1E,kEAAuD;AACvD,4CAAoB;AAapB;;;GAGG;AACH,MAA8B,UAAU;IASpC;;;OAGG;IACH;IAEA,CAAC;IAkBD;;;;;OAKG;IACO,cAAc,CAAC,IAAY,EAAE,WAAmB,aAAa;QACnE,IAAI,CAAC;YACD,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;YAChC,YAAE,CAAC,aAAa,CAAC,QAAQ,EAAE,GAAG,IAAI,EAAE,CAAC,CAAC;QAC1C,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACX,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,yBAAyB,EAAE,GAAG,CAAC,CAAC;QACtD,CAAC;IACL,CAAC;IAGD;;;OAGG;IACH,KAAK,CAAC,OAAO;QACT,MAAM,IAAI,CAAC,cAAc,EAAE,CAAC;QAC5B,IAAI,CAAC;YACD,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,SAAS,EAAE,CAAC;YAC/B,MAAM,IAAI,CAAC,WAAW,EAAE,CAAC;YACzB,IAAI,OAAO,GAAG,IAAI,CAAC,UAAU,EAAE,CAAC;YAChC,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;YAClC,IAAI,CAAC,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;YACvC,MAAM,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;QACvC,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACX,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,yBAAyB,EAAE,GAAG,CAAC,CAAC;QACtD,CAAC;IACL,CAAC;IAED;;;;;OAKG;IACO,KAAK,CAAC,iBAAiB,CAAC,MAAmB;IAErD,CAAC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
|
1
|
+
import { NextFunction, Request, Response } from "express";
|
|
2
2
|
import log4js from "log4js";
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Function signature for RESTful API handlers
|
|
5
|
+
*/
|
|
6
|
+
export type RestfulFunction = (req: Request) => any;
|
|
7
|
+
/**
|
|
8
|
+
* Function signature for control handlers
|
|
9
|
+
*/
|
|
10
|
+
export type ControlFunction = (req: Request, res: Response) => any;
|
|
11
|
+
/**
|
|
12
|
+
* Common router helper class providing middleware and utilities for Express routing
|
|
13
|
+
*/
|
|
5
14
|
export default class CommonRouterHelper {
|
|
15
|
+
/** Logger instance for this router helper */
|
|
6
16
|
protected readonly logger: log4js.Logger;
|
|
7
17
|
/**
|
|
8
|
-
*
|
|
9
|
-
* @param req
|
|
10
|
-
* @param res
|
|
11
|
-
* @param next
|
|
18
|
+
* Sets HTTP response header to JSON format
|
|
19
|
+
* @param req Express request object
|
|
20
|
+
* @param res Express response object
|
|
21
|
+
* @param next Express next function
|
|
12
22
|
*/
|
|
13
|
-
setJsonHeader(req: Request, res: Response, next:
|
|
23
|
+
setJsonHeader(req: Request, res: Response, next: NextFunction): void;
|
|
14
24
|
/**
|
|
15
|
-
*
|
|
16
|
-
* @param req
|
|
17
|
-
* @param res
|
|
18
|
-
* @param next
|
|
19
|
-
* @returns {Promise<void>}
|
|
25
|
+
* Sets response headers to disable caching
|
|
26
|
+
* @param req Express request object
|
|
27
|
+
* @param res Express response object
|
|
28
|
+
* @param next Express next function
|
|
20
29
|
*/
|
|
21
|
-
setNoCache(req: Request, res: Response, next:
|
|
30
|
+
setNoCache(req: Request, res: Response, next: NextFunction): void;
|
|
22
31
|
/**
|
|
23
|
-
*
|
|
24
|
-
* @param func
|
|
32
|
+
* Invokes a RESTful operation and wraps the result in JSON format for the client
|
|
33
|
+
* @param func The RESTful function to execute
|
|
34
|
+
* @returns Express middleware function
|
|
25
35
|
*/
|
|
26
36
|
invokeRestfulAction(func: RestfulFunction): any;
|
|
27
37
|
/**
|
|
28
|
-
*
|
|
29
|
-
* @param func
|
|
38
|
+
* Invokes an asynchronous controller function with error handling
|
|
39
|
+
* @param func The controller function to execute
|
|
40
|
+
* @returns Express middleware function
|
|
30
41
|
*/
|
|
31
42
|
invokeController(func: ControlFunction): (req: Request, res: Response) => Promise<void>;
|
|
32
43
|
/**
|
|
33
|
-
*
|
|
44
|
+
* Handles invalid request paths by throwing ActionNotFoundError
|
|
45
|
+
* @returns Express middleware function for handling 404 errors
|
|
34
46
|
*/
|
|
47
|
+
actionNotFound(): (req: Request, res: Response, next: NextFunction) => void;
|
|
35
48
|
/**
|
|
36
|
-
*
|
|
49
|
+
* Retrieves user information from request headers
|
|
50
|
+
* @param req Express request object
|
|
51
|
+
* @protected
|
|
52
|
+
*/
|
|
53
|
+
protected retrieveUserFormHeader(req: Request): void;
|
|
54
|
+
/**
|
|
55
|
+
* Middleware to retrieve user information from headers
|
|
56
|
+
* @returns Express middleware function
|
|
57
|
+
*/
|
|
58
|
+
retrieveUser(): (req: Request, res: Response, next: any) => void;
|
|
59
|
+
/**
|
|
60
|
+
* Middleware to check if user is authenticated
|
|
61
|
+
* @returns Express middleware function that validates user authentication
|
|
37
62
|
*/
|
|
38
|
-
actionNotFound(): (req: Request, res: Response, next: any) => void;
|
|
39
|
-
protected retrieveUser(req: Request): void;
|
|
40
63
|
checkLoggedUser(): (req: Request, res: Response, next: any) => void;
|
|
41
64
|
}
|
|
@@ -5,26 +5,29 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const express_exception_1 = require("@ticatec/express-exception");
|
|
7
7
|
const log4js_1 = __importDefault(require("log4js"));
|
|
8
|
+
/**
|
|
9
|
+
* Common router helper class providing middleware and utilities for Express routing
|
|
10
|
+
*/
|
|
8
11
|
class CommonRouterHelper {
|
|
9
12
|
constructor() {
|
|
10
|
-
this
|
|
13
|
+
/** Logger instance for this router helper */
|
|
14
|
+
this.logger = log4js_1.default.getLogger(this.constructor.name);
|
|
11
15
|
}
|
|
12
16
|
/**
|
|
13
|
-
*
|
|
14
|
-
* @param req
|
|
15
|
-
* @param res
|
|
16
|
-
* @param next
|
|
17
|
+
* Sets HTTP response header to JSON format
|
|
18
|
+
* @param req Express request object
|
|
19
|
+
* @param res Express response object
|
|
20
|
+
* @param next Express next function
|
|
17
21
|
*/
|
|
18
22
|
setJsonHeader(req, res, next) {
|
|
19
23
|
res.header('Content-Type', 'application/json');
|
|
20
24
|
next();
|
|
21
25
|
}
|
|
22
26
|
/**
|
|
23
|
-
*
|
|
24
|
-
* @param req
|
|
25
|
-
* @param res
|
|
26
|
-
* @param next
|
|
27
|
-
* @returns {Promise<void>}
|
|
27
|
+
* Sets response headers to disable caching
|
|
28
|
+
* @param req Express request object
|
|
29
|
+
* @param res Express response object
|
|
30
|
+
* @param next Express next function
|
|
28
31
|
*/
|
|
29
32
|
setNoCache(req, res, next) {
|
|
30
33
|
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
|
|
@@ -33,8 +36,9 @@ class CommonRouterHelper {
|
|
|
33
36
|
next();
|
|
34
37
|
}
|
|
35
38
|
/**
|
|
36
|
-
*
|
|
37
|
-
* @param func
|
|
39
|
+
* Invokes a RESTful operation and wraps the result in JSON format for the client
|
|
40
|
+
* @param func The RESTful function to execute
|
|
41
|
+
* @returns Express middleware function
|
|
38
42
|
*/
|
|
39
43
|
invokeRestfulAction(func) {
|
|
40
44
|
return async (req, res) => {
|
|
@@ -53,8 +57,9 @@ class CommonRouterHelper {
|
|
|
53
57
|
};
|
|
54
58
|
}
|
|
55
59
|
/**
|
|
56
|
-
*
|
|
57
|
-
* @param func
|
|
60
|
+
* Invokes an asynchronous controller function with error handling
|
|
61
|
+
* @param func The controller function to execute
|
|
62
|
+
* @returns Express middleware function
|
|
58
63
|
*/
|
|
59
64
|
invokeController(func) {
|
|
60
65
|
return async (req, res) => {
|
|
@@ -67,30 +72,55 @@ class CommonRouterHelper {
|
|
|
67
72
|
};
|
|
68
73
|
}
|
|
69
74
|
/**
|
|
70
|
-
*
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* 处理无效的请求路径
|
|
75
|
+
* Handles invalid request paths by throwing ActionNotFoundError
|
|
76
|
+
* @returns Express middleware function for handling 404 errors
|
|
74
77
|
*/
|
|
75
78
|
actionNotFound() {
|
|
76
79
|
return (req, res, next) => {
|
|
77
80
|
(0, express_exception_1.handleError)(new express_exception_1.ActionNotFoundError(), req, res);
|
|
78
81
|
};
|
|
79
82
|
}
|
|
80
|
-
|
|
83
|
+
/**
|
|
84
|
+
* Retrieves user information from request headers
|
|
85
|
+
* @param req Express request object
|
|
86
|
+
* @protected
|
|
87
|
+
*/
|
|
88
|
+
retrieveUserFormHeader(req) {
|
|
81
89
|
let userStr = req.headers['user'];
|
|
82
90
|
if (userStr != null) {
|
|
83
91
|
try {
|
|
84
|
-
|
|
92
|
+
const user = JSON.parse(decodeURIComponent(userStr));
|
|
93
|
+
let language = req.headers['x-language'];
|
|
94
|
+
if (language) {
|
|
95
|
+
if (user.actAs) {
|
|
96
|
+
user.actAs['language'] = language;
|
|
97
|
+
}
|
|
98
|
+
user['language'] = language;
|
|
99
|
+
}
|
|
100
|
+
req['user'] = user;
|
|
85
101
|
}
|
|
86
102
|
catch (ex) {
|
|
87
|
-
this.logger.debug('
|
|
103
|
+
this.logger.debug('Invalid user header', userStr);
|
|
88
104
|
}
|
|
89
105
|
}
|
|
90
106
|
}
|
|
107
|
+
/**
|
|
108
|
+
* Middleware to retrieve user information from headers
|
|
109
|
+
* @returns Express middleware function
|
|
110
|
+
*/
|
|
111
|
+
retrieveUser() {
|
|
112
|
+
return (req, res, next) => {
|
|
113
|
+
this.retrieveUserFormHeader(req);
|
|
114
|
+
next();
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Middleware to check if user is authenticated
|
|
119
|
+
* @returns Express middleware function that validates user authentication
|
|
120
|
+
*/
|
|
91
121
|
checkLoggedUser() {
|
|
92
122
|
return (req, res, next) => {
|
|
93
|
-
this.
|
|
123
|
+
this.retrieveUserFormHeader(req);
|
|
94
124
|
if (req['user'] == null) {
|
|
95
125
|
(0, express_exception_1.handleError)(new express_exception_1.UnauthenticatedError(), req, res);
|
|
96
126
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CommonRouterHelper.js","sourceRoot":"src/","sources":["CommonRouterHelper.ts"],"names":[],"mappings":";;;;;AACA,kEAAkG;AAClG,oDAA4B;
|
|
1
|
+
{"version":3,"file":"CommonRouterHelper.js","sourceRoot":"src/","sources":["CommonRouterHelper.ts"],"names":[],"mappings":";;;;;AACA,kEAAkG;AAClG,oDAA4B;AAa5B;;GAEG;AACH,MAAqB,kBAAkB;IAAvC;QAEI,6CAA6C;QAC1B,WAAM,GAAG,gBAAM,CAAC,SAAS,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;IA4HxE,CAAC;IA1HG;;;;;OAKG;IACH,aAAa,CAAC,GAAY,EAAE,GAAa,EAAE,IAAkB;QACzD,GAAG,CAAC,MAAM,CAAC,cAAc,EAAE,kBAAkB,CAAC,CAAC;QAC/C,IAAI,EAAE,CAAC;IACX,CAAC;IAGD;;;;;OAKG;IACH,UAAU,CAAC,GAAY,EAAE,GAAa,EAAE,IAAkB;QACtD,GAAG,CAAC,MAAM,CAAC,eAAe,EAAE,8CAA8C,CAAC,CAAC;QAC5E,GAAG,CAAC,MAAM,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;QAC5B,GAAG,CAAC,MAAM,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC;QACjC,IAAI,EAAE,CAAC;IACX,CAAC;IAED;;;;OAIG;IACH,mBAAmB,CAAC,IAAqB;QACrC,OAAO,KAAK,EAAE,GAAY,EAAE,GAAa,EAAiB,EAAE;YACxD,IAAI,CAAC;gBACD,IAAI,MAAM,GAAG,MAAM,IAAI,CAAC,GAA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C;oBACX,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;wBACb,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,GAAG,QAAQ,CAAA;oBACrC,CAAC;oBACD,IAAI,CAAC,UAAU,CAAC,GAAG,QAAQ,CAAA;gBAC/B,CAAC;gBACD,GAAG,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC;YACvB,CAAC;YAAC,OAAO,EAAE,EAAE,CAAC;gBACV,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,qBAAqB,EAAE,OAAO,CAAC,CAAC;YACtD,CAAC;QACL,CAAC;IACL,CAAC;IAED;;;OAGG;IACH,YAAY;QACR,OAAO,CAAC,GAAY,EAAE,GAAa,EAAE,IAAS,EAAE,EAAE;YAC9C,IAAI,CAAC,sBAAsB,CAAC,GAAG,CAAC,CAAC;YACjC,IAAI,EAAE,CAAC;QACX,CAAC,CAAA;IACL,CAAC;IAGD;;;OAGG;IACH,eAAe;QACX,OAAO,CAAC,GAAY,EAAE,GAAa,EAAE,IAAS,EAAE,EAAE;YAC9C,IAAI,CAAC,sBAAsB,CAAC,GAAG,CAAC,CAAC;YACjC,IAAI,GAAG,CAAC,MAAM,CAAC,IAAI,IAAI,EAAE,CAAC;gBACtB,IAAA,+BAAW,EAAC,IAAI,wCAAoB,EAAE,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;YACtD,CAAC;iBAAM,CAAC;gBACJ,IAAI,EAAE,CAAC;YACX,CAAC;QACL,CAAC,CAAA;IACL,CAAC;CAEJ;AA/HD,qCA+HC"}
|
package/lib/CommonRoutes.d.ts
CHANGED
|
@@ -1,10 +1,28 @@
|
|
|
1
1
|
import { Express, Router } from "express";
|
|
2
2
|
import CommonRouterHelper from "./CommonRouterHelper";
|
|
3
3
|
import { Logger } from "log4js";
|
|
4
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Abstract base class for defining common routes
|
|
6
|
+
* @template T The type of CommonRouterHelper this routes class uses
|
|
7
|
+
*/
|
|
8
|
+
export default abstract class CommonRoutes<T extends CommonRouterHelper> {
|
|
9
|
+
/** Express router instance */
|
|
5
10
|
readonly router: Router;
|
|
6
|
-
|
|
11
|
+
/** Router helper instance */
|
|
12
|
+
protected helper: T;
|
|
13
|
+
/** Logger instance for this routes class */
|
|
7
14
|
protected logger: Logger;
|
|
8
|
-
|
|
15
|
+
/**
|
|
16
|
+
* Constructor for common routes
|
|
17
|
+
* @param helper Router helper instance
|
|
18
|
+
* @param checkUser Whether to check user authentication (default: true)
|
|
19
|
+
* @protected
|
|
20
|
+
*/
|
|
21
|
+
protected constructor(helper: T, checkUser?: boolean);
|
|
22
|
+
/**
|
|
23
|
+
* Binds this router to the Express application
|
|
24
|
+
* @param app Express application instance
|
|
25
|
+
* @param path The path prefix for this router
|
|
26
|
+
*/
|
|
9
27
|
bindRouter(app: Express, path: string): void;
|
|
10
28
|
}
|
package/lib/CommonRoutes.js
CHANGED
|
@@ -5,16 +5,31 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const express_1 = require("express");
|
|
7
7
|
const log4js_1 = __importDefault(require("log4js"));
|
|
8
|
+
/**
|
|
9
|
+
* Abstract base class for defining common routes
|
|
10
|
+
* @template T The type of CommonRouterHelper this routes class uses
|
|
11
|
+
*/
|
|
8
12
|
class CommonRoutes {
|
|
13
|
+
/**
|
|
14
|
+
* Constructor for common routes
|
|
15
|
+
* @param helper Router helper instance
|
|
16
|
+
* @param checkUser Whether to check user authentication (default: true)
|
|
17
|
+
* @protected
|
|
18
|
+
*/
|
|
9
19
|
constructor(helper, checkUser = true) {
|
|
10
20
|
this.router = (0, express_1.Router)();
|
|
11
21
|
this.helper = helper;
|
|
12
22
|
this.logger = log4js_1.default.getLogger(this.constructor.name);
|
|
13
23
|
if (checkUser) {
|
|
14
|
-
this.logger.debug('
|
|
24
|
+
this.logger.debug('Checking if user is logged in');
|
|
15
25
|
this.router.use(helper.checkLoggedUser());
|
|
16
26
|
}
|
|
17
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* Binds this router to the Express application
|
|
30
|
+
* @param app Express application instance
|
|
31
|
+
* @param path The path prefix for this router
|
|
32
|
+
*/
|
|
18
33
|
bindRouter(app, path) {
|
|
19
34
|
app.use(path, this.router);
|
|
20
35
|
}
|
package/lib/CommonRoutes.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CommonRoutes.js","sourceRoot":"src/","sources":["CommonRoutes.ts"],"names":[],"mappings":";;;;;AAAA,qCAAwC;AAExC,oDAAsC;AAEtC,MAA8B,YAAY;
|
|
1
|
+
{"version":3,"file":"CommonRoutes.js","sourceRoot":"src/","sources":["CommonRoutes.ts"],"names":[],"mappings":";;;;;AAAA,qCAAwC;AAExC,oDAAsC;AAEtC;;;GAGG;AACH,MAA8B,YAAY;IAQtC;;;;;OAKG;IACH,YAAsB,MAAS,EAAE,YAAqB,IAAI;QACtD,IAAI,CAAC,MAAM,GAAG,IAAA,gBAAM,GAAE,CAAC;QACvB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,MAAM,GAAG,gBAAM,CAAC,SAAS,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;QACtD,IAAI,SAAS,EAAE,CAAC;YACZ,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,+BAA+B,CAAC,CAAA;YAClD,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,eAAe,EAAE,CAAC,CAAC;QAC9C,CAAC;IACL,CAAC;IAED;;;;OAIG;IACH,UAAU,CAAC,GAAY,EAAE,IAAY;QACjC,GAAG,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;IAC/B,CAAC;CACJ;AAhCD,+BAgCC"}
|