@ticatec/common-express-server 0.1.4 → 0.1.6
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 +52 -0
- package/lib/CommonRoutes.d.ts +6 -2
- package/lib/CommonRoutes.js +21 -3
- package/lib/CommonRoutes.js.map +1 -1
- package/package.json +1 -1
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
|
package/lib/CommonRoutes.d.ts
CHANGED
|
@@ -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
|
|
@@ -19,7 +23,7 @@ export default abstract class CommonRoutes<T extends CommonRouterHelper> {
|
|
|
19
23
|
* @param mergeParams Whether to merge params (default: false)
|
|
20
24
|
* @protected
|
|
21
25
|
*/
|
|
22
|
-
protected constructor(helper: T, checkUser?: boolean, mergeParams?: boolean);
|
|
26
|
+
protected constructor(helper: T, checkUser?: boolean | UserChecker, mergeParams?: boolean);
|
|
23
27
|
/**
|
|
24
28
|
* Binds this router to the Express application
|
|
25
29
|
* @param app Express application instance
|
package/lib/CommonRoutes.js
CHANGED
|
@@ -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
|
|
@@ -21,9 +34,14 @@ class CommonRoutes {
|
|
|
21
34
|
this.router = (0, express_1.Router)({ mergeParams });
|
|
22
35
|
this.helper = helper;
|
|
23
36
|
this.logger = log4js_1.default.getLogger(this.constructor.name);
|
|
24
|
-
if (checkUser) {
|
|
25
|
-
|
|
26
|
-
|
|
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));
|
|
27
45
|
}
|
|
28
46
|
}
|
|
29
47
|
/**
|
package/lib/CommonRoutes.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CommonRoutes.js","sourceRoot":"src/","sources":["CommonRoutes.ts"],"names":[],"mappings":";;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"CommonRoutes.js","sourceRoot":"src/","sources":["CommonRoutes.ts"],"names":[],"mappings":";;;;;AAAA,qCAA2D;AAE3D,oDAAsC;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|
|
2
2
|
"name": "@ticatec/common-express-server",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
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",
|