befly 3.9.8 → 3.9.10
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/checks/checkApi.ts +3 -2
- package/loader/loadApis.ts +18 -7
- package/package.json +2 -2
- package/types/api.d.ts +3 -2
package/checks/checkApi.ts
CHANGED
|
@@ -67,8 +67,9 @@ export async function checkApi(): Promise<void> {
|
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
// 验证可选属性的类型(如果提供了)
|
|
70
|
-
|
|
71
|
-
|
|
70
|
+
const validMethods = ['GET', 'POST', 'GET,POST', 'POST,GET'];
|
|
71
|
+
if (api.method && !validMethods.includes(api.method.toUpperCase())) {
|
|
72
|
+
Logger.warn(`[${item.displayName}] 接口 ${apiPath} 的 method 属性必须是有效的 HTTP 方法 (GET, POST, GET,POST, POST,GET)`);
|
|
72
73
|
}
|
|
73
74
|
if (api.auth !== undefined && typeof api.auth !== 'boolean') {
|
|
74
75
|
Logger.warn(`[${item.displayName}] 接口 ${apiPath} 的 auth 属性必须是布尔值 (true=需登录, false=公开)`);
|
package/loader/loadApis.ts
CHANGED
|
@@ -59,7 +59,7 @@ const DEFAULT_API_FIELDS = {
|
|
|
59
59
|
|
|
60
60
|
/**
|
|
61
61
|
* 加载所有 API 路由
|
|
62
|
-
* @param apis - API
|
|
62
|
+
* @param apis - API 跁由映射表
|
|
63
63
|
*/
|
|
64
64
|
export async function loadApis(apis: Map<string, ApiRoute>): Promise<void> {
|
|
65
65
|
try {
|
|
@@ -69,7 +69,7 @@ export async function loadApis(apis: Map<string, ApiRoute>): Promise<void> {
|
|
|
69
69
|
filePath: file.filePath,
|
|
70
70
|
relativePath: file.relativePath,
|
|
71
71
|
type: 'project' as const,
|
|
72
|
-
routePrefix: '',
|
|
72
|
+
routePrefix: '/',
|
|
73
73
|
typeName: '项目'
|
|
74
74
|
}));
|
|
75
75
|
|
|
@@ -93,7 +93,7 @@ export async function loadApis(apis: Map<string, ApiRoute>): Promise<void> {
|
|
|
93
93
|
filePath: file.filePath,
|
|
94
94
|
relativePath: file.relativePath,
|
|
95
95
|
type: 'addon' as const,
|
|
96
|
-
routePrefix:
|
|
96
|
+
routePrefix: `/addon/${addon}/`, // 组件 API 默认带斜杠
|
|
97
97
|
typeName: `组件${addon}`
|
|
98
98
|
});
|
|
99
99
|
}
|
|
@@ -111,15 +111,26 @@ export async function loadApis(apis: Map<string, ApiRoute>): Promise<void> {
|
|
|
111
111
|
const api = apiImport.default;
|
|
112
112
|
|
|
113
113
|
// 设置默认值
|
|
114
|
-
|
|
114
|
+
const methodStr = (api.method || 'POST').toUpperCase();
|
|
115
115
|
api.auth = api.auth !== undefined ? api.auth : true;
|
|
116
116
|
// 合并默认字段:默认字段作为基础,API 自定义字段优先级更高
|
|
117
117
|
api.fields = { ...DEFAULT_API_FIELDS, ...(api.fields || {}) };
|
|
118
118
|
api.required = api.required || [];
|
|
119
119
|
|
|
120
|
-
//
|
|
121
|
-
|
|
122
|
-
|
|
120
|
+
// 构建路由路径(不含方法)
|
|
121
|
+
const routePath = `/api${apiFile.routePrefix}${apiFile.relativePath}`;
|
|
122
|
+
|
|
123
|
+
// 支持逗号分隔的多方法,拆分后分别注册
|
|
124
|
+
const methods = methodStr
|
|
125
|
+
.split(',')
|
|
126
|
+
.map((m: string) => m.trim())
|
|
127
|
+
.filter((m: string) => m);
|
|
128
|
+
for (const method of methods) {
|
|
129
|
+
const route = `${method}${routePath}`;
|
|
130
|
+
// 为每个方法创建独立的路由对象
|
|
131
|
+
const routeApi = { ...api, method: method, route: route };
|
|
132
|
+
apis.set(route, routeApi);
|
|
133
|
+
}
|
|
123
134
|
} catch (error: any) {
|
|
124
135
|
Logger.error({ err: error, api: apiFile.relativePath, type: apiFile.typeName }, '接口加载失败');
|
|
125
136
|
process.exit(1);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "befly",
|
|
3
|
-
"version": "3.9.
|
|
3
|
+
"version": "3.9.10",
|
|
4
4
|
"description": "Befly - 为 Bun 专属打造的 TypeScript API 接口框架核心引擎",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"private": false,
|
|
@@ -74,7 +74,7 @@
|
|
|
74
74
|
"pino": "^10.1.0",
|
|
75
75
|
"pino-roll": "^4.0.0"
|
|
76
76
|
},
|
|
77
|
-
"gitHead": "
|
|
77
|
+
"gitHead": "c521a468291c1555672e972db43021dda1763402",
|
|
78
78
|
"devDependencies": {
|
|
79
79
|
"typescript": "^5.9.3"
|
|
80
80
|
}
|
package/types/api.d.ts
CHANGED
|
@@ -8,8 +8,9 @@ import type { RequestContext } from './context.js';
|
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* HTTP 方法类型
|
|
11
|
+
* 支持 GET、POST 或逗号分隔的组合
|
|
11
12
|
*/
|
|
12
|
-
export type HttpMethod = 'GET' | 'POST' | '
|
|
13
|
+
export type HttpMethod = 'GET' | 'POST' | 'GET,POST' | 'POST,GET';
|
|
13
14
|
|
|
14
15
|
/**
|
|
15
16
|
* 用户信息类型
|
|
@@ -69,7 +70,7 @@ export interface ApiRoute<T = any, R = any> {
|
|
|
69
70
|
/** 处理器函数(必填) */
|
|
70
71
|
handler: ApiHandler<T, R>;
|
|
71
72
|
|
|
72
|
-
/** HTTP 方法(可选,默认 POST
|
|
73
|
+
/** HTTP 方法(可选,默认 POST,支持逗号分隔多个方法) */
|
|
73
74
|
method?: HttpMethod;
|
|
74
75
|
|
|
75
76
|
/** 认证类型(可选,默认 true)
|