befly 3.10.18 → 3.11.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.
Files changed (223) hide show
  1. package/README.md +83 -307
  2. package/dist/befly.config.d.ts +7 -0
  3. package/{befly.config.ts → dist/befly.config.js} +11 -36
  4. package/dist/befly.js +15621 -0
  5. package/dist/befly.min.js +21 -0
  6. package/dist/checks/checkApi.d.ts +1 -0
  7. package/{checks/checkApi.ts → dist/checks/checkApi.js} +12 -30
  8. package/dist/checks/checkHook.d.ts +1 -0
  9. package/dist/checks/checkHook.js +86 -0
  10. package/dist/checks/checkMenu.d.ts +7 -0
  11. package/{checks/checkMenu.ts → dist/checks/checkMenu.js} +18 -53
  12. package/dist/checks/checkPlugin.d.ts +1 -0
  13. package/dist/checks/checkPlugin.js +86 -0
  14. package/dist/checks/checkTable.d.ts +6 -0
  15. package/{checks/checkTable.ts → dist/checks/checkTable.js} +17 -41
  16. package/dist/configs/presetFields.d.ts +4 -0
  17. package/{configs/presetFields.ts → dist/configs/presetFields.js} +1 -1
  18. package/dist/configs/presetRegexp.d.ts +145 -0
  19. package/{utils/regex.ts → dist/configs/presetRegexp.js} +8 -31
  20. package/dist/hooks/auth.d.ts +7 -0
  21. package/{hooks/auth.ts → dist/hooks/auth.js} +8 -10
  22. package/dist/hooks/cors.d.ts +11 -0
  23. package/{hooks/cors.ts → dist/hooks/cors.js} +5 -13
  24. package/dist/hooks/parser.d.ts +14 -0
  25. package/{hooks/parser.ts → dist/hooks/parser.js} +31 -45
  26. package/dist/hooks/permission.d.ts +14 -0
  27. package/{hooks/permission.ts → dist/hooks/permission.js} +16 -25
  28. package/dist/hooks/validator.d.ts +11 -0
  29. package/{hooks/validator.ts → dist/hooks/validator.js} +9 -14
  30. package/dist/index.d.ts +26 -0
  31. package/{main.ts → dist/index.js} +61 -100
  32. package/dist/lib/asyncContext.d.ts +21 -0
  33. package/dist/lib/asyncContext.js +27 -0
  34. package/dist/lib/cacheHelper.d.ts +95 -0
  35. package/{lib/cacheHelper.ts → dist/lib/cacheHelper.js} +45 -105
  36. package/dist/lib/cacheKeys.d.ts +23 -0
  37. package/{lib/cacheKeys.ts → dist/lib/cacheKeys.js} +5 -10
  38. package/dist/lib/cipher.d.ts +153 -0
  39. package/{lib/cipher.ts → dist/lib/cipher.js} +23 -44
  40. package/dist/lib/connect.d.ts +91 -0
  41. package/{lib/connect.ts → dist/lib/connect.js} +47 -88
  42. package/dist/lib/dbDialect.d.ts +87 -0
  43. package/{lib/dbDialect.ts → dist/lib/dbDialect.js} +32 -112
  44. package/dist/lib/dbHelper.d.ts +204 -0
  45. package/{lib/dbHelper.ts → dist/lib/dbHelper.js} +82 -241
  46. package/dist/lib/dbUtils.d.ts +68 -0
  47. package/{lib/dbUtils.ts → dist/lib/dbUtils.js} +51 -126
  48. package/dist/lib/jwt.d.ts +13 -0
  49. package/{lib/jwt.ts → dist/lib/jwt.js} +11 -32
  50. package/dist/lib/logger.d.ts +42 -0
  51. package/dist/lib/logger.js +1144 -0
  52. package/dist/lib/redisHelper.d.ts +185 -0
  53. package/{lib/redisHelper.ts → dist/lib/redisHelper.js} +97 -141
  54. package/dist/lib/sqlBuilder.d.ts +160 -0
  55. package/{lib/sqlBuilder.ts → dist/lib/sqlBuilder.js} +132 -278
  56. package/dist/lib/sqlCheck.d.ts +23 -0
  57. package/{lib/sqlCheck.ts → dist/lib/sqlCheck.js} +24 -41
  58. package/dist/lib/validator.d.ts +45 -0
  59. package/{lib/validator.ts → dist/lib/validator.js} +44 -61
  60. package/dist/loader/loadApis.d.ts +12 -0
  61. package/{loader/loadApis.ts → dist/loader/loadApis.js} +10 -20
  62. package/dist/loader/loadHooks.d.ts +7 -0
  63. package/dist/loader/loadHooks.js +35 -0
  64. package/dist/loader/loadPlugins.d.ts +8 -0
  65. package/{loader/loadPlugins.ts → dist/loader/loadPlugins.js} +14 -26
  66. package/dist/paths.d.ts +93 -0
  67. package/{paths.ts → dist/paths.js} +6 -19
  68. package/dist/plugins/cache.d.ts +16 -0
  69. package/{plugins/cache.ts → dist/plugins/cache.js} +7 -12
  70. package/dist/plugins/cipher.d.ts +12 -0
  71. package/{plugins/cipher.ts → dist/plugins/cipher.js} +4 -6
  72. package/dist/plugins/config.d.ts +12 -0
  73. package/dist/plugins/config.js +8 -0
  74. package/dist/plugins/db.d.ts +16 -0
  75. package/{plugins/db.ts → dist/plugins/db.js} +11 -17
  76. package/dist/plugins/jwt.d.ts +12 -0
  77. package/dist/plugins/jwt.js +12 -0
  78. package/dist/plugins/logger.d.ts +32 -0
  79. package/{plugins/logger.ts → dist/plugins/logger.js} +5 -8
  80. package/dist/plugins/redis.d.ts +16 -0
  81. package/{plugins/redis.ts → dist/plugins/redis.js} +9 -12
  82. package/dist/plugins/tool.d.ts +81 -0
  83. package/{plugins/tool.ts → dist/plugins/tool.js} +9 -30
  84. package/dist/router/api.d.ts +14 -0
  85. package/dist/router/api.js +107 -0
  86. package/dist/router/static.d.ts +9 -0
  87. package/{router/static.ts → dist/router/static.js} +20 -34
  88. package/dist/scripts/ensureDist.d.ts +1 -0
  89. package/dist/scripts/ensureDist.js +296 -0
  90. package/dist/sync/syncApi.d.ts +3 -0
  91. package/{sync/syncApi.ts → dist/sync/syncApi.js} +35 -55
  92. package/dist/sync/syncCache.d.ts +2 -0
  93. package/{sync/syncCache.ts → dist/sync/syncCache.js} +1 -6
  94. package/dist/sync/syncDev.d.ts +6 -0
  95. package/{sync/syncDev.ts → dist/sync/syncDev.js} +29 -62
  96. package/dist/sync/syncMenu.d.ts +14 -0
  97. package/{sync/syncMenu.ts → dist/sync/syncMenu.js} +65 -125
  98. package/dist/sync/syncTable.d.ts +151 -0
  99. package/{sync/syncTable.ts → dist/sync/syncTable.js} +172 -379
  100. package/{types → dist/types}/api.d.ts +12 -51
  101. package/dist/types/api.js +4 -0
  102. package/{types → dist/types}/befly.d.ts +32 -227
  103. package/dist/types/befly.js +4 -0
  104. package/{types → dist/types}/cache.d.ts +7 -15
  105. package/dist/types/cache.js +4 -0
  106. package/dist/types/cipher.d.ts +27 -0
  107. package/dist/types/cipher.js +7 -0
  108. package/{types → dist/types}/common.d.ts +8 -33
  109. package/dist/types/common.js +5 -0
  110. package/{types → dist/types}/context.d.ts +3 -5
  111. package/dist/types/context.js +4 -0
  112. package/{types → dist/types}/crypto.d.ts +0 -3
  113. package/dist/types/crypto.js +4 -0
  114. package/dist/types/database.d.ts +138 -0
  115. package/dist/types/database.js +4 -0
  116. package/dist/types/hook.d.ts +17 -0
  117. package/dist/types/hook.js +6 -0
  118. package/dist/types/jwt.d.ts +75 -0
  119. package/dist/types/jwt.js +4 -0
  120. package/dist/types/logger.d.ts +59 -0
  121. package/dist/types/logger.js +6 -0
  122. package/dist/types/plugin.d.ts +16 -0
  123. package/dist/types/plugin.js +6 -0
  124. package/dist/types/redis.d.ts +71 -0
  125. package/dist/types/redis.js +4 -0
  126. package/{types/roleApisCache.ts → dist/types/roleApisCache.d.ts} +0 -2
  127. package/dist/types/roleApisCache.js +8 -0
  128. package/dist/types/sync.d.ts +92 -0
  129. package/dist/types/sync.js +4 -0
  130. package/dist/types/table.d.ts +34 -0
  131. package/dist/types/table.js +4 -0
  132. package/dist/types/validate.d.ts +67 -0
  133. package/dist/types/validate.js +4 -0
  134. package/dist/utils/calcPerfTime.d.ts +4 -0
  135. package/{utils/calcPerfTime.ts → dist/utils/calcPerfTime.js} +3 -3
  136. package/dist/utils/convertBigIntFields.d.ts +11 -0
  137. package/{utils/convertBigIntFields.ts → dist/utils/convertBigIntFields.js} +5 -9
  138. package/dist/utils/cors.d.ts +8 -0
  139. package/{utils/cors.ts → dist/utils/cors.js} +1 -3
  140. package/dist/utils/disableMenusGlob.d.ts +13 -0
  141. package/{utils/disableMenusGlob.ts → dist/utils/disableMenusGlob.js} +9 -29
  142. package/dist/utils/fieldClear.d.ts +11 -0
  143. package/{utils/fieldClear.ts → dist/utils/fieldClear.js} +15 -33
  144. package/dist/utils/getClientIp.d.ts +6 -0
  145. package/{utils/getClientIp.ts → dist/utils/getClientIp.js} +1 -7
  146. package/dist/utils/importDefault.d.ts +1 -0
  147. package/dist/utils/importDefault.js +29 -0
  148. package/dist/utils/isDirentDirectory.d.ts +2 -0
  149. package/{utils/isDirentDirectory.ts → dist/utils/isDirentDirectory.js} +3 -8
  150. package/dist/utils/loadMenuConfigs.d.ts +29 -0
  151. package/{utils/loadMenuConfigs.ts → dist/utils/loadMenuConfigs.js} +66 -52
  152. package/dist/utils/mergeAndConcat.d.ts +7 -0
  153. package/dist/utils/mergeAndConcat.js +72 -0
  154. package/dist/utils/processAtSymbol.d.ts +4 -0
  155. package/{utils/processFields.ts → dist/utils/processAtSymbol.js} +5 -9
  156. package/dist/utils/processInfo.d.ts +24 -0
  157. package/{utils/process.ts → dist/utils/processInfo.js} +2 -18
  158. package/dist/utils/response.d.ts +20 -0
  159. package/{utils/response.ts → dist/utils/response.js} +28 -49
  160. package/dist/utils/scanAddons.d.ts +17 -0
  161. package/{utils/scanAddons.ts → dist/utils/scanAddons.js} +7 -41
  162. package/dist/utils/scanConfig.d.ts +26 -0
  163. package/{utils/scanConfig.ts → dist/utils/scanConfig.js} +28 -66
  164. package/dist/utils/scanCoreBuiltins.d.ts +3 -0
  165. package/dist/utils/scanCoreBuiltins.js +65 -0
  166. package/dist/utils/scanFiles.d.ts +30 -0
  167. package/{utils/scanFiles.ts → dist/utils/scanFiles.js} +44 -71
  168. package/dist/utils/scanSources.d.ts +10 -0
  169. package/dist/utils/scanSources.js +46 -0
  170. package/dist/utils/sortModules.d.ts +28 -0
  171. package/{utils/sortModules.ts → dist/utils/sortModules.js} +26 -66
  172. package/dist/utils/util.d.ts +84 -0
  173. package/dist/utils/util.js +262 -0
  174. package/package.json +26 -34
  175. package/.gitignore +0 -0
  176. package/bunfig.toml +0 -3
  177. package/checks/checkHook.ts +0 -48
  178. package/checks/checkPlugin.ts +0 -48
  179. package/configs/presetRegexp.ts +0 -225
  180. package/docs/README.md +0 -98
  181. package/docs/api/api.md +0 -1921
  182. package/docs/guide/examples.md +0 -926
  183. package/docs/guide/quickstart.md +0 -354
  184. package/docs/hooks/auth.md +0 -38
  185. package/docs/hooks/cors.md +0 -28
  186. package/docs/hooks/hook.md +0 -838
  187. package/docs/hooks/parser.md +0 -19
  188. package/docs/hooks/rateLimit.md +0 -47
  189. package/docs/infra/redis.md +0 -628
  190. package/docs/plugins/cipher.md +0 -61
  191. package/docs/plugins/database.md +0 -189
  192. package/docs/plugins/plugin.md +0 -986
  193. package/docs/reference/addon.md +0 -510
  194. package/docs/reference/config.md +0 -573
  195. package/docs/reference/logger.md +0 -495
  196. package/docs/reference/sync.md +0 -478
  197. package/docs/reference/table.md +0 -763
  198. package/docs/reference/validator.md +0 -620
  199. package/lib/asyncContext.ts +0 -43
  200. package/lib/logger.ts +0 -811
  201. package/loader/loadHooks.ts +0 -51
  202. package/plugins/config.ts +0 -13
  203. package/plugins/jwt.ts +0 -15
  204. package/router/api.ts +0 -130
  205. package/tsconfig.json +0 -8
  206. package/types/database.d.ts +0 -541
  207. package/types/hook.d.ts +0 -25
  208. package/types/jwt.d.ts +0 -118
  209. package/types/logger.d.ts +0 -65
  210. package/types/plugin.d.ts +0 -19
  211. package/types/redis.d.ts +0 -83
  212. package/types/sync.d.ts +0 -398
  213. package/types/table.d.ts +0 -216
  214. package/types/validate.d.ts +0 -69
  215. package/utils/arrayKeysToCamel.ts +0 -18
  216. package/utils/configTypes.ts +0 -3
  217. package/utils/genShortId.ts +0 -12
  218. package/utils/importDefault.ts +0 -21
  219. package/utils/keysToCamel.ts +0 -22
  220. package/utils/keysToSnake.ts +0 -22
  221. package/utils/pickFields.ts +0 -19
  222. package/utils/scanSources.ts +0 -64
  223. package/utils/sqlLog.ts +0 -37
