befly-tpl 3.0.1

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.
Files changed (142) hide show
  1. package/.env.development +83 -0
  2. package/LICENSE +201 -0
  3. package/README.md +20 -0
  4. package/README.ts.md +175 -0
  5. package/addon-loader.example.ts +99 -0
  6. package/addons/_template/README.md +123 -0
  7. package/addons/_template/addon.config.json +17 -0
  8. package/addons/_template/apis/example.ts +33 -0
  9. package/addons/_template/checks/example.ts +23 -0
  10. package/addons/_template/config/default.ts +12 -0
  11. package/addons/_template/plugins/example.ts +39 -0
  12. package/addons/_template/tables/example.json +7 -0
  13. package/addons/_template/types/index.d.ts +21 -0
  14. package/addons/admin/README.md +179 -0
  15. package/addons/admin/addon.config.json +13 -0
  16. package/addons/admin/apis/adminDel.ts +37 -0
  17. package/addons/admin/apis/adminInfo.ts +50 -0
  18. package/addons/admin/apis/adminIns.ts +70 -0
  19. package/addons/admin/apis/adminList.ts +24 -0
  20. package/addons/admin/apis/adminRoleDetail.ts +38 -0
  21. package/addons/admin/apis/adminRoleSave.ts +40 -0
  22. package/addons/admin/apis/adminUpd.ts +54 -0
  23. package/addons/admin/apis/apiAll.ts +38 -0
  24. package/addons/admin/apis/cacheRefresh.ts +36 -0
  25. package/addons/admin/apis/dashboardAddonList.ts +16 -0
  26. package/addons/admin/apis/dashboardChangelog.ts +41 -0
  27. package/addons/admin/apis/dashboardConfigStatus.ts +56 -0
  28. package/addons/admin/apis/dashboardEnvironmentInfo.ts +48 -0
  29. package/addons/admin/apis/dashboardPerformanceMetrics.ts +25 -0
  30. package/addons/admin/apis/dashboardPermissionStats.ts +33 -0
  31. package/addons/admin/apis/dashboardServiceStatus.ts +84 -0
  32. package/addons/admin/apis/dashboardSystemInfo.ts +34 -0
  33. package/addons/admin/apis/dashboardSystemOverview.ts +34 -0
  34. package/addons/admin/apis/dashboardSystemResources.ts +119 -0
  35. package/addons/admin/apis/dictAll.ts +26 -0
  36. package/addons/admin/apis/dictDel.ts +27 -0
  37. package/addons/admin/apis/dictDetail.ts +28 -0
  38. package/addons/admin/apis/dictIns.ts +38 -0
  39. package/addons/admin/apis/dictList.ts +27 -0
  40. package/addons/admin/apis/dictUpd.ts +44 -0
  41. package/addons/admin/apis/login.ts +123 -0
  42. package/addons/admin/apis/logout.ts +23 -0
  43. package/addons/admin/apis/menuAll.ts +70 -0
  44. package/addons/admin/apis/menuDel.ts +40 -0
  45. package/addons/admin/apis/menuIns.ts +31 -0
  46. package/addons/admin/apis/menuList.ts +26 -0
  47. package/addons/admin/apis/menuUpd.ts +41 -0
  48. package/addons/admin/apis/register.ts +50 -0
  49. package/addons/admin/apis/roleApiDetail.ts +34 -0
  50. package/addons/admin/apis/roleApiSave.ts +44 -0
  51. package/addons/admin/apis/roleDel.ts +48 -0
  52. package/addons/admin/apis/roleDetail.ts +28 -0
  53. package/addons/admin/apis/roleIns.ts +40 -0
  54. package/addons/admin/apis/roleList.ts +18 -0
  55. package/addons/admin/apis/roleMenuDetail.ts +33 -0
  56. package/addons/admin/apis/roleMenuSave.ts +39 -0
  57. package/addons/admin/apis/roleSave.ts +45 -0
  58. package/addons/admin/apis/roleUpd.ts +53 -0
  59. package/addons/admin/apis/sendSmsCode.ts +36 -0
  60. package/addons/admin/checks/admin.ts +36 -0
  61. package/addons/admin/config/index.ts +45 -0
  62. package/addons/admin/config/menu.json +44 -0
  63. package/addons/admin/scripts/syncApi.ts +285 -0
  64. package/addons/admin/scripts/syncDev.ts +203 -0
  65. package/addons/admin/scripts/syncMenu.ts +210 -0
  66. package/addons/admin/tables/admin.json +14 -0
  67. package/addons/admin/tables/api.json +8 -0
  68. package/addons/admin/tables/dict.json +8 -0
  69. package/addons/admin/tables/menu.json +8 -0
  70. package/addons/admin/tables/role.json +8 -0
  71. package/addons/admin/types/index.ts +44 -0
  72. package/addons/admin/util.ts +266 -0
  73. package/addons/befly/addon.config.json +13 -0
  74. package/addons/befly/apis/health/info.ts +77 -0
  75. package/addons/befly/apis/tool/tokenCheck.ts +52 -0
  76. package/addons/demo/README.md +62 -0
  77. package/addons/demo/addon.config.json +13 -0
  78. package/addons/demo/apis/demoIns.ts +36 -0
  79. package/addons/demo/apis/demoList.ts +36 -0
  80. package/addons/demo/checks/demo.ts +30 -0
  81. package/addons/demo/config/default.ts +17 -0
  82. package/addons/demo/plugins/tool.ts +61 -0
  83. package/addons/demo/tables/todo.json +6 -0
  84. package/addons/demo/types/index.d.ts +56 -0
  85. package/apis/article/articleDel.ts +33 -0
  86. package/apis/article/articleDetail.ts +26 -0
  87. package/apis/article/articleIns.ts +47 -0
  88. package/apis/article/articleList.ts +47 -0
  89. package/apis/article/articleUpd.ts +55 -0
  90. package/apis/article/increment.ts +37 -0
  91. package/apis/test/hi.ts +9 -0
  92. package/apis/user/login.ts +56 -0
  93. package/apis/user/userList.ts +40 -0
  94. package/bun.lock +140 -0
  95. package/checks/demo.ts +25 -0
  96. package/logs/2025-08-22.0.log +197 -0
  97. package/logs/2025-08-23.0.log +151 -0
  98. package/logs/2025-08-24.0.log +296 -0
  99. package/logs/2025-08-25.0.log +162 -0
  100. package/logs/2025-08-26.0.log +19 -0
  101. package/logs/2025-08-27.0.log +63 -0
  102. package/logs/2025-08-28.0.log +286 -0
  103. package/logs/2025-08-30.0.log +1 -0
  104. package/logs/2025-09-01.0.log +296 -0
  105. package/logs/2025-09-02.0.log +298 -0
  106. package/logs/2025-10-11.0.log +2718 -0
  107. package/logs/2025-10-12.0.log +4374 -0
  108. package/logs/2025-10-13.0.log +759 -0
  109. package/logs/2025-10-14.0.log +2350 -0
  110. package/logs/2025-10-15.0.log +2386 -0
  111. package/logs/2025-10-16.0.log +2807 -0
  112. package/logs/2025-10-17.0.log +1143 -0
  113. package/logs/2025-10-18.0.log +1292 -0
  114. package/logs/2025-10-19.0.log +1752 -0
  115. package/logs/2025-10-20.0.log +722 -0
  116. package/logs/2025-10-21.0.log +1075 -0
  117. package/logs/2025-10-23.0.log +3291 -0
  118. package/logs/2025-10-24.0.log +2341 -0
  119. package/logs/2025-10-25.0.log +1367 -0
  120. package/logs/debug.0.log +25174 -0
  121. package/main.ts +9 -0
  122. package/package.json +29 -0
  123. package/pm2.config.cjs +85 -0
  124. package/tables/article.json +11 -0
  125. package/tables/user.json +8 -0
  126. package/temp/addon-route-prefix-migration.md +400 -0
  127. package/temp/api-route-conflict-analysis.md +441 -0
  128. package/temp/interactive-cli-guide.md +199 -0
  129. package/temp/missing-apis-fix.md +362 -0
  130. package/temp/remove-status-field.md +239 -0
  131. package/temp/roleid-to-rolecode-optimization.md +321 -0
  132. package/temp/status-to-state-migration-complete.md +176 -0
  133. package/temp/syncMenu-guide.md +235 -0
  134. package/temp/test-admin-menus-cache.ts +125 -0
  135. package/temp/test-admin-menus.ts +110 -0
  136. package/temp/test-interactive-cli.ps1 +14 -0
  137. package/tests/core.test.ts +13 -0
  138. package/tsconfig.json +23 -0
  139. package/types/api.ts +128 -0
  140. package/types/index.ts +6 -0
  141. package/types/models.example.ts +267 -0
  142. package/types/models.ts +67 -0
