befly 3.9.37 → 3.9.39

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 (155) hide show
  1. package/README.md +38 -39
  2. package/befly.config.ts +62 -40
  3. package/checks/checkApi.ts +16 -16
  4. package/checks/checkApp.ts +19 -25
  5. package/checks/checkTable.ts +42 -42
  6. package/docs/README.md +42 -35
  7. package/docs/{api.md → api/api.md} +225 -235
  8. package/docs/cipher.md +71 -69
  9. package/docs/database.md +155 -153
  10. package/docs/{examples.md → guide/examples.md} +181 -181
  11. package/docs/guide/quickstart.md +331 -0
  12. package/docs/hooks/auth.md +38 -0
  13. package/docs/hooks/cors.md +28 -0
  14. package/docs/{hook.md → hooks/hook.md} +140 -57
  15. package/docs/hooks/parser.md +19 -0
  16. package/docs/hooks/rateLimit.md +47 -0
  17. package/docs/{redis.md → infra/redis.md} +84 -93
  18. package/docs/plugins/cipher.md +61 -0
  19. package/docs/plugins/database.md +128 -0
  20. package/docs/{plugin.md → plugins/plugin.md} +83 -81
  21. package/docs/quickstart.md +26 -26
  22. package/docs/{addon.md → reference/addon.md} +46 -46
  23. package/docs/{config.md → reference/config.md} +32 -80
  24. package/docs/{logger.md → reference/logger.md} +52 -52
  25. package/docs/{sync.md → reference/sync.md} +32 -35
  26. package/docs/{table.md → reference/table.md} +7 -7
  27. package/docs/{validator.md → reference/validator.md} +57 -57
  28. package/hooks/auth.ts +8 -4
  29. package/hooks/cors.ts +13 -13
  30. package/hooks/parser.ts +37 -17
  31. package/hooks/permission.ts +26 -14
  32. package/hooks/rateLimit.ts +276 -0
  33. package/hooks/validator.ts +15 -7
  34. package/lib/asyncContext.ts +43 -0
  35. package/lib/cacheHelper.ts +212 -81
  36. package/lib/cacheKeys.ts +38 -0
  37. package/lib/cipher.ts +30 -30
  38. package/lib/connect.ts +28 -28
  39. package/lib/dbHelper.ts +211 -109
  40. package/lib/jwt.ts +16 -16
  41. package/lib/logger.ts +610 -19
  42. package/lib/redisHelper.ts +185 -44
  43. package/lib/sqlBuilder.ts +90 -91
  44. package/lib/validator.ts +59 -39
  45. package/loader/loadApis.ts +53 -47
  46. package/loader/loadHooks.ts +40 -14
  47. package/loader/loadPlugins.ts +16 -17
  48. package/main.ts +57 -47
  49. package/package.json +47 -45
  50. package/paths.ts +15 -14
  51. package/plugins/cache.ts +5 -4
  52. package/plugins/cipher.ts +3 -3
  53. package/plugins/config.ts +2 -2
  54. package/plugins/db.ts +9 -9
  55. package/plugins/jwt.ts +3 -3
  56. package/plugins/logger.ts +8 -12
  57. package/plugins/redis.ts +8 -8
  58. package/plugins/tool.ts +6 -6
  59. package/router/api.ts +85 -56
  60. package/router/static.ts +12 -12
  61. package/sync/syncAll.ts +12 -12
  62. package/sync/syncApi.ts +55 -54
  63. package/sync/syncDb/apply.ts +20 -19
  64. package/sync/syncDb/constants.ts +25 -23
  65. package/sync/syncDb/ddl.ts +35 -36
  66. package/sync/syncDb/helpers.ts +6 -9
  67. package/sync/syncDb/schema.ts +10 -9
  68. package/sync/syncDb/sqlite.ts +7 -8
  69. package/sync/syncDb/table.ts +37 -35
  70. package/sync/syncDb/tableCreate.ts +21 -20
  71. package/sync/syncDb/types.ts +23 -20
  72. package/sync/syncDb/version.ts +10 -10
  73. package/sync/syncDb.ts +43 -36
  74. package/sync/syncDev.ts +74 -66
  75. package/sync/syncMenu.ts +190 -57
  76. package/tests/api-integration-array-number.test.ts +282 -0
  77. package/tests/befly-config-env.test.ts +78 -0
  78. package/tests/cacheHelper.test.ts +135 -104
  79. package/tests/cacheKeys.test.ts +41 -0
  80. package/tests/cipher.test.ts +90 -89
  81. package/tests/dbHelper-advanced.test.ts +140 -134
  82. package/tests/dbHelper-all-array-types.test.ts +316 -0
  83. package/tests/dbHelper-array-serialization.test.ts +258 -0
  84. package/tests/dbHelper-columns.test.ts +56 -55
  85. package/tests/dbHelper-execute.test.ts +45 -44
  86. package/tests/dbHelper-joins.test.ts +124 -119
  87. package/tests/fields-redis-cache.test.ts +29 -27
  88. package/tests/fields-validate.test.ts +38 -38
  89. package/tests/getClientIp.test.ts +54 -0
  90. package/tests/integration.test.ts +69 -67
  91. package/tests/jwt.test.ts +27 -26
  92. package/tests/logger.test.ts +267 -34
  93. package/tests/rateLimit-hook.test.ts +477 -0
  94. package/tests/redisHelper.test.ts +187 -188
  95. package/tests/redisKeys.test.ts +6 -73
  96. package/tests/scanConfig.test.ts +144 -0
  97. package/tests/sqlBuilder-advanced.test.ts +217 -215
  98. package/tests/sqlBuilder.test.ts +92 -91
  99. package/tests/sync-connection.test.ts +29 -29
  100. package/tests/syncDb-apply.test.ts +97 -96
  101. package/tests/syncDb-array-number.test.ts +160 -0
  102. package/tests/syncDb-constants.test.ts +48 -47
  103. package/tests/syncDb-ddl.test.ts +99 -98
  104. package/tests/syncDb-helpers.test.ts +29 -28
  105. package/tests/syncDb-schema.test.ts +61 -60
  106. package/tests/syncDb-types.test.ts +60 -59
  107. package/tests/syncMenu-paths.test.ts +68 -0
  108. package/tests/util.test.ts +42 -41
  109. package/tests/validator-array-number.test.ts +310 -0
  110. package/tests/validator-default.test.ts +373 -0
  111. package/tests/validator.test.ts +271 -266
  112. package/tsconfig.json +4 -5
  113. package/types/api.d.ts +7 -12
  114. package/types/befly.d.ts +60 -13
  115. package/types/cache.d.ts +8 -4
  116. package/types/common.d.ts +17 -9
  117. package/types/context.d.ts +2 -2
  118. package/types/crypto.d.ts +23 -0
  119. package/types/database.d.ts +19 -19
  120. package/types/hook.d.ts +2 -2
  121. package/types/jwt.d.ts +118 -0
  122. package/types/logger.d.ts +30 -0
  123. package/types/plugin.d.ts +4 -4
  124. package/types/redis.d.ts +7 -3
  125. package/types/roleApisCache.ts +23 -0
  126. package/types/sync.d.ts +10 -10
  127. package/types/table.d.ts +50 -9
  128. package/types/validate.d.ts +69 -0
  129. package/utils/addonHelper.ts +90 -0
  130. package/utils/arrayKeysToCamel.ts +18 -0
  131. package/utils/calcPerfTime.ts +13 -0
  132. package/utils/configTypes.ts +3 -0
  133. package/utils/cors.ts +19 -0
  134. package/utils/fieldClear.ts +75 -0
  135. package/utils/genShortId.ts +12 -0
  136. package/utils/getClientIp.ts +45 -0
  137. package/utils/keysToCamel.ts +22 -0
  138. package/utils/keysToSnake.ts +22 -0
  139. package/utils/modules.ts +98 -0
  140. package/utils/pickFields.ts +19 -0
  141. package/utils/process.ts +56 -0
  142. package/utils/regex.ts +225 -0
  143. package/utils/response.ts +115 -0
  144. package/utils/route.ts +23 -0
  145. package/utils/scanConfig.ts +142 -0
  146. package/utils/scanFiles.ts +48 -0
  147. package/.prettierignore +0 -2
  148. package/.prettierrc +0 -12
  149. package/docs/1-/345/237/272/346/234/254/344/273/213/347/273/215.md +0 -35
  150. package/docs/2-/345/210/235/346/255/245/344/275/223/351/252/214.md +0 -64
  151. package/docs/3-/347/254/254/344/270/200/344/270/252/346/216/245/345/217/243.md +0 -46
  152. package/docs/4-/346/223/215/344/275/234/346/225/260/346/215/256/345/272/223.md +0 -172
  153. package/hooks/requestLogger.ts +0 -84
  154. package/types/index.ts +0 -24
  155. package/util.ts +0 -283
