@ray-js/api 1.6.10 → 1.6.12

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,22 @@
1
1
  type IRegionCode = 'AY' | 'AZ' | 'EU' | 'IN' | 'UE' | 'WE' | 'RU';
2
2
  type ICdnMap = Record<string, string> | Record<IRegionCode, Record<string, string>>;
3
+ /**
4
+ * 获取 CDN 地址
5
+ * @description 基础库 2.26.0 及以上版本推荐使用此方法,可以准确获取区域码。
6
+ * 基础库 2.26.0 以下版本请使用 getCdnUrlAsync 方法,以避免区域码获取不准确的问题。
7
+ * @param path - 图片路径
8
+ * @param cdnMap - CDN 地址映射表,若未提供,则使用内置 regionMap 直接与 path 进行拼接
9
+ * @param region - 区域代码,若未提供,则使用内置方法获取,EU 兜底
10
+ * @returns CDN 完整地址
11
+ */
3
12
  export default function getCdnUrl(path: string, cdnMap?: ICdnMap, region?: IRegionCode): string;
13
+ /**
14
+ * 获取 CDN 地址
15
+ * @description 基础库 2.26.0 以下版本请使用此方法,以避免区域码获取不准确的问题。
16
+ * @param path - 图片路径
17
+ * @param cdnMap - CDN 地址映射表,若未提供,则使用内置 regionMap 直接与 path 进行拼接
18
+ * @param region - 区域代码,若未提供,则使用内置方法获取,EU 兜底
19
+ * @returns CDN 完整地址
20
+ */
4
21
  export declare function getCdnUrlAsync(path: string, cdnMap?: ICdnMap, region?: IRegionCode): Promise<string>;
5
22
  export {};
@@ -1,6 +1,24 @@
1
+ /**
2
+ * 获取 CDN 地址
3
+ * @description 基础库 2.26.0 及以上版本推荐使用此方法,可以准确获取区域码。
4
+ * 基础库 2.26.0 以下版本请使用 getCdnUrlAsync 方法,以避免区域码获取不准确的问题。
5
+ * @param path - 图片路径
6
+ * @param cdnMap - CDN 地址映射表,若未提供,则使用内置 regionMap 直接与 path 进行拼接
7
+ * @param region - 区域代码,若未提供,则使用内置方法获取,EU 兜底
8
+ * @returns CDN 完整地址
9
+ */
1
10
  export default function getCdnUrl(path) {
2
11
  return path;
3
12
  }
