@mindbase/express-common 1.0.0 → 1.0.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/dist/index.d.mts +375 -0
- package/dist/index.mjs +2635 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +24 -6
- package/bin/mindbase.ts +0 -52
- package/commands/precache.ts +0 -54
- package/core/app.ts +0 -200
- package/core/module/CreateModule.ts +0 -38
- package/core/module/FindPackageRoot.ts +0 -58
- package/core/module/GetModulePath.ts +0 -58
- package/core/state.ts +0 -72
- package/feature/cron/CronManager.ts +0 -63
- package/feature/scanner/FileScanner.ts +0 -288
- package/index.ts +0 -10
- package/middleware/Cors.ts +0 -17
- package/middleware/IpParser.ts +0 -81
- package/middleware/UaParser.ts +0 -50
- package/routes/Doc.route.ts +0 -118
- package/tests/Cors.test.ts +0 -34
- package/tests/Dayjs.test.ts +0 -24
- package/tests/FileScanner.test.ts +0 -85
- package/tests/GetModulePath.test.ts +0 -32
- package/tests/IpParser.test.ts +0 -72
- package/tests/Logger.test.ts +0 -68
- package/tests/UaParser.test.ts +0 -41
- package/tsconfig.json +0 -9
- package/types/DocTypes.ts +0 -111
- package/types/index.ts +0 -19
- package/utils/ComponentRegistry.ts +0 -34
- package/utils/DatabaseMigration.ts +0 -121
- package/utils/Dayjs.ts +0 -16
- package/utils/DocManager.ts +0 -274
- package/utils/HttpServer.ts +0 -41
- package/utils/InitDatabase.ts +0 -149
- package/utils/InitErrorHandler.ts +0 -71
- package/utils/InitExpress.ts +0 -35
- package/utils/Logger.ts +0 -206
- package/utils/MiddlewareRegistry.ts +0 -14
- package/utils/ProjectInitializer.ts +0 -283
- package/utils/RouteParser.ts +0 -408
- package/utils/RouteRegistry.ts +0 -66
- package/utils/SchemaMigrate.ts +0 -73
- package/utils/SchemaSync.ts +0 -47
- package/utils/TSTypeParser.ts +0 -455
- package/utils/Validate.ts +0 -25
- package/utils/ZodSchemaParser.ts +0 -420
- package/vitest.config.ts +0 -18
- package/zod/Doc.schema.ts +0 -9
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
import { Express, Request, Response, NextFunction } from 'express';
|
|
2
|
+
import { BetterSQLite3Database } from 'drizzle-orm/better-sqlite3';
|
|
3
|
+
export { default as dayjs } from 'dayjs';
|
|
4
|
+
import { ZodSchema } from 'zod';
|
|
5
|
+
|
|
6
|
+
type LogLevel = "debug" | "info" | "warn" | "error" | "silent";
|
|
7
|
+
declare const logger: {
|
|
8
|
+
debug(...args: any[]): void;
|
|
9
|
+
info(...args: any[]): void;
|
|
10
|
+
warn(...args: any[]): void;
|
|
11
|
+
error(...args: any[]): void;
|
|
12
|
+
startup(tagOrMessage: string, ...args: any[]): void;
|
|
13
|
+
setLevel(level: LogLevel): void;
|
|
14
|
+
getLevel(): LogLevel;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* IP 解析结果数据结构
|
|
19
|
+
*/
|
|
20
|
+
interface IpInfo {
|
|
21
|
+
address: string;
|
|
22
|
+
location: string;
|
|
23
|
+
country: string;
|
|
24
|
+
province: string;
|
|
25
|
+
city: string;
|
|
26
|
+
isp: string;
|
|
27
|
+
latitude: string;
|
|
28
|
+
longitude: string;
|
|
29
|
+
isLocal: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface UaInfo {
|
|
33
|
+
ua: string;
|
|
34
|
+
browser: {
|
|
35
|
+
name?: string;
|
|
36
|
+
version?: string;
|
|
37
|
+
major?: string;
|
|
38
|
+
};
|
|
39
|
+
engine: {
|
|
40
|
+
name?: string;
|
|
41
|
+
version?: string;
|
|
42
|
+
};
|
|
43
|
+
os: {
|
|
44
|
+
name?: string;
|
|
45
|
+
version?: string;
|
|
46
|
+
};
|
|
47
|
+
device: {
|
|
48
|
+
model?: string;
|
|
49
|
+
type?: string;
|
|
50
|
+
vendor?: string;
|
|
51
|
+
};
|
|
52
|
+
cpu: {
|
|
53
|
+
architecture?: string;
|
|
54
|
+
};
|
|
55
|
+
isMobile: boolean;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 扫描结果接口
|
|
60
|
+
*/
|
|
61
|
+
interface ScanResult {
|
|
62
|
+
fileName: string;
|
|
63
|
+
defaultExport: any;
|
|
64
|
+
allExports?: any;
|
|
65
|
+
type: "route" | "middleware" | "schema";
|
|
66
|
+
filePath: string;
|
|
67
|
+
/** 缓存命中标志(文档已在数据库) */
|
|
68
|
+
cacheHit?: boolean;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
interface CronConfig {
|
|
72
|
+
timezone?: string;
|
|
73
|
+
}
|
|
74
|
+
declare function addCron(pattern: string, handler: () => void | Promise<void>, customId?: string): string;
|
|
75
|
+
declare function stopCron(id: string): boolean;
|
|
76
|
+
declare function stopAllCron(): void;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 标准 API 响应结构
|
|
80
|
+
*/
|
|
81
|
+
interface ApiResponse<T = any> {
|
|
82
|
+
code: number;
|
|
83
|
+
data: T;
|
|
84
|
+
msg: string;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* 字段约束规则
|
|
88
|
+
*/
|
|
89
|
+
interface FieldConstraints {
|
|
90
|
+
min?: number;
|
|
91
|
+
max?: number;
|
|
92
|
+
minLength?: number;
|
|
93
|
+
maxLength?: number;
|
|
94
|
+
pattern?: string;
|
|
95
|
+
email?: boolean;
|
|
96
|
+
url?: boolean;
|
|
97
|
+
uuid?: boolean;
|
|
98
|
+
int?: boolean;
|
|
99
|
+
positive?: boolean;
|
|
100
|
+
nonnegative?: boolean;
|
|
101
|
+
custom?: string[];
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* 字段验证规则
|
|
105
|
+
*/
|
|
106
|
+
interface FieldValidation {
|
|
107
|
+
type: "string" | "number" | "boolean" | "array" | "object" | "date" | "file" | "any";
|
|
108
|
+
required: boolean;
|
|
109
|
+
optional?: boolean;
|
|
110
|
+
nullable?: boolean;
|
|
111
|
+
defaultValue?: any;
|
|
112
|
+
constraints?: FieldConstraints;
|
|
113
|
+
items?: FieldValidation;
|
|
114
|
+
properties?: Record<string, FieldValidation>;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* 请求 Schema 结构
|
|
118
|
+
*/
|
|
119
|
+
interface RequestSchema {
|
|
120
|
+
target: "body" | "query" | "params";
|
|
121
|
+
fields: Record<string, FieldValidation>;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* 响应 Schema 结构
|
|
125
|
+
*/
|
|
126
|
+
interface ResponseSchema {
|
|
127
|
+
description?: string;
|
|
128
|
+
fields: Record<string, FieldValidation>;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* 路由文档配置
|
|
132
|
+
*/
|
|
133
|
+
interface RouteDocConfig<TRequest = any, TResponse = any> {
|
|
134
|
+
summary?: string;
|
|
135
|
+
description?: string;
|
|
136
|
+
request?: TRequest;
|
|
137
|
+
response?: TResponse;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* 路由信息接口
|
|
141
|
+
*/
|
|
142
|
+
interface RouteInfo {
|
|
143
|
+
id?: number;
|
|
144
|
+
module: string;
|
|
145
|
+
method: string;
|
|
146
|
+
path: string;
|
|
147
|
+
fullPath: string;
|
|
148
|
+
summary: string;
|
|
149
|
+
description: string;
|
|
150
|
+
requestSchema?: RequestSchema;
|
|
151
|
+
responseSchema?: ResponseSchema;
|
|
152
|
+
middlewares?: string[];
|
|
153
|
+
filePath: string;
|
|
154
|
+
createdAt?: number;
|
|
155
|
+
updatedAt?: number;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* 标记路由文档配置
|
|
159
|
+
*/
|
|
160
|
+
declare function RouteDoc<TRequest = any, TResponse = any>(config: RouteDocConfig<TRequest, TResponse>): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
161
|
+
/**
|
|
162
|
+
* 模块列表项接口
|
|
163
|
+
*/
|
|
164
|
+
interface ModuleItem {
|
|
165
|
+
name: string;
|
|
166
|
+
count: number;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* 文档数据库配置接口
|
|
170
|
+
*/
|
|
171
|
+
interface DocDatabaseConfig {
|
|
172
|
+
path: string;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
interface MindBaseAppOptions {
|
|
176
|
+
/**
|
|
177
|
+
* 启动模式:
|
|
178
|
+
* - normal: 正常启动服务
|
|
179
|
+
* - sync: 同步数据库 Schema(仅收集)
|
|
180
|
+
* - migrate: 执行数据库迁移
|
|
181
|
+
* @default "normal"
|
|
182
|
+
*/
|
|
183
|
+
mode?: "normal" | "sync" | "migrate";
|
|
184
|
+
/** 服务监听端口 @default 3000 */
|
|
185
|
+
port?: number;
|
|
186
|
+
/** 是否启用请求日志 @default true */
|
|
187
|
+
logging?: boolean;
|
|
188
|
+
/** 静态文件目录路径 */
|
|
189
|
+
staticPath?: string;
|
|
190
|
+
/** 是否解析 User-Agent 信息 @default true */
|
|
191
|
+
userAgent?: boolean;
|
|
192
|
+
/** 是否解析 IP 地理位置信息 @default true */
|
|
193
|
+
ip?: boolean;
|
|
194
|
+
/** 是否启用 CORS 跨域 @default true */
|
|
195
|
+
cors?: boolean;
|
|
196
|
+
/** API 路由前缀,如 "/api" */
|
|
197
|
+
apiPrefix?: string;
|
|
198
|
+
/**
|
|
199
|
+
* 日志级别(优先级从低到高):
|
|
200
|
+
* - debug: 调试信息(显示所有)
|
|
201
|
+
* - info: 普通信息(隐藏 debug)
|
|
202
|
+
* - warn: 警告信息(隐藏 debug、info)
|
|
203
|
+
* - error: 错误信息(隐藏 debug、info、warn)
|
|
204
|
+
* - silent: 静默模式(隐藏所有)
|
|
205
|
+
* @default "info"
|
|
206
|
+
*/
|
|
207
|
+
logLevel?: LogLevel;
|
|
208
|
+
/** 数据库配置 */
|
|
209
|
+
database?: {
|
|
210
|
+
/** SQLite 数据库文件路径 @default "./data/app.db" */
|
|
211
|
+
path?: string;
|
|
212
|
+
};
|
|
213
|
+
/** 认证白名单路径(支持字符串和正则表达式) */
|
|
214
|
+
authWhitelist?: (string | RegExp)[];
|
|
215
|
+
}
|
|
216
|
+
interface AppState {
|
|
217
|
+
options: MindBaseAppOptions;
|
|
218
|
+
express: Express;
|
|
219
|
+
db?: BetterSQLite3Database<Record<string, any>>;
|
|
220
|
+
schemas?: Record<string, any>;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* MindBase 应用实例
|
|
225
|
+
*/
|
|
226
|
+
interface MindBaseApp {
|
|
227
|
+
/** 注册插件模块 */
|
|
228
|
+
use(plugin: any): void;
|
|
229
|
+
/** 启动应用(初始化数据库、注册路由、启动 HTTP 服务) */
|
|
230
|
+
startup(): Promise<void>;
|
|
231
|
+
/** 获取 Express 实例 */
|
|
232
|
+
getApp(): Express;
|
|
233
|
+
/** 获取 Drizzle 数据库实例 */
|
|
234
|
+
getDB(): any;
|
|
235
|
+
/** 获取应用配置选项 */
|
|
236
|
+
getOptions(): MindBaseAppOptions;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* 创建 MindBase 应用实例
|
|
240
|
+
* @param options 应用配置选项
|
|
241
|
+
* @returns MindBase 应用实例
|
|
242
|
+
* @example
|
|
243
|
+
* const app = createApp({
|
|
244
|
+
* port: 3000,
|
|
245
|
+
* logLevel: "debug",
|
|
246
|
+
* });
|
|
247
|
+
* app.use(auth);
|
|
248
|
+
* await app.startup();
|
|
249
|
+
*/
|
|
250
|
+
declare function createApp(options?: MindBaseAppOptions): MindBaseApp;
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* 准备命令
|
|
254
|
+
*
|
|
255
|
+
* 初始化数据库并预热缓存,但不启动服务器
|
|
256
|
+
* 用于在构建/初始化阶段准备环境
|
|
257
|
+
*/
|
|
258
|
+
declare function prepare(cwd?: string): Promise<void>;
|
|
259
|
+
declare function precache(cwd?: string): Promise<void>;
|
|
260
|
+
|
|
261
|
+
interface LightUpModule {
|
|
262
|
+
install(app: MindBaseApp): Promise<void>;
|
|
263
|
+
__modulePath: string;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* 创建一个 LightUp 模块
|
|
267
|
+
* @param install 模块安装函数
|
|
268
|
+
* @param callerPath 调用者的文件路径(通常是 __filename),如果不提供则尝试通过调用栈获取
|
|
269
|
+
* @returns LightUp 模块实例
|
|
270
|
+
* @throws 如果模块创建失败
|
|
271
|
+
*/
|
|
272
|
+
declare function createModule(install: any, callerPath?: string): LightUpModule;
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* 文档管理器
|
|
276
|
+
* 管理文档数据,与独立的 SQLite 数据库交互
|
|
277
|
+
*/
|
|
278
|
+
declare class DocManager {
|
|
279
|
+
private db;
|
|
280
|
+
private config;
|
|
281
|
+
constructor(config: DocDatabaseConfig);
|
|
282
|
+
/**
|
|
283
|
+
* 初始化数据库
|
|
284
|
+
*/
|
|
285
|
+
init(): void;
|
|
286
|
+
/**
|
|
287
|
+
* 创建表结构
|
|
288
|
+
*/
|
|
289
|
+
private createTables;
|
|
290
|
+
/**
|
|
291
|
+
* 保存路由信息
|
|
292
|
+
* @param routeInfo 路由信息
|
|
293
|
+
* @param apiPrefix API 前缀
|
|
294
|
+
* @param moduleName 模块名称
|
|
295
|
+
*/
|
|
296
|
+
saveRoute(routeInfo: RouteInfo, apiPrefix: string, moduleName: string): void;
|
|
297
|
+
/**
|
|
298
|
+
* 获取模块列表
|
|
299
|
+
* @returns 模块列表
|
|
300
|
+
*/
|
|
301
|
+
getModules(): ModuleItem[];
|
|
302
|
+
/**
|
|
303
|
+
* 获取路由列表
|
|
304
|
+
* @param module 模块名称(可选)
|
|
305
|
+
* @returns 路由列表
|
|
306
|
+
*/
|
|
307
|
+
getRoutes(module?: string): RouteInfo[];
|
|
308
|
+
/**
|
|
309
|
+
* 获取路由详情
|
|
310
|
+
* @param id 路由 ID
|
|
311
|
+
* @returns 路由详情
|
|
312
|
+
*/
|
|
313
|
+
getRouteById(id: number): RouteInfo | null;
|
|
314
|
+
/**
|
|
315
|
+
* 清空所有路由数据
|
|
316
|
+
*/
|
|
317
|
+
clearAllRoutes(): void;
|
|
318
|
+
/**
|
|
319
|
+
* 关闭数据库连接
|
|
320
|
+
*/
|
|
321
|
+
close(): void;
|
|
322
|
+
}
|
|
323
|
+
declare let docManager: DocManager;
|
|
324
|
+
/**
|
|
325
|
+
* 初始化文档管理器
|
|
326
|
+
* @param config 配置
|
|
327
|
+
*/
|
|
328
|
+
declare function initDocManager(config: DocDatabaseConfig): DocManager;
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* 验证位置类型
|
|
332
|
+
*/
|
|
333
|
+
type ValidationTarget = "body" | "query" | "params";
|
|
334
|
+
/**
|
|
335
|
+
* Zod 验证中间件
|
|
336
|
+
* @param schema Zod Schema
|
|
337
|
+
* @param target 验证目标 (默认 body)
|
|
338
|
+
*/
|
|
339
|
+
declare const validate: (schema: ZodSchema, target?: ValidationTarget) => (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* 创建业务错误
|
|
343
|
+
* @param code HTTP 状态码
|
|
344
|
+
* @param msg 错误消息
|
|
345
|
+
*/
|
|
346
|
+
declare function createAppError(code: number, msg: string): Error;
|
|
347
|
+
/**
|
|
348
|
+
* 便捷工厂函数
|
|
349
|
+
*/
|
|
350
|
+
declare const Errors: {
|
|
351
|
+
badRequest: (msg: string) => Error;
|
|
352
|
+
unauthorized: (msg: string) => Error;
|
|
353
|
+
forbidden: (msg: string) => Error;
|
|
354
|
+
notFound: (msg: string) => Error;
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* 初始化 SQLite 数据库
|
|
359
|
+
* @param options 配置选项
|
|
360
|
+
* @returns 数据库实例
|
|
361
|
+
* @throws 如果数据库初始化失败
|
|
362
|
+
*/
|
|
363
|
+
declare function initDatabase(options: {
|
|
364
|
+
schemas: Record<string, any>;
|
|
365
|
+
dbPath?: string;
|
|
366
|
+
state?: AppState;
|
|
367
|
+
}): any;
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* 处理数据库迁移(Schema 同步 + Drizzle Push)
|
|
371
|
+
* 这是 migrate 模式的核心逻辑
|
|
372
|
+
*/
|
|
373
|
+
declare function handleSchemaMigrate(): Promise<void>;
|
|
374
|
+
|
|
375
|
+
export { type ApiResponse, type AppState, type CronConfig, type DocDatabaseConfig, DocManager, Errors, type FieldConstraints, type FieldValidation, type IpInfo, type LogLevel, type MindBaseApp, type MindBaseAppOptions, type ModuleItem, type RequestSchema, type ResponseSchema, RouteDoc, type RouteDocConfig, type RouteInfo, type ScanResult, type UaInfo, addCron, createApp, createAppError, createModule, docManager, handleSchemaMigrate, initDatabase, initDocManager, logger, precache, prepare, stopAllCron, stopCron, validate };
|