befly 2.3.2 → 3.0.0
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/apis/health/info.ts +64 -0
- package/apis/tool/tokenCheck.ts +51 -0
- package/bin/befly.ts +202 -0
- package/checks/conflict.ts +408 -0
- package/checks/table.ts +284 -0
- package/config/env.ts +218 -0
- package/config/reserved.ts +96 -0
- package/main.ts +101 -0
- package/package.json +45 -16
- package/plugins/{db.js → db.ts} +25 -12
- package/plugins/logger.ts +28 -0
- package/plugins/redis.ts +51 -0
- package/plugins/tool.ts +34 -0
- package/scripts/syncDb/apply.ts +171 -0
- package/scripts/syncDb/constants.ts +70 -0
- package/scripts/syncDb/ddl.ts +182 -0
- package/scripts/syncDb/helpers.ts +172 -0
- package/scripts/syncDb/index.ts +215 -0
- package/scripts/syncDb/schema.ts +199 -0
- package/scripts/syncDb/sqlite.ts +50 -0
- package/scripts/syncDb/state.ts +104 -0
- package/scripts/syncDb/table.ts +204 -0
- package/scripts/syncDb/tableCreate.ts +142 -0
- package/scripts/syncDb/tests/constants.test.ts +104 -0
- package/scripts/syncDb/tests/ddl.test.ts +134 -0
- package/scripts/syncDb/tests/helpers.test.ts +70 -0
- package/scripts/syncDb/types.ts +92 -0
- package/scripts/syncDb/version.ts +73 -0
- package/scripts/syncDb.ts +9 -0
- package/scripts/syncDev.ts +112 -0
- package/system.ts +149 -0
- package/tables/_common.json +21 -0
- package/tables/admin.json +10 -0
- package/tsconfig.json +58 -0
- package/types/api.d.ts +246 -0
- package/types/befly.d.ts +234 -0
- package/types/common.d.ts +215 -0
- package/types/context.ts +167 -0
- package/types/crypto.d.ts +23 -0
- package/types/database.d.ts +278 -0
- package/types/index.d.ts +16 -0
- package/types/index.ts +459 -0
- package/types/jwt.d.ts +99 -0
- package/types/logger.d.ts +43 -0
- package/types/plugin.d.ts +109 -0
- package/types/redis.d.ts +44 -0
- package/types/tool.d.ts +67 -0
- package/types/validator.d.ts +45 -0
- package/utils/addonHelper.ts +60 -0
- package/utils/api.ts +23 -0
- package/utils/{colors.js → colors.ts} +79 -21
- package/utils/crypto.ts +308 -0
- package/utils/datetime.ts +51 -0
- package/utils/dbHelper.ts +142 -0
- package/utils/errorHandler.ts +68 -0
- package/utils/index.ts +46 -0
- package/utils/jwt.ts +493 -0
- package/utils/logger.ts +284 -0
- package/utils/objectHelper.ts +68 -0
- package/utils/pluginHelper.ts +62 -0
- package/utils/redisHelper.ts +338 -0
- package/utils/response.ts +38 -0
- package/utils/{sqlBuilder.js → sqlBuilder.ts} +233 -97
- package/utils/sqlHelper.ts +447 -0
- package/utils/tableHelper.ts +167 -0
- package/utils/tool.ts +230 -0
- package/utils/typeHelper.ts +101 -0
- package/utils/validate.ts +451 -0
- package/utils/{xml.js → xml.ts} +100 -74
- package/.npmrc +0 -3
- package/.prettierignore +0 -2
- package/.prettierrc +0 -11
- package/apis/health/info.js +0 -49
- package/apis/tool/tokenCheck.js +0 -29
- package/checks/table.js +0 -221
- package/config/env.js +0 -62
- package/main.js +0 -579
- package/plugins/logger.js +0 -14
- package/plugins/redis.js +0 -32
- package/plugins/tool.js +0 -8
- package/scripts/syncDb.js +0 -603
- package/system.js +0 -118
- package/tables/common.json +0 -16
- package/tables/tool.json +0 -6
- package/utils/api.js +0 -27
- package/utils/crypto.js +0 -260
- package/utils/index.js +0 -387
- package/utils/jwt.js +0 -387
- package/utils/logger.js +0 -143
- package/utils/redisHelper.js +0 -74
- package/utils/sqlManager.js +0 -471
- package/utils/tool.js +0 -31
- package/utils/validate.js +0 -228
package/types/api.d.ts
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Befly API 类型定义
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { BeflyContext } from './befly.js';
|
|
6
|
+
import type { KeyValue, TableDefinition } from './common.js';
|
|
7
|
+
import type { RequestContext } from './context.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* HTTP 方法类型
|
|
11
|
+
*/
|
|
12
|
+
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 请求上下文类型(已废弃,使用 RequestContext 类)
|
|
16
|
+
* @deprecated 使用 RequestContext 类替代
|
|
17
|
+
*/
|
|
18
|
+
export interface RequestContext<T = any> {
|
|
19
|
+
/** 请求对象 */
|
|
20
|
+
request: Request;
|
|
21
|
+
|
|
22
|
+
/** 请求体参数 */
|
|
23
|
+
body: T;
|
|
24
|
+
|
|
25
|
+
/** URL 参数 */
|
|
26
|
+
query: KeyValue<string>;
|
|
27
|
+
|
|
28
|
+
/** 路径参数 */
|
|
29
|
+
pathParams: KeyValue<string>;
|
|
30
|
+
|
|
31
|
+
/** 请求头 */
|
|
32
|
+
headers: Headers;
|
|
33
|
+
|
|
34
|
+
/** 用户信息(认证后) */
|
|
35
|
+
user?: UserInfo;
|
|
36
|
+
|
|
37
|
+
/** 客户端 IP */
|
|
38
|
+
ip?: string;
|
|
39
|
+
|
|
40
|
+
/** User-Agent */
|
|
41
|
+
userAgent?: string;
|
|
42
|
+
|
|
43
|
+
/** 请求 ID */
|
|
44
|
+
requestId?: string;
|
|
45
|
+
|
|
46
|
+
/** 开始时间 */
|
|
47
|
+
startTime: number;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 用户信息类型
|
|
52
|
+
*/
|
|
53
|
+
export interface UserInfo {
|
|
54
|
+
id: number;
|
|
55
|
+
username?: string;
|
|
56
|
+
email?: string;
|
|
57
|
+
role?: string;
|
|
58
|
+
[key: string]: any;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 认证类型
|
|
63
|
+
* - false: 不需要认证
|
|
64
|
+
* - true: 需要认证(验证 token)
|
|
65
|
+
* - 'admin': 需要管理员权限
|
|
66
|
+
* - 'user': 需要普通用户权限
|
|
67
|
+
* - string[]: 需要特定角色
|
|
68
|
+
*/
|
|
69
|
+
export type AuthType = boolean | 'admin' | 'user' | string[];
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* API 处理器函数类型
|
|
73
|
+
*/
|
|
74
|
+
export type ApiHandler<T = any, R = any> = (befly: BeflyContext, ctx: RequestContext, req?: Request) => Promise<Response | R> | Response | R;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 字段规则定义
|
|
78
|
+
* 键为字段名,值为字段规则字符串
|
|
79
|
+
*/
|
|
80
|
+
export type FieldRules = Record<string, string>;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* API 配置选项
|
|
84
|
+
*/
|
|
85
|
+
export interface ApiOptions {
|
|
86
|
+
/** HTTP 方法 */
|
|
87
|
+
method: HttpMethod;
|
|
88
|
+
/** 是否需要认证(true/false/角色数组) */
|
|
89
|
+
auth?: boolean | string[];
|
|
90
|
+
/** 字段规则 */
|
|
91
|
+
fields?: FieldRules;
|
|
92
|
+
/** 必填字段 */
|
|
93
|
+
required?: string[];
|
|
94
|
+
/** 处理函数 */
|
|
95
|
+
handler: ApiHandler;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* API 路由配置
|
|
100
|
+
*/
|
|
101
|
+
export interface ApiRoute<T = any, R = any> {
|
|
102
|
+
/** HTTP 方法 */
|
|
103
|
+
method: HttpMethod;
|
|
104
|
+
|
|
105
|
+
/** 接口名称 */
|
|
106
|
+
name: string;
|
|
107
|
+
|
|
108
|
+
/** 路由路径(运行时生成) */
|
|
109
|
+
route?: string;
|
|
110
|
+
|
|
111
|
+
/** 认证类型 */
|
|
112
|
+
auth: boolean | string | string[];
|
|
113
|
+
|
|
114
|
+
/** 字段定义(验证规则) */
|
|
115
|
+
fields: TableDefinition;
|
|
116
|
+
|
|
117
|
+
/** 必填字段 */
|
|
118
|
+
required: string[];
|
|
119
|
+
|
|
120
|
+
/** 处理器函数 */
|
|
121
|
+
handler: ApiHandler<T, R>;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* API 构建器接口
|
|
126
|
+
*/
|
|
127
|
+
export interface ApiBuilder {
|
|
128
|
+
/** 设置 HTTP 方法 */
|
|
129
|
+
method(method: HttpMethod): this;
|
|
130
|
+
|
|
131
|
+
/** 设置路径 */
|
|
132
|
+
path(path: string): this;
|
|
133
|
+
|
|
134
|
+
/** 设置描述 */
|
|
135
|
+
description(desc: string): this;
|
|
136
|
+
|
|
137
|
+
/** 设置认证 */
|
|
138
|
+
auth(auth: AuthType): this;
|
|
139
|
+
|
|
140
|
+
/** 设置验证规则 */
|
|
141
|
+
rules(rules: KeyValue<string>): this;
|
|
142
|
+
|
|
143
|
+
/** 设置必填字段 */
|
|
144
|
+
required(fields: string[]): this;
|
|
145
|
+
|
|
146
|
+
/** 设置处理器 */
|
|
147
|
+
handler(handler: ApiHandler): this;
|
|
148
|
+
|
|
149
|
+
/** 构建路由 */
|
|
150
|
+
build(): ApiRoute;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* API 响应辅助函数
|
|
155
|
+
*/
|
|
156
|
+
export interface ApiResponse {
|
|
157
|
+
/** 成功响应 */
|
|
158
|
+
success<T = any>(message: string, data?: T): Response;
|
|
159
|
+
|
|
160
|
+
/** 失败响应 */
|
|
161
|
+
error(message: string, error?: any): Response;
|
|
162
|
+
|
|
163
|
+
/** JSON 响应 */
|
|
164
|
+
json<T = any>(data: T, status?: number): Response;
|
|
165
|
+
|
|
166
|
+
/** 文本响应 */
|
|
167
|
+
text(text: string, status?: number): Response;
|
|
168
|
+
|
|
169
|
+
/** HTML 响应 */
|
|
170
|
+
html(html: string, status?: number): Response;
|
|
171
|
+
|
|
172
|
+
/** 重定向 */
|
|
173
|
+
redirect(url: string, status?: number): Response;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* 路由匹配结果
|
|
178
|
+
*/
|
|
179
|
+
export interface RouteMatch {
|
|
180
|
+
/** 路由配置 */
|
|
181
|
+
route: ApiRoute;
|
|
182
|
+
|
|
183
|
+
/** 路径参数 */
|
|
184
|
+
params: KeyValue<string>;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* 路由器接口
|
|
189
|
+
*/
|
|
190
|
+
export interface Router {
|
|
191
|
+
/** 添加路由 */
|
|
192
|
+
add(route: ApiRoute): void;
|
|
193
|
+
|
|
194
|
+
/** 匹配路由 */
|
|
195
|
+
match(method: HttpMethod, path: string): RouteMatch | null;
|
|
196
|
+
|
|
197
|
+
/** 获取所有路由 */
|
|
198
|
+
getRoutes(): ApiRoute[];
|
|
199
|
+
|
|
200
|
+
/** 删除路由 */
|
|
201
|
+
remove(method: HttpMethod, path: string): boolean;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// ========== API 响应数据类型 ==========
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* 令牌检测响应数据
|
|
208
|
+
*/
|
|
209
|
+
export interface TokenCheckData {
|
|
210
|
+
/** 令牌是否有效 */
|
|
211
|
+
valid: boolean;
|
|
212
|
+
/** JWT 载荷(有效时返回) */
|
|
213
|
+
payload?: any;
|
|
214
|
+
/** 过期时间(秒) */
|
|
215
|
+
expiresIn?: number;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* 健康检查响应数据
|
|
220
|
+
*/
|
|
221
|
+
export interface HealthInfo {
|
|
222
|
+
/** 服务状态 */
|
|
223
|
+
status: string;
|
|
224
|
+
/** 时间戳 */
|
|
225
|
+
timestamp: string;
|
|
226
|
+
/** 运行时长(秒) */
|
|
227
|
+
uptime: number;
|
|
228
|
+
/** 内存使用情况 */
|
|
229
|
+
memory: NodeJS.MemoryUsage;
|
|
230
|
+
/** 运行时名称 */
|
|
231
|
+
runtime: string;
|
|
232
|
+
/** 版本号 */
|
|
233
|
+
version: string;
|
|
234
|
+
/** 平台 */
|
|
235
|
+
platform: string;
|
|
236
|
+
/** 架构 */
|
|
237
|
+
arch: string;
|
|
238
|
+
/** Redis 状态 */
|
|
239
|
+
redis?: string;
|
|
240
|
+
/** Redis 错误信息 */
|
|
241
|
+
redisError?: string;
|
|
242
|
+
/** 数据库状态 */
|
|
243
|
+
database?: string;
|
|
244
|
+
/** 数据库错误信息 */
|
|
245
|
+
databaseError?: string;
|
|
246
|
+
}
|
package/types/befly.d.ts
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Befly 核心框架类型定义
|
|
3
|
+
*/
|
|
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 { Logger } from '../utils/logger.js';
|
|
9
|
+
import type { Jwt } from '../utils/jwt.js';
|
|
10
|
+
import type { Validator } from '../utils/validate.js';
|
|
11
|
+
import type { SqlHelper } from '../utils/sqlHelper.js';
|
|
12
|
+
import type { Crypto2 } from '../utils/crypto.js';
|
|
13
|
+
import type { Tool } from '../utils/tool.js';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Befly 应用选项
|
|
17
|
+
*/
|
|
18
|
+
export interface BeflyOptions {
|
|
19
|
+
/** 应用名称 */
|
|
20
|
+
name?: string;
|
|
21
|
+
|
|
22
|
+
/** 监听主机 */
|
|
23
|
+
host?: string;
|
|
24
|
+
|
|
25
|
+
/** 监听端口 */
|
|
26
|
+
port?: number;
|
|
27
|
+
|
|
28
|
+
/** 是否启用 CORS */
|
|
29
|
+
cors?: boolean | CorsOptions;
|
|
30
|
+
|
|
31
|
+
/** 静态文件目录 */
|
|
32
|
+
staticDir?: string;
|
|
33
|
+
|
|
34
|
+
/** 上传文件目录 */
|
|
35
|
+
uploadDir?: string;
|
|
36
|
+
|
|
37
|
+
/** 最大请求体大小 */
|
|
38
|
+
maxBodySize?: number;
|
|
39
|
+
|
|
40
|
+
/** 自定义配置 */
|
|
41
|
+
[key: string]: any;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* CORS 配置选项
|
|
46
|
+
*/
|
|
47
|
+
export interface CorsOptions {
|
|
48
|
+
origin?: string | string[] | boolean;
|
|
49
|
+
methods?: string[];
|
|
50
|
+
allowedHeaders?: string[];
|
|
51
|
+
exposedHeaders?: string[];
|
|
52
|
+
credentials?: boolean;
|
|
53
|
+
maxAge?: number;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Befly 核心类
|
|
58
|
+
*/
|
|
59
|
+
export interface Befly {
|
|
60
|
+
/** API 路由映射 */
|
|
61
|
+
apiRoutes: Map<string, ApiRoute>;
|
|
62
|
+
|
|
63
|
+
/** 插件列表 */
|
|
64
|
+
pluginLists: Plugin[];
|
|
65
|
+
|
|
66
|
+
/** 应用上下文 */
|
|
67
|
+
appContext: KeyValue;
|
|
68
|
+
|
|
69
|
+
/** 应用选项 */
|
|
70
|
+
appOptions: BeflyOptions;
|
|
71
|
+
|
|
72
|
+
/** 日志器 */
|
|
73
|
+
logger: Logger;
|
|
74
|
+
|
|
75
|
+
/** JWT 工具 */
|
|
76
|
+
jwt: Jwt;
|
|
77
|
+
|
|
78
|
+
/** 验证器 */
|
|
79
|
+
validator: Validator;
|
|
80
|
+
|
|
81
|
+
/** SQL 管理器 */
|
|
82
|
+
sql: SqlHelper;
|
|
83
|
+
|
|
84
|
+
/** 加密工具 */
|
|
85
|
+
crypto: Crypto2;
|
|
86
|
+
|
|
87
|
+
/** 通用工具 */
|
|
88
|
+
tool: Tool;
|
|
89
|
+
|
|
90
|
+
/** 数据库连接 */
|
|
91
|
+
db: any;
|
|
92
|
+
|
|
93
|
+
/** Redis 连接 */
|
|
94
|
+
redis: any;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* 初始化检查器
|
|
98
|
+
*/
|
|
99
|
+
initCheck(): Promise<void>;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* 加载插件
|
|
103
|
+
*/
|
|
104
|
+
loadPlugins(): Promise<void>;
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* 加载 API 路由
|
|
108
|
+
*/
|
|
109
|
+
loadApis(): Promise<void>;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* 启动服务器
|
|
113
|
+
*/
|
|
114
|
+
start(): Promise<void>;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* 停止服务器
|
|
118
|
+
*/
|
|
119
|
+
stop(): Promise<void>;
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* 处理请求
|
|
123
|
+
*/
|
|
124
|
+
handleRequest(request: Request): Promise<Response>;
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* 注册中间件
|
|
128
|
+
*/
|
|
129
|
+
use(middleware: Function): void;
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* 获取配置
|
|
133
|
+
*/
|
|
134
|
+
getConfig(key: string): any;
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* 设置配置
|
|
138
|
+
*/
|
|
139
|
+
setConfig(key: string, value: any): void;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Befly 构造函数类型
|
|
144
|
+
*/
|
|
145
|
+
export interface BeflyConstructor {
|
|
146
|
+
new (options?: BeflyOptions): Befly;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* 服务器启动选项
|
|
151
|
+
*/
|
|
152
|
+
export interface ServerOptions {
|
|
153
|
+
/** 主机名 */
|
|
154
|
+
hostname: string;
|
|
155
|
+
|
|
156
|
+
/** 端口 */
|
|
157
|
+
port: number;
|
|
158
|
+
|
|
159
|
+
/** 请求处理函数 */
|
|
160
|
+
fetch: (request: Request) => Promise<Response>;
|
|
161
|
+
|
|
162
|
+
/** 错误处理函数 */
|
|
163
|
+
error?: (error: Error) => Response;
|
|
164
|
+
|
|
165
|
+
/** 开发模式 */
|
|
166
|
+
development?: boolean;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* 服务器实例
|
|
171
|
+
*/
|
|
172
|
+
export interface Server {
|
|
173
|
+
/** 主机名 */
|
|
174
|
+
hostname: string;
|
|
175
|
+
|
|
176
|
+
/** 端口 */
|
|
177
|
+
port: number;
|
|
178
|
+
|
|
179
|
+
/** 停止服务器 */
|
|
180
|
+
stop(): Promise<void>;
|
|
181
|
+
|
|
182
|
+
/** 重启服务器 */
|
|
183
|
+
reload(options: ServerOptions): void;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* 检查函数类型
|
|
188
|
+
*/
|
|
189
|
+
export type CheckFunction = (befly: Befly) => Promise<void> | void;
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* 检查结果
|
|
193
|
+
*/
|
|
194
|
+
export interface CheckResult {
|
|
195
|
+
/** 文件名 */
|
|
196
|
+
filename: string;
|
|
197
|
+
|
|
198
|
+
/** 检查名称 */
|
|
199
|
+
checkName: string;
|
|
200
|
+
|
|
201
|
+
/** 是否通过 */
|
|
202
|
+
passed: boolean;
|
|
203
|
+
|
|
204
|
+
/** 执行时间(纳秒) */
|
|
205
|
+
duration: number;
|
|
206
|
+
|
|
207
|
+
/** 错误信息 */
|
|
208
|
+
error?: Error;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* 性能统计
|
|
213
|
+
*/
|
|
214
|
+
export interface PerformanceStats {
|
|
215
|
+
/** 启动时间 */
|
|
216
|
+
startTime: number;
|
|
217
|
+
|
|
218
|
+
/** 检查时间 */
|
|
219
|
+
checkTime: number;
|
|
220
|
+
|
|
221
|
+
/** 插件加载时间 */
|
|
222
|
+
pluginTime: number;
|
|
223
|
+
|
|
224
|
+
/** API 加载时间 */
|
|
225
|
+
apiTime: number;
|
|
226
|
+
|
|
227
|
+
/** 总时间 */
|
|
228
|
+
totalTime: number;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* 导出 Befly 实例创建函数
|
|
233
|
+
*/
|
|
234
|
+
export function createBefly(options?: BeflyOptions): Befly;
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Befly 框架通用类型定义
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 响应结果类型
|
|
7
|
+
*/
|
|
8
|
+
export interface ResponseResult<T = any> {
|
|
9
|
+
code: number;
|
|
10
|
+
msg: string;
|
|
11
|
+
data?: T;
|
|
12
|
+
error?: any;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 验证结果类型
|
|
17
|
+
*/
|
|
18
|
+
export interface ValidationResult {
|
|
19
|
+
code: 0 | 1;
|
|
20
|
+
fields: Record<string, any>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 字段规则字符串
|
|
25
|
+
* 格式: "字段名|类型|最小值|最大值|默认值|是否索引|正则约束"
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* "用户名|string|2|50|null|1|^[a-zA-Z0-9_]+$"
|
|
29
|
+
* "年龄|number|0|150|18|0|null"
|
|
30
|
+
*/
|
|
31
|
+
export type FieldRule = string;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 表定义类型
|
|
35
|
+
*/
|
|
36
|
+
export type TableDefinition = Record<string, FieldRule>;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 解析后的字段规则
|
|
40
|
+
*/
|
|
41
|
+
export interface ParsedFieldRule {
|
|
42
|
+
name: string; // 字段名称
|
|
43
|
+
type: 'string' | 'number' | 'text' | 'array';
|
|
44
|
+
min: number | null; // 最小值
|
|
45
|
+
max: number | null; // 最大值
|
|
46
|
+
default: any; // 默认值
|
|
47
|
+
index: 0 | 1; // 是否索引
|
|
48
|
+
regex: string | null; // 正则约束
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* SQL 查询参数类型
|
|
53
|
+
*/
|
|
54
|
+
export type SqlValue = string | number | boolean | null | Date;
|
|
55
|
+
export type SqlParams = SqlValue[];
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 排序方向
|
|
59
|
+
*/
|
|
60
|
+
export type OrderDirection = 'ASC' | 'DESC' | 'asc' | 'desc';
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 比较运算符
|
|
64
|
+
*/
|
|
65
|
+
export type ComparisonOperator = '=' | '>' | '<' | '>=' | '<=' | '!=' | '<>' | 'LIKE' | 'IN' | 'NOT IN' | 'IS NULL' | 'IS NOT NULL';
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* JOIN 类型
|
|
69
|
+
*/
|
|
70
|
+
export type JoinType = 'INNER' | 'LEFT' | 'RIGHT' | 'FULL';
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 数据库配置类型
|
|
74
|
+
*/
|
|
75
|
+
export interface DatabaseConfig {
|
|
76
|
+
host: string;
|
|
77
|
+
port: number;
|
|
78
|
+
user: string;
|
|
79
|
+
password: string;
|
|
80
|
+
database: string;
|
|
81
|
+
connectionLimit?: number;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Redis 配置类型
|
|
86
|
+
*/
|
|
87
|
+
export interface RedisConfig {
|
|
88
|
+
host: string;
|
|
89
|
+
port: number;
|
|
90
|
+
password?: string;
|
|
91
|
+
db?: number;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* 日志级别
|
|
96
|
+
*/
|
|
97
|
+
export type LogLevel = 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal';
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* 日志配置
|
|
101
|
+
*/
|
|
102
|
+
export interface LoggerConfig {
|
|
103
|
+
level?: LogLevel;
|
|
104
|
+
transport?: {
|
|
105
|
+
target: string;
|
|
106
|
+
options?: Record<string, any>;
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* 环境变量类型
|
|
112
|
+
*/
|
|
113
|
+
export interface EnvConfig {
|
|
114
|
+
// 服务配置
|
|
115
|
+
appName: string;
|
|
116
|
+
appHost: string;
|
|
117
|
+
appPort: number;
|
|
118
|
+
|
|
119
|
+
// 数据库配置
|
|
120
|
+
mysqlHost: string;
|
|
121
|
+
mysqlPort: number;
|
|
122
|
+
mysqlUsername: string;
|
|
123
|
+
mysqlPassword: string;
|
|
124
|
+
mysqlDatabase: string;
|
|
125
|
+
|
|
126
|
+
// Redis 配置
|
|
127
|
+
redisHost?: string;
|
|
128
|
+
redisPort?: number;
|
|
129
|
+
redisPassword?: string;
|
|
130
|
+
|
|
131
|
+
// JWT 配置
|
|
132
|
+
jwtSecret: string;
|
|
133
|
+
jwtExpires?: string;
|
|
134
|
+
|
|
135
|
+
// 其他配置
|
|
136
|
+
[key: string]: any;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* 工具函数返回类型
|
|
141
|
+
*/
|
|
142
|
+
export interface ToolResponse<T = any> {
|
|
143
|
+
success: boolean;
|
|
144
|
+
data?: T;
|
|
145
|
+
error?: string;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* 分页参数
|
|
150
|
+
*/
|
|
151
|
+
export interface PaginationParams {
|
|
152
|
+
page: number;
|
|
153
|
+
limit: number;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* 分页结果
|
|
158
|
+
*/
|
|
159
|
+
export interface PaginatedResult<T = any> {
|
|
160
|
+
total: number;
|
|
161
|
+
page: number;
|
|
162
|
+
limit: number;
|
|
163
|
+
data: T[];
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* 通用键值对
|
|
168
|
+
*/
|
|
169
|
+
export type KeyValue<T = any> = Record<string, T>;
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* 可选字段
|
|
173
|
+
*/
|
|
174
|
+
export type Optional<T> = T | null | undefined;
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* 深度可选
|
|
178
|
+
*/
|
|
179
|
+
export type DeepPartial<T> = T extends object ? { [P in keyof T]?: DeepPartial<T[P]> } : T;
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* 保留字段(系统自动管理)
|
|
183
|
+
*/
|
|
184
|
+
export type ReservedFields = 'id' | 'created_at' | 'updated_at' | 'deleted_at' | 'state';
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* 排除保留字段
|
|
188
|
+
*/
|
|
189
|
+
export type ExcludeReserved<T> = Omit<T, ReservedFields>;
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* 数据库记录基础类型
|
|
193
|
+
*/
|
|
194
|
+
export interface BaseRecord {
|
|
195
|
+
id: number;
|
|
196
|
+
created_at: Date;
|
|
197
|
+
updated_at: Date;
|
|
198
|
+
deleted_at: Date | null;
|
|
199
|
+
state: number;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* 异步函数类型
|
|
204
|
+
*/
|
|
205
|
+
export type AsyncFunction<T = any> = (...args: any[]) => Promise<T>;
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* 同步函数类型
|
|
209
|
+
*/
|
|
210
|
+
export type SyncFunction<T = any> = (...args: any[]) => T;
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* 通用回调函数
|
|
214
|
+
*/
|
|
215
|
+
export type Callback<T = any> = (error: Error | null, result?: T) => void;
|