@@ -0,0 +1,83 @@
1
+ NODE_ENV="development"
2
+ # 调试模式 (1=开启, 0=关闭, development模式下默认开启)
3
+ DEBUG=1
4
+ # 项目名称
5
+ APP_NAME="易接口"
6
+ APP_PORT=3000
7
+ # MD5加密盐
8
+ MD5_SALT="a2ccfeba-818a-51c8-b1a4-939bade73088"
9
+ # 监听端口
10
+ APP_HOST="127.0.0.1"
11
+ # 开发管理员密码
12
+ DEV_PASSWORD="111111"
13
+ # 请求体大小
14
+ BODY_LIMIT=10
15
+ # 参数检查
16
+ PARAMS_CHECK=0
17
+ # Demo Addon 配置
18
+ DEMO_ENABLE=true
19
+ DEMO_DEFAULT_PRIORITY="medium"
20
+ DEMO_PAGE_SIZE=10
21
+ DEMO_MAX_PAGE_SIZE=100
22
+ # 日志配置
23
+ LOG_EXCLUDE_FIELDS="password,token"
24
+ LOG_DEBUG=1 # debug日志开关 (1=开启, 0=关闭)
25
+ LOG_DIR="./logs"
26
+ LOG_TO_CONSOLE=1
27
+ LOG_MAX_SIZE=52428800 # 50MB
28
+ # 时区
29
+ TZ="Asia/Shanghai"
30
+ # 跨域配置
31
+ ALLOWED_ORIGIN="*"
32
+ ALLOWED_METHODS="GET, POST, PUT, DELETE, OPTIONS"
33
+ ALLOWED_HEADERS="Content-Type, Authorization, authorization, token"
34
+ EXPOSE_HEADERS="Content-Range, X-Content-Range, Authorization, authorization, token"
35
+ MAX_AGE=86400 # 1天
36
+ ALLOW_CREDENTIALS="true"
37
+ # 数据库配置(DB_* 通用参数)
38
+ DB_ENABLE=1
39
+ DB_TYPE="mysql" # 支持 sqlite | mysql | postgresql
40
+ DB_HOST="127.0.0.1"
41
+ DB_PORT=3306
42
+ DB_USER="root2"
43
+ DB_PASS="root2"
44
+ DB_NAME="test3"
45
+ DB_POOL_MAX=10
46
+ DB_DEBUG=0
47
+ DB_CONNECTION_TIMEOUT=5000 # 数据库连接超时时间(毫秒),默认5秒
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=3
55
+ REDIS_KEY_PREFIX="befly"
56
+ # JWT 配置
57
+ JWT_SECRET="dbfaf2c3-7ade-5042-b843-eca8814714b3"
58
+ JWT_EXPIRES_IN="30d"
59
+ JWT_ALGORITHM="HS256"
60
+ # 邮箱配置
61
+ MAIL_HOST='demo.com'
62
+ MAIL_PORT=465
63
+ MAIL_POOL=1
64
+ MAIL_SECURE=1
65
+ MAIL_USER='demo@qq.com'
66
+ MAIL_PASS=''
67
+ MAIL_SENDER='易接口'
68
+ MAIL_ADDRESS='demo@qq.com'
69
+ # 上传配置
70
+ LOCAL_DIR="/wwwroot/static2/"
71
+ # 微信配置
72
+ PAY_NOTIFY_URL=""
73
+
74
+ # --- 同步脚本开关(仅影响表结构同步) ---
75
+ # 仅打印计划,不执行(0/1)
76
+ # 合并每表多项 DDL(0/1)
77
+ SYNC_MERGE_ALTER=1
78
+ # 索引操作尽量在线(0/1)
79
+ SYNC_ONLINE_INDEX=0
80
+ # 禁止长度收缩(0/1)
81
+ SYNC_DISALLOW_SHRINK=1
82
+ # 允许类型变更(0/1)
83
+ SYNC_ALLOW_TYPE_CHANGE=0
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,20 @@
1
+ # Befly - 蜜蜂飞舞
2
+
3
+ 道生一,一生二,二生三,三生万物,v3 版本发布后可才是公测版本。
4
+
5
+ ## 数据库配置(DB\_\* 通用参数)
6
+
7
+ 使用以下环境变量统一配置数据库,支持 sqlite/mysql/postgresql:
8
+
9
+ - DB_TYPE:数据库类型(sqlite | mysql | postgresql)
10
+ - DB_HOST:主机(sqlite 可忽略)
11
+ - DB_PORT:端口(sqlite 可忽略)
12
+ - DB_USER:用户名(sqlite 可忽略)
13
+ - DB_PASS:密码(sqlite 可忽略)
14
+ - DB_NAME:数据库名称(sqlite 为文件路径或 :memory:)
15
+
16
+ 示例:
17
+
18
+ - MySQL: DB_TYPE=mysql, DB_HOST=127.0.0.1, DB_PORT=3306, DB_USER=user, DB_PASS=pass, DB_NAME=demo
19
+ - PostgreSQL: DB_TYPE=postgresql, DB_HOST=127.0.0.1, DB_PORT=5432, DB_USER=user, DB_PASS=pass, DB_NAME=demo
20
+ - SQLite: DB_TYPE=sqlite, DB_NAME=/path/to/demo.sqlite 或 :memory:
package/README.ts.md ADDED
@@ -0,0 +1,175 @@
1
+ # Befly 3.0 TypeScript 示例项目
2
+
3
+ 这是一个完整的 Befly 3.0 TypeScript 项目示例,展示了如何使用框架的所有 TypeScript 特性。
4
+
5
+ ## 📁 项目结构
6
+
7
+ ```
8
+ tpl/
9
+ ├── types/ # 类型定义
10
+ │ ├── index.ts # 导出所有类型
11
+ │ ├── models.ts # 数据模型类型
12
+ │ └── api.ts # API 请求/响应类型
13
+ ├── apis/ # API 接口
14
+ │ ├── user/
15
+ │ │ ├── login.ts # 用户登录(公开)
16
+ │ │ └── list.ts # 用户列表(需管理员权限)
17
+ │ └── article/
18
+ │ ├── create.ts # 创建文章(需登录)
19
+ │ └── list.ts # 文章列表(公开)
20
+ ├── tables/ # 表定义(JSON 格式)
21
+ ├── plugins/ # 自定义插件
22
+ ├── tests/ # 测试文件
23
+ ├── main.ts # 项目入口
24
+ ├── tsconfig.json # TypeScript 配置
25
+ └── package.json # 项目配置
26
+ ```
27
+
28
+ ## 🚀 快速开始
29
+
30
+ ### 安装依赖
31
+
32
+ ```bash
33
+ bun install
34
+ ```
35
+
36
+ ### 启动开发服务器
37
+
38
+ ```bash
39
+ bun run dev
40
+ ```
41
+
42
+ 服务器将在 `http://localhost:3000` 启动。
43
+
44
+ ## 📝 核心特性示例
45
+
46
+ ### 1. 类型定义
47
+
48
+ 项目使用完整的 TypeScript 类型定义,包括:
49
+
50
+ - **数据模型类型**(`types/models.ts`)
51
+ - **API 请求/响应类型**(`types/api.ts`)
52
+ - **继承 Befly 核心类型**(从 `befly/types` 导入)
53
+
54
+ ### 2. 类型安全的 API 开发
55
+
56
+ ```typescript
57
+ import { Api, Yes, No } from 'befly';
58
+ import type { BeflyContext, RequestContext } from 'befly/types';
59
+ import type { LoginRequest, LoginResponse } from '../types/api';
60
+
61
+ export default Api.POST(
62
+ '用户登录',
63
+ false,
64
+ {
65
+ /* 字段定义 */
66
+ },
67
+ ['username', 'password'],
68
+ async (befly: BeflyContext, ctx: RequestContext) => {
69
+ const { username, password } = ctx.body as LoginRequest;
70
+
71
+ // 类型安全的数据库查询
72
+ const user = await befly.db.getOne<User>({
73
+ table: 'user',
74
+ where: { username }
75
+ });
76
+
77
+ return Yes<LoginResponse>('登录成功', response);
78
+ }
79
+ );
80
+ ```
81
+
82
+ ### 3. 数据库操作
83
+
84
+ 所有数据库操作都支持泛型:
85
+
86
+ ```typescript
87
+ // 查询单条
88
+ const user = await befly.db.getOne<User>({
89
+ table: 'user',
90
+ where: { id: 1 }
91
+ });
92
+
93
+ // 查询列表
94
+ const result = await befly.db.getList<Article>({
95
+ table: 'article',
96
+ page: 1,
97
+ limit: 10
98
+ });
99
+
100
+ // 插入数据
101
+ const articleId = await befly.db.insData({
102
+ table: 'article',
103
+ data: { title: '标题', content: '内容' }
104
+ });
105
+ ```
106
+
107
+ ### 4. 认证与权限
108
+
109
+ ```typescript
110
+ // 公开接口
111
+ export default Api.POST('接口名', false, ...);
112
+
113
+ // 需要登录
114
+ export default Api.POST('接口名', true, ...);
115
+
116
+ // 需要特定角色
117
+ export default Api.POST('接口名', ['admin', 'editor'], ...);
118
+ ```
119
+
120
+ ### 5. JWT 认证
121
+
122
+ ```typescript
123
+ import { Jwt } from 'befly/utils/jwt';
124
+
125
+ // 生成 Token
126
+ const token = await Jwt.sign({ userId: '123', role: 'admin' }, { expiresIn: '7d' });
127
+
128
+ // 验证 Token
129
+ const payload = await Jwt.verify(token, secret);
130
+
131
+ // 检查权限
132
+ const hasAdmin = Jwt.hasRole(payload, 'admin');
133
+ ```
134
+
135
+ ## 📋 示例接口
136
+
137
+ ### 用户相关
138
+
139
+ - `POST /user/login` - 用户登录(公开)
140
+ - `POST /user/list` - 获取用户列表(仅管理员)
141
+
142
+ ### 文章相关
143
+
144
+ - `POST /article/create` - 创建文章(需登录)
145
+ - `POST /article/list` - 获取文章列表(公开)
146
+
147
+ ## 🧪 测试
148
+
149
+ 运行测试:
150
+
151
+ ```bash
152
+ bun test
153
+ ```
154
+
155
+ ## 📚 相关文档
156
+
157
+ - [TypeScript 支持指南](../docs/10-TypeScript/01-TypeScript支持.md)
158
+ - [类型定义参考](../docs/10-TypeScript/02-类型定义参考.md)
159
+ - [最佳实践](../docs/10-TypeScript/03-最佳实践.md)
160
+ - [迁移指南](../docs/10-TypeScript/04-迁移指南.md)
161
+
162
+ ## 💡 提示
163
+
164
+ 1. **智能提示**:使用 VS Code 可获得完整的类型提示和自动补全
165
+ 2. **类型检查**:TypeScript 会在编译时捕获大部分错误
166
+ 3. **重构友好**:修改类型定义时,所有使用处会自动更新
167
+ 4. **文档即代码**:类型定义本身就是最好的文档
168
+
169
+ ## 🤝 贡献
170
+
171
+ 欢迎提交 Issue 和 Pull Request!
172
+
173
+ ## 📄 许可
174
+
175
+ MIT License
@@ -0,0 +1,99 @@
1
+ /**
2
+ * Addon 配置读取示例
3
+ * 演示如何读取和使用 addon.config.json
4
+ */
5
+
6
+ import { readFileSync, readdirSync } from 'fs';
7
+ import { join } from 'path';
8
+ import type { AddonConfig } from '../core/types/addon.js';
9
+
10
+ /**
11
+ * 读取单个 Addon 配置
12
+ */
13
+ export function loadAddonConfig(addonPath: string): AddonConfig | null {
14
+ try {
15
+ const configPath = join(addonPath, 'addon.config.json');
16
+ const content = readFileSync(configPath, 'utf-8');
17
+ const config: AddonConfig = JSON.parse(content);
18
+
19
+ // 设置默认值
20
+ return {
21
+ enabled: true,
22
+ version: '0.0.1',
23
+ ...config
24
+ };
25
+ } catch (error) {
26
+ console.warn(`无法读取配置: ${addonPath}`, error);
27
+ return null;
28
+ }
29
+ }
30
+
31
+ /**
32
+ * 加载所有 Addon 配置
33
+ */
34
+ export function loadAllAddonConfigs(addonsDir: string): Map<string, AddonConfig> {
35
+ const configs = new Map<string, AddonConfig>();
36
+
37
+ try {
38
+ const dirs = readdirSync(addonsDir, { withFileTypes: true });
39
+
40
+ for (const dir of dirs) {
41
+ // 跳过非目录和特殊目录
42
+ if (!dir.isDirectory() || dir.name.startsWith('.') || dir.name.startsWith('_')) {
43
+ continue;
44
+ }
45
+
46
+ const addonPath = join(addonsDir, dir.name);
47
+ const config = loadAddonConfig(addonPath);
48
+
49
+ if (config) {
50
+ configs.set(config.name, config);
51
+ }
52
+ }
53
+ } catch (error) {
54
+ console.error('加载 Addon 配置失败:', error);
55
+ }
56
+
57
+ return configs;
58
+ }
59
+
60
+ /**
61
+ * 查找启用的 Addons
62
+ */
63
+ export function getEnabledAddons(configs: Map<string, AddonConfig>): AddonConfig[] {
64
+ return Array.from(configs.values()).filter((config) => config.enabled !== false);
65
+ }
66
+
67
+ /**
68
+ * 按关键词搜索 Addons
69
+ */
70
+ export function searchAddonsByKeyword(configs: Map<string, AddonConfig>, keyword: string): AddonConfig[] {
71
+ return Array.from(configs.values()).filter((config) => config.keywords?.some((k) => k.toLowerCase().includes(keyword.toLowerCase())));
72
+ }
73
+
74
+ /**
75
+ * 示例用法
76
+ */
77
+ if (import.meta.main) {
78
+ const addonsDir = join(import.meta.dir, 'addons');
79
+ const configs = loadAllAddonConfigs(addonsDir);
80
+
81
+ console.log(`\n📦 加载了 ${configs.size} 个 Addon:\n`);
82
+
83
+ for (const [name, config] of configs) {
84
+ console.log(` - ${config.title} (${name}@${config.version})`);
85
+ if (config.description) {
86
+ console.log(` ${config.description}`);
87
+ }
88
+ console.log();
89
+ }
90
+
91
+ // 查找启用的 Addons
92
+ const enabled = getEnabledAddons(configs);
93
+ console.log(`✅ 启用的 Addons: ${enabled.length} 个\n`);
94
+
95
+ // 按关键词搜索
96
+ const adminAddons = searchAddonsByKeyword(configs, 'admin');
97
+ console.log(`🔍 包含 "admin" 关键词的 Addons: ${adminAddons.length} 个`);
98
+ adminAddons.forEach((config) => console.log(` - ${config.title}`));
99
+ }