befly 3.16.10 → 3.16.11

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 (178) hide show
  1. package/README.md +0 -129
  2. package/befly.js +12769 -0
  3. package/befly.min.js +47 -0
  4. package/package.json +18 -29
  5. package/dist/befly.config.d.ts +0 -7
  6. package/dist/befly.config.js +0 -128
  7. package/dist/befly.js +0 -17348
  8. package/dist/befly.min.js +0 -23
  9. package/dist/checks/checkApi.d.ts +0 -1
  10. package/dist/checks/checkApi.js +0 -139
  11. package/dist/checks/checkConfig.d.ts +0 -9
  12. package/dist/checks/checkConfig.js +0 -255
  13. package/dist/checks/checkHook.d.ts +0 -1
  14. package/dist/checks/checkHook.js +0 -132
  15. package/dist/checks/checkMenu.d.ts +0 -3
  16. package/dist/checks/checkMenu.js +0 -106
  17. package/dist/checks/checkPlugin.d.ts +0 -1
  18. package/dist/checks/checkPlugin.js +0 -132
  19. package/dist/checks/checkTable.d.ts +0 -7
  20. package/dist/checks/checkTable.js +0 -431
  21. package/dist/configs/presetRegexp.d.ts +0 -145
  22. package/dist/configs/presetRegexp.js +0 -218
  23. package/dist/hooks/auth.d.ts +0 -3
  24. package/dist/hooks/auth.js +0 -24
  25. package/dist/hooks/cors.d.ts +0 -7
  26. package/dist/hooks/cors.js +0 -36
  27. package/dist/hooks/parser.d.ts +0 -10
  28. package/dist/hooks/parser.js +0 -76
  29. package/dist/hooks/permission.d.ts +0 -11
  30. package/dist/hooks/permission.js +0 -78
  31. package/dist/hooks/validator.d.ts +0 -7
  32. package/dist/hooks/validator.js +0 -52
  33. package/dist/index.d.ts +0 -28
  34. package/dist/index.js +0 -316
  35. package/dist/lib/asyncContext.d.ts +0 -21
  36. package/dist/lib/asyncContext.js +0 -27
  37. package/dist/lib/cacheHelper.d.ts +0 -128
  38. package/dist/lib/cacheHelper.js +0 -477
  39. package/dist/lib/cacheKeys.d.ts +0 -27
  40. package/dist/lib/cacheKeys.js +0 -37
  41. package/dist/lib/cipher.d.ts +0 -153
  42. package/dist/lib/cipher.js +0 -237
  43. package/dist/lib/connect.d.ts +0 -95
  44. package/dist/lib/connect.js +0 -313
  45. package/dist/lib/dbHelper.d.ts +0 -229
  46. package/dist/lib/dbHelper.js +0 -1099
  47. package/dist/lib/dbUtils.d.ts +0 -91
  48. package/dist/lib/dbUtils.js +0 -544
  49. package/dist/lib/jwt.d.ts +0 -13
  50. package/dist/lib/jwt.js +0 -77
  51. package/dist/lib/logger.d.ts +0 -46
  52. package/dist/lib/logger.js +0 -731
  53. package/dist/lib/redisHelper.d.ts +0 -193
  54. package/dist/lib/redisHelper.js +0 -598
  55. package/dist/lib/sqlBuilder.d.ts +0 -160
  56. package/dist/lib/sqlBuilder.js +0 -837
  57. package/dist/lib/sqlCheck.d.ts +0 -23
  58. package/dist/lib/sqlCheck.js +0 -119
  59. package/dist/lib/validator.d.ts +0 -45
  60. package/dist/lib/validator.js +0 -424
  61. package/dist/loader/loadApis.d.ts +0 -12
  62. package/dist/loader/loadApis.js +0 -71
  63. package/dist/loader/loadHooks.d.ts +0 -7
  64. package/dist/loader/loadHooks.js +0 -50
  65. package/dist/loader/loadPlugins.d.ts +0 -8
  66. package/dist/loader/loadPlugins.js +0 -69
  67. package/dist/paths.d.ts +0 -93
  68. package/dist/paths.js +0 -100
  69. package/dist/plugins/cache.d.ts +0 -10
  70. package/dist/plugins/cache.js +0 -24
  71. package/dist/plugins/cipher.d.ts +0 -7
  72. package/dist/plugins/cipher.js +0 -14
  73. package/dist/plugins/config.d.ts +0 -3
  74. package/dist/plugins/config.js +0 -9
  75. package/dist/plugins/db.d.ts +0 -10
  76. package/dist/plugins/db.js +0 -48
  77. package/dist/plugins/jwt.d.ts +0 -6
  78. package/dist/plugins/jwt.js +0 -13
  79. package/dist/plugins/logger.d.ts +0 -10
  80. package/dist/plugins/logger.js +0 -21
  81. package/dist/plugins/redis.d.ts +0 -10
  82. package/dist/plugins/redis.js +0 -40
  83. package/dist/plugins/tool.d.ts +0 -75
  84. package/dist/plugins/tool.js +0 -105
  85. package/dist/router/api.d.ts +0 -14
  86. package/dist/router/api.js +0 -109
  87. package/dist/router/static.d.ts +0 -9
  88. package/dist/router/static.js +0 -56
  89. package/dist/scripts/ensureDist.d.ts +0 -1
  90. package/dist/scripts/ensureDist.js +0 -296
  91. package/dist/sync/syncApi.d.ts +0 -3
  92. package/dist/sync/syncApi.js +0 -163
  93. package/dist/sync/syncCache.d.ts +0 -2
  94. package/dist/sync/syncCache.js +0 -14
  95. package/dist/sync/syncDev.d.ts +0 -6
  96. package/dist/sync/syncDev.js +0 -166
  97. package/dist/sync/syncMenu.d.ts +0 -14
  98. package/dist/sync/syncMenu.js +0 -308
  99. package/dist/sync/syncTable.d.ts +0 -126
  100. package/dist/sync/syncTable.js +0 -1129
  101. package/dist/types/api.d.ts +0 -177
  102. package/dist/types/api.js +0 -4
  103. package/dist/types/befly.d.ts +0 -231
  104. package/dist/types/befly.js +0 -4
  105. package/dist/types/cache.d.ts +0 -96
  106. package/dist/types/cache.js +0 -4
  107. package/dist/types/cipher.d.ts +0 -27
  108. package/dist/types/cipher.js +0 -7
  109. package/dist/types/common.d.ts +0 -127
  110. package/dist/types/common.js +0 -5
  111. package/dist/types/context.d.ts +0 -39
  112. package/dist/types/context.js +0 -4
  113. package/dist/types/coreError.d.ts +0 -31
  114. package/dist/types/coreError.js +0 -38
  115. package/dist/types/crypto.d.ts +0 -20
  116. package/dist/types/crypto.js +0 -4
  117. package/dist/types/database.d.ts +0 -182
  118. package/dist/types/database.js +0 -4
  119. package/dist/types/hook.d.ts +0 -30
  120. package/dist/types/hook.js +0 -19
  121. package/dist/types/jwt.d.ts +0 -76
  122. package/dist/types/jwt.js +0 -4
  123. package/dist/types/logger.d.ts +0 -95
  124. package/dist/types/logger.js +0 -6
  125. package/dist/types/plugin.d.ts +0 -27
  126. package/dist/types/plugin.js +0 -17
  127. package/dist/types/redis.d.ts +0 -80
  128. package/dist/types/redis.js +0 -4
  129. package/dist/types/roleApisCache.d.ts +0 -21
  130. package/dist/types/roleApisCache.js +0 -8
  131. package/dist/types/sync.d.ts +0 -93
  132. package/dist/types/sync.js +0 -4
  133. package/dist/types/table.d.ts +0 -34
  134. package/dist/types/table.js +0 -4
  135. package/dist/types/validate.d.ts +0 -113
  136. package/dist/types/validate.js +0 -4
  137. package/dist/utils/calcPerfTime.d.ts +0 -4
  138. package/dist/utils/calcPerfTime.js +0 -13
  139. package/dist/utils/cors.d.ts +0 -8
  140. package/dist/utils/cors.js +0 -17
  141. package/dist/utils/dbFieldRules.d.ts +0 -31
  142. package/dist/utils/dbFieldRules.js +0 -94
  143. package/dist/utils/fieldClear.d.ts +0 -11
  144. package/dist/utils/fieldClear.js +0 -57
  145. package/dist/utils/formatYmdHms.d.ts +0 -1
  146. package/dist/utils/formatYmdHms.js +0 -20
  147. package/dist/utils/getClientIp.d.ts +0 -6
  148. package/dist/utils/getClientIp.js +0 -39
  149. package/dist/utils/importDefault.d.ts +0 -1
  150. package/dist/utils/importDefault.js +0 -53
  151. package/dist/utils/isDirentDirectory.d.ts +0 -3
  152. package/dist/utils/isDirentDirectory.js +0 -18
  153. package/dist/utils/loadMenuConfigs.d.ts +0 -11
  154. package/dist/utils/loadMenuConfigs.js +0 -130
  155. package/dist/utils/loggerUtils.d.ts +0 -18
  156. package/dist/utils/loggerUtils.js +0 -171
  157. package/dist/utils/mergeAndConcat.d.ts +0 -7
  158. package/dist/utils/mergeAndConcat.js +0 -77
  159. package/dist/utils/normalizeFieldDefinition.d.ts +0 -18
  160. package/dist/utils/normalizeFieldDefinition.js +0 -27
  161. package/dist/utils/processInfo.d.ts +0 -26
  162. package/dist/utils/processInfo.js +0 -41
  163. package/dist/utils/response.d.ts +0 -20
  164. package/dist/utils/response.js +0 -96
  165. package/dist/utils/scanAddons.d.ts +0 -15
  166. package/dist/utils/scanAddons.js +0 -35
  167. package/dist/utils/scanCoreBuiltins.d.ts +0 -3
  168. package/dist/utils/scanCoreBuiltins.js +0 -72
  169. package/dist/utils/scanFiles.d.ts +0 -32
  170. package/dist/utils/scanFiles.js +0 -124
  171. package/dist/utils/scanSources.d.ts +0 -10
  172. package/dist/utils/scanSources.js +0 -46
  173. package/dist/utils/sortModules.d.ts +0 -28
  174. package/dist/utils/sortModules.js +0 -105
  175. package/dist/utils/sqlUtil.d.ts +0 -33
  176. package/dist/utils/sqlUtil.js +0 -146
  177. package/dist/utils/util.d.ts +0 -172
  178. package/dist/utils/util.js +0 -517
