befly 3.25.0 → 3.26.0

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.
@@ -32,15 +32,15 @@ export default {
32
32
  });
33
33
  }
34
34
 
35
- const keyword = String(ctx.body.keyword || "").trim();
36
- const errorType = String(ctx.body.errorType || "").trim();
37
- const source = String(ctx.body.source || "").trim();
38
- const productName = String(ctx.body.productName || "").trim();
39
- const productCode = String(ctx.body.productCode || "").trim();
40
- const productVersion = String(ctx.body.productVersion || "").trim();
41
- const deviceType = String(ctx.body.deviceType || "").trim();
42
- const browserName = String(ctx.body.browserName || "").trim();
43
- const osName = String(ctx.body.osName || "").trim();
35
+ const keyword = ctx.body.keyword ?? "";
36
+ const errorType = ctx.body.errorType ?? "";
37
+ const source = ctx.body.source ?? "";
38
+ const productName = ctx.body.productName ?? "";
39
+ const productCode = ctx.body.productCode ?? "";
40
+ const productVersion = ctx.body.productVersion ?? "";
41
+ const deviceType = ctx.body.deviceType ?? "";
42
+ const browserName = ctx.body.browserName ?? "";
43
+ const osName = ctx.body.osName ?? "";
44
44
  const where = {};
45
45
 