@@ -1,18 +1,15 @@
1
1
  /**
2
2
  * Befly API 类型定义
3
3
  */
4
-
5
- import type { BeflyContext } from "./befly.ts";
6
- import type { KeyValue } from "./common.ts";
7
- import type { RequestContext } from "./context.ts";
8
- import type { TableDefinition } from "./validate.ts";
9
-
4
+ import type { BeflyContext } from "./befly";
5
+ import type { KeyValue } from "./common";
6
+ import type { RequestContext } from "./context";
7
+ import type { TableDefinition } from "./validate";
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<T = any, R = any> = (befly: BeflyContext, ctx: RequestContext) => Promise<Response | R> | Response | R;
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<T = any, R = any> {
59
+ export interface ApiRoute<TBody = any, R = any> {
68
60
  /** 接口名称(必填) */
69
61
  name: string;
70
-
71
62
  /** 处理器函数(必填) */
72
- handler: ApiHandler<T, R>;
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
  */
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Befly API 类型定义
3
+ */
4
+ export {};
@@ -1,22 +1,15 @@
1
1
  /**
2
2
  * Befly 核心框架类型定义
3
3
  */
4
-
5
- import type { CacheHelper } from "../lib/cacheHelper.ts";
6
- import type { Cipher } from "../lib/cipher.ts";
7
- import type { DbHelper } from "../lib/dbHelper.ts";
8
- import type { Jwt } from "../lib/jwt.ts";
9
- import type { Logger } from "../lib/logger.ts";
10
- import type { RedisHelper } from "../lib/redisHelper.ts";
11
- import type { Validator } from "../lib/validator.ts";
12
- import type { ApiRoute, HttpMethod } from "./api.ts";
13
- import type { KeyValue } from "./common.ts";
14
- import type { LoggerConfig } from "./logger.ts";
15
- import type { Plugin } from "./plugin.ts";
16
- import type { Database } from "bun:sqlite";
17
-
4
+ import type { CacheHelper } from "./cache";
5
+ import type { CipherStatic } from "./cipher";
6
+ import type { KeyValue } from "./common";
7
+ import type { DbHelper } from "./database";
8
+ import type { Jwt } from "./jwt";
9
+ import type { Logger, LoggerConfig } from "./logger";
10
+ import type { RedisHelper } from "./redis";
11
+ import type { ValidatorStatic } from "./validate";
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
- * - 精确:"/api/auth/login"
97
- * - 前缀:"/api/auth/*"
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,20 +142,15 @@ export interface BeflyOptions {
158
142
  auth?: AuthConfig;
159
143
  /** CORS 插件配置 */
160
144
  cors?: CorsConfig;
161
-
162
145
  /** 全局请求限流配置(Hook) */
163
146
  rateLimit?: RateLimitConfig;
164
- /** 禁用的钩子列表 */
165
- disableHooks?: string[];
166
- /** 禁用的插件列表 */
167
- disablePlugins?: string[];
168
147
  /**
169
- * 禁用的菜单 path 规则(用于菜单同步与加载前过滤)
170
- *
171
- * 仅支持 Bun.Glob 的 glob pattern 语法与 API(即把每条规则当作 glob 模式匹配菜单 path)。
172
- * 示例:
173
- * - 精确:"/addon/admin/login"
174
- * - 通配:"/addon/admin/log/*"、"/addon/admin/**"、"**\/login"
148
+ * 禁用的菜单 path 规则(用于菜单同步与加载前过滤)
149
+ *
150
+ * 仅支持 Bun.Glob 的 glob pattern 语法与 API(即把每条规则当作 glob 模式匹配菜单 path)。
151
+ * 示例:
152
+ * - 精确:"/addon/admin/login"
153
+ * - 通配:"/addon/admin/log/*"、"/addon/admin/**"、"**\/login"
175
154
  */
176
155
  disableMenus?: string[];
177
156
  /**
@@ -182,219 +161,45 @@ export interface BeflyOptions {
182
161
  /** 其他插件配置 */
183
162
  [key: string]: any;
184
163
  }
185
-
186
164
  /**
187
165
  * Befly 应用上下文
188
166
  * 包含所有插件挂载的实例
189
167
  */
190
168
  export interface BeflyContext extends KeyValue {
191
- // ========== 核心插件 ==========
192
169
  /** 数据库助手 */
193
170
  db: DbHelper;
194
-
195
171
  /** Redis 助手 */
196
172
  redis: RedisHelper;
197
-
198
173
  /** 日志器 */
199
- logger: typeof Logger;
200
-
174
+ logger: Logger;
201
175
  /** 缓存助手 */
202
176
  cache: CacheHelper;
203
-
204
177
  /** 工具函数 */
205
178
  tool: {
206
- Yes: (msg: string, data?: any, other?: Record<string, any>) => { code: 0; msg: string; data: any };
207
- No: (msg: string, data?: any, other?: Record<string, any>) => { code: 1; msg: string; data: any };
179
+ Yes: (msg: string, data?: any, other?: Record<string, any>) => {
180
+ code: 0;
181
+ msg: string;
182
+ data: any;
183
+ };
184
+ No: (msg: string, data?: any, other?: Record<string, any>) => {
185
+ code: 1;
186
+ msg: string;
187
+ data: any;
188
+ };
208
189
  };
209
-
210
190
  /** 加密解密 */
211
- cipher: typeof Cipher;
212
-
191
+ cipher: CipherStatic;
213
192
  /** JWT 令牌 */
214
193
  jwt: Jwt;
215
-
216
194
  /** 项目配置 */
217
195
  config: BeflyOptions;
218
-
219
- // ========== 动态插件 ==========
220
196
  /** 组件插件:addon_{addonName}_{pluginName} */
221
197
  /** 项目插件:app_{pluginName} */
222
198
  [key: string]: any;
223
199
  }
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
200
  /**
398
- * 导出 Befly 实例创建函数
201
+ * Validator 静态类(注入到 ctx 的用法)
202
+ *
203
+ * 说明:这不是 befly 默认入口的运行时导出;仅用于在框架内部/插件中提供类型约束。
399
204
  */
400
- export function createBefly(options?: BeflyOptions): Befly;
205
+ export type Validator = ValidatorStatic;
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Befly 核心框架类型定义
3
+ */
4
+ export {};
@@ -1,9 +1,8 @@
1
1
  /**
2
2
  * 缓存助手类型定义
3
3
  */
4
-
5
- import type { BeflyContext } from "./befly.ts";
6
-
4
+ import type { DbHelper } from "./database";
5
+ import type { RedisHelper } from "./redis";
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
- * @param apiPath - 接口路径(url.pathname,例如 /api/user/login;与 method 无关)
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 (befly: BeflyContext): CacheHelper;
65
+ new (deps: {
66
+ db: DbHelper;
67
+ redis: RedisHelper;
68
+ }): CacheHelper;
77
69
  }
@@ -0,0 +1,4 @@
1
+ /**
2
+ * 缓存助手类型定义
3
+ */
4
+ export {};
@@ -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";
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
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Cipher 类型定义(对外)。
3
+ *
4
+ * 说明:runtime 实现位于 core 内部(dist/lib),但对外类型应只从 `befly/types/*` 获取。
5
+ * 此处仅描述 Befly 注入的 `befly.cipher`(静态方法集合)。
6
+ */
7
+ export {};