befly-shared 1.3.2 → 1.3.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "befly-shared",
3
- "version": "1.3.2",
3
+ "version": "1.3.3",
4
4
  "private": false,
5
5
  "description": "Befly 纯函数与纯常量共享包(不包含 Node/浏览器特定实现)",
6
6
  "license": "Apache-2.0",
@@ -19,5 +19,5 @@
19
19
  "engines": {
20
20
  "bun": ">=1.3.0"
21
21
  },
22
- "gitHead": "7dd54bc1ed484139fa52ce5408f1a6c166e2a928"
22
+ "gitHead": "549fff1035081111a5ba32b5088e7c37907fa894"
23
23
  }
@@ -0,0 +1,58 @@
1
+ const HTTP_METHOD_PREFIX_RE = /^(GET|POST|PUT|PATCH|DELETE|OPTIONS|HEAD)\b/i;
2
+
3
+ /**
4
+ * 将未知输入规范化为 pathname 字符串数组。
5
+ *
6
+ * 规则(契约):
7
+ * - value 为“假值”(null/undefined/""/0/false/NaN)时返回空数组 []。
8
+ * - value 必须是 string[],否则抛错:`${fieldLabel} 必须是字符串数组`。
9
+ * - 数组元素必须满足:
10
+ * - 类型为 string
11
+ * - 不允许为空字符串
12
+ * - 不允许包含任何空白字符(空格/制表符/换行等)
13
+ * - 必须以 "/" 开头(pathname)
14
+ * - forbidMethodPrefix=true 时,禁止 "GET/POST/..." 等 method 前缀,并给出更明确的错误提示。
15
+ *
16
+ * 注意:该函数不会做任何隐式修复/转换(例如 trim/split/JSON.parse/去重/排序)。
17
+ */
18
+ export function normalizePathnameListInput(value: unknown, fieldLabel: string, forbidMethodPrefix: boolean): string[] {
19
+ // “假值”统一视为空数组:null/undefined/""/0/false/NaN
20
+ if (!value) return [];
21
+
22
+ if (!Array.isArray(value)) {
23
+ throw new Error(`${fieldLabel} 必须是字符串数组`);
24
+ }
25
+
26
+ const out: string[] = [];
27
+
28
+ for (let i = 0; i < value.length; i += 1) {
29
+ const item = value[i];
30
+ const itemLabel = `${fieldLabel}[${i}]`;
31
+
32
+ if (typeof item !== "string") {
33
+ throw new Error(`${itemLabel} 必须是字符串`);
34
+ }
35
+
36
+ if (item.length === 0) {
37
+ throw new Error(`${itemLabel} 不允许为空字符串`);
38
+ }
39
+
40
+ // 优先给出 method 前缀提示(更明确)
41
+ if (forbidMethodPrefix && HTTP_METHOD_PREFIX_RE.test(item)) {
42
+ throw new Error(`${itemLabel} 不允许包含 method 前缀,应为 url.pathname(例如 /api/app/xxx)`);
43
+ }
44
+
45
+ // 不做 trim 自动转换:含任何空白字符都视为不合法
46
+ if (/\s/.test(item)) {
47
+ throw new Error(`${itemLabel} 不允许包含空白字符(空格/制表符/换行等)`);
48
+ }
49
+
50
+ if (!item.startsWith("/")) {
51
+ throw new Error(`${itemLabel} 必须是 pathname(以 / 开头)`);
52
+ }
53
+
54
+ out.push(item);
55
+ }
56
+
57
+ return out;
58
+ }