13
+
14
+ /**
15
+ * 获取 CDN 地址
16
+ * @description 基础库 2.26.0 以下版本请使用此方法,以避免区域码获取不准确的问题。
17
+ * @param path - 图片路径
18
+ * @param cdnMap - CDN 地址映射表,若未提供,则使用内置 regionMap 直接与 path 进行拼接
19
+ * @param region - 区域代码,若未提供,则使用内置方法获取,EU 兜底
20
+ * @returns CDN 完整地址
21
+ */
4
22
  export async function getCdnUrlAsync(path) {
5
23
  // 暂未实现获取cdn方法,将直接返回path
6
24
  return Promise.resolve(path);
@@ -2,20 +2,22 @@ type IRegionCode = 'AY' | 'AZ' | 'EU' | 'IN' | 'UE' | 'WE' | 'RU';
2
2
  type ICdnMap = Record<string, string> | Record<IRegionCode, Record<string, string>>;
3
3
  export declare function getRegionCode(): Promise<IRegionCode>;
4
4
  /**
5
- * 获取 cdn 地址
5
+ * 获取 CDN 地址
6
+ * @description 基础库 2.26.0 及以上版本推荐使用此方法,可以准确获取区域码。
7
+ * 基础库 2.26.0 以下版本请使用 getCdnUrlAsync 方法,以避免区域码获取不准确的问题。
6
8
  * @param path - 图片路径
7
- * @param cdnMap - cdn 地址映射表
8
- * @param region - 地区
9
- * @deprecated - 请使用 getCdnUrlAsync 方法
10
- * @returns cdn 地址
9
+ * @param cdnMap - CDN 地址映射表,若未提供,则使用内置 regionMap 直接与 path 进行拼接
10
+ * @param region - 区域代码,若未提供,则使用内置方法获取,EU 兜底
11
+ * @returns CDN 完整地址
11
12
  */
12
13
  export default function getCdnUrl(path: string, cdnMap?: ICdnMap, region?: IRegionCode): string;
13
14
  /**
14
- * 获取 cdn 地址
15
+ * 获取 CDN 地址
16
+ * @description 基础库 2.26.0 以下版本请使用此方法,以避免区域码获取不准确的问题。
15
17
  * @param path - 图片路径
16
- * @param cdnMap - cdn 地址映射表
17
- * @param region - 地区
18
- * @returns Promise<string> - cdn 地址
18
+ * @param cdnMap - CDN 地址映射表,若未提供,则使用内置 regionMap 直接与 path 进行拼接
19
+ * @param region - 区域代码,若未提供,则使用内置方法获取,EU 兜底
20
+ * @returns CDN 完整地址
19
21
  */
20
22
  export declare function getCdnUrlAsync(path: string, cdnMap?: ICdnMap, region?: IRegionCode): Promise<string>;
21
23
  export {};
@@ -39,21 +39,33 @@ export function getRegionCode() {
39
39
  });
40
40
  });
41
41
  }
42
+ const concatRegionOrigin = (regionCode, path) => {
43
+ const regionOrigin = get(regionMap, [regionCode]) || get(regionMap, ['EU']);
44
+ if (path.indexOf('/') === 0) {
45
+ return "".concat(regionOrigin).concat(path);
46
+ }
47
+ return "".concat(regionOrigin, "/").concat(path);
48
+ };
49
+ const isEmptyOrUndefined = obj => {
50
+ return !obj || Object.keys(obj).length === 0;
51
+ };
42
52
 
43
53
  /**
44
- * 获取 cdn 地址
54
+ * 获取 CDN 地址
55
+ * @description 基础库 2.26.0 及以上版本推荐使用此方法,可以准确获取区域码。
56
+ * 基础库 2.26.0 以下版本请使用 getCdnUrlAsync 方法,以避免区域码获取不准确的问题。
45
57
  * @param path - 图片路径
46
- * @param cdnMap - cdn 地址映射表
47
- * @param region - 地区
48
- * @deprecated - 请使用 getCdnUrlAsync 方法
49
- * @returns cdn 地址
58
+ * @param cdnMap - CDN 地址映射表,若未提供,则使用内置 regionMap 直接与 path 进行拼接
59
+ * @param region - 区域代码,若未提供,则使用内置方法获取,EU 兜底
60
+ * @returns CDN 完整地址
50
61
  */
