@ticatec/common-express-server 0.3.0 → 0.3.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.
- 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 +125 -0
- package/lib/BaseServer.js +155 -0
- package/lib/BaseServer.js.map +1 -0
- package/lib/CommonProcessor.d.ts +49 -0
- package/lib/CommonProcessor.js +99 -0
- package/lib/CommonProcessor.js.map +1 -0
- package/lib/CommonRouterHelper.d.ts +64 -0
- package/lib/CommonRouterHelper.js +134 -0
- package/lib/CommonRouterHelper.js.map +1 -0
- package/lib/CommonRoutes.d.ts +33 -0
- package/lib/CommonRoutes.js +57 -0
- package/lib/CommonRoutes.js.map +1 -0
- package/lib/LoggedUser.d.ts +33 -0
- package/lib/LoggedUser.js +3 -0
- package/lib/LoggedUser.js.map +1 -0
- package/lib/ProcessorManager.d.ts +14 -0
- package/lib/ProcessorManager.js +30 -0
- package/lib/ProcessorManager.js.map +1 -0
- package/lib/common/AdminBaseController.d.ts +30 -0
- package/lib/common/AdminBaseController.js +41 -0
- package/lib/common/AdminBaseController.js.map +1 -0
- package/lib/common/AdminSearchController.d.ts +13 -0
- package/lib/common/AdminSearchController.js +26 -0
- package/lib/common/AdminSearchController.js.map +1 -0
- package/lib/common/BaseController.d.ts +28 -0
- package/lib/common/BaseController.js +35 -0
- package/lib/common/BaseController.js.map +1 -0
- package/lib/common/CommonController.d.ts +96 -0
- package/lib/common/CommonController.js +129 -0
- package/lib/common/CommonController.js.map +1 -0
- package/lib/common/TenantBaseController.d.ts +30 -0
- package/lib/common/TenantBaseController.js +41 -0
- package/lib/common/TenantBaseController.js.map +1 -0
- package/lib/common/TenantSearchController.d.ts +13 -0
- package/lib/common/TenantSearchController.js +27 -0
- package/lib/common/TenantSearchController.js.map +1 -0
- package/lib/index.d.ts +14 -0
- package/lib/index.js +30 -0
- package/lib/index.js.map +1 -0
- package/package.json +10 -10
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const express_exception_1 = require("@ticatec/express-exception");
|
|
7
|
+
const log4js_1 = __importDefault(require("log4js"));
|
|
8
|
+
/**
|
|
9
|
+
* Common router helper class providing middleware and utilities for Express routing
|
|
10
|
+
*/
|
|
11
|
+
class CommonRouterHelper {
|
|
12
|
+
constructor() {
|
|
13
|
+
/** Logger instance for this router helper */
|
|
14
|
+
this.logger = log4js_1.default.getLogger(this.constructor.name);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
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
|
|
21
|
+
*/
|
|
22
|
+
setJsonHeader(req, res, next) {
|
|
23
|
+
res.header('Content-Type', 'application/json');
|
|
24
|
+
next();
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
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
|
|
31
|
+
*/
|
|
32
|
+
setNoCache(req, res, next) {
|
|
33
|
+
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
|
|
34
|
+
res.header('Expires', '-1');
|
|
35
|
+
res.header('Pragma', 'no-cache');
|
|
36
|
+
next();
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
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
|
|
42
|
+
*/
|
|
43
|
+
invokeRestfulAction(func) {
|
|
44
|
+
return async (req, res) => {
|
|
45
|
+
try {
|
|
46
|
+
let result = await func(req);
|
|
47
|
+
if (result != null) {
|
|
48
|
+
res.json(result);
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
res.status(204).send();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
catch (ex) {
|
|
55
|
+
(0, express_exception_1.handleError)(ex, req, res);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Invokes an asynchronous controller function with error handling
|
|
61
|
+
* @param func The controller function to execute
|
|
62
|
+
* @returns Express middleware function
|
|
63
|
+
*/
|
|
64
|
+
invokeController(func) {
|
|
65
|
+
return async (req, res) => {
|
|
66
|
+
try {
|
|
67
|
+
await func(req, res);
|
|
68
|
+
}
|
|
69
|
+
catch (ex) {
|
|
70
|
+
(0, express_exception_1.handleError)(ex, req, res);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Handles invalid request paths by throwing ActionNotFoundError
|
|
76
|
+
* @returns Express middleware function for handling 404 errors
|
|
77
|
+
*/
|
|
78
|
+
actionNotFound() {
|
|
79
|
+
return (req, res, next) => {
|
|
80
|
+
(0, express_exception_1.handleError)(new express_exception_1.ActionNotFoundError(), req, res);
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Retrieves user information from request headers
|
|
85
|
+
* @param req Express request object
|
|
86
|
+
* @protected
|
|
87
|
+
*/
|
|
88
|
+
retrieveUserFormHeader(req) {
|
|
89
|
+
let userStr = req.headers['user'];
|
|
90
|
+
if (userStr != null) {
|
|
91
|
+
try {
|
|
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;
|
|
101
|
+
}
|
|
102
|
+
catch (ex) {
|
|
103
|
+
this.logger.debug('Invalid user header', userStr);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
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
|
+
*/
|
|
121
|
+
checkLoggedUser() {
|
|
122
|
+
return (req, res, next) => {
|
|
123
|
+
this.retrieveUserFormHeader(req);
|
|
124
|
+
if (req['user'] == null) {
|
|
125
|
+
(0, express_exception_1.handleError)(new express_exception_1.UnauthenticatedError(), req, res);
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
next();
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
exports.default = CommonRouterHelper;
|
|
134
|
+
//# sourceMappingURL=CommonRouterHelper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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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oB,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"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Express, Router, Request } from "express";
|
|
2
|
+
import CommonRouterHelper from "./CommonRouterHelper";
|
|
3
|
+
import { Logger } from "log4js";
|
|
4
|
+
/**
|
|
5
|
+
* 当前用户检查
|
|
6
|
+
*/
|
|
7
|
+
export type UserChecker = (req: Request) => boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Abstract base class for defining common routes
|
|
10
|
+
* @template T The type of CommonRouterHelper this routes class uses
|
|
11
|
+
*/
|
|
12
|
+
export default abstract class CommonRoutes<T extends CommonRouterHelper> {
|
|
13
|
+
/** Express router instance */
|
|
14
|
+
readonly router: Router;
|
|
15
|
+
/** Router helper instance */
|
|
16
|
+
protected helper: T;
|
|
17
|
+
/** Logger instance for this routes class */
|
|
18
|
+
protected logger: Logger;
|
|
19
|
+
/**
|
|
20
|
+
* Constructor for common routes
|
|
21
|
+
* @param helper Router helper instance
|
|
22
|
+
* @param checkUser Whether to check user authentication (default: true)
|
|
23
|
+
* @param mergeParams Whether to merge params (default: false)
|
|
24
|
+
* @protected
|
|
25
|
+
*/
|
|
26
|
+
protected constructor(helper: T, checkUser?: boolean | UserChecker, mergeParams?: boolean);
|
|
27
|
+
/**
|
|
28
|
+
* Binds this router to the Express application
|
|
29
|
+
* @param app Express application instance
|
|
30
|
+
* @param path The path prefix for this router
|
|
31
|
+
*/
|
|
32
|
+
bindRouter(app: Express, path: string): void;
|
|
33
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const express_1 = require("express");
|
|
7
|
+
const log4js_1 = __importDefault(require("log4js"));
|
|
8
|
+
const express_exception_1 = require("@ticatec/express-exception");
|
|
9
|
+
/**
|
|
10
|
+
* 自定义校验
|
|
11
|
+
* @param checker
|
|
12
|
+
*/
|
|
13
|
+
const customCheck = (checker) => (req, res, next) => {
|
|
14
|
+
if (checker(req)) {
|
|
15
|
+
next();
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
throw new express_exception_1.UnauthenticatedError();
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Abstract base class for defining common routes
|
|
23
|
+
* @template T The type of CommonRouterHelper this routes class uses
|
|
24
|
+
*/
|
|
25
|
+
class CommonRoutes {
|
|
26
|
+
/**
|
|
27
|
+
* Constructor for common routes
|
|
28
|
+
* @param helper Router helper instance
|
|
29
|
+
* @param checkUser Whether to check user authentication (default: true)
|
|
30
|
+
* @param mergeParams Whether to merge params (default: false)
|
|
31
|
+
* @protected
|
|
32
|
+
*/
|
|
33
|
+
constructor(helper, checkUser = true, mergeParams = false) {
|
|
34
|
+
this.router = (0, express_1.Router)({ mergeParams });
|
|
35
|
+
this.helper = helper;
|
|
36
|
+
this.logger = log4js_1.default.getLogger(this.constructor.name);
|
|
37
|
+
if (typeof checkUser == "boolean") {
|
|
38
|
+
if (checkUser) {
|
|
39
|
+
this.logger.debug('Checking if user is logged in');
|
|
40
|
+
this.router.use(helper.checkLoggedUser());
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
else if (typeof checkUser == "function") {
|
|
44
|
+
this.router.use(customCheck(checkUser));
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Binds this router to the Express application
|
|
49
|
+
* @param app Express application instance
|
|
50
|
+
* @param path The path prefix for this router
|
|
51
|
+
*/
|
|
52
|
+
bindRouter(app, path) {
|
|
53
|
+
app.use(path, this.router);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.default = CommonRoutes;
|
|
57
|
+
//# sourceMappingURL=CommonRoutes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CommonRoutes.js","sourceRoot":"src/","sources":["CommonRoutes.ts"],"names":[],"mappings":";;;;;AAAA,qCAA2D;AAE3D,oDAAsC;AAEtC,kEAAgE;AAOhE;;;GAGG;AACH,MAAM,WAAW,GAAG,CAAC,OAAoB,EAAE,EAAE,CAAC,CAAC,GAAY,EAAE,GAAa,EAAE,IAAkB,EAAE,EAAE;IAC9F,IAAI,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;QACf,IAAI,EAAE,CAAC;IACX,CAAC;SAAM,CAAC;QACJ,MAAM,IAAI,wCAAoB,EAAE,CAAA;IACpC,CAAC;AACL,CAAC,CAAA;AAED;;;GAGG;AACH,MAA8B,YAAY;IAQtC;;;;;;OAMG;IACH,YAAsB,MAAS,EAAE,YAAmC,IAAI,EAAE,cAAuB,KAAK;QAClG,IAAI,CAAC,MAAM,GAAG,IAAA,gBAAM,EAAC,EAAE,WAAW,EAAE,CAAC,CAAC;QACtC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,MAAM,GAAG,gBAAM,CAAC,SAAS,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;QACtD,IAAI,OAAO,SAAS,IAAI,SAAS,EAAE,CAAC;YAChC,IAAI,SAAS,EAAE,CAAC;gBACZ,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,+BAA+B,CAAC,CAAA;gBAClD,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,eAAe,EAAE,CAAC,CAAC;YAC9C,CAAC;QACL,CAAC;aAAM,IAAI,OAAO,SAAS,IAAI,UAAU,EAAE,CAAC;YACxC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC,CAAA;QAC3C,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;AArCD,+BAqCC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Common user interface representing basic user information
|
|
3
|
+
*/
|
|
4
|
+
export interface CommonUser {
|
|
5
|
+
/**
|
|
6
|
+
* Account code
|
|
7
|
+
*/
|
|
8
|
+
accountCode: string;
|
|
9
|
+
/**
|
|
10
|
+
* User name
|
|
11
|
+
*/
|
|
12
|
+
name: string;
|
|
13
|
+
/**
|
|
14
|
+
* Additional properties
|
|
15
|
+
*/
|
|
16
|
+
[key: string]: any;
|
|
17
|
+
/**
|
|
18
|
+
* Tenant information
|
|
19
|
+
*/
|
|
20
|
+
tenant: {
|
|
21
|
+
code: string;
|
|
22
|
+
name: string;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Interface for currently logged in user
|
|
27
|
+
*/
|
|
28
|
+
export default interface LoggedUser extends CommonUser {
|
|
29
|
+
/**
|
|
30
|
+
* User being acted as (for user impersonation)
|
|
31
|
+
*/
|
|
32
|
+
actAs?: CommonUser;
|
|
33
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LoggedUser.js","sourceRoot":"src/","sources":["LoggedUser.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import CommonProcessor from "./CommonProcessor";
|
|
2
|
+
export default class ProcessorManager {
|
|
3
|
+
private static instance;
|
|
4
|
+
private map;
|
|
5
|
+
private constructor();
|
|
6
|
+
static getInstance(): ProcessorManager;
|
|
7
|
+
get(name: string): CommonProcessor<any>;
|
|
8
|
+
/**
|
|
9
|
+
* 组成一个处理器
|
|
10
|
+
* @param Constructor
|
|
11
|
+
* @param args
|
|
12
|
+
*/
|
|
13
|
+
register(Constructor: new (name: string) => CommonProcessor<any>, args?: any): CommonProcessor<any>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class ProcessorManager {
|
|
4
|
+
constructor() {
|
|
5
|
+
this.map = new Map();
|
|
6
|
+
}
|
|
7
|
+
static getInstance() {
|
|
8
|
+
return ProcessorManager.instance;
|
|
9
|
+
}
|
|
10
|
+
get(name) {
|
|
11
|
+
return this.map.get(name);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* 组成一个处理器
|
|
15
|
+
* @param Constructor
|
|
16
|
+
* @param args
|
|
17
|
+
*/
|
|
18
|
+
register(Constructor, args) {
|
|
19
|
+
let constructorName = Constructor.name;
|
|
20
|
+
let processor = this.map.get(constructorName);
|
|
21
|
+
if (!processor) {
|
|
22
|
+
processor = new Constructor(args);
|
|
23
|
+
this.map.set(constructorName, processor);
|
|
24
|
+
}
|
|
25
|
+
return processor;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
ProcessorManager.instance = new ProcessorManager();
|
|
29
|
+
exports.default = ProcessorManager;
|
|
30
|
+
//# sourceMappingURL=ProcessorManager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ProcessorManager.js","sourceRoot":"src/","sources":["ProcessorManager.ts"],"names":[],"mappings":";;AAEA,MAAqB,gBAAgB;IAKjC;QACI,IAAI,CAAC,GAAG,GAAG,IAAI,GAAG,EAAgC,CAAC;IACvD,CAAC;IAED,MAAM,CAAC,WAAW;QACd,OAAO,gBAAgB,CAAC,QAAQ,CAAC;IACrC,CAAC;IAED,GAAG,CAAC,IAAY;QACZ,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAC9B,CAAC;IAED;;;;OAIG;IACH,QAAQ,CAAC,WAAuD,EAAE,IAAU;QACxE,IAAI,eAAe,GAAG,WAAW,CAAC,IAAI,CAAC;QACvC,IAAI,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC;QAC9C,IAAI,CAAC,SAAS,EAAE,CAAC;YACb,SAAS,GAAG,IAAI,WAAW,CAAC,IAAI,CAAC,CAAA;YACjC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,eAAe,EAAE,SAAS,CAAC,CAAC;QAC7C,CAAC;QACD,OAAO,SAAS,CAAC;IACrB,CAAC;;AA5Bc,yBAAQ,GAAqB,IAAI,gBAAgB,EAAE,CAAC;kBAFlD,gBAAgB"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ValidationRules } from "@ticatec/bean-validator";
|
|
2
|
+
import CommonController from "./CommonController";
|
|
3
|
+
import { Request } from "express";
|
|
4
|
+
/**
|
|
5
|
+
* Base class for platform admin interfaces that are tenant-independent
|
|
6
|
+
* @template T The service type this controller depends on
|
|
7
|
+
*/
|
|
8
|
+
export default abstract class AdminBaseController<T> extends CommonController<T> {
|
|
9
|
+
/**
|
|
10
|
+
* Constructor for admin base controller
|
|
11
|
+
* @param service The service instance to inject
|
|
12
|
+
* @param rules Entity validation rules
|
|
13
|
+
* @protected
|
|
14
|
+
*/
|
|
15
|
+
protected constructor(service: T, rules: ValidationRules);
|
|
16
|
+
/**
|
|
17
|
+
* Gets arguments for creating new entity
|
|
18
|
+
* @param req Express request object
|
|
19
|
+
* @returns Array containing request body as the only argument
|
|
20
|
+
* @protected
|
|
21
|
+
*/
|
|
22
|
+
protected getCreateNewArguments(req: Request): Array<any>;
|
|
23
|
+
/**
|
|
24
|
+
* Gets arguments for updating entity
|
|
25
|
+
* @param req Express request object
|
|
26
|
+
* @returns Array containing request body as the only argument
|
|
27
|
+
* @protected
|
|
28
|
+
*/
|
|
29
|
+
protected getUpdateArguments(req: Request): any[];
|
|
30
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const CommonController_1 = __importDefault(require("./CommonController"));
|
|
7
|
+
/**
|
|
8
|
+
* Base class for platform admin interfaces that are tenant-independent
|
|
9
|
+
* @template T The service type this controller depends on
|
|
10
|
+
*/
|
|
11
|
+
class AdminBaseController extends CommonController_1.default {
|
|
12
|
+
/**
|
|
13
|
+
* Constructor for admin base controller
|
|
14
|
+
* @param service The service instance to inject
|
|
15
|
+
* @param rules Entity validation rules
|
|
16
|
+
* @protected
|
|
17
|
+
*/
|
|
18
|
+
constructor(service, rules) {
|
|
19
|
+
super(service, rules);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Gets arguments for creating new entity
|
|
23
|
+
* @param req Express request object
|
|
24
|
+
* @returns Array containing request body as the only argument
|
|
25
|
+
* @protected
|
|
26
|
+
*/
|
|
27
|
+
getCreateNewArguments(req) {
|
|
28
|
+
return [req.body];
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Gets arguments for updating entity
|
|
32
|
+
* @param req Express request object
|
|
33
|
+
* @returns Array containing request body as the only argument
|
|
34
|
+
* @protected
|
|
35
|
+
*/
|
|
36
|
+
getUpdateArguments(req) {
|
|
37
|
+
return [req.body];
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.default = AdminBaseController;
|
|
41
|
+
//# sourceMappingURL=AdminBaseController.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AdminBaseController.js","sourceRoot":"src/","sources":["common/AdminBaseController.ts"],"names":[],"mappings":";;;;;AACA,0EAAkD;AAGlD;;;GAGG;AACH,MAA8B,mBAAuB,SAAQ,0BAAmB;IAE5E;;;;;OAKG;IACH,YAAsB,OAAU,EAAE,KAAsB;QACpD,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;IAC1B,CAAC;IAED;;;;;OAKG;IACO,qBAAqB,CAAC,GAAY;QACxC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;IACrB,CAAC;IAED;;;;;OAKG;IACO,kBAAkB,CAAC,GAAY;QACrC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;IACrB,CAAC;CACJ;AA/BD,sCA+BC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Request } from "express";
|
|
2
|
+
import AdminBaseController from "./AdminBaseController";
|
|
3
|
+
/**
|
|
4
|
+
* Base class for tenant-independent search interfaces for platform admin
|
|
5
|
+
* @template T The service type this controller depends on
|
|
6
|
+
*/
|
|
7
|
+
export default abstract class AdminSearchController<T> extends AdminBaseController<T> {
|
|
8
|
+
/**
|
|
9
|
+
* Search method for querying entities
|
|
10
|
+
* @returns Function that handles search requests
|
|
11
|
+
*/
|
|
12
|
+
search(): (req: Request) => Promise<any>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const AdminBaseController_1 = __importDefault(require("./AdminBaseController"));
|
|
7
|
+
const BaseController_1 = __importDefault(require("./BaseController"));
|
|
8
|
+
/**
|
|
9
|
+
* Base class for tenant-independent search interfaces for platform admin
|
|
10
|
+
* @template T The service type this controller depends on
|
|
11
|
+
*/
|
|
12
|
+
class AdminSearchController extends AdminBaseController_1.default {
|
|
13
|
+
/**
|
|
14
|
+
* Search method for querying entities
|
|
15
|
+
* @returns Function that handles search requests
|
|
16
|
+
*/
|
|
17
|
+
search() {
|
|
18
|
+
return (req) => {
|
|
19
|
+
let query = req.query;
|
|
20
|
+
BaseController_1.default.debugEnabled && this.logger.debug(`Path: ${req.path}, query by criteria:`, query);
|
|
21
|
+
return this.invokeServiceInterface('search', [query]);
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.default = AdminSearchController;
|
|
26
|
+
//# sourceMappingURL=AdminSearchController.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AdminSearchController.js","sourceRoot":"src/","sources":["common/AdminSearchController.ts"],"names":[],"mappings":";;;;;AACA,gFAAwD;AACxD,sEAA8C;AAG9C;;;GAGG;AACH,MAA8B,qBAAyB,SAAQ,6BAAsB;IAEjF;;;OAGG;IACH,MAAM;QACF,OAAO,CAAC,GAAW,EAAE,EAAE;YACnB,IAAI,KAAK,GAAG,GAAG,CAAC,KAAK,CAAC;YACtB,wBAAc,CAAC,YAAY,IAAI,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,SAAS,GAAG,CAAC,IAAI,sBAAsB,EAAE,KAAK,CAAC,CAAC;YACjG,OAAO,IAAI,CAAC,sBAAsB,CAAC,QAAQ,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;QAC1D,CAAC,CAAA;IACL,CAAC;CACJ;AAbD,wCAaC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Logger } from "log4js";
|
|
2
|
+
import { CommonUser } from "../LoggedUser";
|
|
3
|
+
import { Request } from "express";
|
|
4
|
+
/**
|
|
5
|
+
* Abstract base controller class providing common functionality for all controllers
|
|
6
|
+
* @template T The service type this controller depends on
|
|
7
|
+
*/
|
|
8
|
+
export default abstract class BaseController<T> {
|
|
9
|
+
/** Flag to enable debug logging */
|
|
10
|
+
static debugEnabled: boolean;
|
|
11
|
+
/** The service instance this controller uses */
|
|
12
|
+
protected readonly service: T;
|
|
13
|
+
/** Logger instance for this controller */
|
|
14
|
+
protected readonly logger: Logger;
|
|
15
|
+
/**
|
|
16
|
+
* Constructor for base controller
|
|
17
|
+
* @param service The service instance to inject
|
|
18
|
+
* @protected
|
|
19
|
+
*/
|
|
20
|
+
protected constructor(service: T);
|
|
21
|
+
/**
|
|
22
|
+
* Gets the current logged user, if acting as another user, returns the acted user,
|
|
23
|
+
* returns null for requests without user injection
|
|
24
|
+
* @param req Express request object
|
|
25
|
+
* @returns The current user or null if no user is logged in
|
|
26
|
+
*/
|
|
27
|
+
protected getLoggedUser: (req: Request) => CommonUser;
|
|
28
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const log4js_1 = __importDefault(require("log4js"));
|
|
7
|
+
/**
|
|
8
|
+
* Abstract base controller class providing common functionality for all controllers
|
|
9
|
+
* @template T The service type this controller depends on
|
|
10
|
+
*/
|
|
11
|
+
class BaseController {
|
|
12
|
+
/**
|
|
13
|
+
* Constructor for base controller
|
|
14
|
+
* @param service The service instance to inject
|
|
15
|
+
* @protected
|
|
16
|
+
*/
|
|
17
|
+
constructor(service) {
|
|
18
|
+
/**
|
|
19
|
+
* Gets the current logged user, if acting as another user, returns the acted user,
|
|
20
|
+
* returns null for requests without user injection
|
|
21
|
+
* @param req Express request object
|
|
22
|
+
* @returns The current user or null if no user is logged in
|
|
23
|
+
*/
|
|
24
|
+
this.getLoggedUser = (req) => {
|
|
25
|
+
let user = req['user'];
|
|
26
|
+
return (user === null || user === void 0 ? void 0 : user.actAs) || user;
|
|
27
|
+
};
|
|
28
|
+
this.logger = log4js_1.default.getLogger(this.constructor.name);
|
|
29
|
+
this.service = service;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/** Flag to enable debug logging */
|
|
33
|
+
BaseController.debugEnabled = false;
|
|
34
|
+
exports.default = BaseController;
|
|
35
|
+
//# sourceMappingURL=BaseController.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BaseController.js","sourceRoot":"src/","sources":["common/BaseController.ts"],"names":[],"mappings":";;;;;AACA,oDAAsC;AAItC;;;GAGG;AACH,MAA8B,cAAc;IASxC;;;;OAIG;IACH,YAAsB,OAAU;QAKhC;;;;;WAKG;QACO,kBAAa,GAAG,CAAC,GAAY,EAAc,EAAE;YACnD,IAAI,IAAI,GAAe,GAAG,CAAC,MAAM,CAAC,CAAC;YACnC,OAAO,CAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,KAAK,KAAI,IAAI,CAAC;QAC/B,CAAC,CAAA;QAbG,IAAI,CAAC,MAAM,GAAG,gBAAM,CAAC,SAAS,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;QACtD,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IAC3B,CAAC;;AAfD,mCAAmC;AAC5B,2BAAY,GAAY,KAAK,AAAjB,CAAkB;kBAHX,cAAc"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import BaseController from "./BaseController";
|
|
2
|
+
import { ValidationRules } from "@ticatec/bean-validator";
|
|
3
|
+
import { RestfulFunction } from "../CommonRouterHelper";
|
|
4
|
+
import { Request } from "express";
|
|
5
|
+
/**
|
|
6
|
+
* Controller class that implements Create/Read/Update/Delete operations
|
|
7
|
+
* @template T The service type this controller depends on
|
|
8
|
+
*/
|
|
9
|
+
export default abstract class CommonController<T> extends BaseController<T> {
|
|
10
|
+
/**
|
|
11
|
+
* Entity validation rules
|
|
12
|
+
* @protected
|
|
13
|
+
*/
|
|
14
|
+
protected readonly rules: ValidationRules;
|
|
15
|
+
/**
|
|
16
|
+
* Constructor for common controller
|
|
17
|
+
* @param service The service instance to inject
|
|
18
|
+
* @param rules Validation rules for entities (optional)
|
|
19
|
+
* @protected
|
|
20
|
+
*/
|
|
21
|
+
protected constructor(service: T, rules?: ValidationRules);
|
|
22
|
+
/**
|
|
23
|
+
* Validates entity data
|
|
24
|
+
* @param data The data to validate
|
|
25
|
+
* @protected
|
|
26
|
+
*/
|
|
27
|
+
protected validateEntity(data: any): void;
|
|
28
|
+
/**
|
|
29
|
+
* Creates new entity endpoint
|
|
30
|
+
* @returns RESTful function for creating new entities
|
|
31
|
+
*/
|
|
32
|
+
createNew(): RestfulFunction;
|
|
33
|
+
/**
|
|
34
|
+
* Updates entity endpoint
|
|
35
|
+
* @returns RESTful function for updating entities
|
|
36
|
+
*/
|
|
37
|
+
update(): RestfulFunction;
|
|
38
|
+
/**
|
|
39
|
+
* Deletes entity endpoint
|
|
40
|
+
* @returns RESTful function for deleting entities
|
|
41
|
+
*/
|
|
42
|
+
del(): RestfulFunction;
|
|
43
|
+
/**
|
|
44
|
+
* Checks if a service interface method exists
|
|
45
|
+
* @param name The method name to check
|
|
46
|
+
* @protected
|
|
47
|
+
*/
|
|
48
|
+
protected checkInterface(name: string): void;
|
|
49
|
+
/**
|
|
50
|
+
* Invokes service interface by name
|
|
51
|
+
* @param name The method name to invoke
|
|
52
|
+
* @param args Arguments to pass to the method
|
|
53
|
+
* @returns Promise resolving to the method result
|
|
54
|
+
* @protected
|
|
55
|
+
*/
|
|
56
|
+
protected invokeServiceInterface(name: string, args?: Array<any>): Promise<any>;
|
|
57
|
+
protected buildNewEntry(req: Request): any;
|
|
58
|
+
protected buildUpdatedEntry(req: Request): any;
|
|
59
|
+
/**
|
|
60
|
+
* Creates a new entity
|
|
61
|
+
* @param req Express request object
|
|
62
|
+
* @returns Promise resolving to the created entity
|
|
63
|
+
* @protected
|
|
64
|
+
*/
|
|
65
|
+
protected _createNew(req: Request): Promise<any>;
|
|
66
|
+
/**
|
|
67
|
+
* Updates an entity
|
|
68
|
+
* @param req Express request object
|
|
69
|
+
* @returns Promise resolving to the updated entity
|
|
70
|
+
* @protected
|
|
71
|
+
*/
|
|
72
|
+
protected _update(req: Request): Promise<any>;
|
|
73
|
+
/**
|
|
74
|
+
* Deletes an entity
|
|
75
|
+
* @param req Express request object
|
|
76
|
+
* @returns Promise resolving when entity is deleted
|
|
77
|
+
* @protected
|
|
78
|
+
*/
|
|
79
|
+
protected _del(req: Request): Promise<any>;
|
|
80
|
+
/**
|
|
81
|
+
* Gets arguments for creating new entity
|
|
82
|
+
* @param req Express request object
|
|
83
|
+
* @returns Array of arguments to pass to service create method
|
|
84
|
+
* @protected
|
|
85
|
+
* @abstract
|
|
86
|
+
*/
|
|
87
|
+
protected abstract getCreateNewArguments(req: Request): Array<any>;
|
|
88
|
+
/**
|
|
89
|
+
* Gets arguments for updating entity
|
|
90
|
+
* @param req Express request object
|
|
91
|
+
* @returns Array of arguments to pass to service update method
|
|
92
|
+
* @protected
|
|
93
|
+
* @abstract
|
|
94
|
+
*/
|
|
95
|
+
protected abstract getUpdateArguments(req: Request): Array<any>;
|
|
96
|
+
}
|