befly-shared 1.1.1 → 1.2.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.
@@ -1,45 +0,0 @@
1
- import { join, basename } from 'pathe';
2
-
3
- import { mergeAndConcat } from 'merge-anything';
4
-
5
- import { scanConfig } from './scanConfig.js';
6
-
7
- /**
8
- * Addon 配置定义函数
9
- * 接受 addon 配置对象,从当前执行目录的 config 目录下查找同名配置文件进行合并
10
- * 只能在 addon.config.js 文件中使用
11
- * @param metaDirname - import.meta.dirname
12
- * @param addonConfig - addon 配置对象
13
- * @returns 合并后的配置对象
14
- * @example
15
- * ```ts
16
- * // 在 packages/addonAdmin/addon.config.js 中
17
- * import { defineAddonConfig } from 'befly-shared/defineAddonConfig';
18
- *
19
- * export default defineAddonConfig(import.meta.dirname, {
20
- * menus: [...]
21
- * });
22
- * // 自动从目录名提取 addon 名称,并从 process.cwd()/config/addonAdmin.{js,ts,json} 读取配置并合并
23
- * ```
24
- */
25
- export async function defineAddonConfig(metaDirname: string, addonConfig: Record<string, any> = {}): Promise<Record<string, any>> {
26
- try {
27
- // 1. 使用 pathe 的 basename 获取完整的目录名(保留 addon 前缀)
28
- const addonName = basename(metaDirname);
29
-
30
- // 2. 从当前执行目录的 config 目录查找配置
31
- const projectConfigDir = join(process.cwd(), 'config');
32
-
33
- // 3. 使用 scanConfig 加载项目配置
34
- const projectConfig = await scanConfig({
35
- dirs: [projectConfigDir],
36
- files: [addonName]
37
- });
38
-
39
- // 4. 合并 addon 配置和项目配置(项目配置优先级更高)
40
- return mergeAndConcat({}, addonConfig, projectConfig);
41
- } catch (error: any) {
42
- console.error('defineAddonConfig 失败:', error.message);
43
- return addonConfig;
44
- }
45
- }
package/src/types.ts DELETED
@@ -1,338 +0,0 @@
1
- /**
2
- * Befly 共享类型定义
3
- * 这些类型可以在 core、tpl、admin 等多个包中复用
4
- */
5
-
6
- // ============================================
7
- // 通用响应类型
8
- // ============================================
9
-
10
- /**
11
- * API 响应结果类型
12
- */
13
- export interface ResponseResult<T = any> {
14
- /** 状态码:0 表示成功,非 0 表示失败 */
15
- code: number;
16
- /** 响应消息 */
17
- msg: string;
18
- /** 响应数据 */
19
- data?: T;
20
- /** 错误信息(仅在失败时) */
21
- error?: any;
22
- }
23
-
24
- /**
25
- * 分页响应结果类型
26
- */
27
- export interface PaginatedResult<T = any> {
28
- /** 状态码 */
29
- code: number;
30
- /** 响应消息 */
31
- msg: string;
32
- /** 数据列表 */
33
- data: T[];
34
- /** 总记录数 */
35
- total: number;
36
- /** 当前页码 */
37
- page: number;
38
- /** 每页数量 */
39
- limit: number;
40
- /** 总页数 */
41
- pages: number;
42
- }
43
-
44
- /**
45
- * 验证结果类型
46
- */
47
- export interface ValidationResult {
48
- /** 验证状态:0 成功,1 失败 */
49
- code: 0 | 1;
50
- /** 字段错误信息 */
51
- fields: Record<string, string>;
52
- }
53
-
54
- // ============================================
55
- // HTTP 相关类型
56
- // ============================================
57
-
58
- /**
59
- * HTTP 方法类型
60
- */
61
- export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';
62
-
63
- /**
64
- * 通用键值对类型
65
- */
66
- export type KeyValue<T = any> = Record<string, T>;
67
-
68
- // ============================================
69
- // 字段定义类型
70
- // ============================================
71
-
72
- /**
73
- * 字段类型
74
- */
75
- export type FieldType = 'string' | 'number' | 'text' | 'array_string' | 'array_text';
76
-
77
- /**
78
- * 字段定义类型(对象格式)
79
- */
80
- export interface FieldDefinition {
81
- /** 字段标签/描述 */
82
- name: string;
83
- /** 字段详细说明 */
84
- detail: string;
85
- /** 字段类型 */
86
- type: FieldType;
87
- /** 最小值/最小长度 */
88
- min: number | null;
89
- /** 最大值/最大长度 */
90
- max: number | null;
91
- /** 默认值 */
92
- default: any;
93
- /** 是否创建索引 */
94
- index: boolean;
95
- /** 是否唯一 */
96
- unique: boolean;
97
- /** 字段注释 */
98
- comment: string;
99
- /** 是否允许为空 */
100
- nullable: boolean;
101
- /** 是否无符号(仅 number 类型) */
102
- unsigned: boolean;
103
- /** 正则验证 */
104
- regexp: string | null;
105
- }
106
-
107
- /**
108
- * 表定义类型(对象格式)
109
- */
110
- export type TableDefinition = Record<string, FieldDefinition>;
111
-
112
- // ============================================
113
- // 用户相关类型
114
- // ============================================
115
-
116
- /**
117
- * 用户信息类型
118
- */
119
- export interface UserInfo {
120
- /** 用户 ID */
121
- id: number;
122
- /** 用户名 */
123
- username?: string;
124
- /** 邮箱 */
125
- email?: string;
126
- /** 角色代码 */
127
- roleCode?: string;
128
- /** 其他自定义字段 */
129
- [key: string]: any;
130
- }
131
-
132
- // ============================================
133
- // 请求上下文类型(基础版)
134
- // ============================================
135
-
136
- /**
137
- * 请求上下文基础接口
138
- * 用于跨包共享的最小上下文定义
139
- */
140
- export interface BaseRequestContext {
141
- /** 请求体参数 */
142
- body: Record<string, any>;
143
- /** 用户信息 */
144
- user: Record<string, any>;
145
- /** 请求开始时间(毫秒) */
146
- now: number;
147
- /** 客户端 IP 地址 */
148
- ip: string;
149
- /** API 路由路径(如 POST/api/user/login) */
150
- route: string;
151
- /** 请求唯一 ID */
152
- requestId: string;
153
- }
154
-
155
- // ============================================
156
- // API 路由类型(基础版)
157
- // ============================================
158
-
159
- /**
160
- * API 路由基础配置
161
- * 用于跨包共享的最小路由定义
162
- */
163
- export interface BaseApiRoute {
164
- /** 接口名称(必填) */
165
- name: string;
166
- /** HTTP 方法(可选,默认 POST) */
167
- method?: HttpMethod;
168
- /** 认证类型(可选,默认 true) */
169
- auth?: boolean;
170
- /** 字段定义(验证规则) */
171
- fields?: TableDefinition;
172
- /** 必填字段 */
173
- required?: string[];
174
- /** 路由路径(运行时生成) */
175
- route?: string;
176
- }
177
-
178
- // ============================================
179
- // 数据库相关类型
180
- // ============================================
181
-
182
- /**
183
- * SQL 值类型
184
- */
185
- export type SqlValue = string | number | boolean | null | Date;
186
-
187
- /**
188
- * SQL 参数数组类型
189
- */
190
- export type SqlParams = SqlValue[];
191
-
192
- /**
193
- * 排序方向
194
- */
195
- export type OrderDirection = 'ASC' | 'DESC' | 'asc' | 'desc';
196
-
197
- /**
198
- * 数据库类型
199
- */
200
- export type DatabaseType = 'mysql' | 'postgresql' | 'sqlite';
201
-
202
- /**
203
- * 数据库配置类型
204
- */
205
- export interface DatabaseConfig {
206
- /** 数据库类型 */
207
- type: DatabaseType;
208
- /** 主机地址 */
209
- host: string;
210
- /** 端口号 */
211
- port: number;
212
- /** 用户名 */
213
- user: string;
214
- /** 密码 */
215
- password: string;
216
- /** 数据库名 */
217
- database: string;
218
- }
219
-
220
- /**
221
- * Redis 配置类型
222
- */
223
- export interface RedisConfig {
224
- /** 主机地址 */
225
- host: string;
226
- /** 端口号 */
227
- port: number;
228
- /** 密码 */
229
- password?: string;
230
- /** 数据库索引 */
231
- db?: number;
232
- }
233
-
234
- // ============================================
235
- // 菜单和权限类型
236
- // ============================================
237
-
238
- /**
239
- * 菜单项类型
240
- */
241
- export interface MenuItem {
242
- /** 菜单 ID */
243
- id: number;
244
- /** 父级 ID */
245
- pid: number;
246
- /** 菜单名称 */
247
- name: string;
248
- /** 菜单路径 */
249
- path: string;
250
- /** 菜单图标 */
251
- icon?: string;
252
- /** 排序 */
253
- sort: number;
254
- /** 是否隐藏 */
255
- hidden?: boolean;
256
- /** 子菜单 */
257
- children?: MenuItem[];
258
- }
259
-
260
- /**
261
- * 权限项类型
262
- */
263
- export interface PermissionItem {
264
- /** API 路由(如 POST/api/user/list) */
265
- route: string;
266
- /** 权限名称 */
267
- name: string;
268
- }
269
-
270
- /**
271
- * 角色信息类型
272
- */
273
- export interface RoleInfo {
274
- /** 角色 ID */
275
- id: number;
276
- /** 角色代码 */
277
- code: string;
278
- /** 角色名称 */
279
- name: string;
280
- /** 角色描述 */
281
- desc?: string;
282
- }
283
-
284
- // ============================================
285
- // API 响应码常量
286
- // ============================================
287
-
288
- /**
289
- * API 响应码
290
- */
291
- export const ApiCode = {
292
- /** 成功 */
293
- SUCCESS: 0,
294
- /** 通用失败 */
295
- FAIL: 1,
296
- /** 未授权 */
297
- UNAUTHORIZED: 401,
298
- /** 禁止访问 */
299
- FORBIDDEN: 403,
300
- /** 未找到 */
301
- NOT_FOUND: 404,
302
- /** 服务器错误 */
303
- SERVER_ERROR: 500
304
- } as const;
305
-
306
- /**
307
- * API 响应码类型
308
- */
309
- export type ApiCodeType = (typeof ApiCode)[keyof typeof ApiCode];
310
-
311
- // ============================================
312
- // 错误消息常量
313
- // ============================================
314
-
315
- /**
316
- * 通用错误消息
317
- */
318
- export const ErrorMessages = {
319
- /** 未授权 */
320
- UNAUTHORIZED: '请先登录',
321
- /** 禁止访问 */
322
- FORBIDDEN: '无访问权限',
323
- /** 未找到 */
324
- NOT_FOUND: '资源不存在',
325
- /** 服务器错误 */
326
- SERVER_ERROR: '服务器错误',
327
- /** 参数错误 */
328
- INVALID_PARAMS: '参数错误',
329
- /** Token 过期 */
330
- TOKEN_EXPIRED: 'Token 已过期',
331
- /** Token 无效 */
332
- TOKEN_INVALID: 'Token 无效'
333
- } as const;
334
-
335
- /**
336
- * 错误消息类型
337
- */
338
- export type ErrorMessageType = (typeof ErrorMessages)[keyof typeof ErrorMessages];
@@ -1,19 +0,0 @@
1
- /**
2
- * Addon 配置定义函数
3
- * 接受 addon 配置对象,从当前执行目录的 config 目录下查找同名配置文件进行合并
4
- * 只能在 addon.config.js 文件中使用
5
- * @param metaDirname - import.meta.dirname
6
- * @param addonConfig - addon 配置对象
7
- * @returns 合并后的配置对象
8
- * @example
9
- * ```ts
10
- * // 在 packages/addonAdmin/addon.config.js 中
11
- * import { defineAddonConfig } from 'befly-shared/defineAddonConfig';
12
- *
13
- * export default defineAddonConfig(import.meta.dirname, {
14
- * menus: [...]
15
- * });
16
- * // 自动从目录名提取 addon 名称,并从 process.cwd()/config/addonAdmin.{js,ts,json} 读取配置并合并
17
- * ```
18
- */
19
- export declare function defineAddonConfig(metaDirname: string, addonConfig?: Record<string, any>): Promise<Record<string, any>>;