51
62
  export default function getCdnUrl(path, cdnMap, region) {
52
- if (!cdnMap) {
53
- console.warn('Please provider `cdnImage.json` data');
54
- return path;
55
- }
56
63
  const regionCode = region || getInternalRegionCode();
64
+ if (isEmptyOrUndefined(cdnMap)) {
65
+ // 如果未提供 cdnMap,则使用 regionMap 直接与 path 进行拼接
66
+ return concatRegionOrigin(regionCode, path);
67
+ }
68
+
57
69
  // 总是使用 EU 进行兜底
58
70
  const cdnShortPath = get(cdnMap, [regionCode, path]) || get(cdnMap, ['EU', path]) || get(cdnMap, [path]);
59
71
  if (!cdnShortPath) {
@@ -70,18 +82,19 @@ export default function getCdnUrl(path, cdnMap, region) {
70
82
  }
71
83
 
72
84
  /**
73
- * 获取 cdn 地址
85
+ * 获取 CDN 地址
86
+ * @description 基础库 2.26.0 以下版本请使用此方法,以避免区域码获取不准确的问题。
74
87
  * @param path - 图片路径
75
- * @param cdnMap - cdn 地址映射表
76
- * @param region - 地区
77
- * @returns Promise<string> - cdn 地址
88
+ * @param cdnMap - CDN 地址映射表,若未提供,则使用内置 regionMap 直接与 path 进行拼接
89
+ * @param region - 区域代码,若未提供,则使用内置方法获取,EU 兜底
90
+ * @returns CDN 完整地址
78
91
  */
79
92
  export async function getCdnUrlAsync(path, cdnMap, region) {
80
- if (!cdnMap) {
81
- console.warn('Please provider `cdnImage.json` data');
82
- return path;
83
- }
84
93
  let _regionCode = _globalRegionCode || 'EU';
94
+ if (isEmptyOrUndefined(cdnMap)) {
95
+ // 如果未提供 cdnMap,则使用 regionMap 直接与 path 进行拼接
96
+ return concatRegionOrigin(_regionCode, path);
97
+ }
85
98
  if (region) {
86
99
  _regionCode = region;
87
100
  } else {
@@ -1,5 +1,22 @@
1
1
  type IRegionCode = 'AY' | 'AZ' | 'EU' | 'IN' | 'UE' | 'WE' | 'RU';
2
2
  type ICdnMap = Record<string, string> | Record<IRegionCode, Record<string, string>>;
3
+ /**
4
+ * 获取 CDN 地址
5
+ * @description 基础库 2.26.0 及以上版本推荐使用此方法,可以准确获取区域码。
6
+ * 基础库 2.26.0 以下版本请使用 getCdnUrlAsync 方法,以避免区域码获取不准确的问题。
7
+ * @param path - 图片路径
8
+ * @param cdnMap - CDN 地址映射表,若未提供,则使用内置 regionMap 直接与 path 进行拼接
9
+ * @param region - 区域代码,若未提供,则使用内置方法获取,EU 兜底
10
+ * @returns CDN 完整地址
11
+ */
3
12
  export default function getCdnUrl(path: string, cdnMap?: ICdnMap, region?: IRegionCode): string;
13
+ /**
14
+ * 获取 CDN 地址
15
+ * @description 基础库 2.26.0 以下版本请使用此方法,以避免区域码获取不准确的问题。
16
+ * @param path - 图片路径
17
+ * @param cdnMap - CDN 地址映射表,若未提供,则使用内置 regionMap 直接与 path 进行拼接
18
+ * @param region - 区域代码,若未提供,则使用内置方法获取,EU 兜底
19
+ * @returns CDN 完整地址
20
+ */
4
21
  export declare function getCdnUrlAsync(path: string, cdnMap?: ICdnMap, region?: IRegionCode): Promise<string>;
5
22
  export {};
@@ -1,7 +1,62 @@
1
- export default function getCdnUrl(path) {
2
- return path;
1
+ import get from 'lodash/get';
2
+ const regionMap = {
3
+ AY: 'https://images.tuyacn.com'
4
+ };
5
+ const concatRegionOrigin = (regionCode, path) => {
6
+ const regionOrigin = get(regionMap, [regionCode]) || get(regionMap, ['EU']);
7
+ if (path.indexOf('/') === 0) {
8
+ return "".concat(regionOrigin).concat(path);
9
+ }
10
+ return "".concat(regionOrigin, "/").concat(path);
11
+ };
12
+ const isEmptyOrUndefined = obj => {
13
+ return !obj || Object.keys(obj).length === 0;
14
+ };
15
+
16
+ /**
17
+ * 获取 CDN 地址
18
+ * @description 基础库 2.26.0 及以上版本推荐使用此方法,可以准确获取区域码。
19
+ * 基础库 2.26.0 以下版本请使用 getCdnUrlAsync 方法,以避免区域码获取不准确的问题。
20
+ * @param path - 图片路径
21
+ * @param cdnMap - CDN 地址映射表,若未提供,则使用内置 regionMap 直接与 path 进行拼接
22
+ * @param region - 区域代码,若未提供,则使用内置方法获取,EU 兜底
23
+ * @returns CDN 完整地址
24
+ */
25
+ export default function getCdnUrl(path, cdnMap) {
26
+ const regionCode = 'AY';
27
+ if (isEmptyOrUndefined(cdnMap)) {
28
+ // 如果未提供 cdnMap,则使用 regionMap 直接与 path 进行拼接
29
+ return concatRegionOrigin(regionCode, path);
30
+ }
31
+
32
+ // 总是使用 EU 进行兜底
33
+ const cdnShortPath = get(cdnMap, [regionCode, path]) || get(cdnMap, ['AY', path]) || get(cdnMap, [path]);
34
+ if (!cdnShortPath) {
35
+ console.warn("[App] cdn \"".concat(path, "\" is not exist."));
36
+ return path;
37
+ }
38
+ if (cdnShortPath.indexOf('http') === 0 || cdnShortPath.indexOf('//') === 0) {
39
+ return cdnShortPath;
40
+ }
41
+ if (cdnShortPath.indexOf('/') === 0) {
42
+ return "".concat(regionMap[regionCode]).concat(cdnShortPath);
43
+ }
44
+ return "".concat(regionMap[regionCode], "/").concat(cdnShortPath);
3
45
  }
4
- export async function getCdnUrlAsync(path) {
5
- // 暂未实现获取cdn方法,将直接返回path
6
- return Promise.resolve(path);
46
+
47
+ /**
48
+ * 获取 CDN 地址
49
+ * @description 基础库 2.26.0 以下版本请使用此方法,以避免区域码获取不准确的问题。
50
+ * @param path - 图片路径
51
+ * @param cdnMap - CDN 地址映射表,若未提供,则使用内置 regionMap 直接与 path 进行拼接
52
+ * @param region - 区域代码,若未提供,则使用内置方法获取,EU 兜底
53
+ * @returns CDN 完整地址
54
+ */
55
+ export async function getCdnUrlAsync(path, cdnMap) {
56
+ let _regionCode = 'AY';
57
+ if (isEmptyOrUndefined(cdnMap)) {
58
+ // 如果未提供 cdnMap,则使用 regionMap 直接与 path 进行拼接
59
+ return concatRegionOrigin(_regionCode, path);
60
+ }
61
+ return getCdnUrl(path, cdnMap, _regionCode);
7
62
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/api",
3
- "version": "1.6.10",
3
+ "version": "1.6.12",
4
4
  "description": "Ray universal api",
5
5
  "keywords": [
6
6
  "ray"
@@ -29,14 +29,14 @@
29
29
  "watch": "ray start --type=component"
30
30
  },
31
31
  "dependencies": {
32
- "@ray-js/framework": "1.6.10",
33
- "@ray-js/router": "1.6.10",
32
+ "@ray-js/framework": "1.6.12",
33
+ "@ray-js/router": "1.6.12",
34
34
  "@ray-js/wechat": "^0.2.19",
35
35
  "base64-browser": "^1.0.1",
36
36
  "query-string": "^7.1.3"
37
37
  },
38
38
  "devDependencies": {
39
- "@ray-js/cli": "1.6.10",
39
+ "@ray-js/cli": "1.6.12",
40
40
  "art-template": "^4.13.2",
41
41
  "fs-extra": "^10.1.0",
42
42
  "miniprogram-api-typings": "^3.12.3",
@@ -46,5 +46,5 @@
46
46
  "access": "public",
47
47
  "registry": "https://registry.npmjs.com"
48
48
  },
49
- "gitHead": "1fe082574c262f1470437db0bbd720ae9453f0fc"
49
+ "gitHead": "fff5d64de5adddf835edafb9556ed54b814e661a"
50
50
  }