befly-shared 1.1.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.
- package/LICENSE +201 -0
- package/README.md +442 -0
- package/dist/addonHelper.js +83 -0
- package/dist/arrayKeysToCamel.js +18 -0
- package/dist/arrayToTree.js +23 -0
- package/dist/calcPerfTime.js +13 -0
- package/dist/configTypes.js +1 -0
- package/dist/defineAddonConfig.js +40 -0
- package/dist/fieldClear.js +57 -0
- package/dist/genShortId.js +12 -0
- package/dist/index.js +25 -0
- package/dist/keysToCamel.js +21 -0
- package/dist/keysToSnake.js +21 -0
- package/dist/layouts.js +59 -0
- package/dist/pickFields.js +16 -0
- package/dist/redisKeys.js +34 -0
- package/dist/regex.js +200 -0
- package/dist/scanConfig.js +82 -0
- package/dist/scanFiles.js +39 -0
- package/dist/scanViews.js +48 -0
- package/dist/types.js +46 -0
- package/package.json +40 -0
- package/src/addonHelper.ts +88 -0
- package/src/arrayKeysToCamel.ts +18 -0
- package/src/arrayToTree.ts +31 -0
- package/src/calcPerfTime.ts +13 -0
- package/src/configTypes.ts +27 -0
- package/src/defineAddonConfig.ts +45 -0
- package/src/fieldClear.ts +75 -0
- package/src/genShortId.ts +12 -0
- package/src/index.ts +28 -0
- package/src/keysToCamel.ts +22 -0
- package/src/keysToSnake.ts +22 -0
- package/src/layouts.ts +90 -0
- package/src/pickFields.ts +19 -0
- package/src/redisKeys.ts +44 -0
- package/src/regex.ts +223 -0
- package/src/scanConfig.ts +104 -0
- package/src/scanFiles.ts +49 -0
- package/src/scanViews.ts +55 -0
- package/src/types.ts +338 -0
- package/tests/addonHelper.test.ts +55 -0
- package/tests/arrayKeysToCamel.test.ts +21 -0
- package/tests/arrayToTree.test.ts +98 -0
- package/tests/calcPerfTime.test.ts +19 -0
- package/tests/fieldClear.test.ts +39 -0
- package/tests/keysToCamel.test.ts +22 -0
- package/tests/keysToSnake.test.ts +22 -0
- package/tests/layouts.test.ts +93 -0
- package/tests/pickFields.test.ts +22 -0
- package/tests/regex.test.ts +308 -0
- package/tests/scanFiles.test.ts +58 -0
- package/tests/types.test.ts +283 -0
- package/types/addonConfigMerge.d.ts +17 -0
- package/types/addonHelper.d.ts +24 -0
- package/types/arrayKeysToCamel.d.ts +13 -0
- package/types/arrayToTree.d.ts +8 -0
- package/types/calcPerfTime.d.ts +4 -0
- package/types/configMerge.d.ts +49 -0
- package/types/configTypes.d.ts +26 -0
- package/types/defineAddonConfig.d.ts +19 -0
- package/types/fieldClear.d.ts +16 -0
- package/types/genShortId.d.ts +10 -0
- package/types/index.d.ts +22 -0
- package/types/keysToCamel.d.ts +10 -0
- package/types/keysToSnake.d.ts +10 -0
- package/types/layouts.d.ts +29 -0
- package/types/loadAndMergeConfig.d.ts +7 -0
- package/types/mergeConfig.d.ts +7 -0
- package/types/pickFields.d.ts +4 -0
- package/types/redisKeys.d.ts +34 -0
- package/types/regex.d.ts +143 -0
- package/types/scanConfig.d.ts +7 -0
- package/types/scanFiles.d.ts +12 -0
- package/types/scanViews.d.ts +11 -0
- package/types/types.d.ts +274 -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,442 @@
|
|
|
1
|
+
# Befly Shared
|
|
2
|
+
|
|
3
|
+
Befly 框架的共享模块,包含跨包共享的工具函数、常量、类型定义和配置。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add befly-shared
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 类型定义
|
|
12
|
+
|
|
13
|
+
### 响应类型
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import type { ResponseResult, PaginatedResult, ValidationResult } from 'befly-shared/types';
|
|
17
|
+
|
|
18
|
+
// API 响应
|
|
19
|
+
const result: ResponseResult<{ id: number }> = {
|
|
20
|
+
code: 0,
|
|
21
|
+
msg: '操作成功',
|
|
22
|
+
data: { id: 1 }
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// 分页响应
|
|
26
|
+
const list: PaginatedResult<User> = {
|
|
27
|
+
code: 0,
|
|
28
|
+
msg: '查询成功',
|
|
29
|
+
data: users,
|
|
30
|
+
total: 100,
|
|
31
|
+
page: 1,
|
|
32
|
+
limit: 10,
|
|
33
|
+
pages: 10
|
|
34
|
+
};
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### 常量
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import { ApiCode, ErrorMessages } from 'befly-shared/types';
|
|
41
|
+
|
|
42
|
+
// 响应码
|
|
43
|
+
ApiCode.SUCCESS; // 0
|
|
44
|
+
ApiCode.FAIL; // 1
|
|
45
|
+
ApiCode.UNAUTHORIZED; // 401
|
|
46
|
+
ApiCode.FORBIDDEN; // 403
|
|
47
|
+
ApiCode.NOT_FOUND; // 404
|
|
48
|
+
ApiCode.SERVER_ERROR; // 500
|
|
49
|
+
|
|
50
|
+
// 错误消息
|
|
51
|
+
ErrorMessages.UNAUTHORIZED; // '请先登录'
|
|
52
|
+
ErrorMessages.FORBIDDEN; // '无访问权限'
|
|
53
|
+
ErrorMessages.TOKEN_EXPIRED; // 'Token 已过期'
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### 配置类型
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
import type { DatabaseConfig, RedisConfig } from 'befly-shared/types';
|
|
60
|
+
|
|
61
|
+
const dbConfig: DatabaseConfig = {
|
|
62
|
+
type: 'mysql',
|
|
63
|
+
host: 'localhost',
|
|
64
|
+
port: 3306,
|
|
65
|
+
user: 'root',
|
|
66
|
+
password: 'password',
|
|
67
|
+
database: 'befly'
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const redisConfig: RedisConfig = {
|
|
71
|
+
host: 'localhost',
|
|
72
|
+
port: 6379,
|
|
73
|
+
password: 'password',
|
|
74
|
+
db: 0
|
|
75
|
+
};
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Redis 键和 TTL
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
import { RedisKeys, RedisTTL } from 'befly-shared/redisKeys';
|
|
82
|
+
|
|
83
|
+
// 生成 Redis 键
|
|
84
|
+
RedisKeys.apisAll(); // 'befly:apis:all'
|
|
85
|
+
RedisKeys.menusAll(); // 'befly:menus:all'
|
|
86
|
+
RedisKeys.roleInfo('admin'); // 'befly:role:info:admin'
|
|
87
|
+
RedisKeys.roleApis('admin'); // 'befly:role:apis:admin'
|
|
88
|
+
RedisKeys.tableColumns('user'); // 'befly:table:columns:user'
|
|
89
|
+
|
|
90
|
+
// TTL 配置(秒)
|
|
91
|
+
RedisTTL.tableColumns; // 3600 (1小时)
|
|
92
|
+
RedisTTL.roleApis; // 86400 (24小时)
|
|
93
|
+
RedisTTL.roleInfo; // 86400 (24小时)
|
|
94
|
+
RedisTTL.apisAll; // null (永久)
|
|
95
|
+
RedisTTL.menusAll; // null (永久)
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## 配置管理
|
|
99
|
+
|
|
100
|
+
### mergeConfig
|
|
101
|
+
|
|
102
|
+
加载并合并配置文件(矩阵搜索:dirs × files)。
|
|
103
|
+
|
|
104
|
+
**参数:**
|
|
105
|
+
|
|
106
|
+
- `dirs`: 目录数组(绝对路径)
|
|
107
|
+
- `files`: 文件名数组(不含扩展名)
|
|
108
|
+
- `required`: 是否必填,默认 `false`
|
|
109
|
+
- `extensions`: 扩展名数组,默认 `['.js', '.ts', '.json']`
|
|
110
|
+
|
|
111
|
+
**返回:** `Promise<Record<string, any>>`
|
|
112
|
+
|
|
113
|
+
**示例:**
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
import { mergeConfig } from 'befly-shared/scanConfig';
|
|
117
|
+
|
|
118
|
+
const config = await mergeConfig({
|
|
119
|
+
dirs: ['/path/to/config1', '/path/to/config2'],
|
|
120
|
+
files: ['app', 'database'],
|
|
121
|
+
required: false
|
|
122
|
+
});
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### addonConfigMerge
|
|
126
|
+
|
|
127
|
+
Addon 配置自动合并函数,自动读取 addon 的 `addon.config.js` 和项目 `config/` 目录下的同名配置文件进行合并。
|
|
128
|
+
|
|
129
|
+
**参数:**
|
|
130
|
+
|
|
131
|
+
- `options`: 合并选项(可选)
|
|
132
|
+
|
|
133
|
+
**返回:** `Promise<Record<string, any>>`
|
|
134
|
+
|
|
135
|
+
**示例:**
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
import { addonConfigMerge } from 'befly-shared/scanConfig';
|
|
139
|
+
|
|
140
|
+
// 在 @befly-addon/admin 包根目录文件中调用
|
|
141
|
+
const config = await addonConfigMerge();
|
|
142
|
+
// 自动读取:
|
|
143
|
+
// 1. @befly-addon/admin/addon.config.js
|
|
144
|
+
// 2. 项目根目录/config/admin.{js,ts,json}
|
|
145
|
+
// 3. 合并后返回
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## 字段名转换
|
|
149
|
+
|
|
150
|
+
### keysToCamel
|
|
151
|
+
|
|
152
|
+
将对象字段名转为小驼峰格式。
|
|
153
|
+
|
|
154
|
+
**参数:**
|
|
155
|
+
|
|
156
|
+
- `obj`: 源对象
|
|
157
|
+
|
|
158
|
+
**返回:** `T`
|
|
159
|
+
|
|
160
|
+
**示例:**
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
import { keysToCamel } from 'befly-shared/keysToCamel';
|
|
164
|
+
|
|
165
|
+
keysToCamel({ user_id: 123, user_name: 'John' });
|
|
166
|
+
// { userId: 123, userName: 'John' }
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### keysToSnake
|
|
170
|
+
|
|
171
|
+
将对象字段名转为下划线格式。
|
|
172
|
+
|
|
173
|
+
**参数:**
|
|
174
|
+
|
|
175
|
+
- `obj`: 源对象
|
|
176
|
+
|
|
177
|
+
**返回:** `T`
|
|
178
|
+
|
|
179
|
+
**示例:**
|
|
180
|
+
|
|
181
|
+
```typescript
|
|
182
|
+
import { keysToSnake } from 'befly-shared/keysToSnake';
|
|
183
|
+
|
|
184
|
+
keysToSnake({ userId: 123, userName: 'John' });
|
|
185
|
+
// { user_id: 123, user_name: 'John' }
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### arrayKeysToCamel
|
|
189
|
+
|
|
190
|
+
批量将数组对象的字段名转为小驼峰格式。
|
|
191
|
+
|
|
192
|
+
**参数:**
|
|
193
|
+
|
|
194
|
+
- `arr`: 源数组
|
|
195
|
+
|
|
196
|
+
**返回:** `T[]`
|
|
197
|
+
|
|
198
|
+
**示例:**
|
|
199
|
+
|
|
200
|
+
```typescript
|
|
201
|
+
import { arrayKeysToCamel } from 'befly-shared/arrayKeysToCamel';
|
|
202
|
+
|
|
203
|
+
arrayKeysToCamel([
|
|
204
|
+
{ user_id: 1, user_name: 'John' },
|
|
205
|
+
{ user_id: 2, user_name: 'Jane' }
|
|
206
|
+
]);
|
|
207
|
+
// [{ userId: 1, userName: 'John' }, { userId: 2, userName: 'Jane' }]
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
## 数据处理
|
|
211
|
+
|
|
212
|
+
### arrayToTree
|
|
213
|
+
|
|
214
|
+
将扁平数组转为树形结构。
|
|
215
|
+
|
|
216
|
+
**参数:**
|
|
217
|
+
|
|
218
|
+
- `items`: 数组
|
|
219
|
+
- `options`: 配置项
|
|
220
|
+
- `idField`: 节点 id 字段名,默认 `'id'`
|
|
221
|
+
- `pidField`: 父节点 id 字段名,默认 `'pid'`
|
|
222
|
+
- `childrenField`: 子节点字段名,默认 `'children'`
|
|
223
|
+
- `rootPid`: 根节点的父 id,默认 `0`
|
|
224
|
+
- `mapFn`: 节点转换函数(可选)
|
|
225
|
+
|
|
226
|
+
**返回:** `T[]`
|
|
227
|
+
|
|
228
|
+
**示例:**
|
|
229
|
+
|
|
230
|
+
```typescript
|
|
231
|
+
import { arrayToTree } from 'befly-shared/arrayToTree';
|
|
232
|
+
|
|
233
|
+
const items = [
|
|
234
|
+
{ id: 1, pid: 0, name: '根节点' },
|
|
235
|
+
{ id: 2, pid: 1, name: '子节点1' },
|
|
236
|
+
{ id: 3, pid: 1, name: '子节点2' }
|
|
237
|
+
];
|
|
238
|
+
|
|
239
|
+
const tree = arrayToTree(items);
|
|
240
|
+
// [
|
|
241
|
+
// {
|
|
242
|
+
// id: 1, pid: 0, name: '根节点',
|
|
243
|
+
// children: [
|
|
244
|
+
// { id: 2, pid: 1, name: '子节点1' },
|
|
245
|
+
// { id: 3, pid: 1, name: '子节点2' }
|
|
246
|
+
// ]
|
|
247
|
+
// }
|
|
248
|
+
// ]
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
### fieldClear
|
|
252
|
+
|
|
253
|
+
字段过滤和清理工具,支持字段挑选、排除、值保留、值排除等操作。
|
|
254
|
+
|
|
255
|
+
**参数:**
|
|
256
|
+
|
|
257
|
+
- `data`: 数据(对象或数组)
|
|
258
|
+
- `options`: 配置项
|
|
259
|
+
- `pickKeys`: 只保留这些字段
|
|
260
|
+
- `omitKeys`: 排除这些字段
|
|
261
|
+
- `keepValues`: 只保留这些值
|
|
262
|
+
- `excludeValues`: 排除这些值
|
|
263
|
+
- `keepMap`: 强制保留的键值对(优先级最高)
|
|
264
|
+
|
|
265
|
+
**返回:** `FieldClearResult<T>`
|
|
266
|
+
|
|
267
|
+
**示例:**
|
|
268
|
+
|
|
269
|
+
```typescript
|
|
270
|
+
import { fieldClear } from 'befly-shared/fieldClear';
|
|
271
|
+
|
|
272
|
+
// 只保留指定字段
|
|
273
|
+
fieldClear({ id: 1, name: 'John', age: 30 }, { pickKeys: ['id', 'name'] });
|
|
274
|
+
// { id: 1, name: 'John' }
|
|
275
|
+
|
|
276
|
+
// 排除指定字段
|
|
277
|
+
fieldClear({ id: 1, name: 'John', age: 30 }, { omitKeys: ['age'] });
|
|
278
|
+
// { id: 1, name: 'John' }
|
|
279
|
+
|
|
280
|
+
// 排除指定值
|
|
281
|
+
fieldClear({ id: 1, name: null, age: undefined }, { excludeValues: [null, undefined] });
|
|
282
|
+
// { id: 1 }
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
### pickFields
|
|
286
|
+
|
|
287
|
+
挑选对象中的指定字段。
|
|
288
|
+
|
|
289
|
+
**参数:**
|
|
290
|
+
|
|
291
|
+
- `obj`: 源对象
|
|
292
|
+
- `keys`: 字段名数组
|
|
293
|
+
|
|
294
|
+
**返回:** `Partial<T>`
|
|
295
|
+
|
|
296
|
+
**示例:**
|
|
297
|
+
|
|
298
|
+
```typescript
|
|
299
|
+
import { pickFields } from 'befly-shared/pickFields';
|
|
300
|
+
|
|
301
|
+
pickFields({ id: 1, name: 'John', age: 30 }, ['id', 'name']);
|
|
302
|
+
// { id: 1, name: 'John' }
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
## 文件扫描
|
|
306
|
+
|
|
307
|
+
### scanFiles
|
|
308
|
+
|
|
309
|
+
扫描指定目录下的文件。
|
|
310
|
+
|
|
311
|
+
**参数:**
|
|
312
|
+
|
|
313
|
+
- `dir`: 目录路径
|
|
314
|
+
- `pattern`: Glob 模式,默认 `'**/*.{ts,js}'`
|
|
315
|
+
- `ignoreUnderline`: 是否忽略下划线开头的文件/目录,默认 `true`
|
|
316
|
+
|
|
317
|
+
**返回:** `Promise<ScanFileResult[]>`
|
|
318
|
+
|
|
319
|
+
**示例:**
|
|
320
|
+
|
|
321
|
+
```typescript
|
|
322
|
+
import { scanFiles } from 'befly-shared/scanFiles';
|
|
323
|
+
|
|
324
|
+
const files = await scanFiles('/path/to/dir', '**/*.ts');
|
|
325
|
+
// [
|
|
326
|
+
// { filePath: '/path/to/dir/file1.ts', relativePath: 'file1', fileName: 'file1' },
|
|
327
|
+
// { filePath: '/path/to/dir/sub/file2.ts', relativePath: 'sub/file2', fileName: 'file2' }
|
|
328
|
+
// ]
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
### scanViews
|
|
332
|
+
|
|
333
|
+
扫描项目和所有 `@befly-addon` 包的 views 目录,用于 unplugin-vue-router 的 routesFolder 配置。
|
|
334
|
+
|
|
335
|
+
**注意:** 此函数只能在 vite.config.js 中使用(Node.js 环境),不能在浏览器中使用。
|
|
336
|
+
|
|
337
|
+
**返回:** 路由文件夹配置数组
|
|
338
|
+
|
|
339
|
+
**示例:**
|
|
340
|
+
|
|
341
|
+
```typescript
|
|
342
|
+
import { scanViews } from 'befly-shared/scanViews';
|
|
343
|
+
|
|
344
|
+
// 在 vite.config.js 中使用
|
|
345
|
+
export default defineConfig({
|
|
346
|
+
plugins: [
|
|
347
|
+
VueRouter({
|
|
348
|
+
routesFolder: scanViews()
|
|
349
|
+
})
|
|
350
|
+
]
|
|
351
|
+
});
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
### scanAddons
|
|
355
|
+
|
|
356
|
+
扫描所有可用的 addon,从 `node_modules/@befly-addon/` 加载。
|
|
357
|
+
|
|
358
|
+
**参数:**
|
|
359
|
+
|
|
360
|
+
- `cwd`: 项目根目录,默认 `process.cwd()`
|
|
361
|
+
|
|
362
|
+
**返回:** `string[]` - addon 名称数组
|
|
363
|
+
|
|
364
|
+
**示例:**
|
|
365
|
+
|
|
366
|
+
```typescript
|
|
367
|
+
import { scanAddons } from 'befly-shared/addonHelper';
|
|
368
|
+
|
|
369
|
+
const addons = scanAddons();
|
|
370
|
+
// ['admin', 'blog', 'shop']
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
## 路由处理
|
|
374
|
+
|
|
375
|
+
### layouts
|
|
376
|
+
|
|
377
|
+
自定义布局处理函数,根据文件名后缀判断使用哪个布局。
|
|
378
|
+
|
|
379
|
+
**参数:**
|
|
380
|
+
|
|
381
|
+
- `routes`: 原始路由配置
|
|
382
|
+
- `inheritLayout`: 继承的布局名称(来自父级目录)
|
|
383
|
+
|
|
384
|
+
**返回:** 处理后的布局配置
|
|
385
|
+
|
|
386
|
+
**示例:**
|
|
387
|
+
|
|
388
|
+
```typescript
|
|
389
|
+
import { layouts } from 'befly-shared/layouts';
|
|
390
|
+
|
|
391
|
+
// 根据文件名后缀判断布局:
|
|
392
|
+
// - index.vue -> 默认布局
|
|
393
|
+
// - login_n.vue -> n 布局
|
|
394
|
+
// - admin_a.vue -> a 布局
|
|
395
|
+
```
|
|
396
|
+
|
|
397
|
+
## 性能工具
|
|
398
|
+
|
|
399
|
+
### calcPerfTime
|
|
400
|
+
|
|
401
|
+
计算性能时间差(支持纳秒级精度)。
|
|
402
|
+
|
|
403
|
+
**参数:**
|
|
404
|
+
|
|
405
|
+
- `startTime`: 开始时间(纳秒)
|
|
406
|
+
- `endTime`: 结束时间(纳秒),默认 `Bun.nanoseconds()`
|
|
407
|
+
|
|
408
|
+
**返回:** `string` - 格式化的时间字符串
|
|
409
|
+
|
|
410
|
+
**示例:**
|
|
411
|
+
|
|
412
|
+
```typescript
|
|
413
|
+
import { calcPerfTime } from 'befly-shared/calcPerfTime';
|
|
414
|
+
|
|
415
|
+
const start = Bun.nanoseconds();
|
|
416
|
+
// ... 执行操作 ...
|
|
417
|
+
const duration = calcPerfTime(start);
|
|
418
|
+
// "12.34 毫秒" 或 "1.23 秒"
|
|
419
|
+
```
|
|
420
|
+
|
|
421
|
+
## Addon 辅助
|
|
422
|
+
|
|
423
|
+
### addonHelper
|
|
424
|
+
|
|
425
|
+
提供 addon 相关的辅助功能,包括扫描、路径解析等。
|
|
426
|
+
|
|
427
|
+
**导出函数:**
|
|
428
|
+
|
|
429
|
+
- `scanAddons(cwd?)`: 扫描所有可用的 addon
|
|
430
|
+
- 其他辅助函数...
|
|
431
|
+
|
|
432
|
+
## 类型定义
|
|
433
|
+
|
|
434
|
+
所有类型定义自动生成到 `types/` 目录下,可直接导入使用:
|
|
435
|
+
|
|
436
|
+
```typescript
|
|
437
|
+
import type { LoadConfigOptions, MergeConfigOptions } from 'befly-shared/configTypes';
|
|
438
|
+
```
|
|
439
|
+
|
|
440
|
+
## License
|
|
441
|
+
|
|
442
|
+
Apache-2.0
|