package/tsconfig.json CHANGED
@@ -1,4 +1,5 @@
1
1
  {
2
+ "extends": "../../tsconfig.base.json",
2
3
  "compilerOptions": {
3
4
  // 基础配置
4
5
  "target": "ESNext",
@@ -39,9 +40,7 @@
39
40
 
40
41
  // 高级
41
42
  "skipLibCheck": true,
42
- "allowJs": true,
43
- "checkJs": false,
44
-
43
+ "allowJs": false,
45
44
  // Bun 特定
46
45
  "types": ["bun"],
47
46
 
@@ -50,6 +49,6 @@
50
49
  "@/*": ["./*"]
51
50
  }
52
51
  },
53
- "include": ["**/*.ts", "**/*.js"],
54
- "exclude": ["node_modules", "dist", "logs", "temp", "main.single.ts", "**/*.test.ts", "**/*.test.js"]
52
+ "include": ["**/*.ts"],
53
+ "exclude": ["node_modules", "dist", "logs", "temp", "tests", "main.single.ts"]
55
54
  }
package/types/api.d.ts CHANGED
@@ -1,16 +1,17 @@
1
- /**
1
+ /**
2
2
  * Befly API 类型定义
3
3
  */
4
4
 
5
- import type { BeflyContext } from './befly.js';
6
- import type { KeyValue, TableDefinition } from './common.js';
7
- import type { RequestContext } from './context.js';
5
+ import type { BeflyContext } from "./befly.js";
6
+ import type { KeyValue } from "./common.js";
7
+ import type { RequestContext } from "./context.js";
8
+ import type { TableDefinition } from "./validate.js";
8
9
 
