befly 3.18.15 → 3.18.17

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 (3) hide show
  1. package/package.json +2 -2
  2. package/sync/api.js +1 -12
  3. package/sync/dev.js +57 -2
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "befly",
3
- "version": "3.18.15",
4
- "gitHead": "52de35bf463398d4be0988fc5aaa2e0dda9fe185",
3
+ "version": "3.18.17",
4
+ "gitHead": "6bb80da8ad441667bca408c328e11ad37d858754",
5
5
  "private": false,
6
6
  "description": "Befly - 为 Bun 专属打造的 JavaScript API 接口框架核心引擎",
7
7
  "keywords": [
package/sync/api.js CHANGED
@@ -15,17 +15,6 @@ const getApiParentPath = (apiPath) => {
15
15
  return `/${parentSegments.join("/")}`;
16
16
  };
17
17
 
18
- const normalizeAuthForDb = (value) => {
19
- if (value === false) {
20
- return "否";
21
- }
22
-
23
- if (Array.isArray(value)) {
24
- return value.join(",");
25
- }
26
- return "是";
27
- };
28
-
29
18
  export async function syncApi(ctx, apis) {
30
19
  const allDbApis = await ctx.mysql.getAll({
31
20
  table: API_TABLE_NAME,
@@ -48,7 +37,7 @@ export async function syncApi(ctx, apis) {
48
37
  for (const api of apis) {
49
38
  scannedPathSet.add(api.apiPath);
50
39
 
51
- const auth = normalizeAuthForDb(api.auth);
40
+ const auth = api.auth === false ? "否" : Array.isArray(api.auth) ? api.auth.join(",") : "是";
52
41
  const parentPath = getApiParentPath(api.apiPath);
53
42
  const existing = existingApiMap.get(api.apiPath);
54
43
 
package/sync/dev.js CHANGED
@@ -11,7 +11,7 @@ export async function syncDev(ctx) {
11
11
 
12
12
  const allApis = await ctx.mysql.getAll({
13
13
  table: "beflyApi",
14
- fields: ["path"],
14
+ fields: ["path", "auth"],
15
15
  where: { state$gte: 0 },
16
16
  orderBy: ["id#ASC"]
17
17
  });
@@ -134,6 +134,60 @@ export async function syncDev(ctx) {
134
134
  }
135
135
  });
136
136
 
137
+ const defaultApis = [];
138
+ for (const api of allApis.data.lists) {
139
+ const apiPath = api.path;
140
+ if (typeof apiPath !== "string" || apiPath.length === 0) {
141
+ continue;
142
+ }
143
+
144
+ const auth = api.auth;
145
+ if (auth === "否" || auth === false) {
146
+ defaultApis.push(apiPath);
147
+ continue;
148
+ }
149
+
150
+ // 与 syncApi 的存储语义保持一致:
151
+ // - true -> "是"
152
+ // - false -> "否"
153
+ // - string[] -> "a,b,c"
154
+ if (typeof auth === "string" && auth !== "是") {
155
+ const roleCodes = auth.split(",");
156
+ for (const roleCode of roleCodes) {
157
+ if (roleCode.trim() === roleConfig.code) {
158
+ defaultApis.push(apiPath);
159
+ break;
160
+ }
161
+ }
162
+ }
163
+ }
164
+
165
+ const mergedApis = [];
166
+ const mergedApiSet = new Set();
167
+
168
+ if (Array.isArray(existingRole.data?.apis)) {
169
+ for (const apiPath of existingRole.data.apis) {
170
+ if (typeof apiPath !== "string") {
171
+ continue;
172
+ }
173
+ const value = apiPath.trim();
174
+ if (value.length === 0 || mergedApiSet.has(value)) {
175
+ continue;
176
+ }
177
+ mergedApiSet.add(value);
178
+ mergedApis.push(value);
179
+ }
180
+ }
181
+
182
+ for (const apiPath of defaultApis) {
183
+ const value = apiPath.trim();
184
+ if (value.length === 0 || mergedApiSet.has(value)) {
185
+ continue;
186
+ }
187
+ mergedApiSet.add(value);
188
+ mergedApis.push(value);
189
+ }
190
+
137
191
  if (existingRole.data?.id) {
138
192
  // 角色存在则强制更新(不做差异判断)
139
193
  await ctx.mysql.updData({
@@ -142,6 +196,7 @@ export async function syncDev(ctx) {
142
196
  data: {
143
197
  name: roleConfig.name,
144
198
  description: roleConfig.description,
199
+ apis: mergedApis,
145
200
  sort: roleConfig.sort,
146
201
  state: 1
147
202
  }
@@ -154,7 +209,7 @@ export async function syncDev(ctx) {
154
209
  name: roleConfig.name,
155
210
  description: roleConfig.description,
156
211
  menus: [],
157
- apis: [],
212
+ apis: mergedApis,
158
213
  sort: roleConfig.sort
159
214
  }
160
215
  });