befly 1.2.2 → 1.2.4

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.
@@ -5,22 +5,18 @@ import { Jwt } from '../../utils/jwt.js';
5
5
 
6
6
  export default Api.POST('令牌检测', false, {}, [], async (befly, ctx) => {
7
7
  try {
8
- const jwtData = await Jwt.verify(ctx.headers?.authorization?.split(' ')[1] || '');
9
- return RYes(
10
- '令牌有效',
11
- {
12
- state: 'yes'
13
- },
14
- {
15
- detail: jwtData
16
- }
17
- );
8
+ const token = ctx.headers?.authorization?.split(' ')[1] || '';
9
+ if (!token) {
10
+ return RNo('令牌不能为空');
11
+ }
12
+ const jwtData = await Jwt.verify(token);
13
+ return RYes('令牌有效');
18
14
  } catch (error) {
19
15
  befly.logger.error({
20
16
  msg: '令牌检测失败',
21
17
  error: error.message,
22
18
  stack: error.stack
23
19
  });
24
- return RNo('令牌检测失败', { state: 'no' });
20
+ return RNo('令牌检测失败');
25
21
  }
26
22
  });
package/main.js CHANGED
@@ -319,6 +319,7 @@ class Befly {
319
319
  hostname: Env.APP_HOST,
320
320
  routes: {
321
321
  '/': async (req) => {
322
+ const corsOptions = setCorsOptions(req);
322
323
  return Response.json(
323
324
  {
324
325
  code: 0,
@@ -335,6 +336,7 @@ class Befly {
335
336
  '/api/*': async (req) => {
336
337
  try {
337
338
  const corsOptions = setCorsOptions(req);
339
+
338
340
  // 直接返回options请求
339
341
  if (req.method === 'OPTIONS') {
340
342
  return new Response(null, {
@@ -342,6 +344,7 @@ class Befly {
342
344
  headers: corsOptions.headers
343
345
  });
344
346
  }
347
+
345
348
  // 初始化请求数据存储
346
349
  const ctx = {
347
350
  headers: Object.fromEntries(req.headers.entries()),
@@ -448,7 +451,15 @@ class Befly {
448
451
  });
449
452
  }
450
453
 
451
- if (api.auth && api.auth !== true && ctx.user.role !== api.auth) {
454
+ // 如果为字符串,则判断是否等于角色类型
455
+ if (isType(api.auth, 'string') && api.auth !== ctx.user?.role_type) {
456
+ return Response.json(RNo('没有权限'), {
457
+ headers: corsOptions.headers
458
+ });
459
+ }
460
+
461
+ // 如果为数组,则判断角色是否在数组中
462
+ if (isType(api.auth, 'array') && !api.auth.includes(ctx.user?.role)) {
452
463
  return Response.json(RNo('没有权限'), {
453
464
  headers: corsOptions.headers
454
465
  });
@@ -489,6 +500,15 @@ class Befly {
489
500
  },
490
501
  '/*': async (req) => {
491
502
  const corsOptions = setCorsOptions(req);
503
+
504
+ // 直接返回options请求
505
+ if (req.method === 'OPTIONS') {
506
+ return new Response(null, {
507
+ status: 204,
508
+ headers: corsOptions.headers
509
+ });
510
+ }
511
+
492
512
  const url = new URL(req.url);
493
513
  const filePath = path.join(process.cwd(), 'public', url.pathname);
494
514
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "befly",
3
- "version": "1.2.2",
3
+ "version": "1.2.4",
4
4
  "description": "Buma - 为 Bun 专属打造的 API 接口框架核心引擎",
5
5
  "type": "module",
6
6
  "private": false,
@@ -49,5 +49,5 @@
49
49
  "README.md",
50
50
  "vitest.config.js"
51
51
  ],
52
- "gitHead": "2fc8a0aa987c42e983e0e2ce2e74cddbfed53a7f"
52
+ "gitHead": "2615f45da0551949dee6f75123c54259b91c05ef"
53
53
  }