befly 3.2.1 → 3.3.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.
- package/bin/index.ts +138 -0
- package/checks/conflict.ts +35 -25
- package/checks/table.ts +6 -6
- package/commands/addon.ts +57 -0
- package/commands/build.ts +74 -0
- package/commands/dev.ts +94 -0
- package/commands/index.ts +252 -0
- package/commands/script.ts +303 -0
- package/commands/start.ts +80 -0
- package/commands/syncApi.ts +327 -0
- package/{scripts → commands}/syncDb/apply.ts +2 -2
- package/{scripts → commands}/syncDb/constants.ts +13 -7
- package/{scripts → commands}/syncDb/ddl.ts +7 -5
- package/{scripts → commands}/syncDb/helpers.ts +18 -18
- package/{scripts → commands}/syncDb/index.ts +37 -23
- package/{scripts → commands}/syncDb/sqlite.ts +1 -1
- package/{scripts → commands}/syncDb/state.ts +10 -4
- package/{scripts → commands}/syncDb/table.ts +7 -7
- package/{scripts → commands}/syncDb/tableCreate.ts +7 -6
- package/{scripts → commands}/syncDb/types.ts +5 -5
- package/{scripts → commands}/syncDb/version.ts +1 -1
- package/commands/syncDb.ts +35 -0
- package/commands/syncDev.ts +174 -0
- package/commands/syncMenu.ts +368 -0
- package/config/env.ts +4 -4
- package/config/menu.json +67 -0
- package/{utils/crypto.ts → lib/cipher.ts} +16 -67
- package/lib/database.ts +296 -0
- package/{utils → lib}/dbHelper.ts +102 -56
- package/{utils → lib}/jwt.ts +124 -151
- package/{utils → lib}/logger.ts +47 -24
- package/lib/middleware.ts +271 -0
- package/{utils → lib}/redisHelper.ts +4 -4
- package/{utils/validate.ts → lib/validator.ts} +101 -78
- package/lifecycle/bootstrap.ts +63 -0
- package/lifecycle/checker.ts +165 -0
- package/lifecycle/cluster.ts +241 -0
- package/lifecycle/lifecycle.ts +139 -0
- package/lifecycle/loader.ts +513 -0
- package/main.ts +14 -12
- package/package.json +21 -9
- package/paths.ts +34 -0
- package/plugins/cache.ts +187 -0
- package/plugins/db.ts +4 -4
- package/plugins/logger.ts +1 -1
- package/plugins/redis.ts +4 -4
- package/router/api.ts +155 -0
- package/router/root.ts +53 -0
- package/router/static.ts +76 -0
- package/types/api.d.ts +0 -36
- package/types/befly.d.ts +8 -6
- package/types/common.d.ts +1 -1
- package/types/context.d.ts +3 -3
- package/types/util.d.ts +45 -0
- package/util.ts +299 -0
- package/config/fields.ts +0 -55
- package/config/regexAliases.ts +0 -51
- package/config/reserved.ts +0 -96
- package/scripts/syncDb/tests/constants.test.ts +0 -105
- package/scripts/syncDb/tests/ddl.test.ts +0 -134
- package/scripts/syncDb/tests/helpers.test.ts +0 -70
- package/scripts/syncDb.ts +0 -10
- package/types/index.d.ts +0 -450
- package/types/index.ts +0 -438
- package/types/validator.ts +0 -43
- package/utils/colors.ts +0 -221
- package/utils/database.ts +0 -348
- package/utils/helper.ts +0 -812
- package/utils/index.ts +0 -33
- package/utils/requestContext.ts +0 -167
- /package/{scripts → commands}/syncDb/schema.ts +0 -0
- /package/{utils → lib}/sqlBuilder.ts +0 -0
- /package/{utils → lib}/xml.ts +0 -0
package/types/index.ts
DELETED
|
@@ -1,438 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Befly 核心类型定义
|
|
3
|
-
* 统一导出所有类型,简化导入路径
|
|
4
|
-
*
|
|
5
|
-
* @example
|
|
6
|
-
* ```typescript
|
|
7
|
-
* import type { ApiRoute, BeflyContext, Plugin } from 'befly/types';
|
|
8
|
-
* ```
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
// 基础类型
|
|
12
|
-
export * from './common.js';
|
|
13
|
-
export * from './context.js';
|
|
14
|
-
|
|
15
|
-
// 核心功能类型
|
|
16
|
-
export * from './api.js';
|
|
17
|
-
export * from './befly.js';
|
|
18
|
-
export * from './plugin.js';
|
|
19
|
-
export * from './addon.js';
|
|
20
|
-
|
|
21
|
-
// 功能模块类型
|
|
22
|
-
export * from './database.js';
|
|
23
|
-
export * from './crypto.js';
|
|
24
|
-
export * from './jwt.js';
|
|
25
|
-
export * from './validator.js';
|
|
26
|
-
export * from './redis.js';
|
|
27
|
-
export * from './logger.js';
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* API 请求上下文
|
|
31
|
-
*/
|
|
32
|
-
export interface RequestContext {
|
|
33
|
-
/** 原始请求对象 */
|
|
34
|
-
req: Request;
|
|
35
|
-
/** 解析后的 URL */
|
|
36
|
-
url: URL;
|
|
37
|
-
/** 路径名 */
|
|
38
|
-
pathname: string;
|
|
39
|
-
/** 查询参数 */
|
|
40
|
-
query: URLSearchParams;
|
|
41
|
-
/** 请求体数据 */
|
|
42
|
-
body: any;
|
|
43
|
-
/** 请求头 */
|
|
44
|
-
headers: Headers;
|
|
45
|
-
/** 路由参数 */
|
|
46
|
-
params?: Record<string, string>;
|
|
47
|
-
/** JWT 载荷(认证后) */
|
|
48
|
-
jwt?: JwtPayload;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* API 响应格式
|
|
53
|
-
*/
|
|
54
|
-
export interface ApiResponse<T = any> {
|
|
55
|
-
/** 状态码:0=成功,1=失败 */
|
|
56
|
-
code: 0 | 1;
|
|
57
|
-
/** 提示信息 */
|
|
58
|
-
msg: string;
|
|
59
|
-
/** 响应数据 */
|
|
60
|
-
data: T;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* API 处理函数
|
|
65
|
-
*/
|
|
66
|
-
export type ApiHandler = (befly: BeflyContext, ctx: RequestContext, req: Request) => Promise<ApiResponse>;
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* API 路由定义
|
|
70
|
-
*/
|
|
71
|
-
export interface ApiRoute {
|
|
72
|
-
/** HTTP 方法 */
|
|
73
|
-
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS';
|
|
74
|
-
/** 接口名称 */
|
|
75
|
-
name: string;
|
|
76
|
-
/** 认证要求: false=公开,true=需登录 */
|
|
77
|
-
auth: boolean;
|
|
78
|
-
/** 字段规则定义 */
|
|
79
|
-
fields: Record<string, string>;
|
|
80
|
-
/** 必填字段列表 */
|
|
81
|
-
required: string[];
|
|
82
|
-
/** 处理函数 */
|
|
83
|
-
handler: ApiHandler;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* JWT 载荷
|
|
88
|
-
*/
|
|
89
|
-
export interface JwtPayload {
|
|
90
|
-
/** 用户 ID */
|
|
91
|
-
userId?: string;
|
|
92
|
-
/** 用户名 */
|
|
93
|
-
username?: string;
|
|
94
|
-
/** 角色 */
|
|
95
|
-
role?: string;
|
|
96
|
-
/** 权限列表 */
|
|
97
|
-
permissions?: string[];
|
|
98
|
-
/** 签发时间 */
|
|
99
|
-
iat?: number;
|
|
100
|
-
/** 过期时间 */
|
|
101
|
-
exp?: number;
|
|
102
|
-
/** 主题 */
|
|
103
|
-
sub?: string;
|
|
104
|
-
/** 签发者 */
|
|
105
|
-
iss?: string;
|
|
106
|
-
/** 受众 */
|
|
107
|
-
aud?: string;
|
|
108
|
-
/** JWT ID */
|
|
109
|
-
jti?: string;
|
|
110
|
-
/** 生效时间 */
|
|
111
|
-
nbf?: number;
|
|
112
|
-
/** 其他自定义字段 */
|
|
113
|
-
[key: string]: any;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* JWT 签名选项
|
|
118
|
-
*/
|
|
119
|
-
export interface JwtSignOptions {
|
|
120
|
-
/** 密钥 */
|
|
121
|
-
secret?: string;
|
|
122
|
-
/** 算法 */
|
|
123
|
-
algorithm?: string;
|
|
124
|
-
/** 过期时间 */
|
|
125
|
-
expiresIn?: string;
|
|
126
|
-
/** 签发者 */
|
|
127
|
-
issuer?: string;
|
|
128
|
-
/** 受众 */
|
|
129
|
-
audience?: string;
|
|
130
|
-
/** 主题 */
|
|
131
|
-
subject?: string;
|
|
132
|
-
/** 生效时间 */
|
|
133
|
-
notBefore?: string | number;
|
|
134
|
-
/** JWT ID */
|
|
135
|
-
jwtId?: string;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Befly 上下文接口
|
|
140
|
-
*/
|
|
141
|
-
export interface BeflyContext {
|
|
142
|
-
/** 数据库管理器 */
|
|
143
|
-
db: DbHelper;
|
|
144
|
-
/** Redis 助手 */
|
|
145
|
-
redis: typeof RedisHelper;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* 插件定义
|
|
150
|
-
*/
|
|
151
|
-
export interface Plugin {
|
|
152
|
-
/** 插件名称 */
|
|
153
|
-
name?: string;
|
|
154
|
-
/** 依赖的插件列表 */
|
|
155
|
-
after?: string[];
|
|
156
|
-
/** 初始化函数 */
|
|
157
|
-
onInit?: (befly: BeflyContext) => Promise<any>;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* 服务器配置
|
|
162
|
-
*/
|
|
163
|
-
export interface ServerConfig {
|
|
164
|
-
/** 服务名称 */
|
|
165
|
-
name: string;
|
|
166
|
-
/** 端口号 */
|
|
167
|
-
port: number;
|
|
168
|
-
/** 主机 */
|
|
169
|
-
host?: string;
|
|
170
|
-
/** 静态文件目录 */
|
|
171
|
-
static?: string;
|
|
172
|
-
/** 自定义插件 */
|
|
173
|
-
plugins?: Plugin[];
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* SQL 查询操作符
|
|
178
|
-
*/
|
|
179
|
-
export interface WhereOperator {
|
|
180
|
-
/** 大于 */
|
|
181
|
-
$gt?: number;
|
|
182
|
-
/** 大于等于 */
|
|
183
|
-
$gte?: number;
|
|
184
|
-
/** 小于 */
|
|
185
|
-
$lt?: number;
|
|
186
|
-
/** 小于等于 */
|
|
187
|
-
$lte?: number;
|
|
188
|
-
/** 不等于 */
|
|
189
|
-
$ne?: any;
|
|
190
|
-
/** 不等于($ne 的别名) */
|
|
191
|
-
$not?: any;
|
|
192
|
-
/** 在...之中 */
|
|
193
|
-
$in?: any[];
|
|
194
|
-
/** 不在...之中 */
|
|
195
|
-
$nin?: any[];
|
|
196
|
-
/** 不在...之中($nin 的别名) */
|
|
197
|
-
$notIn?: any[];
|
|
198
|
-
/** LIKE 模糊匹配 */
|
|
199
|
-
$like?: string;
|
|
200
|
-
/** NOT LIKE 模糊匹配 */
|
|
201
|
-
$notLike?: string;
|
|
202
|
-
/** BETWEEN...AND 范围查询 */
|
|
203
|
-
$between?: [any, any];
|
|
204
|
-
/** NOT BETWEEN...AND 范围查询 */
|
|
205
|
-
$notBetween?: [any, any];
|
|
206
|
-
/** IS NULL 空值检查 */
|
|
207
|
-
$null?: boolean;
|
|
208
|
-
/** IS NOT NULL 非空检查 */
|
|
209
|
-
$notNull?: boolean;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
/**
|
|
213
|
-
* WHERE 条件
|
|
214
|
-
* 支持两种格式:
|
|
215
|
-
* 1. 嵌套格式:{ age: { $gt: 18 } }
|
|
216
|
-
* 2. 一级属性格式:{ 'age$gt': 18 }(推荐)
|
|
217
|
-
*/
|
|
218
|
-
export type WhereConditions = {
|
|
219
|
-
/** 普通字段条件(支持一级属性格式:'field$operator') */
|
|
220
|
-
[field: string]:
|
|
221
|
-
| string
|
|
222
|
-
| number
|
|
223
|
-
| boolean
|
|
224
|
-
| null
|
|
225
|
-
| any[] // 支持 $in、$nin 的数组值
|
|
226
|
-
| WhereOperator // 支持嵌套格式
|
|
227
|
-
| WhereConditions[]; // 支持 $or、$and 的数组值
|
|
228
|
-
|
|
229
|
-
/** OR 逻辑操作符 */
|
|
230
|
-
$or?: WhereConditions[];
|
|
231
|
-
|
|
232
|
-
/** AND 逻辑操作符 */
|
|
233
|
-
$and?: WhereConditions[];
|
|
234
|
-
};
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* SQL 查询选项
|
|
238
|
-
*/
|
|
239
|
-
export interface QueryOptions {
|
|
240
|
-
/** 表名 */
|
|
241
|
-
table: string;
|
|
242
|
-
/** 查询字段,默认 ['*'] */
|
|
243
|
-
fields?: string[];
|
|
244
|
-
/** WHERE 条件 */
|
|
245
|
-
where?: WhereConditions;
|
|
246
|
-
/** 排序(格式:["字段#ASC", "字段#DESC"]) */
|
|
247
|
-
orderBy?: string[];
|
|
248
|
-
/** 页码(从 1 开始) */
|
|
249
|
-
page?: number;
|
|
250
|
-
/** 每页数量 */
|
|
251
|
-
limit?: number;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
/**
|
|
255
|
-
* 插入选项
|
|
256
|
-
*/
|
|
257
|
-
export interface InsertOptions {
|
|
258
|
-
/** 表名 */
|
|
259
|
-
table: string;
|
|
260
|
-
/** 插入数据(ID、时间戳、state 会自动生成) */
|
|
261
|
-
data: Record<string, any>;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
/**
|
|
265
|
-
* 更新选项
|
|
266
|
-
*/
|
|
267
|
-
export interface UpdateOptions {
|
|
268
|
-
/** 表名 */
|
|
269
|
-
table: string;
|
|
270
|
-
/** 更新数据(updated_at 会自动更新) */
|
|
271
|
-
data: Record<string, any>;
|
|
272
|
-
/** WHERE 条件 */
|
|
273
|
-
where: WhereConditions;
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
/**
|
|
277
|
-
* 删除选项
|
|
278
|
-
*/
|
|
279
|
-
export interface DeleteOptions {
|
|
280
|
-
/** 表名 */
|
|
281
|
-
table: string;
|
|
282
|
-
/** WHERE 条件 */
|
|
283
|
-
where: WhereConditions;
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
/**
|
|
287
|
-
* 列表查询结果
|
|
288
|
-
*/
|
|
289
|
-
export interface ListResult<T = any> {
|
|
290
|
-
/** 数据列表 */
|
|
291
|
-
list: T[];
|
|
292
|
-
/** 总条数 */
|
|
293
|
-
total: number;
|
|
294
|
-
/** 当前页码 */
|
|
295
|
-
page: number;
|
|
296
|
-
/** 每页数量 */
|
|
297
|
-
limit: number;
|
|
298
|
-
/** 总页数 */
|
|
299
|
-
pages: number;
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
/**
|
|
303
|
-
* 事务回调函数
|
|
304
|
-
*/
|
|
305
|
-
export type TransactionCallback<T = any> = (trans: DbHelper) => Promise<T>;
|
|
306
|
-
|
|
307
|
-
/**
|
|
308
|
-
* SQL 查询对象
|
|
309
|
-
*/
|
|
310
|
-
export interface SqlQuery {
|
|
311
|
-
/** SQL 语句 */
|
|
312
|
-
sql: string;
|
|
313
|
-
/** 参数列表 */
|
|
314
|
-
params: any[];
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
/**
|
|
318
|
-
* 数据库管理器接口
|
|
319
|
-
*/
|
|
320
|
-
export interface DbHelper {
|
|
321
|
-
/** 查询记录数 */
|
|
322
|
-
getCount(options: Omit<QueryOptions, 'fields' | 'page' | 'limit' | 'orderBy'>): Promise<number>;
|
|
323
|
-
/** 查询单条数据 */
|
|
324
|
-
getOne<T = any>(options: QueryOptions): Promise<T | null>;
|
|
325
|
-
/** 查询列表(带分页) */
|
|
326
|
-
getList<T = any>(options: QueryOptions): Promise<ListResult<T>>;
|
|
327
|
-
/** 查询所有数据 */
|
|
328
|
-
getAll<T = any>(options: Omit<QueryOptions, 'page' | 'limit'>): Promise<T[]>;
|
|
329
|
-
/** 插入数据 */
|
|
330
|
-
insData(options: InsertOptions): Promise<number>;
|
|
331
|
-
/** 更新数据 */
|
|
332
|
-
updData(options: UpdateOptions): Promise<number>;
|
|
333
|
-
/** 删除数据 */
|
|
334
|
-
delData(options: DeleteOptions): Promise<number>;
|
|
335
|
-
/** 执行事务 */
|
|
336
|
-
trans<T = any>(callback: TransactionCallback<T>): Promise<T>;
|
|
337
|
-
/** 执行原始 SQL */
|
|
338
|
-
query(sql: string, params?: any[]): Promise<any>;
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
/**
|
|
342
|
-
* Redis 助手接口
|
|
343
|
-
*/
|
|
344
|
-
export interface RedisHelper {
|
|
345
|
-
/** 设置对象到 Redis */
|
|
346
|
-
setObject<T = any>(key: string, obj: T, ttl?: number | null): Promise<string | null>;
|
|
347
|
-
/** 从 Redis 获取对象 */
|
|
348
|
-
getObject<T = any>(key: string): Promise<T | null>;
|
|
349
|
-
/** 从 Redis 删除对象 */
|
|
350
|
-
delObject(key: string): Promise<void>;
|
|
351
|
-
/** 生成基于时间的唯一 ID */
|
|
352
|
-
genTimeID(): Promise<number>;
|
|
353
|
-
/** 设置字符串值 */
|
|
354
|
-
setString(key: string, value: string, ttl?: number | null): Promise<string | null>;
|
|
355
|
-
/** 获取字符串值 */
|
|
356
|
-
getString(key: string): Promise<string | null>;
|
|
357
|
-
/** 检查键是否存在 */
|
|
358
|
-
exists(key: string): Promise<number>;
|
|
359
|
-
/** 设置过期时间 */
|
|
360
|
-
expire(key: string, seconds: number): Promise<number>;
|
|
361
|
-
/** 获取剩余过期时间 */
|
|
362
|
-
ttl(key: string): Promise<number>;
|
|
363
|
-
/** 测试 Redis 连接 */
|
|
364
|
-
ping(): Promise<string>;
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
/**
|
|
368
|
-
* 日志记录器接口
|
|
369
|
-
*/
|
|
370
|
-
export interface Logger {
|
|
371
|
-
/** 记录信息日志 */
|
|
372
|
-
info(message: string | object): void;
|
|
373
|
-
/** 记录警告日志 */
|
|
374
|
-
warn(message: string | object): void;
|
|
375
|
-
/** 记录错误日志 */
|
|
376
|
-
error(message: string | object): void;
|
|
377
|
-
/** 记录调试日志 */
|
|
378
|
-
debug(message: string | object): void;
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
/**
|
|
382
|
-
* 验证结果
|
|
383
|
-
*/
|
|
384
|
-
export interface ValidationResult {
|
|
385
|
-
/** 是否有效 */
|
|
386
|
-
valid: boolean;
|
|
387
|
-
/** 处理后的值 */
|
|
388
|
-
value: any;
|
|
389
|
-
/** 错误列表 */
|
|
390
|
-
errors: string[];
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
/**
|
|
394
|
-
* 数据库类型
|
|
395
|
-
*/
|
|
396
|
-
export type DbType = 'mysql' | 'postgresql' | 'sqlite';
|
|
397
|
-
|
|
398
|
-
/**
|
|
399
|
-
* 列信息
|
|
400
|
-
*/
|
|
401
|
-
export interface ColumnInfo {
|
|
402
|
-
/** 列名 */
|
|
403
|
-
name: string;
|
|
404
|
-
/** 数据类型 */
|
|
405
|
-
type: string;
|
|
406
|
-
/** 是否可空 */
|
|
407
|
-
nullable: boolean;
|
|
408
|
-
/** 默认值 */
|
|
409
|
-
default: any;
|
|
410
|
-
/** 注释 */
|
|
411
|
-
comment: string;
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
/**
|
|
415
|
-
* 索引信息
|
|
416
|
-
*/
|
|
417
|
-
export interface IndexInfo {
|
|
418
|
-
/** 索引名 */
|
|
419
|
-
name: string;
|
|
420
|
-
/** 列名 */
|
|
421
|
-
column: string;
|
|
422
|
-
/** 是否唯一 */
|
|
423
|
-
unique: boolean;
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
/**
|
|
427
|
-
* 同步统计
|
|
428
|
-
*/
|
|
429
|
-
export interface SyncStats {
|
|
430
|
-
/** 创建的表数 */
|
|
431
|
-
created: number;
|
|
432
|
-
/** 修改的表数 */
|
|
433
|
-
modified: number;
|
|
434
|
-
/** 创建的索引数 */
|
|
435
|
-
indexesCreated: number;
|
|
436
|
-
/** 删除的索引数 */
|
|
437
|
-
indexesDropped: number;
|
|
438
|
-
}
|
package/types/validator.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 验证器相关类型定义
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* 字段验证错误信息
|
|
7
|
-
*/
|
|
8
|
-
export type ValidationError = string | null;
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* 字段类型
|
|
12
|
-
*/
|
|
13
|
-
export type FieldType = 'string' | 'number' | 'text' | 'array';
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* 验证结果
|
|
17
|
-
*/
|
|
18
|
-
export interface ValidationResult {
|
|
19
|
-
/** 验证状态码:0=成功,1=失败 */
|
|
20
|
-
code: 0 | 1;
|
|
21
|
-
/** 字段验证结果 */
|
|
22
|
-
fields: Record<string, any>;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* 字段规则
|
|
27
|
-
*/
|
|
28
|
-
export interface FieldRule {
|
|
29
|
-
/** 字段名 */
|
|
30
|
-
name: string;
|
|
31
|
-
/** 字段类型 */
|
|
32
|
-
type: FieldType;
|
|
33
|
-
/** 最小值/长度 */
|
|
34
|
-
min: number | null;
|
|
35
|
-
/** 最大值/长度 */
|
|
36
|
-
max: number | null;
|
|
37
|
-
/** 默认值 */
|
|
38
|
-
default: any;
|
|
39
|
-
/** 是否索引 */
|
|
40
|
-
index: 0 | 1;
|
|
41
|
-
/** 正则约束 */
|
|
42
|
-
regex: string | null;
|
|
43
|
-
}
|
package/utils/colors.ts
DELETED
|
@@ -1,221 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 终端颜色工具 - TypeScript 版本
|
|
3
|
-
* 提供 ANSI 颜色和样式支持,自动检测终端颜色能力
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* 格式化函数类型
|
|
8
|
-
*/
|
|
9
|
-
type Formatter = (input: string | number) => string;
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* 颜色工具接口
|
|
13
|
-
*/
|
|
14
|
-
interface ColorsInterface {
|
|
15
|
-
/** 是否支持颜色 */
|
|
16
|
-
isColorSupported: boolean;
|
|
17
|
-
|
|
18
|
-
// 样式
|
|
19
|
-
/** 重置所有样式 */
|
|
20
|
-
reset: Formatter;
|
|
21
|
-
/** 加粗 */
|
|
22
|
-
bold: Formatter;
|
|
23
|
-
/** 变暗 */
|
|
24
|
-
dim: Formatter;
|
|
25
|
-
/** 斜体 */
|
|
26
|
-
italic: Formatter;
|
|
27
|
-
/** 下划线 */
|
|
28
|
-
underline: Formatter;
|
|
29
|
-
/** 反色 */
|
|
30
|
-
inverse: Formatter;
|
|
31
|
-
/** 隐藏 */
|
|
32
|
-
hidden: Formatter;
|
|
33
|
-
/** 删除线 */
|
|
34
|
-
strikethrough: Formatter;
|
|
35
|
-
|
|
36
|
-
// 前景色(标准)
|
|
37
|
-
black: Formatter;
|
|
38
|
-
red: Formatter;
|
|
39
|
-
green: Formatter;
|
|
40
|
-
yellow: Formatter;
|
|
41
|
-
blue: Formatter;
|
|
42
|
-
magenta: Formatter;
|
|
43
|
-
cyan: Formatter;
|
|
44
|
-
white: Formatter;
|
|
45
|
-
gray: Formatter;
|
|
46
|
-
|
|
47
|
-
// 前景色(高亮)
|
|
48
|
-
blackBright: Formatter;
|
|
49
|
-
redBright: Formatter;
|
|
50
|
-
greenBright: Formatter;
|
|
51
|
-
yellowBright: Formatter;
|
|
52
|
-
blueBright: Formatter;
|
|
53
|
-
magentaBright: Formatter;
|
|
54
|
-
cyanBright: Formatter;
|
|
55
|
-
whiteBright: Formatter;
|
|
56
|
-
|
|
57
|
-
// 背景色(标准)
|
|
58
|
-
bgBlack: Formatter;
|
|
59
|
-
bgRed: Formatter;
|
|
60
|
-
bgGreen: Formatter;
|
|
61
|
-
bgYellow: Formatter;
|
|
62
|
-
bgBlue: Formatter;
|
|
63
|
-
bgMagenta: Formatter;
|
|
64
|
-
bgCyan: Formatter;
|
|
65
|
-
bgWhite: Formatter;
|
|
66
|
-
|
|
67
|
-
// 背景色(高亮)
|
|
68
|
-
bgBlackBright: Formatter;
|
|
69
|
-
bgRedBright: Formatter;
|
|
70
|
-
bgGreenBright: Formatter;
|
|
71
|
-
bgYellowBright: Formatter;
|
|
72
|
-
bgBlueBright: Formatter;
|
|
73
|
-
bgMagentaBright: Formatter;
|
|
74
|
-
bgCyanBright: Formatter;
|
|
75
|
-
bgWhiteBright: Formatter;
|
|
76
|
-
|
|
77
|
-
// 语义化颜色(带图标)
|
|
78
|
-
info: Formatter;
|
|
79
|
-
success: Formatter;
|
|
80
|
-
warn: Formatter;
|
|
81
|
-
error: Formatter;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* 检测是否支持颜色
|
|
86
|
-
*/
|
|
87
|
-
const detectColorSupport = (): boolean => {
|
|
88
|
-
const env = process.env || {};
|
|
89
|
-
const argv = process.argv || [];
|
|
90
|
-
|
|
91
|
-
// 明确禁用颜色
|
|
92
|
-
if (env.NO_COLOR || argv.includes('--no-color')) {
|
|
93
|
-
return false;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// 明确启用颜色
|
|
97
|
-
if (env.FORCE_COLOR || argv.includes('--color')) {
|
|
98
|
-
return true;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// 根据平台和终端环境判断
|
|
102
|
-
return process.platform === 'win32' || (process.stdout?.isTTY && env.TERM !== 'dumb') || !!env.CI;
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
const isColorSupported = detectColorSupport();
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* 创建格式化函数
|
|
109
|
-
* @param open - 开始 ANSI 码
|
|
110
|
-
* @param close - 结束 ANSI 码
|
|
111
|
-
* @param replace - 替换的 ANSI 码(用于嵌套)
|
|
112
|
-
* @returns 格式化函数
|
|
113
|
-
*/
|
|
114
|
-
const formatter =
|
|
115
|
-
(open: string, close: string, replace: string = open): Formatter =>
|
|
116
|
-
(input: string | number): string => {
|
|
117
|
-
const string = String(input);
|
|
118
|
-
const index = string.indexOf(close, open.length);
|
|
119
|
-
// 如果字符串中包含结束码,需要处理嵌套情况
|
|
120
|
-
return ~index ? open + replaceClose(string, close, replace, index) + close : open + string + close;
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* 替换字符串中的结束码(处理嵌套颜色)
|
|
125
|
-
* @param string - 原始字符串
|
|
126
|
-
* @param close - 结束码
|
|
127
|
-
* @param replace - 替换码
|
|
128
|
-
* @param index - 起始索引
|
|
129
|
-
* @returns 处理后的字符串
|
|
130
|
-
*/
|
|
131
|
-
const replaceClose = (string: string, close: string, replace: string, index: number): string => {
|
|
132
|
-
let result = '';
|
|
133
|
-
let cursor = 0;
|
|
134
|
-
do {
|
|
135
|
-
result += string.substring(cursor, index) + replace;
|
|
136
|
-
cursor = index + close.length;
|
|
137
|
-
index = string.indexOf(close, cursor);
|
|
138
|
-
} while (~index);
|
|
139
|
-
return result + string.substring(cursor);
|
|
140
|
-
};
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* 创建颜色工具实例
|
|
144
|
-
* @param enabled - 是否启用颜色
|
|
145
|
-
* @returns 颜色工具对象
|
|
146
|
-
*/
|
|
147
|
-
const createColors = (enabled: boolean = isColorSupported): ColorsInterface => {
|
|
148
|
-
// 如果禁用颜色,返回直接转字符串的函数
|
|
149
|
-
const f = enabled ? formatter : (): Formatter => (input: string | number) => String(input);
|
|
150
|
-
|
|
151
|
-
return {
|
|
152
|
-
isColorSupported: enabled,
|
|
153
|
-
|
|
154
|
-
// 样式 - https://en.wikipedia.org/wiki/ANSI_escape_code#SGR
|
|
155
|
-
reset: f('\x1b[0m', '\x1b[0m'),
|
|
156
|
-
bold: f('\x1b[1m', '\x1b[22m', '\x1b[22m\x1b[1m'),
|
|
157
|
-
dim: f('\x1b[2m', '\x1b[22m', '\x1b[22m\x1b[2m'),
|
|
158
|
-
italic: f('\x1b[3m', '\x1b[23m'),
|
|
159
|
-
underline: f('\x1b[4m', '\x1b[24m'),
|
|
160
|
-
inverse: f('\x1b[7m', '\x1b[27m'),
|
|
161
|
-
hidden: f('\x1b[8m', '\x1b[28m'),
|
|
162
|
-
strikethrough: f('\x1b[9m', '\x1b[29m'),
|
|
163
|
-
|
|
164
|
-
// 前景色(标准) - 30-37
|
|
165
|
-
black: f('\x1b[30m', '\x1b[39m'),
|
|
166
|
-
red: f('\x1b[31m', '\x1b[39m'),
|
|
167
|
-
green: f('\x1b[32m', '\x1b[39m'),
|
|
168
|
-
yellow: f('\x1b[33m', '\x1b[39m'),
|
|
169
|
-
blue: f('\x1b[34m', '\x1b[39m'),
|
|
170
|
-
magenta: f('\x1b[35m', '\x1b[39m'),
|
|
171
|
-
cyan: f('\x1b[36m', '\x1b[39m'),
|
|
172
|
-
white: f('\x1b[37m', '\x1b[39m'),
|
|
173
|
-
gray: f('\x1b[90m', '\x1b[39m'), // 实际上是 blackBright
|
|
174
|
-
|
|
175
|
-
// 前景色(高亮) - 90-97
|
|
176
|
-
blackBright: f('\x1b[90m', '\x1b[39m'),
|
|
177
|
-
redBright: f('\x1b[91m', '\x1b[39m'),
|
|
178
|
-
greenBright: f('\x1b[92m', '\x1b[39m'),
|
|
179
|
-
yellowBright: f('\x1b[93m', '\x1b[39m'),
|
|
180
|
-
blueBright: f('\x1b[94m', '\x1b[39m'),
|
|
181
|
-
magentaBright: f('\x1b[95m', '\x1b[39m'),
|
|
182
|
-
cyanBright: f('\x1b[96m', '\x1b[39m'),
|
|
183
|
-
whiteBright: f('\x1b[97m', '\x1b[39m'),
|
|
184
|
-
|
|
185
|
-
// 背景色(标准) - 40-47
|
|
186
|
-
bgBlack: f('\x1b[40m', '\x1b[49m'),
|
|
187
|
-
bgRed: f('\x1b[41m', '\x1b[49m'),
|
|
188
|
-
bgGreen: f('\x1b[42m', '\x1b[49m'),
|
|
189
|
-
bgYellow: f('\x1b[43m', '\x1b[49m'),
|
|
190
|
-
bgBlue: f('\x1b[44m', '\x1b[49m'),
|
|
191
|
-
bgMagenta: f('\x1b[45m', '\x1b[49m'),
|
|
192
|
-
bgCyan: f('\x1b[46m', '\x1b[49m'),
|
|
193
|
-
bgWhite: f('\x1b[47m', '\x1b[49m'),
|
|
194
|
-
|
|
195
|
-
// 背景色(高亮) - 100-107
|
|
196
|
-
bgBlackBright: f('\x1b[100m', '\x1b[49m'),
|
|
197
|
-
bgRedBright: f('\x1b[101m', '\x1b[49m'),
|
|
198
|
-
bgGreenBright: f('\x1b[102m', '\x1b[49m'),
|
|
199
|
-
bgYellowBright: f('\x1b[103m', '\x1b[49m'),
|
|
200
|
-
bgBlueBright: f('\x1b[104m', '\x1b[49m'),
|
|
201
|
-
bgMagentaBright: f('\x1b[105m', '\x1b[49m'),
|
|
202
|
-
bgCyanBright: f('\x1b[106m', '\x1b[49m'),
|
|
203
|
-
bgWhiteBright: f('\x1b[107m', '\x1b[49m'),
|
|
204
|
-
|
|
205
|
-
// 语义化颜色(函数形式,带图标前缀)
|
|
206
|
-
info: (input: string | number) => f('\x1b[34m', '\x1b[39m')(`ℹ ${input}`),
|
|
207
|
-
success: (input: string | number) => f('\x1b[32m', '\x1b[39m')(`✓ ${input}`),
|
|
208
|
-
warn: (input: string | number) => f('\x1b[33m', '\x1b[39m')(`⚠ ${input}`),
|
|
209
|
-
error: (input: string | number) => f('\x1b[31m', '\x1b[39m')(`✖ ${input}`)
|
|
210
|
-
};
|
|
211
|
-
};
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* 默认颜色工具实例
|
|
215
|
-
*/
|
|
216
|
-
export const Colors = createColors();
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* 导出类型
|
|
220
|
-
*/
|
|
221
|
-
export type { ColorsInterface, Formatter };
|