befly-api 3.17.13 → 3.17.15
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 +20 -56
- package/apis/test/hi.js +20 -0
- package/configs/menus.json +19 -0
- package/{tsconfig.json → jsconfig.json} +2 -2
- package/package.json +5 -9
- package/pm2.config.cjs +2 -2
- package/apis/test/hi.ts +0 -15
- package/main.ts +0 -15
- package/menus.json +0 -1
package/README.md
CHANGED
|
@@ -4,18 +4,18 @@
|
|
|
4
4
|
|
|
5
5
|
> 道生一,一生二,二生三,三生万物
|
|
6
6
|
|
|
7
|
-
**Befly 3.0 -
|
|
7
|
+
**Befly 3.0 - JavaScript 版本**
|
|
8
8
|
|
|
9
9
|
## 🎯 简介
|
|
10
10
|
|
|
11
11
|
Befly 是专为 Bun 运行时设计的现代化 API 框架,提供:
|
|
12
12
|
|
|
13
|
-
- ⚡
|
|
13
|
+
- ⚡ **简洁 JavaScript** - 轻量直观
|
|
14
14
|
- 🚀 **高性能** - 基于 Bun 运行时,超快的启动和执行速度
|
|
15
15
|
- 🔌 **插件化架构** - 灵活的插件系统,轻松扩展功能
|
|
16
16
|
- 🗄️ **数据库(MySQL 8.0+)** - core 仅支持 MySQL 8.0 及以上
|
|
17
17
|
- 📝 **自动化表管理** - 基于 JSON 的表定义,自动同步数据库结构
|
|
18
|
-
- 🔐 **内置身份验证** -
|
|
18
|
+
- 🔐 **内置身份验证** - sid 会话认证,角色权限管理
|
|
19
19
|
- 📊 **完整日志系统** - 结构化日志,敏感字段过滤
|
|
20
20
|
|
|
21
21
|
## 📦 快速开始
|
|
@@ -32,27 +32,27 @@ bun add befly
|
|
|
32
32
|
|
|
33
33
|
### 最简示例
|
|
34
34
|
|
|
35
|
-
```
|
|
36
|
-
//
|
|
35
|
+
```javascript
|
|
36
|
+
// index.js
|
|
37
37
|
import { Befly } from "befly";
|
|
38
38
|
|
|
39
|
-
// 配置从 configs/befly.*.json 扫描加载(见 packages/core/befly.config.
|
|
40
|
-
const app = new Befly(
|
|
41
|
-
|
|
39
|
+
// 配置从 configs/befly.*.json 扫描加载(见 packages/core/befly.config.js)
|
|
40
|
+
const app = new Befly({
|
|
41
|
+
env: Bun.env
|
|
42
|
+
});
|
|
43
|
+
await app.start();
|
|
42
44
|
```
|
|
43
45
|
|
|
44
46
|
运行项目:
|
|
45
47
|
|
|
46
48
|
```bash
|
|
47
|
-
bun run
|
|
49
|
+
bun run index.js
|
|
48
50
|
```
|
|
49
51
|
|
|
50
52
|
### 创建第一个接口
|
|
51
53
|
|
|
52
|
-
```
|
|
53
|
-
// apis/user/hello.
|
|
54
|
-
import type { ApiRoute } from "befly/types/api";
|
|
55
|
-
|
|
54
|
+
```javascript
|
|
55
|
+
// apis/user/hello.js
|
|
56
56
|
export default {
|
|
57
57
|
name: "问候接口",
|
|
58
58
|
auth: false, // 公开接口
|
|
@@ -65,58 +65,30 @@ export default {
|
|
|
65
65
|
}
|
|
66
66
|
};
|
|
67
67
|
}
|
|
68
|
-
}
|
|
68
|
+
};
|
|
69
69
|
```
|
|
70
70
|
|
|
71
71
|
访问:`http://localhost:3000/api/app/user/hello`
|
|
72
72
|
|
|
73
73
|
### 最小 smoke:在 api 模板项目新增接口并访问
|
|
74
74
|
|
|
75
|
-
1. 新增文件:`apis/user/hello.
|
|
75
|
+
1. 新增文件:`apis/user/hello.js`(上面示例可直接使用)
|
|
76
76
|
2. 启动服务:`bun run dev`
|
|
77
77
|
3. 访问:`http://localhost:3000/api/app/user/hello`
|
|
78
78
|
|
|
79
79
|
## 🔥 新版本特性(3.0)
|
|
80
80
|
|
|
81
|
-
### TypeScript 全面支持
|
|
82
|
-
|
|
83
|
-
```typescript
|
|
84
|
-
import type { ApiRoute } from "befly/types/api";
|
|
85
|
-
import type { BeflyContext } from "befly/types/befly";
|
|
86
|
-
import type { User } from "./types/models";
|
|
87
|
-
|
|
88
|
-
export default {
|
|
89
|
-
name: "获取用户",
|
|
90
|
-
auth: true,
|
|
91
|
-
fields: {
|
|
92
|
-
id: "用户ID|number|1|999999|null|1|null"
|
|
93
|
-
},
|
|
94
|
-
required: ["id"],
|
|
95
|
-
handler: async (befly: BeflyContext, ctx) => {
|
|
96
|
-
const { id } = ctx.body;
|
|
97
|
-
|
|
98
|
-
// 类型安全的数据库查询
|
|
99
|
-
const user = await befly.db.getOne<User>({
|
|
100
|
-
table: "user",
|
|
101
|
-
where: { id }
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
return { msg: "查询成功", data: user };
|
|
105
|
-
}
|
|
106
|
-
} as ApiRoute;
|
|
107
|
-
```
|
|
108
|
-
|
|
109
81
|
### 增强的数据库操作
|
|
110
82
|
|
|
111
|
-
```
|
|
83
|
+
```javascript
|
|
112
84
|
// 查询单条
|
|
113
|
-
const user = await befly.db.getOne
|
|
85
|
+
const user = await befly.db.getOne({
|
|
114
86
|
table: "user",
|
|
115
87
|
where: { id: 1 }
|
|
116
88
|
});
|
|
117
89
|
|
|
118
90
|
// 分页列表
|
|
119
|
-
const result = await befly.db.getList
|
|
91
|
+
const result = await befly.db.getList({
|
|
120
92
|
table: "product",
|
|
121
93
|
where: { category: "electronics" },
|
|
122
94
|
page: 1,
|
|
@@ -163,9 +135,7 @@ await befly.db.delData({
|
|
|
163
135
|
|
|
164
136
|
字段定义格式:`"字段名|类型|最小值|最大值|默认值|是否索引|正则约束"`
|
|
165
137
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
请参考:`befly` 的同步文档:`packages/core/docs/reference/sync.md`(`syncDb` / `syncAll` 等同步流程说明)。
|
|
138
|
+
数据库与同步相关规则请参考 `.github/skills/database-development/SKILL.md` 与 `.github/skills/core-runtime-overview/SKILL.md`。
|
|
169
139
|
|
|
170
140
|
## 🗄️ 数据库配置(MySQL 8.0+)
|
|
171
141
|
|
|
@@ -181,13 +151,7 @@ DB_NAME=my_database
|
|
|
181
151
|
|
|
182
152
|
## 📖 文档
|
|
183
153
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
- [快速开始](./docs/02-快速上手/01-10分钟体验.md)
|
|
187
|
-
- [核心概念](./docs/03-核心概念/)
|
|
188
|
-
- [API 开发](./docs/04-API开发/)
|
|
189
|
-
- [数据库操作](./docs/05-数据库/)
|
|
190
|
-
- [TypeScript 支持](./docs/10-TypeScript/01-TypeScript支持.md)
|
|
154
|
+
请优先参考仓库技能文档(`.github/skills/*/SKILL.md`),其中 API/表定义/插件/钩子/菜单/页面/配置/数据库/Redis/日志/运行链路已与当前代码对齐。
|
|
191
155
|
|
|
192
156
|
### 目录说明
|
|
193
157
|
|
package/apis/test/hi.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { buildHelloMessage } from "../../utils/util.js";
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
name: "测试接口",
|
|
5
|
+
method: "POST",
|
|
6
|
+
body: "none",
|
|
7
|
+
auth: false,
|
|
8
|
+
fields: {},
|
|
9
|
+
required: [],
|
|
10
|
+
handler: async (befly, ctx) => {
|
|
11
|
+
if (!ctx.body) {
|
|
12
|
+
ctx.body = {};
|
|
13
|
+
}
|
|
14
|
+
const name = ctx.input && ctx.input.name ? String(ctx.input.name) : "";
|
|
15
|
+
ctx.body.msg = buildHelloMessage(name);
|
|
16
|
+
ctx.body.data = {
|
|
17
|
+
timestamp: Date.now()
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"/": "首页|1",
|
|
3
|
+
"/people": "人员管理|2",
|
|
4
|
+
"/permission": "权限设置|3",
|
|
5
|
+
"/config": "配置管理|4",
|
|
6
|
+
"/log": "日志管理|5",
|
|
7
|
+
"/login": "登录页|6",
|
|
8
|
+
"/403": "403|7",
|
|
9
|
+
"/people/admin": "管理员|1",
|
|
10
|
+
"/permission/role": "角色管理|1",
|
|
11
|
+
"/permission/menu": "菜单列表|2",
|
|
12
|
+
"/permission/api": "接口列表|3",
|
|
13
|
+
"/config/dictType": "字典类型|1",
|
|
14
|
+
"/config/dict": "字典列表|2",
|
|
15
|
+
"/config/system": "系统配置|3",
|
|
16
|
+
"/log/login": "登录日志|1",
|
|
17
|
+
"/log/email": "邮件日志|2",
|
|
18
|
+
"/log/operate": "操作日志|3"
|
|
19
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"extends": "../../
|
|
2
|
+
"extends": "../../jsconfig.base.json",
|
|
3
3
|
"compilerOptions": {
|
|
4
4
|
"types": ["bun"],
|
|
5
5
|
|
|
@@ -8,6 +8,6 @@
|
|
|
8
8
|
"exactOptionalPropertyTypes": true,
|
|
9
9
|
"noPropertyAccessFromIndexSignature": true
|
|
10
10
|
},
|
|
11
|
-
"include": ["**/*.
|
|
11
|
+
"include": ["**/*.js"],
|
|
12
12
|
"exclude": ["node_modules", "data", "logs", "tests"]
|
|
13
13
|
}
|
package/package.json
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "befly-api",
|
|
3
|
-
"version": "3.17.
|
|
4
|
-
"gitHead": "
|
|
3
|
+
"version": "3.17.15",
|
|
4
|
+
"gitHead": "3f389979b8d565fe0476e0859df5a2c94627137c",
|
|
5
5
|
"private": false,
|
|
6
6
|
"description": "Befly 3.0 API Template",
|
|
7
7
|
"files": [
|
|
8
8
|
"bunfig.toml",
|
|
9
9
|
"LICENSE",
|
|
10
|
-
"main.ts",
|
|
11
10
|
"menus.json",
|
|
12
11
|
"package.json",
|
|
13
12
|
"pm2.config.cjs",
|
|
14
13
|
"README.md",
|
|
15
|
-
"
|
|
14
|
+
"jsconfig.json",
|
|
16
15
|
"apis/",
|
|
17
16
|
"configs/",
|
|
18
17
|
"tables/"
|
|
@@ -23,15 +22,12 @@
|
|
|
23
22
|
"registry": "https://registry.npmjs.org"
|
|
24
23
|
},
|
|
25
24
|
"scripts": {
|
|
26
|
-
"dev": "NODE_ENV=development bun run main.ts",
|
|
27
|
-
"start": "NODE_ENV=production bun run main.ts",
|
|
28
25
|
"serve": "bunx --bun pm2 start pm2.config.cjs",
|
|
29
|
-
"
|
|
26
|
+
"dev": "bun run index.js",
|
|
30
27
|
"test": "bun test"
|
|
31
28
|
},
|
|
32
29
|
"dependencies": {
|
|
33
|
-
"
|
|
34
|
-
"befly": "^3.16.9"
|
|
30
|
+
"befly": "3.16.11"
|
|
35
31
|
},
|
|
36
32
|
"engines": {
|
|
37
33
|
"bun": ">=1.3.0"
|
package/pm2.config.cjs
CHANGED
|
@@ -2,7 +2,7 @@ module.exports = {
|
|
|
2
2
|
apps: [
|
|
3
3
|
{
|
|
4
4
|
name: "befly",
|
|
5
|
-
script: "
|
|
5
|
+
script: "index.js",
|
|
6
6
|
interpreter: "bun",
|
|
7
7
|
|
|
8
8
|
// 集群模式配置
|
|
@@ -20,7 +20,7 @@ module.exports = {
|
|
|
20
20
|
|
|
21
21
|
// 从 .env.production 动态加载的环境变量(使用 Bun API)
|
|
22
22
|
env: {
|
|
23
|
-
|
|
23
|
+
RUN_MODE: "production"
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
]
|
package/apis/test/hi.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 示例 / 测试接口
|
|
3
|
-
*
|
|
4
|
-
* 注意:本文件仅用于模板演示与本地测试,不建议在生产环境启用或依赖其行为。
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import type { BeflyContext } from "befly/types/befly";
|
|
8
|
-
|
|
9
|
-
export default {
|
|
10
|
-
name: "测试接口",
|
|
11
|
-
handler: async (befly: BeflyContext) => {
|
|
12
|
-
// 返回成功信息
|
|
13
|
-
return befly.tool.Yes("测试成功");
|
|
14
|
-
}
|
|
15
|
-
};
|
package/main.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { Befly } from "befly";
|
|
2
|
-
|
|
3
|
-
import { isCoreErrorAlreadyLogged } from "./utils/util";
|
|
4
|
-
|
|
5
|
-
const befly = new Befly();
|
|
6
|
-
|
|
7
|
-
befly.start(Bun.env).catch((err) => {
|
|
8
|
-
const alreadyLogged = isCoreErrorAlreadyLogged(err);
|
|
9
|
-
|
|
10
|
-
if (!alreadyLogged) {
|
|
11
|
-
const msg = err instanceof Error ? err.stack || err.message : String(err);
|
|
12
|
-
process.stderr.write(`[befly] 启动失败: ${msg}\n`);
|
|
13
|
-
}
|
|
14
|
-
process.exit(1);
|
|
15
|
-
});
|
package/menus.json
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
[]
|