kb-server 0.0.1-beta.6 → 0.0.1-beta.8
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 +10 -1
- package/dist/common/api-middleware.d.ts +1 -0
- package/dist/common/api-middleware.js +10 -6
- package/dist/common/create-server.d.ts +7 -0
- package/dist/common/create-server.js +2 -2
- package/dist/helper/logger.d.ts +6 -0
- package/dist/helper/logger.js +17 -0
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -2,4 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
快速创建一个 Node 服务
|
|
4
4
|
|
|
5
|
-
快速创建API、标准错误码、Server、支持统一鉴权函数(API级别)
|
|
5
|
+
快速创建 API、标准错误码、Server、支持统一鉴权函数(API 级别)
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
import { createServer } from "kb-server";
|
|
9
|
+
import * as apis from "./apis";
|
|
10
|
+
|
|
11
|
+
(async () => {
|
|
12
|
+
return createServer({ apis });
|
|
13
|
+
})().then((app) => app.listen(3000));
|
|
14
|
+
```
|
|
@@ -26,6 +26,7 @@ export interface APIErrorResponse {
|
|
|
26
26
|
export type AuthFunction = (action: string, req: express.Request) => Promise<Record<string, any> | boolean> | boolean | Record<string, any>;
|
|
27
27
|
interface IPackAPIOptions {
|
|
28
28
|
authFn?: AuthFunction;
|
|
29
|
+
log?: boolean;
|
|
29
30
|
}
|
|
30
31
|
/**
|
|
31
32
|
* API包装函数
|
|
@@ -3,13 +3,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.packAPI = void 0;
|
|
4
4
|
const create_errors_1 = require("./create-errors");
|
|
5
5
|
const uuid_1 = require("uuid");
|
|
6
|
+
const logger_1 = require("../helper/logger");
|
|
6
7
|
/**
|
|
7
8
|
* API包装函数
|
|
8
9
|
* @param apis API列表
|
|
9
10
|
* @returns
|
|
10
11
|
*/
|
|
11
12
|
const packAPI = (apis, options) => {
|
|
12
|
-
const { authFn } = options || {};
|
|
13
|
+
const { authFn, log = true } = options || {};
|
|
13
14
|
return async (req, res) => {
|
|
14
15
|
// 生成API映射
|
|
15
16
|
const apiMap = new Map(Object.entries(apis).map(([action, execution]) => [action, execution]));
|
|
@@ -25,8 +26,9 @@ const packAPI = (apis, options) => {
|
|
|
25
26
|
try {
|
|
26
27
|
// API 解析
|
|
27
28
|
const { Action, ...params } = req.body || {};
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
if (log) {
|
|
30
|
+
logger_1.logger.log("info", `请求入参:${JSON.stringify(req.body)} - RequestId: ${requestId}`);
|
|
31
|
+
}
|
|
30
32
|
// 接口未定义
|
|
31
33
|
if (!Action) {
|
|
32
34
|
throw new create_errors_1.CommonErrors.InvalidParameter.EmptyAPIRequest();
|
|
@@ -61,11 +63,12 @@ const packAPI = (apis, options) => {
|
|
|
61
63
|
};
|
|
62
64
|
// 完成响应
|
|
63
65
|
took = Date.now() - start;
|
|
64
|
-
|
|
66
|
+
logger_1.logger.log("info", response);
|
|
67
|
+
logger_1.logger.log("info", `耗时:${took} ms - RequestId: ${requestId}`);
|
|
65
68
|
return res.send(response);
|
|
66
69
|
}
|
|
67
70
|
catch (rawError) {
|
|
68
|
-
|
|
71
|
+
logger_1.logger.log("error", rawError);
|
|
69
72
|
// 未知错误
|
|
70
73
|
let error = new create_errors_1.CommonErrors.InternalError.UnknownError();
|
|
71
74
|
// 可控错误
|
|
@@ -87,7 +90,8 @@ const packAPI = (apis, options) => {
|
|
|
87
90
|
};
|
|
88
91
|
// 完成响应
|
|
89
92
|
took = Date.now() - start;
|
|
90
|
-
|
|
93
|
+
logger_1.logger.log("info", response);
|
|
94
|
+
logger_1.logger.log("info", `耗时:${took} ms - RequestId: ${requestId}`);
|
|
91
95
|
return res.send(response);
|
|
92
96
|
}
|
|
93
97
|
};
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { AuthFunction } from "./api-middleware";
|
|
2
2
|
import { APIs } from "./create-api";
|
|
3
3
|
export interface ICreateServerParams {
|
|
4
|
+
/**
|
|
5
|
+
* API列表
|
|
6
|
+
*/
|
|
4
7
|
apis: APIs;
|
|
5
8
|
/**
|
|
6
9
|
* 鉴权函数
|
|
@@ -8,6 +11,10 @@ export interface ICreateServerParams {
|
|
|
8
11
|
* 可以通过返回布尔值来定义权限,也可以直接返回数据对象,数据对象会被传递到上下文 ctx 中(AuthInfo)
|
|
9
12
|
*/
|
|
10
13
|
authFn?: AuthFunction;
|
|
14
|
+
/**
|
|
15
|
+
* 默认请求日志,true开启,false关闭
|
|
16
|
+
*/
|
|
17
|
+
log?: boolean;
|
|
11
18
|
}
|
|
12
19
|
export interface ICreateServerOptions {
|
|
13
20
|
limit?: string | number | undefined;
|
|
@@ -30,14 +30,14 @@ const api_middleware_1 = require("./api-middleware");
|
|
|
30
30
|
* 创建 Server
|
|
31
31
|
*/
|
|
32
32
|
function createServer(params, options) {
|
|
33
|
-
const { apis, authFn } = params || {};
|
|
33
|
+
const { apis, authFn, log } = params || {};
|
|
34
34
|
const { limit = "10mb" } = options || {};
|
|
35
35
|
const app = (0, express_1.default)();
|
|
36
36
|
// POST 参数获取
|
|
37
37
|
app.use((0, express_1.urlencoded)({ extended: true, limit }));
|
|
38
38
|
app.use((0, express_1.json)({ limit }));
|
|
39
39
|
// 注入API
|
|
40
|
-
app.use((0, api_middleware_1.packAPI)(apis, { authFn }));
|
|
40
|
+
app.use((0, api_middleware_1.packAPI)(apis, { authFn, log }));
|
|
41
41
|
return app;
|
|
42
42
|
}
|
|
43
43
|
exports.createServer = createServer;
|
|
@@ -0,0 +1,17 @@
|
|
|
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
|
+
exports.logger = void 0;
|
|
7
|
+
const moment_1 = __importDefault(require("moment"));
|
|
8
|
+
exports.logger = { log };
|
|
9
|
+
function log(level, message) {
|
|
10
|
+
const logTime = (0, moment_1.default)().format(`YYYY-MM-DD HH:ss:mm`);
|
|
11
|
+
if (typeof message === "object") {
|
|
12
|
+
console.log(`[${level}] - ${logTime} - ${JSON.stringify(message)}`);
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
console.log(`[${level}] - ${logTime} - ${message}`);
|
|
16
|
+
}
|
|
17
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kb-server",
|
|
3
|
-
"version": "0.0.1-beta.
|
|
3
|
+
"version": "0.0.1-beta.8",
|
|
4
4
|
"description": "A fast server for Node.JS,made by express.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
"class-validator": "^0.14.1",
|
|
24
24
|
"express": "^4.21.1",
|
|
25
25
|
"is-plain-object": "^5.0.0",
|
|
26
|
+
"moment": "^2.30.1",
|
|
26
27
|
"utility-types": "^3.11.0",
|
|
27
28
|
"uuid": "^11.0.3"
|
|
28
29
|
},
|