9
10
  /**
10
11
  * HTTP 方法类型
11
12
  * 支持 GET、POST 或逗号分隔的组合
12
13
  */
13
- export type HttpMethod = 'GET' | 'POST' | 'GET,POST' | 'POST,GET';
14
+ export type HttpMethod = "GET" | "POST" | "GET,POST" | "POST,GET";
14
15
 
15
16
  /**
16
17
  * 用户信息类型
@@ -31,7 +32,7 @@ export interface UserInfo {
31
32
  * - 'user': 需要普通用户权限
32
33
  * - string[]: 需要特定角色
33
34
  */
34
- export type AuthType = boolean | 'admin' | 'user' | string[];
35
+ export type AuthType = boolean | "admin" | "user" | string[];
35
36
 
36
37
  /**
37
38
  * API 处理器函数类型
@@ -91,12 +92,6 @@ export interface ApiRoute<T = any, R = any> {
91
92
  */
92
93
  rawBody?: boolean;
93
94
 
94
- /** 缓存配置(可选,单位:秒) */
95
- cache?: number;
96
-
97
- /** 限流配置(可选,格式:次数/秒,如 "10/60" 表示 60秒内10次) */
98
- rateLimit?: string;
99
-
100
95
  /** 路由路径(运行时生成) */
101
96
  route?: string;
102
97
  }
package/types/befly.d.ts CHANGED
@@ -1,19 +1,19 @@
1
- /**
1
+ /**
2
2
  * Befly 核心框架类型定义
3
3
  */
4
4
 
5
- import type { Plugin } from './plugin.js';
6
- import type { ApiRoute, HttpMethod } from './api.js';
7
- import type { KeyValue } from './common.js';
8
- import type { LoggerConfig } from './logger.js';
9
- import type { Logger } from '../lib/logger.js';
10
- import type { Jwt } from '../lib/jwt.js';
11
- import type { Validator } from '../lib/validator.js';
12
- import type { Database } from 'bun:sqlite';
13
- import type { DbHelper } from '../lib/dbHelper.js';
14
- import type { RedisHelper } from '../lib/redisHelper.js';
15
- import type { Cipher } from '../lib/cipher.js';
16
- import type { CacheHelper } from '../lib/cacheHelper.js';
5
+ import type { CacheHelper } from "../lib/cacheHelper.js";
6
+ import type { Cipher } from "../lib/cipher.js";
7
+ import type { DbHelper } from "../lib/dbHelper.js";
8
+ import type { Jwt } from "../lib/jwt.js";
9
+ import type { Logger } from "../lib/logger.js";
10
+ import type { RedisHelper } from "../lib/redisHelper.js";
11
+ import type { Validator } from "../lib/validator.js";
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
17
 
18
18
  export type { LoggerConfig };
19
19
 
@@ -87,6 +87,44 @@ export interface CorsConfig {
87
87
  credentials?: string;
88
88
  }
89
89
 
