befly 3.10.17 → 3.10.19
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 +83 -307
- package/dist/befly.config.d.ts +7 -0
- package/{befly.config.ts → dist/befly.config.js} +8 -31
- package/dist/checks/checkApi.d.ts +1 -0
- package/{checks/checkApi.ts → dist/checks/checkApi.js} +10 -27
- package/dist/checks/checkHook.d.ts +1 -0
- package/{checks/checkHook.ts → dist/checks/checkHook.js} +10 -19
- package/dist/checks/checkMenu.d.ts +7 -0
- package/{checks/checkMenu.ts → dist/checks/checkMenu.js} +15 -50
- package/dist/checks/checkPlugin.d.ts +1 -0
- package/{checks/checkPlugin.ts → dist/checks/checkPlugin.js} +10 -19
- package/dist/checks/checkTable.d.ts +6 -0
- package/{checks/checkTable.ts → dist/checks/checkTable.js} +16 -40
- package/dist/configs/presetFields.d.ts +4 -0
- package/{configs/presetFields.ts → dist/configs/presetFields.js} +1 -1
- package/dist/configs/presetRegexp.d.ts +145 -0
- package/{utils/regex.ts → dist/configs/presetRegexp.js} +8 -31
- package/dist/hooks/auth.d.ts +5 -0
- package/{hooks/auth.ts → dist/hooks/auth.js} +5 -9
- package/dist/hooks/cors.d.ts +9 -0
- package/{hooks/cors.ts → dist/hooks/cors.js} +2 -12
- package/dist/hooks/parser.d.ts +12 -0
- package/{hooks/parser.ts → dist/hooks/parser.js} +27 -42
- package/dist/hooks/permission.d.ts +12 -0
- package/{hooks/permission.ts → dist/hooks/permission.js} +11 -22
- package/dist/hooks/validator.d.ts +9 -0
- package/{hooks/validator.ts → dist/hooks/validator.js} +5 -12
- package/dist/lib/asyncContext.d.ts +21 -0
- package/dist/lib/asyncContext.js +27 -0
- package/dist/lib/cacheHelper.d.ts +95 -0
- package/{lib/cacheHelper.ts → dist/lib/cacheHelper.js} +43 -103
- package/dist/lib/cacheKeys.d.ts +23 -0
- package/{lib/cacheKeys.ts → dist/lib/cacheKeys.js} +5 -10
- package/dist/lib/cipher.d.ts +153 -0
- package/{lib/cipher.ts → dist/lib/cipher.js} +23 -44
- package/dist/lib/connect.d.ts +91 -0
- package/{lib/connect.ts → dist/lib/connect.js} +46 -87
- package/dist/lib/dbDialect.d.ts +87 -0
- package/{lib/dbDialect.ts → dist/lib/dbDialect.js} +32 -112
- package/dist/lib/dbHelper.d.ts +204 -0
- package/{lib/dbHelper.ts → dist/lib/dbHelper.js} +73 -230
- package/dist/lib/dbUtils.d.ts +68 -0
- package/{lib/dbUtils.ts → dist/lib/dbUtils.js} +49 -123
- package/dist/lib/jwt.d.ts +13 -0
- package/{lib/jwt.ts → dist/lib/jwt.js} +11 -32
- package/dist/lib/logger.d.ts +32 -0
- package/{lib/logger.ts → dist/lib/logger.js} +201 -278
- package/dist/lib/redisHelper.d.ts +185 -0
- package/{lib/redisHelper.ts → dist/lib/redisHelper.js} +95 -139
- package/dist/lib/sqlBuilder.d.ts +160 -0
- package/{lib/sqlBuilder.ts → dist/lib/sqlBuilder.js} +131 -277
- package/dist/lib/sqlCheck.d.ts +23 -0
- package/{lib/sqlCheck.ts → dist/lib/sqlCheck.js} +24 -41
- package/dist/lib/validator.d.ts +45 -0
- package/{lib/validator.ts → dist/lib/validator.js} +43 -60
- package/dist/loader/loadApis.d.ts +12 -0
- package/{loader/loadApis.ts → dist/loader/loadApis.js} +7 -17
- package/dist/loader/loadHooks.d.ts +8 -0
- package/{loader/loadHooks.ts → dist/loader/loadHooks.js} +5 -19
- package/dist/loader/loadPlugins.d.ts +8 -0
- package/{loader/loadPlugins.ts → dist/loader/loadPlugins.js} +8 -20
- package/dist/main.d.ts +26 -0
- package/{main.ts → dist/main.js} +39 -78
- package/dist/paths.d.ts +93 -0
- package/{paths.ts → dist/paths.js} +6 -19
- package/dist/plugins/cache.d.ts +14 -0
- package/{plugins/cache.ts → dist/plugins/cache.js} +4 -11
- package/dist/plugins/cipher.d.ts +10 -0
- package/{plugins/cipher.ts → dist/plugins/cipher.js} +1 -5
- package/dist/plugins/config.d.ts +10 -0
- package/dist/plugins/config.js +6 -0
- package/dist/plugins/db.d.ts +14 -0
- package/{plugins/db.ts → dist/plugins/db.js} +5 -13
- package/dist/plugins/jwt.d.ts +10 -0
- package/{plugins/jwt.ts → dist/plugins/jwt.js} +2 -7
- package/dist/plugins/logger.d.ts +28 -0
- package/{plugins/logger.ts → dist/plugins/logger.js} +2 -7
- package/dist/plugins/redis.d.ts +14 -0
- package/{plugins/redis.ts → dist/plugins/redis.js} +4 -9
- package/dist/plugins/tool.d.ts +79 -0
- package/{plugins/tool.ts → dist/plugins/tool.js} +7 -30
- package/dist/router/api.d.ts +14 -0
- package/dist/router/api.js +107 -0
- package/dist/router/static.d.ts +9 -0
- package/{router/static.ts → dist/router/static.js} +17 -31
- package/dist/scripts/ensureDist.d.ts +1 -0
- package/dist/scripts/ensureDist.js +80 -0
- package/dist/sync/syncApi.d.ts +3 -0
- package/{sync/syncApi.ts → dist/sync/syncApi.js} +33 -53
- package/dist/sync/syncCache.d.ts +2 -0
- package/{sync/syncCache.ts → dist/sync/syncCache.js} +1 -6
- package/dist/sync/syncDev.d.ts +6 -0
- package/{sync/syncDev.ts → dist/sync/syncDev.js} +27 -60
- package/dist/sync/syncMenu.d.ts +14 -0
- package/{sync/syncMenu.ts → dist/sync/syncMenu.js} +61 -121
- package/dist/sync/syncTable.d.ts +151 -0
- package/{sync/syncTable.ts → dist/sync/syncTable.js} +168 -375
- package/{types → dist/types}/api.d.ts +12 -51
- package/dist/types/api.js +4 -0
- package/{types → dist/types}/befly.d.ts +32 -223
- package/dist/types/befly.js +4 -0
- package/{types → dist/types}/cache.d.ts +7 -15
- package/dist/types/cache.js +4 -0
- package/dist/types/cipher.d.ts +27 -0
- package/dist/types/cipher.js +7 -0
- package/{types → dist/types}/common.d.ts +8 -33
- package/dist/types/common.js +5 -0
- package/{types → dist/types}/context.d.ts +3 -5
- package/dist/types/context.js +4 -0
- package/{types → dist/types}/crypto.d.ts +0 -3
- package/dist/types/crypto.js +4 -0
- package/dist/types/database.d.ts +138 -0
- package/dist/types/database.js +4 -0
- package/dist/types/hook.d.ts +15 -0
- package/dist/types/hook.js +6 -0
- package/dist/types/jwt.d.ts +75 -0
- package/dist/types/jwt.js +4 -0
- package/dist/types/logger.d.ts +47 -0
- package/dist/types/logger.js +6 -0
- package/dist/types/plugin.d.ts +14 -0
- package/dist/types/plugin.js +6 -0
- package/dist/types/redis.d.ts +71 -0
- package/dist/types/redis.js +4 -0
- package/{types/roleApisCache.ts → dist/types/roleApisCache.d.ts} +0 -2
- package/dist/types/roleApisCache.js +8 -0
- package/dist/types/sync.d.ts +92 -0
- package/dist/types/sync.js +4 -0
- package/dist/types/table.d.ts +34 -0
- package/dist/types/table.js +4 -0
- package/dist/types/validate.d.ts +67 -0
- package/dist/types/validate.js +4 -0
- package/dist/utils/arrayKeysToCamel.d.ts +13 -0
- package/{utils/arrayKeysToCamel.ts → dist/utils/arrayKeysToCamel.js} +4 -4
- package/dist/utils/calcPerfTime.d.ts +4 -0
- package/{utils/calcPerfTime.ts → dist/utils/calcPerfTime.js} +3 -3
- package/dist/utils/configTypes.d.ts +1 -0
- package/dist/utils/configTypes.js +1 -0
- package/dist/utils/convertBigIntFields.d.ts +11 -0
- package/{utils/convertBigIntFields.ts → dist/utils/convertBigIntFields.js} +5 -9
- package/dist/utils/cors.d.ts +8 -0
- package/{utils/cors.ts → dist/utils/cors.js} +1 -3
- package/dist/utils/disableMenusGlob.d.ts +13 -0
- package/{utils/disableMenusGlob.ts → dist/utils/disableMenusGlob.js} +9 -29
- package/dist/utils/fieldClear.d.ts +11 -0
- package/{utils/fieldClear.ts → dist/utils/fieldClear.js} +15 -33
- package/dist/utils/genShortId.d.ts +10 -0
- package/{utils/genShortId.ts → dist/utils/genShortId.js} +1 -1
- package/dist/utils/getClientIp.d.ts +6 -0
- package/{utils/getClientIp.ts → dist/utils/getClientIp.js} +1 -7
- package/dist/utils/importDefault.d.ts +1 -0
- package/dist/utils/importDefault.js +29 -0
- package/dist/utils/isDirentDirectory.d.ts +2 -0
- package/{utils/isDirentDirectory.ts → dist/utils/isDirentDirectory.js} +3 -8
- package/dist/utils/keysToCamel.d.ts +10 -0
- package/{utils/keysToCamel.ts → dist/utils/keysToCamel.js} +4 -5
- package/dist/utils/keysToSnake.d.ts +10 -0
- package/{utils/keysToSnake.ts → dist/utils/keysToSnake.js} +4 -5
- package/dist/utils/loadMenuConfigs.d.ts +5 -0
- package/{utils/loadMenuConfigs.ts → dist/utils/loadMenuConfigs.js} +22 -49
- package/dist/utils/pickFields.d.ts +4 -0
- package/{utils/pickFields.ts → dist/utils/pickFields.js} +2 -5
- package/dist/utils/process.d.ts +24 -0
- package/{utils/process.ts → dist/utils/process.js} +2 -18
- package/dist/utils/processFields.d.ts +4 -0
- package/{utils/processFields.ts → dist/utils/processFields.js} +4 -8
- package/dist/utils/regex.d.ts +145 -0
- package/{configs/presetRegexp.ts → dist/utils/regex.js} +8 -31
- package/dist/utils/response.d.ts +20 -0
- package/{utils/response.ts → dist/utils/response.js} +27 -48
- package/dist/utils/scanAddons.d.ts +17 -0
- package/{utils/scanAddons.ts → dist/utils/scanAddons.js} +4 -38
- package/dist/utils/scanConfig.d.ts +26 -0
- package/{utils/scanConfig.ts → dist/utils/scanConfig.js} +22 -59
- package/dist/utils/scanFiles.d.ts +30 -0
- package/{utils/scanFiles.ts → dist/utils/scanFiles.js} +25 -65
- package/dist/utils/scanSources.d.ts +10 -0
- package/{utils/scanSources.ts → dist/utils/scanSources.js} +16 -39
- package/dist/utils/sortModules.d.ts +28 -0
- package/{utils/sortModules.ts → dist/utils/sortModules.js} +24 -64
- package/dist/utils/sqlLog.d.ts +14 -0
- package/{utils/sqlLog.ts → dist/utils/sqlLog.js} +2 -14
- package/package.json +15 -32
- package/.gitignore +0 -0
- package/bunfig.toml +0 -3
- package/docs/README.md +0 -98
- package/docs/api/api.md +0 -1921
- package/docs/guide/examples.md +0 -926
- package/docs/guide/quickstart.md +0 -354
- package/docs/hooks/auth.md +0 -38
- package/docs/hooks/cors.md +0 -28
- package/docs/hooks/hook.md +0 -838
- package/docs/hooks/parser.md +0 -19
- package/docs/hooks/rateLimit.md +0 -47
- package/docs/infra/redis.md +0 -628
- package/docs/plugins/cipher.md +0 -61
- package/docs/plugins/database.md +0 -189
- package/docs/plugins/plugin.md +0 -986
- package/docs/reference/addon.md +0 -510
- package/docs/reference/config.md +0 -573
- package/docs/reference/logger.md +0 -495
- package/docs/reference/sync.md +0 -478
- package/docs/reference/table.md +0 -763
- package/docs/reference/validator.md +0 -620
- package/lib/asyncContext.ts +0 -43
- package/plugins/config.ts +0 -13
- package/router/api.ts +0 -130
- package/tsconfig.json +0 -54
- package/types/database.d.ts +0 -541
- package/types/hook.d.ts +0 -25
- package/types/jwt.d.ts +0 -118
- package/types/logger.d.ts +0 -65
- package/types/plugin.d.ts +0 -19
- package/types/redis.d.ts +0 -83
- package/types/sync.d.ts +0 -398
- package/types/table.d.ts +0 -216
- package/types/validate.d.ts +0 -69
- package/utils/configTypes.ts +0 -3
- package/utils/importDefault.ts +0 -21
|
@@ -1,18 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Befly API 类型定义
|
|
3
3
|
*/
|
|
4
|
-
|
|
5
|
-
import type {
|
|
6
|
-
import type {
|
|
7
|
-
import type {
|
|
8
|
-
import type { TableDefinition } from "./validate.js";
|
|
9
|
-
|
|
4
|
+
import type { BeflyContext } from "./befly.ts";
|
|
5
|
+
import type { KeyValue } from "./common.ts";
|
|
6
|
+
import type { RequestContext } from "./context.ts";
|
|
7
|
+
import type { TableDefinition } from "./validate.ts";
|
|
10
8
|
/**
|
|
11
9
|
* HTTP 方法类型
|
|
12
10
|
* 支持 GET、POST 或逗号分隔的组合
|
|
13
11
|
*/
|
|
14
12
|
export type HttpMethod = "GET" | "POST" | "GET,POST";
|
|
15
|
-
|
|
16
13
|
/**
|
|
17
14
|
* 用户信息类型
|
|
18
15
|
*/
|
|
@@ -23,7 +20,6 @@ export interface UserInfo {
|
|
|
23
20
|
role?: string;
|
|
24
21
|
[key: string]: any;
|
|
25
22
|
}
|
|
26
|
-
|
|
27
23
|
/**
|
|
28
24
|
* 认证类型
|
|
29
25
|
* - false: 不需要认证
|
|
@@ -33,22 +29,19 @@ export interface UserInfo {
|
|
|
33
29
|
* - string[]: 需要特定角色
|
|
34
30
|
*/
|
|
35
31
|
export type AuthType = boolean | "admin" | "user" | string[];
|
|
36
|
-
|
|
37
32
|
/**
|
|
38
33
|
* API 处理器函数类型
|
|
39
34
|
*/
|
|
40
|
-
export type ApiHandler<
|
|
41
|
-
|
|
35
|
+
export type ApiHandler<TBody = any, R = any> = (befly: BeflyContext, ctx: RequestContext<TBody>) => Promise<Response | R> | Response | R;
|
|
42
36
|
/**
|
|
43
37
|
* 字段规则定义
|
|
44
38
|
* 键为字段名,值为字段规则字符串
|
|
45
39
|
*/
|
|
46
40
|
export type FieldRules = Record<string, string>;
|
|
47
|
-
|
|
48
41
|
/**
|
|
49
42
|
* API 配置选项
|
|
50
43
|
*/
|
|
51
|
-
export interface ApiOptions {
|
|
44
|
+
export interface ApiOptions<TBody = any, R = any> {
|
|
52
45
|
/** HTTP 方法 */
|
|
53
46
|
method: HttpMethod;
|
|
54
47
|
/** 是否需要认证(true/false/角色数组) */
|
|
@@ -58,126 +51,95 @@ export interface ApiOptions {
|
|
|
58
51
|
/** 必填字段 */
|
|
59
52
|
required?: string[];
|
|
60
53
|
/** 处理函数 */
|
|
61
|
-
handler: ApiHandler
|
|
54
|
+
handler: ApiHandler<TBody, R>;
|
|
62
55
|
}
|
|
63
|
-
|
|
64
56
|
/**
|
|
65
57
|
* API 路由配置
|
|
66
58
|
*/
|
|
67
|
-
export interface ApiRoute<
|
|
59
|
+
export interface ApiRoute<TBody = any, R = any> {
|
|
68
60
|
/** 接口名称(必填) */
|
|
69
61
|
name: string;
|
|
70
|
-
|
|
71
62
|
/** 处理器函数(必填) */
|
|
72
|
-
handler: ApiHandler<
|
|
73
|
-
|
|
63
|
+
handler: ApiHandler<TBody, R>;
|
|
74
64
|
/** HTTP 方法(可选,默认 POST,支持逗号分隔多个方法) */
|
|
75
65
|
method?: HttpMethod;
|
|
76
|
-
|
|
77
|
-
/** 认证类型(可选,默认 true)
|
|
66
|
+
/** 认证类型(可选,默认 true)
|
|
78
67
|
* - true: 需要登录
|
|
79
68
|
* - false: 公开访问(无需登录)
|
|
80
69
|
*/
|
|
81
70
|
auth?: boolean;
|
|
82
|
-
|
|
83
71
|
/** 字段定义(验证规则)(可选,默认 {}) */
|
|
84
72
|
fields?: TableDefinition;
|
|
85
|
-
|
|
86
73
|
/** 必填字段(可选,默认 []) */
|
|
87
74
|
required?: string[];
|
|
88
|
-
|
|
89
|
-
/** 是否保留原始请求体(可选,默认 false)
|
|
75
|
+
/** 是否保留原始请求体(可选,默认 false)
|
|
90
76
|
* - true: 不过滤字段,保留完整请求体(适用于微信回调、webhook 等场景)
|
|
91
77
|
* - false: 根据 fields 定义过滤字段
|
|
92
78
|
*/
|
|
93
79
|
rawBody?: boolean;
|
|
94
|
-
|
|
95
80
|
/** 路由路径(运行时生成) */
|
|
96
81
|
route?: string;
|
|
97
82
|
}
|
|
98
|
-
|
|
99
83
|
/**
|
|
100
84
|
* API 构建器接口
|
|
101
85
|
*/
|
|
102
86
|
export interface ApiBuilder {
|
|
103
87
|
/** 设置 HTTP 方法 */
|
|
104
88
|
method(method: HttpMethod): this;
|
|
105
|
-
|
|
106
89
|
/** 设置路径 */
|
|
107
90
|
path(path: string): this;
|
|
108
|
-
|
|
109
91
|
/** 设置描述 */
|
|
110
92
|
description(desc: string): this;
|
|
111
|
-
|
|
112
93
|
/** 设置认证 */
|
|
113
94
|
auth(auth: AuthType): this;
|
|
114
|
-
|
|
115
95
|
/** 设置验证规则 */
|
|
116
96
|
rules(rules: KeyValue<string>): this;
|
|
117
|
-
|
|
118
97
|
/** 设置必填字段 */
|
|
119
98
|
required(fields: string[]): this;
|
|
120
|
-
|
|
121
99
|
/** 设置处理器 */
|
|
122
|
-
handler(handler: ApiHandler): this;
|
|
123
|
-
|
|
100
|
+
handler<TBody = any, R = any>(handler: ApiHandler<TBody, R>): this;
|
|
124
101
|
/** 构建路由 */
|
|
125
102
|
build(): ApiRoute;
|
|
126
103
|
}
|
|
127
|
-
|
|
128
104
|
/**
|
|
129
105
|
* API 响应辅助函数
|
|
130
106
|
*/
|
|
131
107
|
export interface ApiResponse {
|
|
132
108
|
/** 成功响应 */
|
|
133
109
|
success<T = any>(message: string, data?: T): Response;
|
|
134
|
-
|
|
135
110
|
/** 失败响应 */
|
|
136
111
|
error(message: string, error?: any): Response;
|
|
137
|
-
|
|
138
112
|
/** JSON 响应 */
|
|
139
113
|
json<T = any>(data: T, status?: number): Response;
|
|
140
|
-
|
|
141
114
|
/** 文本响应 */
|
|
142
115
|
text(text: string, status?: number): Response;
|
|
143
|
-
|
|
144
116
|
/** HTML 响应 */
|
|
145
117
|
html(html: string, status?: number): Response;
|
|
146
|
-
|
|
147
118
|
/** 重定向 */
|
|
148
119
|
redirect(url: string, status?: number): Response;
|
|
149
120
|
}
|
|
150
|
-
|
|
151
121
|
/**
|
|
152
122
|
* 路由匹配结果
|
|
153
123
|
*/
|
|
154
124
|
export interface RouteMatch {
|
|
155
125
|
/** 路由配置 */
|
|
156
126
|
route: ApiRoute;
|
|
157
|
-
|
|
158
127
|
/** 路径参数 */
|
|
159
128
|
params: KeyValue<string>;
|
|
160
129
|
}
|
|
161
|
-
|
|
162
130
|
/**
|
|
163
131
|
* 路由器接口
|
|
164
132
|
*/
|
|
165
133
|
export interface Router {
|
|
166
134
|
/** 添加路由 */
|
|
167
135
|
add(route: ApiRoute): void;
|
|
168
|
-
|
|
169
136
|
/** 匹配路由 */
|
|
170
137
|
match(method: HttpMethod, path: string): RouteMatch | null;
|
|
171
|
-
|
|
172
138
|
/** 获取所有路由 */
|
|
173
139
|
getRoutes(): ApiRoute[];
|
|
174
|
-
|
|
175
140
|
/** 删除路由 */
|
|
176
141
|
remove(method: HttpMethod, path: string): boolean;
|
|
177
142
|
}
|
|
178
|
-
|
|
179
|
-
// ========== API 响应数据类型 ==========
|
|
180
|
-
|
|
181
143
|
/**
|
|
182
144
|
* 令牌检测响应数据
|
|
183
145
|
*/
|
|
@@ -189,7 +151,6 @@ export interface TokenCheckData {
|
|
|
189
151
|
/** 过期时间(秒) */
|
|
190
152
|
expiresIn?: number;
|
|
191
153
|
}
|
|
192
|
-
|
|
193
154
|
/**
|
|
194
155
|
* 健康检查响应数据
|
|
195
156
|
*/
|
|
@@ -1,22 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Befly 核心框架类型定义
|
|
3
3
|
*/
|
|
4
|
-
|
|
5
|
-
import type {
|
|
6
|
-
import type {
|
|
7
|
-
import type { DbHelper } from "
|
|
8
|
-
import type { Jwt } from "
|
|
9
|
-
import type { Logger } from "
|
|
10
|
-
import type { RedisHelper } from "
|
|
11
|
-
import type {
|
|
12
|
-
import type { ApiRoute, HttpMethod } from "./api.js";
|
|
13
|
-
import type { KeyValue } from "./common.js";
|
|
14
|
-
import type { LoggerConfig } from "./logger.js";
|
|
15
|
-
import type { Plugin } from "./plugin.js";
|
|
16
|
-
import type { Database } from "bun:sqlite";
|
|
17
|
-
|
|
4
|
+
import type { CacheHelper } from "./cache.ts";
|
|
5
|
+
import type { CipherStatic } from "./cipher.ts";
|
|
6
|
+
import type { KeyValue } from "./common.ts";
|
|
7
|
+
import type { DbHelper } from "./database.ts";
|
|
8
|
+
import type { Jwt } from "./jwt.ts";
|
|
9
|
+
import type { Logger, LoggerConfig } from "./logger.ts";
|
|
10
|
+
import type { RedisHelper } from "./redis.ts";
|
|
11
|
+
import type { ValidatorStatic } from "./validate.ts";
|
|
18
12
|
export type { LoggerConfig };
|
|
19
|
-
|
|
20
13
|
/**
|
|
21
14
|
* 数据库配置
|
|
22
15
|
*/
|
|
@@ -38,7 +31,6 @@ export interface DatabaseConfig {
|
|
|
38
31
|
/** 连接池最大连接数 @default 1 */
|
|
39
32
|
poolMax?: number;
|
|
40
33
|
}
|
|
41
|
-
|
|
42
34
|
/**
|
|
43
35
|
* Redis 配置
|
|
44
36
|
*/
|
|
@@ -56,7 +48,6 @@ export interface RedisConfig {
|
|
|
56
48
|
/** Redis Key 前缀 @default 'befly:' */
|
|
57
49
|
prefix?: string;
|
|
58
50
|
}
|
|
59
|
-
|
|
60
51
|
/**
|
|
61
52
|
* JWT 配置
|
|
62
53
|
*/
|
|
@@ -68,7 +59,6 @@ export interface AuthConfig {
|
|
|
68
59
|
/** 签名算法 @default 'HS256' */
|
|
69
60
|
algorithm?: string;
|
|
70
61
|
}
|
|
71
|
-
|
|
72
62
|
/**
|
|
73
63
|
* CORS 配置
|
|
74
64
|
*/
|
|
@@ -86,15 +76,14 @@ export interface CorsConfig {
|
|
|
86
76
|
/** 是否允许凭证 @default 'true' */
|
|
87
77
|
credentials?: string;
|
|
88
78
|
}
|
|
89
|
-
|
|
90
79
|
/**
|
|
91
80
|
* 全局请求限流配置(Hook)
|
|
92
81
|
*/
|
|
93
82
|
export interface RateLimitRule {
|
|
94
83
|
/**
|
|
95
84
|
* 路由匹配串
|
|
96
|
-
|
|
97
|
-
|
|
85
|
+
* - 精确:"/api/auth/login"
|
|
86
|
+
* - 前缀:"/api/auth/*"
|
|
98
87
|
* - 全量:"*"
|
|
99
88
|
*/
|
|
100
89
|
route: string;
|
|
@@ -105,7 +94,6 @@ export interface RateLimitRule {
|
|
|
105
94
|
/** 计数维度(默认 ip) */
|
|
106
95
|
key?: "ip" | "user" | "ip_user";
|
|
107
96
|
}
|
|
108
|
-
|
|
109
97
|
export interface RateLimitConfig {
|
|
110
98
|
/** 是否启用 (0/1) */
|
|
111
99
|
enable?: number;
|
|
@@ -124,12 +112,10 @@ export interface RateLimitConfig {
|
|
|
124
112
|
/** 路由规则列表 */
|
|
125
113
|
rules?: RateLimitRule[];
|
|
126
114
|
}
|
|
127
|
-
|
|
128
115
|
/**
|
|
129
116
|
* Befly 构造函数选项(最多 2 级结构)
|
|
130
117
|
*/
|
|
131
118
|
export interface BeflyOptions {
|
|
132
|
-
// ========== 核心参数 ==========
|
|
133
119
|
/** 运行环境 ('development' | 'production') @default 'development' */
|
|
134
120
|
nodeEnv?: string;
|
|
135
121
|
/** 应用名称 @default 'Befly App' */
|
|
@@ -146,8 +132,6 @@ export interface BeflyOptions {
|
|
|
146
132
|
bodyLimit?: number;
|
|
147
133
|
/** 时区 @default 'Asia/Shanghai' */
|
|
148
134
|
tz?: string;
|
|
149
|
-
|
|
150
|
-
// ========== 插件配置 ==========
|
|
151
135
|
/** 日志插件配置 */
|
|
152
136
|
logger?: LoggerConfig;
|
|
153
137
|
/** 数据库插件配置 */
|
|
@@ -158,7 +142,6 @@ export interface BeflyOptions {
|
|
|
158
142
|
auth?: AuthConfig;
|
|
159
143
|
/** CORS 插件配置 */
|
|
160
144
|
cors?: CorsConfig;
|
|
161
|
-
|
|
162
145
|
/** 全局请求限流配置(Hook) */
|
|
163
146
|
rateLimit?: RateLimitConfig;
|
|
164
147
|
/** 禁用的钩子列表 */
|
|
@@ -166,12 +149,12 @@ export interface BeflyOptions {
|
|
|
166
149
|
/** 禁用的插件列表 */
|
|
167
150
|
disablePlugins?: string[];
|
|
168
151
|
/**
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
152
|
+
* 禁用的菜单 path 规则(用于菜单同步与加载前过滤)
|
|
153
|
+
*
|
|
154
|
+
* 仅支持 Bun.Glob 的 glob pattern 语法与 API(即把每条规则当作 glob 模式匹配菜单 path)。
|
|
155
|
+
* 示例:
|
|
156
|
+
* - 精确:"/addon/admin/login"
|
|
157
|
+
* - 通配:"/addon/admin/log/*"、"/addon/admin/**"、"**\/login"
|
|
175
158
|
*/
|
|
176
159
|
disableMenus?: string[];
|
|
177
160
|
/**
|
|
@@ -182,219 +165,45 @@ export interface BeflyOptions {
|
|
|
182
165
|
/** 其他插件配置 */
|
|
183
166
|
[key: string]: any;
|
|
184
167
|
}
|
|
185
|
-
|
|
186
168
|
/**
|
|
187
169
|
* Befly 应用上下文
|
|
188
170
|
* 包含所有插件挂载的实例
|
|
189
171
|
*/
|
|
190
172
|
export interface BeflyContext extends KeyValue {
|
|
191
|
-
// ========== 核心插件 ==========
|
|
192
173
|
/** 数据库助手 */
|
|
193
174
|
db: DbHelper;
|
|
194
|
-
|
|
195
175
|
/** Redis 助手 */
|
|
196
176
|
redis: RedisHelper;
|
|
197
|
-
|
|
198
177
|
/** 日志器 */
|
|
199
|
-
logger:
|
|
200
|
-
|
|
178
|
+
logger: Logger;
|
|
201
179
|
/** 缓存助手 */
|
|
202
180
|
cache: CacheHelper;
|
|
203
|
-
|
|
204
181
|
/** 工具函数 */
|
|
205
182
|
tool: {
|
|
206
|
-
Yes: (msg: string, data?: any, other?: Record<string, any>) => {
|
|
207
|
-
|
|
183
|
+
Yes: (msg: string, data?: any, other?: Record<string, any>) => {
|
|
184
|
+
code: 0;
|
|
185
|
+
msg: string;
|
|
186
|
+
data: any;
|
|
187
|
+
};
|
|
188
|
+
No: (msg: string, data?: any, other?: Record<string, any>) => {
|
|
189
|
+
code: 1;
|
|
190
|
+
msg: string;
|
|
191
|
+
data: any;
|
|
192
|
+
};
|
|
208
193
|
};
|
|
209
|
-
|
|
210
194
|
/** 加密解密 */
|
|
211
|
-
cipher:
|
|
212
|
-
|
|
195
|
+
cipher: CipherStatic;
|
|
213
196
|
/** JWT 令牌 */
|
|
214
197
|
jwt: Jwt;
|
|
215
|
-
|
|
216
198
|
/** 项目配置 */
|
|
217
199
|
config: BeflyOptions;
|
|
218
|
-
|
|
219
|
-
// ========== 动态插件 ==========
|
|
220
200
|
/** 组件插件:addon_{addonName}_{pluginName} */
|
|
221
201
|
/** 项目插件:app_{pluginName} */
|
|
222
202
|
[key: string]: any;
|
|
223
203
|
}
|
|
224
|
-
|
|
225
|
-
/**
|
|
226
|
-
* Befly 核心类
|
|
227
|
-
*/
|
|
228
|
-
export interface Befly {
|
|
229
|
-
/** API 路由映射 */
|
|
230
|
-
apis: Map<string, ApiRoute>;
|
|
231
|
-
|
|
232
|
-
/** 插件列表 */
|
|
233
|
-
plugins: Plugin[];
|
|
234
|
-
|
|
235
|
-
/** 应用上下文 */
|
|
236
|
-
context: KeyValue;
|
|
237
|
-
|
|
238
|
-
/** 应用选项 */
|
|
239
|
-
appOptions: BeflyOptions;
|
|
240
|
-
|
|
241
|
-
/** 日志器 */
|
|
242
|
-
logger: Logger;
|
|
243
|
-
|
|
244
|
-
/** JWT 工具 */
|
|
245
|
-
jwt: Jwt;
|
|
246
|
-
|
|
247
|
-
/** 验证器 */
|
|
248
|
-
validator: Validator;
|
|
249
|
-
|
|
250
|
-
/** SQL 管理器 */
|
|
251
|
-
sql: DbHelper;
|
|
252
|
-
|
|
253
|
-
/** 加密工具 */
|
|
254
|
-
crypto: Cipher;
|
|
255
|
-
|
|
256
|
-
/** 数据库连接 */
|
|
257
|
-
db: any;
|
|
258
|
-
|
|
259
|
-
/** Redis 连接 */
|
|
260
|
-
redis: any;
|
|
261
|
-
|
|
262
|
-
/**
|
|
263
|
-
* 初始化检查器
|
|
264
|
-
*/
|
|
265
|
-
initCheck(): Promise<void>;
|
|
266
|
-
|
|
267
|
-
/**
|
|
268
|
-
* 加载插件
|
|
269
|
-
*/
|
|
270
|
-
loadPlugins(): Promise<void>;
|
|
271
|
-
|
|
272
|
-
/**
|
|
273
|
-
* 加载 API 路由
|
|
274
|
-
*/
|
|
275
|
-
loadApis(): Promise<void>;
|
|
276
|
-
|
|
277
|
-
/**
|
|
278
|
-
* 启动服务器
|
|
279
|
-
*/
|
|
280
|
-
start(): Promise<void>;
|
|
281
|
-
|
|
282
|
-
/**
|
|
283
|
-
* 停止服务器
|
|
284
|
-
*/
|
|
285
|
-
stop(): Promise<void>;
|
|
286
|
-
|
|
287
|
-
/**
|
|
288
|
-
* 处理请求
|
|
289
|
-
*/
|
|
290
|
-
handleRequest(request: Request): Promise<Response>;
|
|
291
|
-
|
|
292
|
-
/**
|
|
293
|
-
* 注册钩子
|
|
294
|
-
*/
|
|
295
|
-
use(hook: Function): void;
|
|
296
|
-
|
|
297
|
-
/**
|
|
298
|
-
* 获取配置
|
|
299
|
-
*/
|
|
300
|
-
getConfig(key: string): any;
|
|
301
|
-
|
|
302
|
-
/**
|
|
303
|
-
* 设置配置
|
|
304
|
-
*/
|
|
305
|
-
setConfig(key: string, value: any): void;
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
/**
|
|
309
|
-
* Befly 构造函数类型
|
|
310
|
-
*/
|
|
311
|
-
export interface BeflyConstructor {
|
|
312
|
-
new (options?: BeflyOptions): Befly;
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
/**
|
|
316
|
-
* 服务器启动选项
|
|
317
|
-
*/
|
|
318
|
-
export interface ServerOptions {
|
|
319
|
-
/** 主机名 */
|
|
320
|
-
hostname: string;
|
|
321
|
-
|
|
322
|
-
/** 端口 */
|
|
323
|
-
port: number;
|
|
324
|
-
|
|
325
|
-
/** 请求处理函数 */
|
|
326
|
-
fetch: (request: Request) => Promise<Response>;
|
|
327
|
-
|
|
328
|
-
/** 错误处理函数 */
|
|
329
|
-
error?: (error: Error) => Response;
|
|
330
|
-
|
|
331
|
-
/** 开发模式 */
|
|
332
|
-
development?: boolean;
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
/**
|
|
336
|
-
* 服务器实例
|
|
337
|
-
*/
|
|
338
|
-
export interface Server {
|
|
339
|
-
/** 主机名 */
|
|
340
|
-
hostname: string;
|
|
341
|
-
|
|
342
|
-
/** 端口 */
|
|
343
|
-
port: number;
|
|
344
|
-
|
|
345
|
-
/** 停止服务器 */
|
|
346
|
-
stop(): Promise<void>;
|
|
347
|
-
|
|
348
|
-
/** 重启服务器 */
|
|
349
|
-
reload(options: ServerOptions): void;
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
/**
|
|
353
|
-
* 检查函数类型
|
|
354
|
-
*/
|
|
355
|
-
export type CheckFunction = (befly: Befly) => Promise<void> | void;
|
|
356
|
-
|
|
357
|
-
/**
|
|
358
|
-
* 检查结果
|
|
359
|
-
*/
|
|
360
|
-
export interface CheckResult {
|
|
361
|
-
/** 文件名 */
|
|
362
|
-
filename: string;
|
|
363
|
-
|
|
364
|
-
/** 检查名称 */
|
|
365
|
-
checkName: string;
|
|
366
|
-
|
|
367
|
-
/** 是否通过 */
|
|
368
|
-
passed: boolean;
|
|
369
|
-
|
|
370
|
-
/** 执行时间(纳秒) */
|
|
371
|
-
duration: number;
|
|
372
|
-
|
|
373
|
-
/** 错误信息 */
|
|
374
|
-
error?: Error;
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
/**
|
|
378
|
-
* 性能统计
|
|
379
|
-
*/
|
|
380
|
-
export interface PerformanceStats {
|
|
381
|
-
/** 启动时间 */
|
|
382
|
-
startTime: number;
|
|
383
|
-
|
|
384
|
-
/** 检查时间 */
|
|
385
|
-
checkTime: number;
|
|
386
|
-
|
|
387
|
-
/** 插件加载时间 */
|
|
388
|
-
pluginTime: number;
|
|
389
|
-
|
|
390
|
-
/** API 加载时间 */
|
|
391
|
-
apiTime: number;
|
|
392
|
-
|
|
393
|
-
/** 总时间 */
|
|
394
|
-
totalTime: number;
|
|
395
|
-
}
|
|
396
|
-
|
|
397
204
|
/**
|
|
398
|
-
*
|
|
205
|
+
* Validator 静态类(注入到 ctx 的用法)
|
|
206
|
+
*
|
|
207
|
+
* 说明:这不是 befly 默认入口的运行时导出;仅用于在框架内部/插件中提供类型约束。
|
|
399
208
|
*/
|
|
400
|
-
export
|
|
209
|
+
export type Validator = ValidatorStatic;
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 缓存助手类型定义
|
|
3
3
|
*/
|
|
4
|
-
|
|
5
|
-
import type {
|
|
6
|
-
|
|
4
|
+
import type { DbHelper } from "./database.ts";
|
|
5
|
+
import type { RedisHelper } from "./redis.ts";
|
|
7
6
|
/**
|
|
8
7
|
* 缓存助手类
|
|
9
8
|
* 负责在服务器启动时缓存接口、菜单和角色权限到 Redis
|
|
@@ -13,54 +12,45 @@ export interface CacheHelper {
|
|
|
13
12
|
* 缓存所有接口到 Redis
|
|
14
13
|
*/
|
|
15
14
|
cacheApis(): Promise<void>;
|
|
16
|
-
|
|
17
15
|
/**
|
|
18
16
|
* 缓存所有菜单到 Redis(从数据库读取)
|
|
19
17
|
*/
|
|
20
18
|
cacheMenus(): Promise<void>;
|
|
21
|
-
|
|
22
19
|
/**
|
|
23
20
|
* 全量重建所有角色的接口权限缓存
|
|
24
21
|
*/
|
|
25
22
|
rebuildRoleApiPermissions(): Promise<void>;
|
|
26
|
-
|
|
27
23
|
/**
|
|
28
24
|
* 增量刷新单个角色的接口权限缓存
|
|
29
25
|
*/
|
|
30
26
|
refreshRoleApiPermissions(roleCode: string, apiPaths: string[]): Promise<void>;
|
|
31
|
-
|
|
32
27
|
/**
|
|
33
28
|
* 缓存所有数据(接口、菜单、角色权限)
|
|
34
29
|
*/
|
|
35
30
|
cacheAll(): Promise<void>;
|
|
36
|
-
|
|
37
31
|
/**
|
|
38
32
|
* 获取缓存的所有接口
|
|
39
33
|
* @returns 接口列表
|
|
40
34
|
*/
|
|
41
35
|
getApis(): Promise<any[]>;
|
|
42
|
-
|
|
43
36
|
/**
|
|
44
37
|
* 获取缓存的所有菜单
|
|
45
38
|
* @returns 菜单列表
|
|
46
39
|
*/
|
|
47
40
|
getMenus(): Promise<any[]>;
|
|
48
|
-
|
|
49
41
|
/**
|
|
50
42
|
* 获取角色的接口权限
|
|
51
43
|
* @param roleCode - 角色代码
|
|
52
44
|
* @returns 接口路径列表
|
|
53
45
|
*/
|
|
54
46
|
getRolePermissions(roleCode: string): Promise<string[]>;
|
|
55
|
-
|
|
56
47
|
/**
|
|
57
48
|
* 检查角色是否有指定接口权限
|
|
58
49
|
* @param roleCode - 角色代码
|
|
59
|
-
|
|
50
|
+
* @param apiPath - 接口路径(url.pathname,例如 /api/user/login;与 method 无关)
|
|
60
51
|
* @returns 是否有权限
|
|
61
52
|
*/
|
|
62
53
|
checkRolePermission(roleCode: string, apiPath: string): Promise<boolean>;
|
|
63
|
-
|
|
64
54
|
/**
|
|
65
55
|
* 删除角色的接口权限缓存
|
|
66
56
|
* @param roleCode - 角色代码
|
|
@@ -68,10 +58,12 @@ export interface CacheHelper {
|
|
|
68
58
|
*/
|
|
69
59
|
deleteRolePermissions(roleCode: string): Promise<boolean>;
|
|
70
60
|
}
|
|
71
|
-
|
|
72
61
|
/**
|
|
73
62
|
* CacheHelper 构造函数类型
|
|
74
63
|
*/
|
|
75
64
|
export interface CacheHelperConstructor {
|
|
76
|
-
new (
|
|
65
|
+
new (deps: {
|
|
66
|
+
db: DbHelper;
|
|
67
|
+
redis: RedisHelper;
|
|
68
|
+
}): CacheHelper;
|
|
77
69
|
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cipher 类型定义(对外)。
|
|
3
|
+
*
|
|
4
|
+
* 说明:runtime 实现位于 core 内部(dist/lib),但对外类型应只从 `befly/types/*` 获取。
|
|
5
|
+
* 此处仅描述 Befly 注入的 `befly.cipher`(静态方法集合)。
|
|
6
|
+
*/
|
|
7
|
+
import type { EncodingType, HashAlgorithm, PasswordHashOptions } from "./crypto.ts";
|
|
8
|
+
export interface CipherStatic {
|
|
9
|
+
md5(data: string | Uint8Array, encoding?: EncodingType): string;
|
|
10
|
+
hmacMd5(key: string | Uint8Array, data: string | Uint8Array, encoding?: EncodingType): string;
|
|
11
|
+
sha1(data: string | Uint8Array, encoding?: EncodingType): string;
|
|
12
|
+
hmacSha1(key: string | Uint8Array, data: string | Uint8Array, encoding?: EncodingType): string;
|
|
13
|
+
sha256(data: string | Uint8Array, encoding?: EncodingType): string;
|
|
14
|
+
hmacSha256(key: string | Uint8Array, data: string | Uint8Array, encoding?: EncodingType): string;
|
|
15
|
+
sha512(data: string | Uint8Array, encoding?: EncodingType): string;
|
|
16
|
+
hmacSha512(key: string | Uint8Array, data: string | Uint8Array, encoding?: EncodingType): string;
|
|
17
|
+
rsaSha256(data: string, privateKey: string | Buffer, encoding?: "hex" | "base64" | "base64url"): string;
|
|
18
|
+
hash(algorithm: HashAlgorithm, data: string | Uint8Array, encoding?: EncodingType): string;
|
|
19
|
+
hmac(algorithm: HashAlgorithm, key: string | Uint8Array, data: string | Uint8Array, encoding?: EncodingType): string;
|
|
20
|
+
hashFile(filePath: string, algorithm?: HashAlgorithm, encoding?: EncodingType): Promise<string>;
|
|
21
|
+
hashPassword(password: string, options?: PasswordHashOptions): Promise<string>;
|
|
22
|
+
verifyPassword(password: string, hash: string): Promise<boolean>;
|
|
23
|
+
base64Encode(data: string): string;
|
|
24
|
+
base64Decode(data: string): string;
|
|
25
|
+
randomString(length: number): string;
|
|
26
|
+
fastHash(data: string | Uint8Array, seed?: number): number;
|
|
27
|
+
}
|