@@ -1,80 +0,0 @@
1
- /**
2
- * Redis 助手类型定义
3
- */
4
- /**
5
- * Redis Key 类型
6
- */
7
- export type RedisKey = string;
8
- /**
9
- * Redis Value 类型
10
- */
11
- export type RedisValue = string | number | boolean | object | null;
12
- /**
13
- * Redis 配置选项
14
- */
15
- export interface RedisOptions {
16
- host: string;
17
- port: number;
18
- password?: string;
19
- db?: number;
20
- prefix?: string;
21
- }
22
- /**
23
- * Redis 助手接口
24
- */
25
- export interface RedisHelper {
26
- setObject<T = any>(key: string, obj: T, ttl?: number | null): Promise<string | null>;
27
- getObject<T = any>(key: string): Promise<T | null>;
28
- delObject(key: string): Promise<void>;
29
- genTimeID(): Promise<number>;
30
- setString(key: string, value: string, ttl?: number | null): Promise<string | null>;
31
- getString(key: string): Promise<string | null>;
32
- exists(key: string): Promise<boolean>;
33
- incr(key: string): Promise<number>;
34
- incrWithExpire(key: string, seconds: number): Promise<number>;
35
- expire(key: string, seconds: number): Promise<number>;
36
- ttl(key: string): Promise<number>;
37
- ttlBatch(keys: string[]): Promise<number[]>;
38
- sadd(key: string, members: string[]): Promise<number>;
39
- sismember(key: string, member: string): Promise<boolean>;
40
- scard(key: string): Promise<number>;
41
- smembers(key: string): Promise<string[]>;
42
- saddBatch(items: Array<{
43
- key: string;
44
- members: string[];
45
- }>): Promise<number>;
46
- sismemberBatch(items: Array<{
47
- key: string;
48
- member: string;
49
- }>): Promise<boolean[]>;
50
- del(key: string): Promise<number>;
51
- delBatch(keys: string[]): Promise<number>;
52
- setBatch<T = any>(items: Array<{
53
- key: string;
54
- value: T;
55
- ttl?: number | null;
56
- }>): Promise<number>;
57
- getBatch<T = any>(keys: string[]): Promise<Array<T | null>>;
58
- existsBatch(keys: string[]): Promise<boolean[]>;
59
- expireBatch(items: Array<{
60
- key: string;
61
- seconds: number;
62
- }>): Promise<number>;
63
- ping(): Promise<string>;
64
- /**
65
- * 尝试获取分布式锁(SET key value NX PX ttlMs)
66
- * @returns true=获取成功;false=未获取到(被占用)
67
- */
68
- tryAcquireLock(key: string, token: string, ttlMs: number): Promise<boolean>;
69
- /**
70
- * 释放分布式锁(仅当 value==token 时删除)
71
- * @returns true=释放成功;false=未释放(锁不存在/不归属/实现不支持)
72
- */
73
- releaseLock(key: string, token: string): Promise<boolean>;
74
- }
75
- /**
76
- * RedisHelper 构造函数类型
77
- */
78
- export interface RedisHelperConstructor {
79
- new (prefix?: string): RedisHelper;
80
- }
@@ -1,4 +0,0 @@
1
- /**
2
- * Redis 助手类型定义
3
- */
4
- export {};
@@ -1,21 +0,0 @@
1
- /**
2
- * 角色接口权限缓存(Redis)元信息类型
3
- *
4
- * 说明:
5
- * - active/ready 用于全局版本切换与就绪门槛
6
- * - role meta 用于单角色增量刷新后的可观测性
7
- */
8
- export type RoleApisCacheMeta = {
9
- hash: string;
10
- at: number;
11
- roles: number;
12
- members: number;
13
- /** 可选:用于和 DB 侧的更新时间做一致性对比 */
14
- updatedAt?: number;
15
- };
16
- export type RoleApisRoleMeta = {
17
- roleCode: string;
18
- at: number;
19
- members: number;
20
- hash: string;
21
- };
@@ -1,8 +0,0 @@
1
- /**
2
- * 角色接口权限缓存(Redis)元信息类型
3
- *
4
- * 说明:
5
- * - active/ready 用于全局版本切换与就绪门槛
6
- * - role meta 用于单角色增量刷新后的可观测性
7
- */
8
- export {};
@@ -1,93 +0,0 @@
1
- /**
2
- * 数据同步相关类型定义
3
- */
4
- import type { BeflyContext } from "./befly";
5
- import type { JsonValue, KeyValue } from "./common";
6
- /**
7
- * 同步结果
8
- */
9
- export interface SyncResult {
10
- /** 是否成功 */
11
- success: boolean;
12
- /** 同步的数量 */
13
- count: number;
14
- /** 错误信息 */
15
- error?: string;
16
- /** 额外数据 */
17
- data?: KeyValue;
18
- }
19
- /**
20
- * 同步选项
21
- */
22
- export interface SyncOptions {
23
- /** 是否强制同步(覆盖已有数据) */
24
- force?: boolean;
25
- /** 是否跳过已存在的数据 */
26
- skipExisting?: boolean;
27
- /** 同步批次大小 */
28
- batchSize?: number;
29
- /** 其他选项 */
30
- [key: string]: JsonValue | undefined;
31
- }
32
- /**
33
- * 同步器接口
34
- */
35
- export interface Syncer {
36
- /** 同步菜单 */
37
- syncMenus(): Promise<SyncResult>;
38
- /** 同步 API */
39
- syncApis(): Promise<SyncResult>;
40
- /** 同步字段定义 */
41
- syncFields(): Promise<SyncResult>;
42
- /** 同步所有 */
43
- syncAll(): Promise<SyncResult>;
44
- }
45
- /**
46
- * Syncer 构造函数
47
- */
48
- export interface SyncerConstructor {
49
- new (befly: BeflyContext, options?: SyncOptions): Syncer;
50
- }
51
- export interface MenuConfig {
52
- path?: string;
53
- name?: string;
54
- sort?: number;
55
- parentPath?: string;
56
- children?: MenuConfig[];
57
- [key: string]: JsonValue | undefined;
58
- }
59
- export interface SyncApiItem {
60
- type?: "api" | string;
61
- name: string;
62
- /** 是否需要登录:运行时支持 boolean/string[];同步写库为字符串(否/是/角色列表) */
63
- auth?: boolean | string[] | string;
64
- path: string;
65
- parentPath: string;
66
- addonName: string;
67
- [key: string]: JsonValue | undefined;
68
- }
69
- export interface ColumnInfo {
70
- type: string;
71
- columnType: string;
72
- length: number | null;
73
- max: number | null;
74
- nullable: boolean;
75
- defaultValue: JsonValue;
76
- comment: string | null;
77
- }
78
- export type IndexInfo = Record<string, string[]>;
79
- export interface FieldChange {
80
- type: string;
81
- current: JsonValue;
82
- expected: JsonValue;
83
- }
84
- export interface TablePlan {
85
- changed: boolean;
86
- alterClauses: string[];
87
- indexActions: Array<{
88
- action: "create" | "drop";
89
- indexName: string;
90
- fieldName: string;
91
- }>;
92
- commentActions?: string[];
93
- }
@@ -1,4 +0,0 @@
1
- /**
2
- * 数据同步相关类型定义
3
- */
4
- export {};
@@ -1,34 +0,0 @@
1
- /**
2
- * 表结构相关类型定义
3
- */
4
- /**
5
- * 字段信息
6
- */
7
- export interface FieldInfo {
8
- name: string;
9
- type: string;
10
- isPrimary: boolean;
11
- isNotNull: boolean;
12
- defaultValue?: any;
13
- isAutoIncrement?: boolean;
14
- isUnique?: boolean;
15
- comment?: string;
16
- }
17
- /**
18
- * 创建表选项
19
- */
20
- export interface CreateTableOptions {
21
- table: string;
22
- fields: Record<string, string>;
23
- indexes?: string[];
24
- unique?: string[];
25
- }
26
- /**
27
- * SQL 帮助器选项
28
- */
29
- export interface SqlHelperOptions {
30
- /** 数据库类型 */
31
- type: "mysql";
32
- /** 表名前缀 */
33
- prefix?: string;
34
- }
@@ -1,4 +0,0 @@
1
- /**
2
- * 表结构相关类型定义
3
- */
4
- export {};
@@ -1,113 +0,0 @@
1
- /**
2
- * 数据验证相关类型定义
3
- */
4
- import type { JsonValue } from "./common";
5
- /**
6
- * 数据库字段类型
7
- */
8
- export type DbFieldType = "tinyint" | "smallint" | "mediumint" | "int" | "bigint" | "decimal" | "float" | "double" | "char" | "varchar" | "enum" | "tinytext" | "text" | "mediumtext" | "longtext" | "datetime" | "timestamp" | "json";
9
- /**
10
- * 输入参数类型
11
- */
12
- export type InputType = "number" | "integer" | "string" | "char" | "array" | "array_number" | "array_integer" | "json" | "json_number" | "json_integer";
13
- /**
14
- * 字段定义(表字段 JSON 结构)
15
- */
16
- export interface FieldDefinition {
17
- /** 字段标签(中文名) */
18
- name: string;
19
- /** 字段类型 */
20
- type: DbFieldType | string;
21
- /** 输入类型(校验用) */
22
- input?: InputType | string;
23
- /**
24
- * 最小值/最小长度。
25
- * - 缺省默认:null(表示不限制)
26
- */
27
- min?: number | null;
28
- /**
29
- * 最大值/最大长度。
30
- * - 缺省默认:null(表示不限制)
31
- */
32
- max?: number | null;
33
- /**
34
- * 默认值。
35
- * - 缺省默认:null(表示字段未提供默认值)
36
- */
37
- default?: JsonValue | null;
38
- /**
39
- * 字段描述。
40
- * - 缺省默认:""(空字符串)
41
- */
42
- detail?: string;
43
- /**
44
- * decimal 精度(总位数)。
45
- * - 缺省默认:null
46
- */
47
- precision?: number | null;
48
- /**
49
- * decimal 小数位数。
50
- * - 缺省默认:null
51
- */
52
- scale?: number | null;
53
- /**
54
- * 是否创建索引。
55
- * - 缺省默认:false
56
- */
57
- index?: boolean;
58
- /**
59
- * 是否唯一约束。
60
- * - 缺省默认:false
61
- */
62
- unique?: boolean;
63
- /**
64
- * 是否可为 NULL。
65
- * - 缺省默认:false
66
- */
67
- nullable?: boolean;
68
- /**
69
- * number 类型是否无符号(仅 MySQL 语义生效)。
70
- * - 缺省默认:false
71
- */
72
- unsigned?: boolean;
73
- }
74
- /**
75
- * 表定义(字段名 -> 字段定义)
76
- */
77
- export type TableDefinition = Record<string, FieldDefinition>;
78
- /**
79
- * validate(data, rules) 的返回结构
80
- */
81
- export interface ValidateResult {
82
- code: 0 | 1;
83
- failed: boolean;
84
- firstError: string | null;
85
- errors: string[];
86
- errorFields: string[];
87
- fieldErrors: Record<string, string>;
88
- }
89
- /**
90
- * single(value, fieldDef) 的返回结构
91
- */
92
- export interface SingleResult {
93
- value: JsonValue | null;
94
- error: string | null;
95
- }
96
- /**
97
- * Validator 静态类类型(对外)。
98
- *
99
- * 说明:runtime 实现是 `export class Validator`,以静态方法形式提供:
100
- * - `Validator.validate(data, rules, required)`
101
- * - `Validator.single(value, fieldDef)`
102
- */
103
- export interface ValidatorStatic {
104
- /**
105
- * 推荐签名:正常业务使用。
106
- */
107
- validate(data: Record<string, JsonValue>, rules: TableDefinition, required?: string[]): ValidateResult;
108
- /**
109
- * 宽签名:用于边界输入(例如测试传入非法参数)——实现内部会做运行时校验。
110
- */
111
- validate(data: unknown, rules: unknown, required?: string[]): ValidateResult;
112
- single(value: unknown, fieldDef: FieldDefinition): SingleResult;
113
- }
@@ -1,4 +0,0 @@
1
- /**
2
- * 数据验证相关类型定义
3
- */
4
- export {};
@@ -1,4 +0,0 @@
1
- /**
2
- * 计算性能时间差
3
- */
4
- export declare const calcPerfTime: (startTime: number, endTime?: number) => string;
@@ -1,13 +0,0 @@
1
- /**
2
- * 计算性能时间差
3
- */
4
- export const calcPerfTime = (startTime, endTime = Bun.nanoseconds()) => {
5
- const elapsedMs = (endTime - startTime) / 1_000_000;
6
- if (elapsedMs < 1000) {
7
- return `${elapsedMs.toFixed(2)} 毫秒`;
8
- }
9
- else {
10
- const elapsedSeconds = elapsedMs / 1000;
11
- return `${elapsedSeconds.toFixed(2)} 秒`;
12
- }
13
- };
@@ -1,8 +0,0 @@
1
- import type { CorsConfig } from "../types/befly";
2
- /**
3
- * 设置 CORS 响应头
4
- * @param req - 请求对象
5
- * @param config - CORS 配置(可选)
6
- * @returns CORS 响应头对象
7
- */
8
- export declare function setCorsOptions(req: Request, config?: CorsConfig): Record<string, string>;
@@ -1,17 +0,0 @@
1
- /**
2
- * 设置 CORS 响应头
3
- * @param req - 请求对象
4
- * @param config - CORS 配置(可选)
5
- * @returns CORS 响应头对象
6
- */
7
- export function setCorsOptions(req, config = {}) {
8
- const origin = config.origin || "*";
9
- return {
10
- "Access-Control-Allow-Origin": origin === "*" ? req.headers.get("origin") || "*" : origin,
11
- "Access-Control-Allow-Methods": config.methods || "GET, POST, PUT, DELETE, OPTIONS",
12
- "Access-Control-Allow-Headers": config.allowedHeaders || "Content-Type, Authorization, authorization, token",
13
- "Access-Control-Expose-Headers": config.exposedHeaders || "Content-Range, X-Content-Range, Authorization, authorization, token",
14
- "Access-Control-Max-Age": String(config.maxAge || 86400),
15
- "Access-Control-Allow-Credentials": config.credentials || "true"
16
- };
17
- }
@@ -1,31 +0,0 @@
1
- /**
2
- * 数据库字段规则(供 checkTable / normalizeFieldDefinition / validator / syncTable 复用)
3
- *
4
- * 目标:
5
- * - 避免同一套规则在多个模块重复实现导致漂移
6
- * - 保持简单直观,不引入过度抽象
7
- */
8
- export declare function normalizeDbType(dbType: string): string;
9
- export declare function isStringDbType(dbType: string): boolean;
10
- export declare function isTextDbType(dbType: string): boolean;
11
- export declare function isIntDbType(dbType: string): boolean;
12
- export declare function isDecimalDbType(dbType: string): boolean;
13
- export declare function isFloatDbType(dbType: string): boolean;
14
- export declare function isJsonDbType(dbType: string): boolean;
15
- export declare function isEnumDbType(dbType: string): boolean;
16
- export declare function isNumericDbType(dbType: string): boolean;
17
- /**
18
- * 根据数据库类型推导默认 input 类型。
19
- */
20
- export declare function inferInputByDbType(dbType: string): string;
21
- /**
22
- * 归一化 type 与 input,input 为空时使用推导值。
23
- */
24
- export declare function normalizeTypeAndInput(type: string, input: string): {
25
- type: string;
26
- input: string;
27
- };
28
- /**
29
- * 解析枚举规则(a|b|c)为去重后的值列表(保持顺序)。
30
- */
31
- export declare function parseEnumRuleValues(enumInput: string): string[];
@@ -1,94 +0,0 @@
1
- /**
2
- * 数据库字段规则(供 checkTable / normalizeFieldDefinition / validator / syncTable 复用)
3
- *
4
- * 目标:
5
- * - 避免同一套规则在多个模块重复实现导致漂移
6
- * - 保持简单直观,不引入过度抽象
7
- */
8
- const STRING_DB_TYPES = new Set(["char", "varchar", "enum", "tinytext", "text", "mediumtext", "longtext"]);
9
- const TEXT_DB_TYPES = new Set(["tinytext", "text", "mediumtext", "longtext"]);
10
- const INT_DB_TYPES = new Set(["tinyint", "smallint", "mediumint", "int", "bigint"]);
11
- const DECIMAL_DB_TYPES = new Set(["decimal"]);
12
- const FLOAT_DB_TYPES = new Set(["float", "double"]);
13
- const JSON_DB_TYPES = new Set(["json"]);
14
- const ENUM_DB_TYPES = new Set(["enum"]);
15
- export function normalizeDbType(dbType) {
16
- return String(dbType || "")
17
- .trim()
18
- .toLowerCase();
19
- }
20
- export function isStringDbType(dbType) {
21
- return STRING_DB_TYPES.has(normalizeDbType(dbType));
22
- }
23
- export function isTextDbType(dbType) {
24
- return TEXT_DB_TYPES.has(normalizeDbType(dbType));
25
- }
26
- export function isIntDbType(dbType) {
27
- return INT_DB_TYPES.has(normalizeDbType(dbType));
28
- }
29
- export function isDecimalDbType(dbType) {
30
- return DECIMAL_DB_TYPES.has(normalizeDbType(dbType));
31
- }
32
- export function isFloatDbType(dbType) {
33
- return FLOAT_DB_TYPES.has(normalizeDbType(dbType));
34
- }
35
- export function isJsonDbType(dbType) {
36
- return JSON_DB_TYPES.has(normalizeDbType(dbType));
37
- }
38
- export function isEnumDbType(dbType) {
39
- return ENUM_DB_TYPES.has(normalizeDbType(dbType));
40
- }
41
- export function isNumericDbType(dbType) {
42
- const normalized = normalizeDbType(dbType);
43
- return INT_DB_TYPES.has(normalized) || DECIMAL_DB_TYPES.has(normalized) || FLOAT_DB_TYPES.has(normalized);
44
- }
45
- /**
46
- * 根据数据库类型推导默认 input 类型。
47
- */
48
- export function inferInputByDbType(dbType) {
49
- const normalized = normalizeDbType(dbType);
50
- if (INT_DB_TYPES.has(normalized)) {
51
- return "integer";
52
- }
53
- if (DECIMAL_DB_TYPES.has(normalized) || FLOAT_DB_TYPES.has(normalized)) {
54
- return "number";
55
- }
56
- if (STRING_DB_TYPES.has(normalized) || normalized === "datetime" || normalized === "timestamp") {
57
- return "string";
58
- }
59
- if (normalized === "json") {
60
- return "json";
61
- }
62
- return "string";
63
- }
64
- /**
65
- * 归一化 type 与 input,input 为空时使用推导值。
66
- */
67
- export function normalizeTypeAndInput(type, input) {
68
- const rawType = String(type || "").trim();
69
- const rawInput = String(input || "").trim();
70
- return {
71
- type: rawType,
72
- input: rawInput || inferInputByDbType(rawType)
73
- };
74
- }
75
- /**
76
- * 解析枚举规则(a|b|c)为去重后的值列表(保持顺序)。
77
- */
78
- export function parseEnumRuleValues(enumInput) {
79
- const raw = String(enumInput || "").trim();
80
- if (raw === "") {
81
- return [];
82
- }
83
- const parts = raw
84
- .split("|")
85
- .map((x) => x.trim())
86
- .filter((x) => x !== "");
87
- const out = [];
88
- for (const p of parts) {
89
- if (!out.includes(p)) {
90
- out.push(p);
91
- }
92
- }
93
- return out;
94
- }
@@ -1,11 +0,0 @@
1
- export interface FieldClearOptions {
2
- pickKeys?: string[];
3
- omitKeys?: string[];
4
- keepValues?: any[];
5
- excludeValues?: any[];
6
- keepMap?: Record<string, any>;
7
- }
8
- export type FieldClearResult<T> = T extends Array<infer U> ? Array<FieldClearResult<U>> : T extends object ? {
9
- [K in keyof T]?: T[K];
10
- } : T;
11
- export declare function fieldClear<T = any>(data: T | T[], options?: FieldClearOptions): FieldClearResult<T>;
@@ -1,57 +0,0 @@
1
- // fieldClear 工具函数实现
2
- // 支持 pick/omit/keepValues/excludeValues,处理对象和数组
3
- function isObject(val) {
4
- return val !== null && typeof val === "object" && !Array.isArray(val);
5
- }
6
- function isArray(val) {
7
- return Array.isArray(val);
8
- }
9
- export function fieldClear(data, options = {}) {
10
- const { pickKeys, omitKeys, keepValues, excludeValues, keepMap } = options;
11
- const filterObj = (obj) => {
12
- let result = {};
13
- let keys = Object.keys(obj);
14
- if (pickKeys && pickKeys.length) {
15
- keys = keys.filter((k) => pickKeys.includes(k));
16
- }
17
- if (omitKeys && omitKeys.length) {
18
- keys = keys.filter((k) => !omitKeys.includes(k));
19
- }
20
- for (const key of keys) {
21
- const value = obj[key];
22
- // 1. 优先检查 keepMap
23
- if (keepMap && key in keepMap) {
24
- if (Object.is(keepMap[key], value)) {
25
- result[key] = value;
26
- continue;
27
- }
28
- }
29
- // 2. 检查 keepValues (只保留指定值)
30
- if (keepValues && keepValues.length && !keepValues.includes(value)) {
31
- continue;
32
- }
33
- // 3. 检查 excludeValues (排除指定值)
34
- if (excludeValues && excludeValues.length && excludeValues.includes(value)) {
35
- continue;
36
- }
37
- result[key] = value;
38
- }
39
- return result;
40
- };
41
- if (isArray(data)) {
42
- return data
43
- .map((item) => (isObject(item) ? filterObj(item) : item))
44
- .filter((item) => {
45
- if (isObject(item)) {
46
- // 只保留有内容的对象
47
- return Object.keys(item).length > 0;
48
- }
49
- // 原始值直接保留
50
- return true;
51
- });
52
- }
53
- if (isObject(data)) {
54
- return filterObj(data);
55
- }
56
- return data;
57
- }
@@ -1 +0,0 @@
1
- export declare function formatYmdHms(date: Date, format?: "date" | "time" | "dateTime"): string;
@@ -1,20 +0,0 @@
1
- export function formatYmdHms(date, format = "dateTime") {
2
- const y = date.getFullYear();
3
- const m = date.getMonth() + 1;
4
- const d = date.getDate();
5
- const h = date.getHours();
6
- const mi = date.getMinutes();
7
- const s = date.getSeconds();
8
- const mm = m < 10 ? `0${m}` : String(m);
9
- const dd = d < 10 ? `0${d}` : String(d);
10
- const hh = h < 10 ? `0${h}` : String(h);
11
- const mii = mi < 10 ? `0${mi}` : String(mi);
12
- const ss = s < 10 ? `0${s}` : String(s);
13
- if (format === "date") {
14
- return `${y}-${mm}-${dd}`;
15
- }
16
- if (format === "time") {
17
- return `${hh}:${mii}:${ss}`;
18
- }
19
- return `${y}-${mm}-${dd} ${hh}:${mii}:${ss}`;
20
- }
@@ -1,6 +0,0 @@
1
- /**
2
- * 获取客户端 IP(优先代理头,其次 Bun server.requestIP 兜底)
3
- *
4
- * 注意:目前策略是“尽量取到 IP”,未做 trustProxy 防伪造控制。
5
- */
6
- export declare function getClientIp(req: Request, server?: any): string;