90
+ /**
91
+ * 全局请求限流配置(Hook)
92
+ */
93
+ export interface RateLimitRule {
94
+ /**
95
+ * 路由匹配串
96
+ * - 精确:"POST/api/auth/login"
97
+ * - 前缀:"POST/api/auth/*" 或 "/api/auth/*"
98
+ * - 全量:"*"
99
+ */
100
+ route: string;
101
+ /** 窗口期内允许次数 */
102
+ limit: number;
103
+ /** 窗口期秒数 */
104
+ window: number;
105
+ /** 计数维度(默认 ip) */
106
+ key?: "ip" | "user" | "ip_user";
107
+ }
108
+
109
+ export interface RateLimitConfig {
110
+ /** 是否启用 (0/1) */
111
+ enable?: number;
112
+ /** 未命中 rules 时的默认次数(<=0 表示不启用默认规则) */
113
+ defaultLimit?: number;
114
+ /** 未命中 rules 时的默认窗口秒数(<=0 表示不启用默认规则) */
115
+ defaultWindow?: number;
116
+ /** 默认计数维度(默认 ip) */
117
+ key?: "ip" | "user" | "ip_user";
118
+ /**
119
+ * 直接跳过限流的路由列表(优先级最高)
120
+ * - 精确:"POST/api/health" 或 "/api/health"
121
+ * - 前缀:"POST/api/health/*" 或 "/api/health/*"
122
+ */
123
+ skipRoutes?: string[];
124
+ /** 路由规则列表 */
125
+ rules?: RateLimitRule[];
126
+ }
127
+
90
128
  /**
91
129
  * Befly 构造函数选项(最多 2 级结构)
92
130
  */