46
46
  if (keyword) {
@@ -201,7 +201,7 @@ export default {
201
201
  const reportDate = getDateYmdNumber(now);
202
202
  const weekStartDate = getTongJiWeekStartDate(now);
203
203
  const monthStartDate = getTongJiMonthStartDate(now);
204
- const productName = String(ctx?.body?.productName || "").trim();
204
+ const productName = ctx?.body?.productName ?? "";
205
205
  const hasProductFilter = productName.length > 0;
206
206
 
207
207
  if (befly.redis && !hasProductFilter) {
@@ -13,7 +13,7 @@ function getOnlineStatsMember(ctx) {
13
13
  return `user:${ctx.userId}`;
14
14
  }
15
15
 
16
- const clientId = String(ctx.body.clientId || "").trim();
16
+ const clientId = ctx.body.clientId ?? "";
17
17
 
18
18
  if (clientId) {
19
19
  return `client:${clientId}`;
@@ -23,22 +23,20 @@ function getOnlineStatsMember(ctx) {
23
23
  }
24
24
 
25
25
  async function updateOnlineStatsActiveMemberProduct(befly, member, productName, expireAt) {
26
- const normalizedProductName = String(productName || "").trim();
27
-
28
- if (!normalizedProductName) {
26
+ if (!productName) {
29
27
  return;
30
28
  }
31
29
 
32
30
  const memberProductKey = `online:active:member:${encodeURIComponent(member)}:product`;
33
31
  const previousProductName = String((await befly.redis.getString(memberProductKey)) || "").trim();
34
32
 
35
- if (previousProductName && previousProductName !== normalizedProductName) {
33
+ if (previousProductName && previousProductName !== productName) {
36
34
  await befly.redis.zrem(`online:active:product:${encodeURIComponent(previousProductName)}`, [member]);
37
35
  }
38
36
 
39
- const activeProductKey = `online:active:product:${encodeURIComponent(normalizedProductName)}`;
37
+ const activeProductKey = `online:active:product:${encodeURIComponent(productName)}`;
40
38
 
41
- await befly.redis.setString(memberProductKey, normalizedProductName, ONLINE_STATS_ONLINE_TTL_SECONDS);
39
+ await befly.redis.setString(memberProductKey, productName, ONLINE_STATS_ONLINE_TTL_SECONDS);
42
40
  await befly.redis.zadd(activeProductKey, [
43
41
  {
44
42
  score: expireAt,
@@ -115,7 +113,7 @@ export default {
115
113
  const weekStartDate = getTongJiWeekStartDate(now);
116
114
  const monthStartDate = getTongJiMonthStartDate(now);
117
115
  const member = getOnlineStatsMember(ctx);
118
- const productName = String(ctx.body.productName || "").trim();
116
+ const productName = ctx.body.productName ?? "";
119
117
 
120
118
  await updateOnlineStatsActiveMember(befly, member, now, productName);
121
119
 
@@ -190,7 +190,7 @@ export default {
190
190
  const weekStartDate = getTongJiWeekStartDate(now);
191
191
  const monthStartDate = getTongJiMonthStartDate(now);
192
192
  const recentDateList = getTongJiRecentDateList(now, ONLINE_STATS_DAY_LIMIT);
193
- const productName = String(ctx?.body?.productName || "").trim();
193
+ const productName = ctx?.body?.productName ?? "";
194
194
  const hasProductFilter = productName.length > 0;
195
195
  const days = hasProductFilter ? await buildOnlineStatsProductDays(befly, recentDateList, productName) : await buildOnlineStatsDays(befly, recentDateList);
196
196
 
@@ -1,6 +1,6 @@
1
1
  // 相对导入
2
2
  import { Validator } from "../lib/validator.js";
3
- import { isPlainObject } from "../utils/is.js";
3
+ import { isPlainObject, isString } from "../utils/is.js";
4
4
  import { ErrorResponse } from "../utils/response.js";
5
5
  import { snakeCase } from "../utils/util.js";
6
6
 
@@ -25,6 +25,10 @@ export default {
25
25
  }
26
26
  }
27
27
 
28
+ if (isString(value)) {
29
+ value = value.trim();
30
+ }
31
+
28
32
  if (value !== undefined) {
29
33
  nextBody[field] = value;
30
34
  }
package/lib/validator.js CHANGED
@@ -38,6 +38,28 @@ function checkJsonLeaves(value, rule) {
38
38
  return false;
39
39
  }
40
40
 
41
+ function convertStringNumber(value, error, integer = false) {
42
+ if (!isString(value)) {
43
+ return { value: null, error: error };
44
+ }
45
+
46
+ const normalized = value.trim();
47
+ if (normalized.length === 0) {
48
+ return { value: null, error: error };
49
+ }
50
+
51
+ const num = Number(normalized);
52
+ if (!Number.isFinite(num)) {
53
+ return { value: null, error: error };
54
+ }
55
+
56
+ if (integer && !Number.isInteger(num)) {
57
+ return { value: null, error: error };
58
+ }
59
+
60
+ return { value: num, error: null };
61
+ }
62
+
41
63
  /**
42
64
  * 验证器类
43
65
  */
@@ -188,20 +210,12 @@ export class Validator {
188
210
  switch (input) {
189
211
  case "number": {
190
212
  if (isFiniteNumber(value)) return { value: value, error: null };
191
- if (isString(value)) {
192
- const num = Number(value);
193
- return Number.isFinite(num) ? { value: num, error: null } : { value: null, error: "必须是数字" };
194
- }
195
- return { value: null, error: "必须是数字" };
213
+ return convertStringNumber(value, "必须是数字");
196
214
  }
197
215
 
198
216
  case "integer": {
199
217
  if (isIntegerNumber(value)) return { value: value, error: null };
200
- if (isString(value)) {
201
- const num = Number(value);
202
- return Number.isFinite(num) && Number.isInteger(num) ? { value: num, error: null } : { value: null, error: "必须是整数" };
203
- }
204
- return { value: null, error: "必须是整数" };
218
+ return convertStringNumber(value, "必须是整数", true);
205
219
  }
206
220
 
207
221
  case "string": {
@@ -249,20 +263,12 @@ export class Validator {
249
263
 
250
264
  case "enumNumber": {
251
265
  if (isFiniteNumber(value)) return { value: value, error: null };
252
- if (isString(value)) {
253
- const num = Number(value);
254
- return Number.isFinite(num) ? { value: num, error: null } : { value: null, error: "必须是数字" };
255
- }
256
- return { value: null, error: "必须是数字" };
266
+ return convertStringNumber(value, "必须是数字");
257
267
  }
258
268
 
259
269
  case "enumInteger": {
260
270
  if (isIntegerNumber(value)) return { value: value, error: null };
261
- if (isString(value)) {
262
- const num = Number(value);
263
- return Number.isFinite(num) && Number.isInteger(num) ? { value: num, error: null } : { value: null, error: "必须是整数" };
264
- }
265
- return { value: null, error: "必须是整数" };
271
+ return convertStringNumber(value, "必须是整数", true);
266
272
  }
267
273
 
268
274
  default: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "befly",
3
- "version": "3.25.0",
3
+ "version": "3.26.0",
4
4
  "gitHead": "49c39d36695036e85fc64083cc43c1652fff96cb",
5
5
  "private": false,
6
6
  "description": "Befly - 为 Bun 专属打造的 JavaScript API 接口框架核心引擎",