befly 3.9.8 → 3.9.9

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.
@@ -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: `addon/${addon}`,
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
- api.method = api.method || 'POST';
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
- api.route = `${api.method.toUpperCase()}/api/${apiFile.routePrefix ? apiFile.routePrefix + '/' : ''}${apiFile.relativePath}`;
122
- apis.set(api.route, api);
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.8",
3
+ "version": "3.9.9",
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": "6ee1c2a6513518c9864a8652ed9c50115ada5c7b",
77
+ "gitHead": "dc1b7266b708e6ab5192f0bcdca20627f9bce5b2",
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' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';
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)