befly 3.5.4 → 3.5.6

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.
@@ -16,6 +16,7 @@ import { Logger } from '../lib/logger.js';
16
16
  import { Database } from '../lib/database.js';
17
17
  import { RedisHelper } from '../lib/redisHelper.js';
18
18
  import { scanAddons, getAddonDir, addonDirExists } from '../util.js';
19
+ import { coreApiDir, projectApiDir } from '../paths.js';
19
20
  import { readdirSync, statSync } from 'node:fs';
20
21
  import { join, dirname, relative, basename } from 'pathe';
21
22
 
@@ -118,23 +119,21 @@ async function scanAllApis(projectRoot: string): Promise<ApiInfo[]> {
118
119
  const apis: ApiInfo[] = [];
119
120
 
120
121
  // 1. 扫描 Core 框架 API
121
- const coreApisDir = join(dirname(projectRoot), 'core', 'apis');
122
122
  try {
123
- const coreApiFiles = scanTsFiles(coreApisDir);
123
+ const coreApiFiles = scanTsFiles(coreApiDir);
124
124
 
125
125
  for (const filePath of coreApiFiles) {
126
- const apiInfo = await extractApiInfo(filePath, coreApisDir, 'core', '', '核心接口');
126
+ const apiInfo = await extractApiInfo(filePath, coreApiDir, 'core', '', '核心接口');
127
127
  if (apiInfo) {
128
128
  apis.push(apiInfo);
129
129
  }
130
130
  }
131
131
 
132
132
  // 2. 扫描项目 API
133
- const projectApisDir = join(projectRoot, 'apis');
134
- const projectApiFiles = scanTsFiles(projectApisDir);
133
+ const projectApiFiles = scanTsFiles(projectApiDir);
135
134
 
136
135
  for (const filePath of projectApiFiles) {
137
- const apiInfo = await extractApiInfo(filePath, projectApisDir, 'app', '', '项目接口');
136
+ const apiInfo = await extractApiInfo(filePath, projectApiDir, 'app', '', '项目接口');
138
137
  if (apiInfo) {
139
138
  apis.push(apiInfo);
140
139
  }
package/entry.ts ADDED
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Befly 默认入口文件
3
+ * 当用户项目没有自定义 main.ts 时使用此文件
4
+ */
5
+
6
+ import { Befly } from 'befly';
7
+
8
+ const app = new Befly();
9
+ await app.listen();
package/env.ts ADDED
@@ -0,0 +1,112 @@
1
+ /**
2
+ * 环境变量配置
3
+ * 根据 NODE_ENV 自动切换开发/生产环境配置
4
+ * 项目可通过创建 env.ts 文件覆盖这些配置
5
+ */
6
+
7
+ import { existsSync } from 'node:fs';
8
+ import type { EnvConfig } from './types/env.js';
9
+
10
+ const isDev = process.env.NODE_ENV === 'development';
11
+
12
+ /**
13
+ * 核心默认配置
14
+ * 使用三元运算符根据环境切换配置
15
+ */
16
+ const coreEnv: EnvConfig = {
17
+ // ========== 项目配置 ==========
18
+ NODE_ENV: process.env.NODE_ENV || 'development',
19
+ APP_NAME: isDev ? '野蜂飞舞开发环境' : '野蜂飞舞正式环境',
20
+ APP_PORT: 3000,
21
+ APP_HOST: isDev ? '0.0.0.0' : '127.0.0.1',
22
+ DEV_EMAIL: 'dev@qq.com',
23
+ DEV_PASSWORD: isDev ? '123456' : '123456',
24
+ BODY_LIMIT: 10485760, // 10MB
25
+ PARAMS_CHECK: false,
26
+
27
+ // ========== 日志配置 ==========
28
+ LOG_DEBUG: isDev ? 1 : 0,
29
+ LOG_EXCLUDE_FIELDS: 'password,token,secret',
30
+ LOG_DIR: './logs',
31
+ LOG_TO_CONSOLE: 1,
32
+ LOG_MAX_SIZE: 52428800, // 50MB
33
+
34
+ // ========== 时区配置 ==========
35
+ TZ: 'Asia/Shanghai',
36
+
37
+ // ========== 数据库配置 ==========
38
+ DB_ENABLE: 1,
39
+ DB_TYPE: 'mysql',
40
+ DB_HOST: '127.0.0.1',
41
+ DB_PORT: 3306,
42
+ DB_USER: 'root',
43
+ DB_PASS: '',
44
+ DB_NAME: 'befly_demo',
45
+ DB_DEBUG: 0,
46
+ DB_POOL_MAX: 10,
47
+
48
+ // ========== Redis 配置 ==========
49
+ REDIS_ENABLE: 1,
50
+ REDIS_HOST: '127.0.0.1',
51
+ REDIS_PORT: 6379,
52
+ REDIS_USERNAME: '',
53
+ REDIS_PASSWORD: '',
54
+ REDIS_DB: 0,
55
+ REDIS_KEY_PREFIX: 'befly',
56
+
57
+ // ========== JWT 配置 ==========
58
+ JWT_SECRET: isDev ? 'befly-dev-secret' : 'befly-prod-secret',
59
+ JWT_EXPIRES_IN: '7d',
60
+ JWT_ALGORITHM: 'HS256',
61
+
62
+ // ========== CORS 配置 ==========
63
+ CORS_ALLOWED_ORIGIN: '*',
64
+ CORS_ALLOWED_METHODS: 'GET, POST, PUT, DELETE, OPTIONS',
65
+ CORS_ALLOWED_HEADERS: 'Content-Type, Authorization, authorization, token',
66
+ CORS_EXPOSE_HEADERS: 'Content-Range, X-Content-Range, Authorization, authorization, token',
67
+ CORS_MAX_AGE: 86400,
68
+ CORS_ALLOW_CREDENTIALS: 'true',
69
+
70
+ // ========== 邮件配置 ==========
71
+ MAIL_HOST: '',
72
+ MAIL_PORT: 587,
73
+ MAIL_POOL: 'true',
74
+ MAIL_SECURE: 'false',
75
+ MAIL_USER: '',
76
+ MAIL_PASS: '',
77
+ MAIL_SENDER: '',
78
+ MAIL_ADDRESS: ''
79
+ };
80
+
81
+ /**
82
+ * 尝试加载项目级别的 env.ts 配置
83
+ */
84
+ async function loadProjectEnv(): Promise<Partial<EnvConfig>> {
85
+ try {
86
+ // 尝试从项目根目录加载 env.ts
87
+ const projectEnvPath = process.cwd() + '/env.ts';
88
+
89
+ // 检查文件是否存在
90
+ if (!existsSync(projectEnvPath)) {
91
+ return {};
92
+ }
93
+
94
+ // 动态导入
95
+ const module = await import(projectEnvPath);
96
+ return module.Env || module.default || {};
97
+ } catch (error) {
98
+ // 项目没有自定义配置,使用核心默认配置
99
+ return {};
100
+ }
101
+ }
102
+
103
+ // 使用 top-level await 加载项目配置
104
+ const projectEnv = await loadProjectEnv();
105
+
106
+ /**
107
+ * 合并配置:项目配置覆盖核心配置
108
+ */
109
+ export const Env: EnvConfig = {
110
+ ...coreEnv,
111
+ ...projectEnv
112
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "befly",
3
- "version": "3.5.4",
3
+ "version": "3.5.6",
4
4
  "description": "Befly - 为 Bun 专属打造的 TypeScript API 接口框架核心引擎",
5
5
  "type": "module",
6
6
  "private": false,
@@ -64,6 +64,8 @@
64
64
  "tsconfig.json",
65
65
  "LICENSE",
66
66
  "main.ts",
67
+ "env.ts",
68
+ "entry.ts",
67
69
  "menu.json",
68
70
  "paths.ts",
69
71
  "util.ts",
@@ -78,5 +80,5 @@
78
80
  "es-toolkit": "^1.41.0",
79
81
  "pathe": "^2.0.3"
80
82
  },
81
- "gitHead": "561c5e43741ceb111f677cf3d5ce2421a79d3975"
83
+ "gitHead": "7ef2b7ddf3c66a79d3d0af5e6f9653d7d7d02fdb"
82
84
  }