@arcblock/ux 2.10.83 → 2.10.84

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.
@@ -1,5 +1,6 @@
1
- export declare function getSafeUrl(url: string, { returnRaw, allowProtocol, allowHost, }?: {
2
- returnRaw?: boolean;
3
- allowProtocol?: Array<string> | null;
4
- allowHost?: Array<string> | null;
1
+ export declare function getSafeUrl(url: string, { throwOnError, returnRaw, allowProtocol, allowHost, }?: {
2
+ throwOnError?: boolean | undefined;
3
+ returnRaw?: boolean | undefined;
4
+ allowProtocol?: string[] | undefined;
5
+ allowHost?: string[] | undefined;
5
6
  }): string | null;
@@ -1,8 +1,10 @@
1
1
  /* eslint-disable import/prefer-default-export */
2
2
 
3
+ class CustomError extends Error {}
3
4
  export function getSafeUrl(
4
5
  // 只允许 / 开头, ./ 开头和带有 protocol 的 URL
5
6
  url, {
7
+ throwOnError = true,
6
8
  returnRaw = false,
7
9
  // 根据 URL 的规范,protocol 以 : 结尾
8
10
  allowProtocol = ['https:', 'http:'],
@@ -20,15 +22,22 @@ url, {
20
22
  const allowHostName = allowHost ? allowHost.map(host => new URL(host).hostname) : allowHost;
21
23
  if (allowProtocol !== null && !allowProtocol.includes(urlInstance.protocol)) {
22
24
  console.error(`Invalid protocol: ${urlInstance.protocol}`);
25
+ if (throwOnError) throw new CustomError(`Invalid protocol: ${urlInstance.protocol}`);
23
26
  return null;
24
27
  }
25
28
  if (allowHostName !== null && !allowHostName.includes(urlInstance.hostname)) {
26
29
  console.error(`Invalid host: ${urlInstance.hostname}`);
30
+ if (throwOnError) throw new CustomError(`Invalid host: ${urlInstance.hostname}`);
27
31
  return null;
28
32
  }
29
33
  return returnRaw ? url : urlInstance.href;
30
34
  } catch (error) {
35
+ if (error instanceof CustomError) {
36
+ if (throwOnError) throw error;
37
+ return null;
38
+ }
31
39
  console.error(`Failed to convert url: ${url}`);
40
+ if (throwOnError) throw new Error(`Failed to convert url: ${url}`);
41
+ return null;
32
42
  }
33
- return null;
34
43
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcblock/ux",
3
- "version": "2.10.83",
3
+ "version": "2.10.84",
4
4
  "description": "Common used react components for arcblock products",
5
5
  "keywords": [
6
6
  "react",
@@ -68,12 +68,12 @@
68
68
  "react": ">=18.2.0",
69
69
  "react-router-dom": ">=6.22.3"
70
70
  },
71
- "gitHead": "3dc3cfcb73c77c423d7f8d5bab93c38ddfbf55f9",
71
+ "gitHead": "1311dd7d9ce3a6b0164404de828e64dcd900ce6d",
72
72
  "dependencies": {
73
73
  "@arcblock/did-motif": "^1.1.13",
74
- "@arcblock/icons": "^2.10.83",
75
- "@arcblock/nft-display": "^2.10.83",
76
- "@arcblock/react-hooks": "^2.10.83",
74
+ "@arcblock/icons": "^2.10.84",
75
+ "@arcblock/nft-display": "^2.10.84",
76
+ "@arcblock/react-hooks": "^2.10.84",
77
77
  "@babel/plugin-syntax-dynamic-import": "^7.8.3",
78
78
  "@fontsource/inter": "^5.0.16",
79
79
  "@fontsource/ubuntu-mono": "^5.0.18",
@@ -1,20 +1,26 @@
1
1
  /* eslint-disable import/prefer-default-export */
2
2
 
3
+ type GetSafeUrlOptions = {
4
+ returnRaw?: boolean;
5
+ throwOnError?: Boolean;
6
+ // 为 null 时代表不检查 protocol
7
+ allowProtocol?: Array<string> | null;
8
+ // 为 null 时代表不检查 host
9
+ allowHost?: Array<string> | null;
10
+ };
11
+
12
+ class CustomError extends Error {}
13
+
3
14
  export function getSafeUrl(
4
15
  // 只允许 / 开头, ./ 开头和带有 protocol 的 URL
5
16
  url: string,
6
17
  {
18
+ throwOnError = true,
7
19
  returnRaw = false,
8
20
  // 根据 URL 的规范,protocol 以 : 结尾
9
21
  allowProtocol = ['https:', 'http:'],
10
22
  // host 中可能会携带端口号,需要使用 hostname 来获取干净的域名
11
23
  allowHost = [window.location.href],
12
- }: {
13
- returnRaw?: boolean;
14
- // 为 null 时代表不检查 protocol
15
- allowProtocol?: Array<string> | null;
16
- // 为 null 时代表不检查 host
17
- allowHost?: Array<string> | null;
18
24
  } = {}
19
25
  ) {
20
26
  try {
@@ -29,17 +35,23 @@ export function getSafeUrl(
29
35
  const allowHostName = allowHost ? allowHost.map((host) => new URL(host).hostname) : allowHost;
30
36
  if (allowProtocol !== null && !allowProtocol.includes(urlInstance.protocol)) {
31
37
  console.error(`Invalid protocol: ${urlInstance.protocol}`);
38
+ if (throwOnError) throw new CustomError(`Invalid protocol: ${urlInstance.protocol}`);
32
39
  return null;
33
40
  }
34
41
  if (allowHostName !== null && !allowHostName.includes(urlInstance.hostname)) {
35
42
  console.error(`Invalid host: ${urlInstance.hostname}`);
43
+ if (throwOnError) throw new CustomError(`Invalid host: ${urlInstance.hostname}`);
36
44
  return null;
37
45
  }
38
46
 
39
47
  return returnRaw ? url : urlInstance.href;
40
48
  } catch (error) {
49
+ if (error instanceof CustomError) {
50
+ if (throwOnError) throw error;
51
+ return null;
52
+ }
41
53
  console.error(`Failed to convert url: ${url}`);
54
+ if (throwOnError) throw new Error(`Failed to convert url: ${url}`);
55
+ return null;
42
56
  }
43
-
44
- return null;
45
57
  }