@@ -120,10 +158,19 @@ export interface BeflyOptions {
120
158
  auth?: AuthConfig;
121
159
  /** CORS 插件配置 */
122
160
  cors?: CorsConfig;
161
+
162
+ /** 全局请求限流配置(Hook) */
163
+ rateLimit?: RateLimitConfig;
123
164
  /** 禁用的钩子列表 */
124
165
  disableHooks?: string[];
125
166
  /** 禁用的插件列表 */
126
167
  disablePlugins?: string[];
168
+
169
+ /** 是否启用组件钩子扫描(默认 false,仅加载 core hooks) */
170
+ enableAddonHooks?: boolean;
171
+
172
+ /** 是否启用项目钩子扫描(默认 false,仅加载 core hooks) */
173
+ enableAppHooks?: boolean;
127
174
  /** 隐藏的菜单路径列表(不同步到数据库) */
128
175
  hiddenMenus?: string[];
129
176
  /**
package/types/cache.d.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  * 缓存助手类型定义
3
3
  */
4
4
 
5
- import type { BeflyContext } from './befly.js';
5
+ import type { BeflyContext } from "./befly.js";
6
6
 
7
7
  /**
8
8
  * 缓存助手类
@@ -20,10 +20,14 @@ export interface CacheHelper {
20
20
  cacheMenus(): Promise<void>;
21
21
 
22
22
  /**
23
- * 缓存所有角色的接口权限到 Redis
24
- * 优化:使用 Promise.all 利用 Bun Redis 自动 pipeline 特性
23
+ * 全量重建所有角色的接口权限缓存
25
24
  */
26
- cacheRolePermissions(): Promise<void>;
25
+ rebuildRoleApiPermissions(): Promise<void>;
26
+
27
+ /**
28
+ * 增量刷新单个角色的接口权限缓存
29
+ */
30
+ refreshRoleApiPermissions(roleCode: string, apiIds: number[]): Promise<void>;
27
31
 
28
32
  /**
29
33
  * 缓存所有数据(接口、菜单、角色权限)
package/types/common.d.ts CHANGED
@@ -1,9 +1,17 @@
1
- /**
1
+ /**
2
2
  * Befly 框架通用类型定义
3
- * Core 专用类型,通用类型请直接从 befly-shared/types 导入
3
+ * 框架核心通用类型定义
4
+ */
5
+
6
+ /**
7
+ * SQL 值类型
4
8
  */
9
+ export type SqlValue = string | number | boolean | null | Date;
5
10
 
6
- import type { SqlValue } from 'befly-shared/types';
11
+ /**
12
+ * 通用键值对类型
13
+ */
14
+ export type KeyValue<T = any> = Record<string, T>;
7
15
 
8
16
  // ============================================
9
17
  // SQL 查询相关类型
@@ -12,7 +20,7 @@ import type { SqlValue } from 'befly-shared/types';
12
20
  /**
13
21
  * WHERE 条件操作符
14
22
  */
15
- export type WhereOperator = '$eq' | '$ne' | '$not' | '$gt' | '$gte' | '$lt' | '$lte' | '$like' | '$notLike' | '$in' | '$notIn' | '$nin' | '$isNull' | '$isNotNull' | '$null' | '$notNull' | '$between' | '$notBetween';
23
+ export type WhereOperator = "$eq" | "$ne" | "$not" | "$gt" | "$gte" | "$lt" | "$lte" | "$like" | "$notLike" | "$in" | "$notIn" | "$nin" | "$isNull" | "$isNotNull" | "$null" | "$notNull" | "$between" | "$notBetween";
16
24
 
17
25
  /**
18
26
  * WHERE 条件类型
@@ -22,7 +30,7 @@ export type WhereConditions = Record<string, any>;
22
30
  /**
23
31
  * 排序方向
24
32
  */
25
- export type OrderDirection = 'ASC' | 'DESC';
33
+ export type OrderDirection = "ASC" | "DESC";
26
34
 
27
35
  /**
28
36
  * 排序字段
@@ -74,7 +82,7 @@ export type FieldRule = string;
74
82
  */
75
83
  export interface ParsedFieldRule {
76
84
  name: string;
77
- type: 'string' | 'number' | 'text' | 'array_string' | 'array_text';
85
+ type: "string" | "number" | "text" | "array_string" | "array_text";
78
86
  min: number | null;
79
87
  max: number | null;
80
88
  default: any;
@@ -85,12 +93,12 @@ export interface ParsedFieldRule {
85
93
  /**
86
94
  * 比较运算符
87
95
  */
88
- export type ComparisonOperator = '=' | '>' | '<' | '>=' | '<=' | '!=' | '<>' | 'LIKE' | 'IN' | 'NOT IN' | 'IS NULL' | 'IS NOT NULL';
96
+ export type ComparisonOperator = "=" | ">" | "<" | ">=" | "<=" | "!=" | "<>" | "LIKE" | "IN" | "NOT IN" | "IS NULL" | "IS NOT NULL";
89
97
 
90
98
  /**
91
99
  * JOIN 类型
92
100
  */
93
- export type JoinType = 'INNER' | 'LEFT' | 'RIGHT' | 'FULL';
101
+ export type JoinType = "INNER" | "LEFT" | "RIGHT" | "FULL";
94
102
 
95
103
  /**
96
104
  * JOIN 选项
@@ -98,7 +106,7 @@ export type JoinType = 'INNER' | 'LEFT' | 'RIGHT' | 'FULL';
98
106
  */
99
107
  export interface JoinOption {
100
108
  /** JOIN 类型,默认 'left' */
101
- type?: 'left' | 'right' | 'inner';
109
+ type?: "left" | "right" | "inner";
102
110
  /** 要联接的表名(不支持别名) */
103
111
  table: string;
104
112
  /** JOIN 条件(如 'order.user_id = user.id') */
@@ -1,8 +1,8 @@
1
- /**
1
+ /**
2
2
  * 请求上下文类型定义
3
3
  */
4
4
 
5
- import type { ApiRoute } from './api.js';
5
+ import type { ApiRoute } from "./api.js";
6
6
 
7
7
  /**
8
8
  * 请求上下文接口
@@ -0,0 +1,23 @@
1
+ /**
2
+ * 加密相关类型定义
3
+ */
4
+
5
+ /**
6
+ * 编码类型
7
+ */
8
+ export type EncodingType = "hex" | "base64" | "base64url";
9
+
10
+ /**
11
+ * 哈希算法类型
12
+ */
13
+ export type HashAlgorithm = "md5" | "sha1" | "sha256" | "sha512" | "sha384" | "sha224";
14
+
15
+ /**
16
+ * 密码哈希选项
17
+ */
18
+ export interface PasswordHashOptions {
19
+ algorithm?: "argon2" | "bcrypt";
20
+ memoryCost?: number;
21
+ timeCost?: number;
22
+ [key: string]: any;
23
+ }
@@ -1,13 +1,13 @@
1
- /**
1
+ /**
2
2
  * 数据库相关类型定义
3
3
  */
4
4
 
5
- import type { SqlValue } from 'befly-shared/types';
6
- import type { DatabaseTables, TableName, TableType, TableInsertType, TableUpdateType, TypedWhereConditions } from './table';
7
- import type { JoinOption } from './common';
5
+ import type { SqlValue } from "./common";
6
+ import type { JoinOption } from "./common";
7
+ import type { DatabaseTables, TableName, TableType, TableInsertType, TableUpdateType, TypedWhereConditions } from "./table";
8
8
 
9
9
  // 重新导出表类型工具
10
- export type { DatabaseTables, TableName, TableType, TableInsertType, TableUpdateType, SystemFields, BaseTable, InsertType, UpdateType, SelectType, TypedWhereConditions } from './table';
10
+ export type { DatabaseTables, TableName, TableType, TableInsertType, TableUpdateType, SystemFields, BaseTable, InsertType, UpdateType, SelectType, TypedWhereConditions } from "./table";
11
11
 
12
12
  // ============================================
13
13
  // SQL 查询相关类型
@@ -16,7 +16,7 @@ export type { DatabaseTables, TableName, TableType, TableInsertType, TableUpdate
16
16
  /**
17
17
  * WHERE 条件操作符
18
18
  */
19
- export type WhereOperator = '$eq' | '$ne' | '$not' | '$gt' | '$gte' | '$lt' | '$lte' | '$like' | '$notLike' | '$in' | '$notIn' | '$nin' | '$isNull' | '$isNotNull' | '$null' | '$notNull' | '$between' | '$notBetween';
19
+ export type WhereOperator = "$eq" | "$ne" | "$not" | "$gt" | "$gte" | "$lt" | "$lte" | "$like" | "$notLike" | "$in" | "$notIn" | "$nin" | "$isNull" | "$isNotNull" | "$null" | "$notNull" | "$between" | "$notBetween";
20
20
 
21
21
  /**
22
22
  * WHERE 条件类型
@@ -26,7 +26,7 @@ export type WhereConditions = Record<string, any>;
26
26
  /**
27
27
  * 排序方向
28
28
  */
29
- export type OrderDirection = 'ASC' | 'DESC';
29
+ export type OrderDirection = "ASC" | "DESC";
30
30
 
31
31
  /**
32
32
  * 排序字段
@@ -199,7 +199,7 @@ export type UpdateData = Record<string, SqlValue>;
199
199
  /**
200
200
  * 数据库类型
201
201
  */
202
- export type DbType = 'mysql' | 'postgresql' | 'sqlite';
202
+ export type DbType = "mysql" | "postgresql" | "sqlite";
203
203
 
204
204
  /**
205
205
  * 列信息
@@ -258,7 +258,7 @@ export interface DbHelper {
258
258
  * 查询记录数(类型安全版本)
259
259
  * @template K - 表名类型
260
260
  */
261
- getCount<K extends TableName>(options: Omit<TypedQueryOptions<K>, 'fields' | 'page' | 'limit' | 'orderBy'>): Promise<number>;
261
+ getCount<K extends TableName>(options: Omit<TypedQueryOptions<K>, "fields" | "page" | "limit" | "orderBy">): Promise<number>;
262
262
 
263
263
  /**
264
264
  * 查询单条数据(类型安全版本)
@@ -279,7 +279,7 @@ export interface DbHelper {
279
279
  * @template K - 表名类型
280
280
  * @returns 返回类型自动推断为对应表的记录数组
281
281
  */
282
- getAll<K extends TableName>(options: Omit<TypedQueryOptions<K>, 'page' | 'limit'>): Promise<AllResult<TableType<K>>>;
282
+ getAll<K extends TableName>(options: Omit<TypedQueryOptions<K>, "page" | "limit">): Promise<AllResult<TableType<K>>>;
283
283
 
284
284
  /**
285
285
  * 插入数据(类型安全版本)
@@ -306,7 +306,7 @@ export interface DbHelper {
306
306
  /**
307
307
  * 查询记录数(兼容版本)
308
308
  */
309
- getCount(options: Omit<QueryOptions, 'fields' | 'page' | 'limit' | 'orderBy'>): Promise<number>;
309
+ getCount(options: Omit<QueryOptions, "fields" | "page" | "limit" | "orderBy">): Promise<number>;
310
310
 
311
311
  /**
312
312
  * 查询单条数据(兼容版本,需手动指定泛型)
@@ -324,7 +324,7 @@ export interface DbHelper {
324
324
  * 查询所有数据(兼容版本,需手动指定泛型)
325
325
  * @template T - 返回类型
326
326
  */
327
- getAll<T = any>(options: Omit<QueryOptions, 'page' | 'limit'>): Promise<AllResult<T>>;
327
+ getAll<T = any>(options: Omit<QueryOptions, "page" | "limit">): Promise<AllResult<T>>;
328
328
 
329
329
  /**
330
330
  * 插入数据(兼容版本)
@@ -359,7 +359,7 @@ export interface DbHelper {
359
359
  /**
360
360
  * 检查数据是否存在
361
361
  */
362
- exists(options: Omit<QueryOptions, 'fields' | 'orderBy' | 'page' | 'limit'>): Promise<boolean>;
362
+ exists(options: Omit<QueryOptions, "fields" | "orderBy" | "page" | "limit">): Promise<boolean>;
363
363
 
364
364
  /**
365
365
  * 检查表是否存在
@@ -374,17 +374,17 @@ export interface DbHelper {
374
374
  /**
375
375
  * 禁用数据(设置 state=2)
376
376
  */
377
- disableData(options: Omit<DeleteOptions, 'hard'>): Promise<number>;
377
+ disableData(options: Omit<DeleteOptions, "hard">): Promise<number>;
378
378
 
379
379
  /**
380
380
  * 启用数据(设置 state=1)
381
381
  */
382
- enableData(options: Omit<DeleteOptions, 'hard'>): Promise<number>;
382
+ enableData(options: Omit<DeleteOptions, "hard">): Promise<number>;
383
383
 
384
384
  /**
385
385
  * 硬删除数据(物理删除)
386
386
  */
387
- delForce(options: Omit<DeleteOptions, 'hard'>): Promise<number>;
387
+ delForce(options: Omit<DeleteOptions, "hard">): Promise<number>;
388
388
 
389
389
  /**
390
390
  * 自增字段
@@ -399,7 +399,7 @@ export interface DbHelper {
399
399
  /**
400
400
  * 查询单个字段值
401
401
  */
402
- getFieldValue<T = any>(options: Omit<QueryOptions, 'fields'> & { field: string }): Promise<T | null>;
402
+ getFieldValue<T = any>(options: Omit<QueryOptions, "fields"> & { field: string }): Promise<T | null>;
403
403
 
404
404
  /**
405
405
  * 清理数据或 where 条件(默认排除 null 和 undefined)
@@ -454,7 +454,7 @@ export interface IndexInfo {
454
454
  */
455
455
  export interface FieldChange {
456
456
  /** 变更类型 */
457
- type: 'length' | 'comment' | 'datatype' | 'default';
457
+ type: "length" | "comment" | "datatype" | "default";
458
458
  /** 当前值 */
459
459
  current: any;
460
460
  /** 新值 */
@@ -466,7 +466,7 @@ export interface FieldChange {
466
466
  */
467
467
  export interface IndexAction {
468
468
  /** 操作类型:create(创建)或 drop(删除) */
469
- action: 'create' | 'drop';
469
+ action: "create" | "drop";
470
470
  /** 索引名称 */
471
471
  indexName: string;
472
472
  /** 字段名称 */
package/types/hook.d.ts CHANGED
@@ -2,8 +2,8 @@
2
2
  * Befly 钩子系统类型定义
3
3
  */
4
4
 
5
- import type { BeflyContext } from './befly.js';
6
- import type { RequestContext } from './context.js';
5
+ import type { BeflyContext } from "./befly.js";
6
+ import type { RequestContext } from "./context.js";
7
7
 
8
8
  /**
9
9
  * 钩子处理函数类型(串联模式,无 next 参数)
package/types/jwt.d.ts ADDED
@@ -0,0 +1,118 @@
1
+ /**
2
+ * JWT 相关类型定义
3
+ */
4
+
5
+ /**
6
+ * JWT 算法类型
7
+ */
8
+ export type JwtAlgorithm = "HS256" | "HS384" | "HS512";
9
+
10
+ /**
11
+ * JWT Header 接口
12
+ */
13
+ export interface JwtHeader {
14
+ alg: JwtAlgorithm;
15
+ typ: "JWT";
16
+ }
17
+
18
+ /**
19
+ * JWT 载荷
20
+ */
21
+ export interface JwtPayload {
22
+ /**
23
+ * 注意:JWT payload 的业务字段来自 `befly.jwt.sign(payload)` 的入参。
24
+ * 也就是说:你在 sign 时放了什么字段,verify/decode 时就能取到什么字段。
25
+ *
26
+ * 另外,JWT 标准 claim(如 iat/exp/nbf/iss/aud/sub/jti)可能由 signer 注入。
27
+ */
28
+
29
+ /** 用户 ID(历史字段:部分代码用 userId) */
30
+ userId?: string | number;
31
+
32
+ /** 用户 ID(推荐字段:部分项目使用 id) */
33
+ id?: string | number;
34
+
35
+ /** 角色编码 */
36
+ roleCode?: string;
37
+
38
+ /** 昵称 */
39
+ nickname?: string;
40
+
41
+ /** 角色类型 */
42
+ roleType?: string;
43
+
44
+ /** 签发时间(秒级或毫秒级取决于实现,当前由底层库决定) */
45
+ iat?: number;
46
+
47
+ /** 过期时间(秒级或毫秒级取决于实现,当前由底层库决定) */
48
+ exp?: number;
49
+
50
+ /** 生效时间 */
51
+ nbf?: number;
52
+
53
+ /** 签发者 */
54
+ iss?: string;
55
+
56
+ /** 受众 */
57
+ aud?: string | string[];
58
+
59
+ /** 主题 */
60
+ sub?: string;
61
+
62
+ /** JWT ID */
63
+ jti?: string;
64
+
65
+ /** 其他自定义字段(由 sign 入参决定) */
66
+ [key: string]: unknown;
67
+ }
68
+
69
+ /**
70
+ * JWT 完整解码结果
71
+ */
72
+ export interface JwtDecoded {
73
+ header: JwtHeader;
74
+ payload: JwtPayload;
75
+ signature: string;
76
+ }
77
+
78
+ /**
79
+ * JWT 签名选项
80
+ */
81
+ export interface JwtSignOptions {
82
+ /** 密钥 */
83
+ secret?: string;
84
+ /** 算法 */
85
+ algorithm?: JwtAlgorithm;
86
+ /** 过期时间 */
87
+ expiresIn?: string | number;
88
+ /** 签发者 */
89
+ issuer?: string;
90
+ /** 受众 */
91
+ audience?: string;
92
+ /** 主题 */
93
+ subject?: string;
94
+ /** 生效时间 */
95
+ notBefore?: string | number;
96
+ /** JWT ID */
97
+ jwtId?: string;
98
+ }
99
+
100
+ /**
101
+ * JWT 验证选项
102
+ */
103
+ export interface JwtVerifyOptions {
104
+ /** 密钥 */
105
+ secret?: string;
106
+ /** 算法列表 */
107
+ algorithms?: JwtAlgorithm[];
108
+ /** 签发者 */
109
+ issuer?: string;
110
+ /** 受众 */
111
+ audience?: string;
112
+ /** 主题 */
113
+ subject?: string;
114
+ /** 是否忽略过期 */
115
+ ignoreExpiration?: boolean;
116
+ /** 是否忽略生效时间 */
117
+ ignoreNotBefore?: boolean;
118
+ }
@@ -0,0 +1,30 @@
1
+ /**
2
+ * 日志相关类型定义
3
+ */
4
+
5
+ /**
6
+ * 日志级别
7
+ */
8
+ export type LogLevel = "trace" | "debug" | "info" | "warn" | "error" | "fatal";
9
+
10
+ /**
11
+ * 日志配置
12
+ */
13
+ export interface LoggerConfig {
14
+ /** 是否开启调试模式 (0: 关闭, 1: 开启) @default 0 */
15
+ debug?: number;
16
+ /**
17
+ * 需要在日志中脱敏/排除的字段(仅支持数组)
18
+ * - 支持模糊匹配:"token" 可命中 "accessToken" / "user_token" 等
19
+ * - 支持后缀匹配:"*Secret" 可命中 "mySecret" / "appSecret" 等
20
+ * - 支持前缀匹配:"auth*" 可命中 "authorization" 等
21
+ * - 支持包含匹配:"*token*" 可命中包含 token 的任意 key
22
+ */
23
+ excludeFields?: string[];
24
+ /** 日志目录 @default './logs' */
25
+ dir?: string;
26
+ /** 是否输出到控制台 (0: 关闭, 1: 开启) @default 1 */
27
+ console?: number;
28
+ /** 单个日志文件最大大小 (MB) @default 10 */
29
+ maxSize?: number;
30
+ }
package/types/plugin.d.ts CHANGED
@@ -1,9 +1,9 @@
1
- /**
1
+ /**
2
2
  * Befly 插件系统类型定义
3
3
  */
4
4
 
5
- import type { BeflyContext } from './befly.js';
6
- import type { RequestContext } from './context.js';
5
+ import type { BeflyContext } from "./befly.js";
6
+ import type { RequestContext } from "./context.js";
7
7
 
8
8
  /**
9
9
  * 插件初始化函数类型
@@ -83,7 +83,7 @@ export interface PluginContext {
83
83
  endTime?: number;
84
84
 
85
85
  /** 执行状态 */
86
- status: 'pending' | 'running' | 'success' | 'error';
86
+ status: "pending" | "running" | "success" | "error";
87
87
 
88
88
  /** 错误信息 */
89
89
  error?: Error;
package/types/redis.d.ts CHANGED
@@ -1,8 +1,8 @@
1
- /**
1
+ /**
2
2
  * Redis 相关类型定义
3
3
  */
4
4
 
5
- import { RedisClient as BunRedisClient } from 'bun';
5
+ import { RedisClient as BunRedisClient } from "bun";
6
6
 
7
7
  /**
8
8
  * Redis 客户端类型
@@ -35,7 +35,11 @@ export interface RedisHelper {
35
35
  /** 获取字符串值 */
36
36
  getString(key: string): Promise<string | null>;
37
37
  /** 检查键是否存在 */
38
- exists(key: string): Promise<number>;
38
+ exists(key: string): Promise<boolean>;
39
+ /** 原子自增 */
40
+ incr(key: string): Promise<number>;
41
+ /** 原子自增并在首次自增时设置过期时间 */
42
+ incrWithExpire(key: string, seconds: number): Promise<number>;
39
43
  /** 设置过期时间 */
40
44
  expire(key: string, seconds: number): Promise<number>;
41
45
  /** 获取剩余过期时间 */