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.
- package/README.md +22 -25
- package/dist/{types.js → constants.js} +2 -2
- package/dist/defineAddonConfig.js +7 -26
- package/dist/defineBeflyConfig.js +28 -0
- package/dist/index.js +4 -3
- package/dist/regex.js +3 -1
- package/package.json +2 -2
- package/src/configTypes.ts +4 -2
- package/src/constants.ts +60 -0
- package/src/index.ts +3 -4
- package/src/regex.ts +3 -1
- package/src/scanConfig.ts +4 -2
- package/tests/regex.test.ts +3 -3
- package/tests/types.test.ts +8 -2
- package/types/addon.d.ts +50 -0
- package/types/api.d.ts +63 -0
- package/types/common.d.ts +8 -0
- package/types/configTypes.d.ts +2 -0
- package/types/constants.d.ts +48 -0
- package/types/context.d.ts +38 -0
- package/types/crypto.d.ts +23 -0
- package/types/database.d.ts +55 -0
- package/types/index.d.ts +1 -2
- package/types/jwt.d.ts +99 -0
- package/types/logger.d.ts +22 -0
- package/types/menu.d.ts +49 -0
- package/types/regex.d.ts +3 -1
- package/types/table.d.ts +49 -0
- package/types/tool.d.ts +67 -0
- package/types/types.d.ts +41 -271
- package/types/validate.d.ts +71 -0
- package/src/defineAddonConfig.ts +0 -45
- package/src/types.ts +0 -338
- package/types/defineAddonConfig.d.ts +0 -19
package/README.md
CHANGED
|
@@ -10,6 +10,23 @@ bun add befly-shared
|
|
|
10
10
|
|
|
11
11
|
## 类型定义
|
|
12
12
|
|
|
13
|
+
所有类型统一从 `befly-shared/types` 导入,按功能分类:
|
|
14
|
+
|
|
15
|
+
| 分类 | 类型 |
|
|
16
|
+
| ------ | ----------------------------------------------------------------------------- |
|
|
17
|
+
| 验证 | `ValidateResult`, `SingleResult`, `FieldDefinition`, `FieldRules` |
|
|
18
|
+
| API | `ResponseResult`, `PaginatedResult`, `ApiCode`, `ErrorMessages` |
|
|
19
|
+
| 数据库 | `DatabaseConfig`, `SqlValue`, `WhereConditions` |
|
|
20
|
+
| 上下文 | `RequestContext`, `ApiRoute` |
|
|
21
|
+
| 菜单 | `MenuItem`, `MenuTree` |
|
|
22
|
+
| 加密 | `EncodingType`, `HashAlgorithm`, `PasswordHashOptions` |
|
|
23
|
+
| JWT | `JwtPayload`, `JwtSignOptions`, `JwtVerifyOptions`, `JwtDecoded`, `JwtHeader` |
|
|
24
|
+
| 日志 | `LogLevel`, `LoggerConfig` |
|
|
25
|
+
| 工具 | `DateFormat`, `PaginationParams`, `PaginationResult` |
|
|
26
|
+
| 表 | `SystemFields`, `BaseTable`, `InsertType`, `UpdateType`, `SelectType` |
|
|
27
|
+
| Addon | `AddonAuthor`, `AddonConfig` |
|
|
28
|
+
| 通用 | `ReservedFields`, `ExcludeReserved` |
|
|
29
|
+
|
|
13
30
|
### 响应类型
|
|
14
31
|
|
|
15
32
|
```typescript
|
|
@@ -62,7 +79,7 @@ const dbConfig: DatabaseConfig = {
|
|
|
62
79
|
type: 'mysql',
|
|
63
80
|
host: 'localhost',
|
|
64
81
|
port: 3306,
|
|
65
|
-
|
|
82
|
+
username: 'root',
|
|
66
83
|
password: 'password',
|
|
67
84
|
database: 'befly'
|
|
68
85
|
};
|
|
@@ -71,10 +88,13 @@ const redisConfig: RedisConfig = {
|
|
|
71
88
|
host: 'localhost',
|
|
72
89
|
port: 6379,
|
|
73
90
|
password: 'password',
|
|
74
|
-
db: 0
|
|
91
|
+
db: 0,
|
|
92
|
+
prefix: 'befly:'
|
|
75
93
|
};
|
|
76
94
|
```
|
|
77
95
|
|
|
96
|
+
> **注意**:这些类型主要用于类型检查和文档,实际连接时框架会自动从 `beflyConfig` 获取配置。
|
|
97
|
+
|
|
78
98
|
### Redis 键和 TTL
|
|
79
99
|
|
|
80
100
|
```typescript
|
|
@@ -122,29 +142,6 @@ const config = await mergeConfig({
|
|
|
122
142
|
});
|
|
123
143
|
```
|
|
124
144
|
|
|
125
|
-
### addonConfigMerge
|
|
126
|
-
|
|
127
|
-
Addon 配置自动合并函数,自动读取 addon 的 `addon.config.js` 和项目 `config/` 目录下的同名配置文件进行合并。
|
|
128
|
-
|
|
129
|
-
**参数:**
|
|
130
|
-
|
|
131
|
-
- `options`: 合并选项(可选)
|
|
132
|
-
|
|
133
|
-
**返回:** `Promise<Record<string, any>>`
|
|
134
|
-
|
|
135
|
-
**示例:**
|
|
136
|
-
|
|
137
|
-
```typescript
|
|
138
|
-
import { addonConfigMerge } from 'befly-shared/scanConfig';
|
|
139
|
-
|
|
140
|
-
// 在 @befly-addon/admin 包根目录文件中调用
|
|
141
|
-
const config = await addonConfigMerge();
|
|
142
|
-
// 自动读取:
|
|
143
|
-
// 1. @befly-addon/admin/addon.config.js
|
|
144
|
-
// 2. 项目根目录/config/admin.{js,ts,json}
|
|
145
|
-
// 3. 合并后返回
|
|
146
|
-
```
|
|
147
|
-
|
|
148
145
|
## 字段名转换
|
|
149
146
|
|
|
150
147
|
### keysToCamel
|
|
@@ -1,40 +1,21 @@
|
|
|
1
|
-
import { join, basename } from 'pathe';
|
|
2
|
-
import { mergeAndConcat } from 'merge-anything';
|
|
3
|
-
import { scanConfig } from './scanConfig.js';
|
|
4
1
|
/**
|
|
5
2
|
* Addon 配置定义函数
|
|
6
|
-
*
|
|
3
|
+
* 用于定义 addon 的静态配置(如菜单)
|
|
7
4
|
* 只能在 addon.config.js 文件中使用
|
|
8
|
-
*
|
|
5
|
+
*
|
|
6
|
+
* @param _metaDirname - import.meta.dirname(保留参数,便于未来扩展)
|
|
9
7
|
* @param addonConfig - addon 配置对象
|
|
10
|
-
* @returns
|
|
8
|
+
* @returns addon 配置对象
|
|
11
9
|
* @example
|
|
12
10
|
* ```ts
|
|
13
11
|
* // 在 packages/addonAdmin/addon.config.js 中
|
|
14
|
-
* import { defineAddonConfig } from 'befly-shared';
|
|
12
|
+
* import { defineAddonConfig } from 'befly-shared/defineAddonConfig';
|
|
15
13
|
*
|
|
16
14
|
* export default defineAddonConfig(import.meta.dirname, {
|
|
17
15
|
* menus: [...]
|
|
18
16
|
* });
|
|
19
|
-
* // 自动从目录名提取 addon 名称,并从 process.cwd()/config/addonAdmin.{js,ts,json} 读取配置并合并
|
|
20
17
|
* ```
|
|
21
18
|
*/
|
|
22
|
-
export
|
|
23
|
-
|
|
24
|
-
// 1. 使用 pathe 的 basename 获取完整的目录名(保留 addon 前缀)
|
|
25
|
-
const addonName = basename(metaDirname);
|
|
26
|
-
// 2. 从当前执行目录的 config 目录查找配置
|
|
27
|
-
const projectConfigDir = join(process.cwd(), 'config');
|
|
28
|
-
// 3. 使用 scanConfig 加载项目配置
|
|
29
|
-
const projectConfig = await scanConfig({
|
|
30
|
-
dirs: [projectConfigDir],
|
|
31
|
-
files: [addonName]
|
|
32
|
-
});
|
|
33
|
-
// 4. 合并 addon 配置和项目配置(项目配置优先级更高)
|
|
34
|
-
return mergeAndConcat({}, addonConfig, projectConfig);
|
|
35
|
-
}
|
|
36
|
-
catch (error) {
|
|
37
|
-
console.error('defineAddonConfig 失败:', error.message);
|
|
38
|
-
return addonConfig;
|
|
39
|
-
}
|
|
19
|
+
export function defineAddonConfig(_metaDirname, addonConfig = {}) {
|
|
20
|
+
return addonConfig;
|
|
40
21
|
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Befly 配置定义函数
|
|
3
|
+
* 提供类型提示,不做任何处理,直接返回配置对象
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* 定义 Befly 配置
|
|
7
|
+
* 提供类型提示,不做任何处理,直接返回配置对象
|
|
8
|
+
* @param config 配置对象
|
|
9
|
+
* @returns 配置对象
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* // app.config.ts
|
|
13
|
+
* import { defineBeflyConfig } from 'befly-shared/defineBeflyConfig';
|
|
14
|
+
*
|
|
15
|
+
* export default defineBeflyConfig({
|
|
16
|
+
* appName: '我的应用',
|
|
17
|
+
* appPort: 3000,
|
|
18
|
+
* addons: {
|
|
19
|
+
* admin: {
|
|
20
|
+
* email: { host: 'smtp.qq.com' }
|
|
21
|
+
* }
|
|
22
|
+
* }
|
|
23
|
+
* });
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export function defineBeflyConfig(config) {
|
|
27
|
+
return config;
|
|
28
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -2,13 +2,14 @@
|
|
|
2
2
|
* Befly Shared 统一导出
|
|
3
3
|
* 跨包共享的工具函数、常量、类型定义
|
|
4
4
|
*/
|
|
5
|
-
//
|
|
6
|
-
export * from './
|
|
7
|
-
//
|
|
5
|
+
// 常量(运行时)
|
|
6
|
+
export * from './constants.js';
|
|
7
|
+
// Redis 键和正则
|
|
8
8
|
export * from './redisKeys.js';
|
|
9
9
|
export * from './regex.js';
|
|
10
10
|
// 工具函数
|
|
11
11
|
export * from './defineAddonConfig.js';
|
|
12
|
+
export * from './defineBeflyConfig.js';
|
|
12
13
|
export * from './addonHelper.js';
|
|
13
14
|
export * from './arrayKeysToCamel.js';
|
|
14
15
|
export * from './arrayToTree.js';
|
package/dist/regex.js
CHANGED
|
@@ -27,7 +27,9 @@ export const RegexAliases = {
|
|
|
27
27
|
/** 字母和数字 */
|
|
28
28
|
alphanumeric: '^[a-zA-Z0-9]+$',
|
|
29
29
|
/** 字母、数字和下划线 */
|
|
30
|
-
|
|
30
|
+
alphanumeric_: '^[a-zA-Z0-9_]+$',
|
|
31
|
+
/** 字母、数字、下划线和短横线 */
|
|
32
|
+
alphanumericDash_: '^[a-zA-Z0-9_-]+$',
|
|
31
33
|
/** 小写字母 */
|
|
32
34
|
lowercase: '^[a-z]+$',
|
|
33
35
|
/** 大写字母 */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "befly-shared",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -36,5 +36,5 @@
|
|
|
36
36
|
"merge-anything": "^6.0.6",
|
|
37
37
|
"pathe": "^2.0.3"
|
|
38
38
|
},
|
|
39
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "ebb51601e566ac01ef09824309a6ec60a5d32388"
|
|
40
40
|
}
|
package/src/configTypes.ts
CHANGED
|
@@ -10,10 +10,12 @@ export interface LoadConfigOptions {
|
|
|
10
10
|
files: string[];
|
|
11
11
|
/** 文件扩展名,默认 ['.js', '.ts', '.json'] */
|
|
12
12
|
extensions?: string[];
|
|
13
|
-
/** 加载模式:'first' = 返回第一个找到的配置(默认),'
|
|
14
|
-
mode?: '
|
|
13
|
+
/** 加载模式:'first' = 返回第一个找到的配置(默认),'merge' = 合并所有配置 */
|
|
14
|
+
mode?: 'merge' | 'first';
|
|
15
15
|
/** 指定要提取的字段路径数组,如 ['menus', 'database.host'],为空则返回完整对象 */
|
|
16
16
|
paths?: string[];
|
|
17
|
+
/** 默认配置,会与找到的配置合并(优先级最低) */
|
|
18
|
+
defaults?: Record<string, any>;
|
|
17
19
|
}
|
|
18
20
|
|
|
19
21
|
/**
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Befly 共享常量定义
|
|
3
|
+
* 运行时使用的常量(非纯类型)
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// ============================================
|
|
7
|
+
// API 响应码常量
|
|
8
|
+
// ============================================
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* API 响应码
|
|
12
|
+
*/
|
|
13
|
+
export const ApiCode = {
|
|
14
|
+
/** 成功 */
|
|
15
|
+
SUCCESS: 0,
|
|
16
|
+
/** 通用失败 */
|
|
17
|
+
FAIL: 1,
|
|
18
|
+
/** 未授权 */
|
|
19
|
+
UNAUTHORIZED: 401,
|
|
20
|
+
/** 禁止访问 */
|
|
21
|
+
FORBIDDEN: 403,
|
|
22
|
+
/** 未找到 */
|
|
23
|
+
NOT_FOUND: 404,
|
|
24
|
+
/** 服务器错误 */
|
|
25
|
+
SERVER_ERROR: 500
|
|
26
|
+
} as const;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* API 响应码类型
|
|
30
|
+
*/
|
|
31
|
+
export type ApiCodeType = (typeof ApiCode)[keyof typeof ApiCode];
|
|
32
|
+
|
|
33
|
+
// ============================================
|
|
34
|
+
// 错误消息常量
|
|
35
|
+
// ============================================
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 通用错误消息
|
|
39
|
+
*/
|
|
40
|
+
export const ErrorMessages = {
|
|
41
|
+
/** 未授权 */
|
|
42
|
+
UNAUTHORIZED: '请先登录',
|
|
43
|
+
/** 禁止访问 */
|
|
44
|
+
FORBIDDEN: '无访问权限',
|
|
45
|
+
/** 未找到 */
|
|
46
|
+
NOT_FOUND: '资源不存在',
|
|
47
|
+
/** 服务器错误 */
|
|
48
|
+
SERVER_ERROR: '服务器错误',
|
|
49
|
+
/** 参数错误 */
|
|
50
|
+
INVALID_PARAMS: '参数错误',
|
|
51
|
+
/** Token 过期 */
|
|
52
|
+
TOKEN_EXPIRED: 'Token 已过期',
|
|
53
|
+
/** Token 无效 */
|
|
54
|
+
TOKEN_INVALID: 'Token 无效'
|
|
55
|
+
} as const;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 错误消息类型
|
|
59
|
+
*/
|
|
60
|
+
export type ErrorMessageType = (typeof ErrorMessages)[keyof typeof ErrorMessages];
|
package/src/index.ts
CHANGED
|
@@ -3,15 +3,14 @@
|
|
|
3
3
|
* 跨包共享的工具函数、常量、类型定义
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
//
|
|
7
|
-
export * from './
|
|
6
|
+
// 常量(运行时)
|
|
7
|
+
export * from './constants.js';
|
|
8
8
|
|
|
9
|
-
//
|
|
9
|
+
// Redis 键和正则
|
|
10
10
|
export * from './redisKeys.js';
|
|
11
11
|
export * from './regex.js';
|
|
12
12
|
|
|
13
13
|
// 工具函数
|
|
14
|
-
export * from './defineAddonConfig.js';
|
|
15
14
|
export * from './addonHelper.js';
|
|
16
15
|
export * from './arrayKeysToCamel.js';
|
|
17
16
|
export * from './arrayToTree.js';
|
package/src/regex.ts
CHANGED
|
@@ -28,7 +28,9 @@ export const RegexAliases = {
|
|
|
28
28
|
/** 字母和数字 */
|
|
29
29
|
alphanumeric: '^[a-zA-Z0-9]+$',
|
|
30
30
|
/** 字母、数字和下划线 */
|
|
31
|
-
|
|
31
|
+
alphanumeric_: '^[a-zA-Z0-9_]+$',
|
|
32
|
+
/** 字母、数字、下划线和短横线 */
|
|
33
|
+
alphanumericDash_: '^[a-zA-Z0-9_-]+$',
|
|
32
34
|
/** 小写字母 */
|
|
33
35
|
lowercase: '^[a-z]+$',
|
|
34
36
|
/** 大写字母 */
|
package/src/scanConfig.ts
CHANGED
|
@@ -20,7 +20,8 @@ export async function scanConfig(options: LoadConfigOptions): Promise<Record<str
|
|
|
20
20
|
files,
|
|
21
21
|
extensions = ['.js', '.ts', '.json'],
|
|
22
22
|
mode = 'first',
|
|
23
|
-
paths
|
|
23
|
+
paths,
|
|
24
|
+
defaults = {}
|
|
24
25
|
} = options;
|
|
25
26
|
|
|
26
27
|
// 参数验证
|
|
@@ -86,7 +87,8 @@ export async function scanConfig(options: LoadConfigOptions): Promise<Record<str
|
|
|
86
87
|
}
|
|
87
88
|
|
|
88
89
|
// 合并配置(使用 mergeAndConcat 深度合并)
|
|
89
|
-
|
|
90
|
+
// 合并顺序:defaults ← configs[0] ← configs[1] ← ...
|
|
91
|
+
const finalConfig = mergeAndConcat(defaults, ...configs);
|
|
90
92
|
|
|
91
93
|
// 如果指定了 paths,则只返回指定路径的字段
|
|
92
94
|
if (paths && paths.length > 0) {
|
package/tests/regex.test.ts
CHANGED
|
@@ -44,9 +44,9 @@ describe('RegexAliases - 正则别名常量', () => {
|
|
|
44
44
|
expect(new RegExp(RegexAliases.alphanumeric).test('Hello_123')).toBe(false);
|
|
45
45
|
});
|
|
46
46
|
|
|
47
|
-
it('
|
|
48
|
-
expect(new RegExp(RegexAliases.
|
|
49
|
-
expect(new RegExp(RegexAliases.
|
|
47
|
+
it('alphanumeric_ - 字母、数字和下划线', () => {
|
|
48
|
+
expect(new RegExp(RegexAliases.alphanumeric_).test('Hello_123')).toBe(true);
|
|
49
|
+
expect(new RegExp(RegexAliases.alphanumeric_).test('Hello-123')).toBe(false);
|
|
50
50
|
});
|
|
51
51
|
});
|
|
52
52
|
|
package/tests/types.test.ts
CHANGED
|
@@ -3,7 +3,14 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import { describe, expect, it } from 'bun:test';
|
|
5
5
|
|
|
6
|
-
import { ApiCode, ErrorMessages
|
|
6
|
+
import { ApiCode, ErrorMessages } from '../src/constants.js';
|
|
7
|
+
import type { DatabaseConfig, FieldDefinition, HttpMethod, MenuItem, PaginatedResult, RedisConfig, ResponseResult, RoleInfo, UserInfo } from '../types/types.js';
|
|
8
|
+
|
|
9
|
+
// ValidationResult 在新结构中可能不存在,使用本地定义
|
|
10
|
+
interface ValidationResult {
|
|
11
|
+
code: 0 | 1;
|
|
12
|
+
fields: Record<string, string>;
|
|
13
|
+
}
|
|
7
14
|
|
|
8
15
|
describe('befly-shared 类型定义', () => {
|
|
9
16
|
describe('ResponseResult 类型', () => {
|
|
@@ -241,7 +248,6 @@ describe('befly-shared 类型定义', () => {
|
|
|
241
248
|
|
|
242
249
|
describe('BaseRequestContext 类型', () => {
|
|
243
250
|
it('请求上下文基础结构正确', () => {
|
|
244
|
-
const { BaseRequestContext } = require('../src/types.js') as { BaseRequestContext: any };
|
|
245
251
|
// BaseRequestContext 是接口,只验证可以创建符合结构的对象
|
|
246
252
|
const ctx = {
|
|
247
253
|
body: { username: 'test' },
|
package/types/addon.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Addon 配置类型定义
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Addon 作者信息
|
|
7
|
+
*/
|
|
8
|
+
export interface AddonAuthor {
|
|
9
|
+
/** 作者名称 */
|
|
10
|
+
name: string;
|
|
11
|
+
/** 作者邮箱 */
|
|
12
|
+
email?: string;
|
|
13
|
+
/** 作者网站 */
|
|
14
|
+
url?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Addon 配置
|
|
19
|
+
*/
|
|
20
|
+
export interface AddonConfig {
|
|
21
|
+
/** Addon 唯一标识(小写、短横线或下划线),例如 "admin"、"demo" */
|
|
22
|
+
name: string;
|
|
23
|
+
|
|
24
|
+
/** Addon 人类可读名称,例如 "管理后台" */
|
|
25
|
+
title: string;
|
|
26
|
+
|
|
27
|
+
/** 版本号(语义化版本),例如 "1.0.0" */
|
|
28
|
+
version?: string;
|
|
29
|
+
|
|
30
|
+
/** 简短描述 */
|
|
31
|
+
description?: string;
|
|
32
|
+
|
|
33
|
+
/** 作者信息 */
|
|
34
|
+
author?: AddonAuthor | string;
|
|
35
|
+
|
|
36
|
+
/** 源码仓库链接 */
|
|
37
|
+
repo?: string;
|
|
38
|
+
|
|
39
|
+
/** 关键词(用于搜索和分类) */
|
|
40
|
+
keywords?: string[];
|
|
41
|
+
|
|
42
|
+
/** 主入口文件路径(相对于 addon 目录),例如 "index.ts" */
|
|
43
|
+
entry?: string;
|
|
44
|
+
|
|
45
|
+
/** 依赖的其他 addon 或核心包 */
|
|
46
|
+
dependencies?: Record<string, string>;
|
|
47
|
+
|
|
48
|
+
/** 许可证 */
|
|
49
|
+
license?: string;
|
|
50
|
+
}
|
package/types/api.d.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* API 相关类型定义
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { TableDefinition } from './validate';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* HTTP 方法类型
|
|
9
|
+
*/
|
|
10
|
+
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* API 响应结果类型
|
|
14
|
+
*/
|
|
15
|
+
export interface ResponseResult<T = any> {
|
|
16
|
+
/** 状态码:0 表示成功,非 0 表示失败 */
|
|
17
|
+
code: number;
|
|
18
|
+
/** 响应消息 */
|
|
19
|
+
msg: string;
|
|
20
|
+
/** 响应数据 */
|
|
21
|
+
data?: T;
|
|
22
|
+
/** 错误信息(仅在失败时) */
|
|
23
|
+
error?: any;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 分页响应结果类型
|
|
28
|
+
*/
|
|
29
|
+
export interface PaginatedResult<T = any> {
|
|
30
|
+
/** 状态码 */
|
|
31
|
+
code: number;
|
|
32
|
+
/** 响应消息 */
|
|
33
|
+
msg: string;
|
|
34
|
+
/** 数据列表 */
|
|
35
|
+
data: T[];
|
|
36
|
+
/** 总记录数 */
|
|
37
|
+
total: number;
|
|
38
|
+
/** 当前页码 */
|
|
39
|
+
page: number;
|
|
40
|
+
/** 每页数量 */
|
|
41
|
+
limit: number;
|
|
42
|
+
/** 总页数 */
|
|
43
|
+
pages: number;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* API 路由基础配置
|
|
48
|
+
* 用于跨包共享的最小路由定义
|
|
49
|
+
*/
|
|
50
|
+
export interface BaseApiRoute {
|
|
51
|
+
/** 接口名称(必填) */
|
|
52
|
+
name: string;
|
|
53
|
+
/** HTTP 方法(可选,默认 POST) */
|
|
54
|
+
method?: HttpMethod;
|
|
55
|
+
/** 认证类型(可选,默认 true) */
|
|
56
|
+
auth?: boolean;
|
|
57
|
+
/** 字段定义(验证规则) */
|
|
58
|
+
fields?: TableDefinition;
|
|
59
|
+
/** 必填字段 */
|
|
60
|
+
required?: string[];
|
|
61
|
+
/** 路由路径(运行时生成) */
|
|
62
|
+
route?: string;
|
|
63
|
+
}
|
package/types/configTypes.d.ts
CHANGED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Befly 共享常量定义
|
|
3
|
+
* 运行时使用的常量(非纯类型)
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* API 响应码
|
|
7
|
+
*/
|
|
8
|
+
export declare const ApiCode: {
|
|
9
|
+
/** 成功 */
|
|
10
|
+
readonly SUCCESS: 0;
|
|
11
|
+
/** 通用失败 */
|
|
12
|
+
readonly FAIL: 1;
|
|
13
|
+
/** 未授权 */
|
|
14
|
+
readonly UNAUTHORIZED: 401;
|
|
15
|
+
/** 禁止访问 */
|
|
16
|
+
readonly FORBIDDEN: 403;
|
|
17
|
+
/** 未找到 */
|
|
18
|
+
readonly NOT_FOUND: 404;
|
|
19
|
+
/** 服务器错误 */
|
|
20
|
+
readonly SERVER_ERROR: 500;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* API 响应码类型
|
|
24
|
+
*/
|
|
25
|
+
export type ApiCodeType = (typeof ApiCode)[keyof typeof ApiCode];
|
|
26
|
+
/**
|
|
27
|
+
* 通用错误消息
|
|
28
|
+
*/
|
|
29
|
+
export declare const ErrorMessages: {
|
|
30
|
+
/** 未授权 */
|
|
31
|
+
readonly UNAUTHORIZED: '请先登录';
|
|
32
|
+
/** 禁止访问 */
|
|
33
|
+
readonly FORBIDDEN: '无访问权限';
|
|
34
|
+
/** 未找到 */
|
|
35
|
+
readonly NOT_FOUND: '资源不存在';
|
|
36
|
+
/** 服务器错误 */
|
|
37
|
+
readonly SERVER_ERROR: '服务器错误';
|
|
38
|
+
/** 参数错误 */
|
|
39
|
+
readonly INVALID_PARAMS: '参数错误';
|
|
40
|
+
/** Token 过期 */
|
|
41
|
+
readonly TOKEN_EXPIRED: 'Token 已过期';
|
|
42
|
+
/** Token 无效 */
|
|
43
|
+
readonly TOKEN_INVALID: 'Token 无效';
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* 错误消息类型
|
|
47
|
+
*/
|
|
48
|
+
export type ErrorMessageType = (typeof ErrorMessages)[keyof typeof ErrorMessages];
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 请求上下文相关类型定义
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 用户信息类型
|
|
7
|
+
*/
|
|
8
|
+
export interface UserInfo {
|
|
9
|
+
/** 用户 ID */
|
|
10
|
+
id: number;
|
|
11
|
+
/** 用户名 */
|
|
12
|
+
username?: string;
|
|
13
|
+
/** 邮箱 */
|
|
14
|
+
email?: string;
|
|
15
|
+
/** 角色代码 */
|
|
16
|
+
roleCode?: string;
|
|
17
|
+
/** 其他自定义字段 */
|
|
18
|
+
[key: string]: any;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 请求上下文基础接口
|
|
23
|
+
* 用于跨包共享的最小上下文定义
|
|
24
|
+
*/
|
|
25
|
+
export interface BaseRequestContext {
|
|
26
|
+
/** 请求体参数 */
|
|
27
|
+
body: Record<string, any>;
|
|
28
|
+
/** 用户信息 */
|
|
29
|
+
user: Record<string, any>;
|
|
30
|
+
/** 请求开始时间(毫秒) */
|
|
31
|
+
now: number;
|
|
32
|
+
/** 客户端 IP 地址 */
|
|
33
|
+
ip: string;
|
|
34
|
+
/** API 路由路径(如 POST/api/user/login) */
|
|
35
|
+
route: string;
|
|
36
|
+
/** 请求唯一 ID */
|
|
37
|
+
requestId: string;
|
|
38
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 加密相关类型定义
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 编码类型
|
|
7
|
+
*/
|
|
8
|
+
export type EncodingType = 'hex' | 'base64' | 'base64url';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 哈希算法类型
|
|
12
|
+
*/
|
|
13
|
+
export type HashAlgorithm = 'md5' | 'sha1' | 'sha256' | 'sha512' | 'sha384' | 'sha224';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 密码哈希选项
|
|
17
|
+
*/
|
|
18
|
+
export interface PasswordHashOptions {
|
|
19
|
+
algorithm?: 'argon2' | 'bcrypt';
|
|
20
|
+
memoryCost?: number;
|
|
21
|
+
timeCost?: number;
|
|
22
|
+
[key: string]: any;
|
|
23
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 数据库相关类型定义
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* SQL 值类型
|
|
7
|
+
*/
|
|
8
|
+
export type SqlValue = string | number | boolean | null | Date;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* SQL 参数数组类型
|
|
12
|
+
*/
|
|
13
|
+
export type SqlParams = SqlValue[];
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 排序方向
|
|
17
|
+
*/
|
|
18
|
+
export type OrderDirection = 'ASC' | 'DESC' | 'asc' | 'desc';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 数据库类型
|
|
22
|
+
*/
|
|
23
|
+
export type DatabaseType = 'mysql' | 'postgresql' | 'sqlite';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 数据库配置类型
|
|
27
|
+
*/
|
|
28
|
+
export interface DatabaseConfig {
|
|
29
|
+
/** 数据库类型 */
|
|
30
|
+
type: DatabaseType;
|
|
31
|
+
/** 主机地址 */
|
|
32
|
+
host: string;
|
|
33
|
+
/** 端口号 */
|
|
34
|
+
port: number;
|
|
35
|
+
/** 用户名 */
|
|
36
|
+
user: string;
|
|
37
|
+
/** 密码 */
|
|
38
|
+
password: string;
|
|
39
|
+
/** 数据库名 */
|
|
40
|
+
database: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Redis 配置类型
|
|
45
|
+
*/
|
|
46
|
+
export interface RedisConfig {
|
|
47
|
+
/** 主机地址 */
|
|
48
|
+
host: string;
|
|
49
|
+
/** 端口号 */
|
|
50
|
+
port: number;
|
|
51
|
+
/** 密码 */
|
|
52
|
+
password?: string;
|
|
53
|
+
/** 数据库索引 */
|
|
54
|
+
db?: number;
|
|
55
|
+
}
|