@ticatec/common-express-server 0.1.3 → 0.1.5

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 CHANGED
@@ -101,6 +101,58 @@ class UserRoutes extends CommonRoutes<CommonRouterHelper> {
101
101
  export default UserRoutes;
102
102
  ```
103
103
 
104
+ #### Custom User Authentication
105
+
106
+ You can provide a custom user verification function to the `CommonRoutes` constructor:
107
+
108
+ ```typescript
109
+ import { CommonRoutes, CommonRouterHelper, UserChecker } from '@ticatec/common-express-server';
110
+
111
+ // Custom user checker function
112
+ const customUserChecker: UserChecker = (req: Request): boolean => {
113
+ // Your custom authentication logic
114
+ const userRole = req.headers['user-role'];
115
+ return userRole === 'admin' || userRole === 'moderator';
116
+ };
117
+
118
+ class AdminRoutes extends CommonRoutes<CommonRouterHelper> {
119
+ constructor(helper: CommonRouterHelper) {
120
+ // Use custom user checker instead of default
121
+ super(helper, customUserChecker);
122
+ this.setupRoutes();
123
+ }
124
+
125
+ private setupRoutes() {
126
+ this.router.get('/dashboard', this.helper.invokeRestfulAction(this.getDashboard));
127
+ }
128
+
129
+ private getDashboard = async (req: Request) => {
130
+ return { message: 'Admin dashboard' };
131
+ };
132
+ }
133
+
134
+ // Skip authentication entirely
135
+ class PublicRoutes extends CommonRoutes<CommonRouterHelper> {
136
+ constructor(helper: CommonRouterHelper) {
137
+ super(helper, false); // No authentication check
138
+ this.setupRoutes();
139
+ }
140
+
141
+ private setupRoutes() {
142
+ this.router.get('/info', this.helper.invokeRestfulAction(this.getInfo));
143
+ }
144
+
145
+ private getInfo = async (req: Request) => {
146
+ return { message: 'Public information' };
147
+ };
148
+ }
149
+ ```
150
+
151
+ The `checkUser` parameter accepts three types:
152
+ - `true` (default): Uses the default `helper.checkLoggedUser()` middleware
153
+ - `false`: Skips user authentication entirely
154
+ - `UserChecker` function: A custom function `(req: Request) => boolean` that returns true if authenticated
155
+
104
156
  ### 3. Create Controllers
105
157
 
106
158
  ```typescript
@@ -1,6 +1,10 @@
1
- import { Express, Router } from "express";
1
+ import { Express, Router, Request } from "express";
2
2
  import CommonRouterHelper from "./CommonRouterHelper";
3
3
  import { Logger } from "log4js";
4
+ /**
5
+ * 当前用户检查
6
+ */
7
+ export type UserChecker = (req: Request) => boolean;
4
8
  /**
5
9
  * Abstract base class for defining common routes
6
10
  * @template T The type of CommonRouterHelper this routes class uses
@@ -16,9 +20,10 @@ export default abstract class CommonRoutes<T extends CommonRouterHelper> {
16
20
  * Constructor for common routes
17
21
  * @param helper Router helper instance
18
22
  * @param checkUser Whether to check user authentication (default: true)
23
+ * @param mergeParams Whether to merge params (default: false)
19
24
  * @protected
20
25
  */
21
- protected constructor(helper: T, checkUser?: boolean);
26
+ protected constructor(helper: T, checkUser?: boolean | UserChecker, mergeParams?: boolean);
22
27
  /**
23
28
  * Binds this router to the Express application
24
29
  * @param app Express application instance
@@ -5,6 +5,19 @@ 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
+ 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
+ };
8
21
  /**
9
22
  * Abstract base class for defining common routes
10
23
  * @template T The type of CommonRouterHelper this routes class uses
@@ -14,16 +27,20 @@ class CommonRoutes {
14
27
  * Constructor for common routes
15
28
  * @param helper Router helper instance
16
29
  * @param checkUser Whether to check user authentication (default: true)
30
+ * @param mergeParams Whether to merge params (default: false)
17
31
  * @protected
18
32
  */
19
- constructor(helper, checkUser = true) {
20
- this.router = (0, express_1.Router)();
33
+ constructor(helper, checkUser = true, mergeParams = false) {
34
+ this.router = (0, express_1.Router)({ mergeParams });
21
35
  this.helper = helper;
22
36
  this.logger = log4js_1.default.getLogger(this.constructor.name);
23
- if (checkUser) {
37
+ if (typeof checkUser == "boolean") {
24
38
  this.logger.debug('Checking if user is logged in');
25
39
  this.router.use(helper.checkLoggedUser());
26
40
  }
41
+ else if (typeof checkUser == "function") {
42
+ this.router.use(customCheck(checkUser));
43
+ }
27
44
  }
28
45
  /**
29
46
  * Binds this router to the Express application
@@ -1 +1 @@
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"}
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,CAAC,MAAM,CAAC,KAAK,CAAC,+BAA+B,CAAC,CAAA;YAClD,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,eAAe,EAAE,CAAC,CAAC;QAC9C,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;AAnCD,+BAmCC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ticatec/common-express-server",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "A comprehensive TypeScript library providing common classes, controllers, and middleware for building scalable Express.js applications with multi-tenant support.",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",