@ray-js/api 1.6.9 → 1.6.10

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.
@@ -228,8 +228,19 @@ interface WebviewContext {
228
228
  }
229
229
 
230
230
  interface SocketTask {
231
- send(option: { data: string | ArrayBuffer; success?: () => void; fail?: () => void; complete?: () => void }): void
232
- close(option: { code?: number; reason?: string; success?: () => void; fail?: () => void; complete?: () => void }): void
231
+ send(option: {
232
+ data: string | ArrayBuffer
233
+ success?: () => void
234
+ fail?: () => void
235
+ complete?: () => void
236
+ }): void
237
+ close(option: {
238
+ code?: number
239
+ reason?: string
240
+ success?: () => void
241
+ fail?: () => void
242
+ complete?: () => void
243
+ }): void
233
244
  onOpen(callback: () => void): void
234
245
  onClose(callback: () => void): void
235
246
  onError(callback: () => void): void
@@ -237,15 +248,15 @@ interface SocketTask {
237
248
  }
238
249
 
239
250
  interface ConnectSocketProps {
240
- url: string;
241
- protocols?: string[];
242
- header?: Record<string, string>;
243
- timeout?: number;
244
- tcpNoDelay?: boolean;
245
- perMessageDeflate?: boolean;
246
- forceCellularNetwork?: boolean;
247
- success?: () => void;
248
- fail?: () => void;
251
+ url: string
252
+ protocols?: string[]
253
+ header?: Record<string, string>
254
+ timeout?: number
255
+ tcpNoDelay?: boolean
256
+ perMessageDeflate?: boolean
257
+ forceCellularNetwork?: boolean
258
+ success?: () => void
259
+ fail?: () => void
249
260
  complete?: () => void
250
261
  }
251
262
 
@@ -315,6 +326,13 @@ declare namespace ty {
315
326
  * @returns 是否可用
316
327
  */
317
328
  export function canIUse(schema: string): boolean
329
+ type IRegionCode = 'AY' | 'AZ' | 'EU' | 'IN' | 'UE' | 'WE' | 'RU'
330
+
331
+ /**
332
+ * 获取当前小程序的区域码
333
+ * @returns 区域码 可选值:AY, AZ, EU, IN, RU, UE, WE
334
+ */
335
+ export function getRegionCode(): IRegionCode
318
336
  }
319
337
 
320
338
  declare namespace WechatMiniprogram {
@@ -1,2 +1,5 @@
1
- export default function getCdnUrl(path: string, cdnMap?: Record<string, string>, region?: string): string;
2
- export declare function getCdnUrlAsync(path: string, cdnMap?: Record<string, string>, region?: string): Promise<string>;
1
+ type IRegionCode = 'AY' | 'AZ' | 'EU' | 'IN' | 'UE' | 'WE' | 'RU';
2
+ type ICdnMap = Record<string, string> | Record<IRegionCode, Record<string, string>>;
3
+ export default function getCdnUrl(path: string, cdnMap?: ICdnMap, region?: IRegionCode): string;
4
+ export declare function getCdnUrlAsync(path: string, cdnMap?: ICdnMap, region?: IRegionCode): Promise<string>;
5
+ export {};
@@ -1,3 +1,21 @@
1
- export declare function getRegionCode(): Promise<string>;
2
- export default function getCdnUrl(path: string, cdnMap?: Record<string, string>, region?: string): string;
3
- export declare function getCdnUrlAsync(path: string, cdnMap?: Record<string, string>, region?: string): Promise<string>;
1
+ type IRegionCode = 'AY' | 'AZ' | 'EU' | 'IN' | 'UE' | 'WE' | 'RU';
2
+ type ICdnMap = Record<string, string> | Record<IRegionCode, Record<string, string>>;
3
+ export declare function getRegionCode(): Promise<IRegionCode>;
4
+ /**
5
+ * 获取 cdn 地址
6
+ * @param path - 图片路径
7
+ * @param cdnMap - cdn 地址映射表
8
+ * @param region - 地区
9
+ * @deprecated - 请使用 getCdnUrlAsync 方法
10
+ * @returns cdn 地址
11
+ */
12
+ export default function getCdnUrl(path: string, cdnMap?: ICdnMap, region?: IRegionCode): string;
13
+ /**
14
+ * 获取 cdn 地址
15
+ * @param path - 图片路径
16
+ * @param cdnMap - cdn 地址映射表
17
+ * @param region - 地区
18
+ * @returns Promise<string> - cdn 地址
19
+ */
20
+ export declare function getCdnUrlAsync(path: string, cdnMap?: ICdnMap, region?: IRegionCode): Promise<string>;
21
+ export {};
@@ -1,28 +1,36 @@
1
+ import get from 'lodash/get';
1
2
  const regionMap = {
2
- AY: 'images.tuyacn.com',
3
- AZ: 'usimagesd1448c85ulz2o4.cdn5th.com',
4
- EU: 'euimagesd2h2yqnfpu4gl5.cdn5th.com',
5
- IN: 'inimagesd1jqokb9wptk2t.cdn5th.com',
6
- RU: 'euimagesd2h2yqnfpu4gl5.cdn5th.com',
7
- WE: 'weimages57vdxnr.cdn5th.com',
8
- UE: 'ueimages58yny2.cdn5th.com'
3
+ AY: 'https://images.tuyacn.com',
4
+ AZ: 'https://usimagesd1448c85ulz2o4.cdn5th.com',
5
+ EU: 'https://euimagesd2h2yqnfpu4gl5.cdn5th.com',
6
+ IN: 'https://inimagesd1jqokb9wptk2t.cdn5th.com',
7
+ RU: 'https://euimagesd2h2yqnfpu4gl5.cdn5th.com',
8
+ WE: 'https://weimages57vdxnr.cdn5th.com',
9
+ UE: 'https://ueimages58yny2.cdn5th.com'
9
10
  };
10
- let _regionCode;
11
- let getInternalRegionCode = () => {
12
- if (!_regionCode) {
11
+ let _globalRegionCode;
12
+ const getInternalRegionCode = () => {
13
+ if (!_globalRegionCode) {
14
+ // 2.26.0 基础库才加上的方法
15
+
16
+ if (typeof ty.getRegionCode === 'function') {
17
+ _globalRegionCode = ty.getRegionCode();
18
+ return _globalRegionCode || 'EU';
19
+ }
13
20
  ty.getAppInfo({
14
21
  success(res) {
15
- _regionCode = res.regionCode;
22
+ _globalRegionCode = res.regionCode;
16
23
  }
17
24
  });
18
25
  }
19
- return 'EU';
26
+ // FIXME: 第一次调用时,_regionCode 为 undefined 时,返回 'EU'
27
+ return _globalRegionCode || 'EU';
20
28
  };
21
29
  export function getRegionCode() {
22
30
  return new Promise((resolve, reject) => {
23
31
  ty.getAppInfo({
24
32
  success(res) {
25
- _regionCode = res.regionCode;
33
+ _globalRegionCode = res.regionCode;
26
34
  resolve(res.regionCode);
27
35
  },
28
36
  fail(err) {
@@ -31,24 +39,49 @@ export function getRegionCode() {
31
39
  });
32
40
  });
33
41
  }
42
+
43
+ /**
44
+ * 获取 cdn 地址
45
+ * @param path - 图片路径
46
+ * @param cdnMap - cdn 地址映射表
47
+ * @param region - 地区
48
+ * @deprecated - 请使用 getCdnUrlAsync 方法
49
+ * @returns cdn 地址
50
+ */
34
51
  export default function getCdnUrl(path, cdnMap, region) {
35
52
  if (!cdnMap) {
36
- console.warn('Please pass in cdnMap');
53
+ console.warn('Please provider `cdnImage.json` data');
37
54
  return path;
38
55
  }
39
- const cdnShortPath = cdnMap[path];
56
+ const regionCode = region || getInternalRegionCode();
57
+ // 总是使用 EU 进行兜底
58
+ const cdnShortPath = get(cdnMap, [regionCode, path]) || get(cdnMap, ['EU', path]) || get(cdnMap, [path]);
40
59
  if (!cdnShortPath) {
41
60
  console.warn("[App] cdn \"".concat(path, "\" is not exist."));
42
61
  return path;
43
62
  }
44
- return "https://".concat(regionMap[region || getInternalRegionCode()], "/").concat(cdnShortPath);
63
+ if (cdnShortPath.indexOf('http') === 0 || cdnShortPath.indexOf('//') === 0) {
64
+ return cdnShortPath;
65
+ }
66
+ if (cdnShortPath.indexOf('/') === 0) {
67
+ return "".concat(regionMap[regionCode]).concat(cdnShortPath);
68
+ }
69
+ return "".concat(regionMap[regionCode], "/").concat(cdnShortPath);
45
70
  }
71
+
72
+ /**
73
+ * 获取 cdn 地址
74
+ * @param path - 图片路径
75
+ * @param cdnMap - cdn 地址映射表
76
+ * @param region - 地区
77
+ * @returns Promise<string> - cdn 地址
78
+ */
46
79
  export async function getCdnUrlAsync(path, cdnMap, region) {
47
80
  if (!cdnMap) {
48
- console.warn('Please pass in cdnMap');
81
+ console.warn('Please provider `cdnImage.json` data');
49
82
  return path;
50
83
  }
51
- let _regionCode = 'EU';
84
+ let _regionCode = _globalRegionCode || 'EU';
52
85
  if (region) {
53
86
  _regionCode = region;
54
87
  } else {
@@ -58,10 +91,5 @@ export async function getCdnUrlAsync(path, cdnMap, region) {
58
91
  console.warn('[App] getRegionCode error, use default region EU', error);
59
92
  }
60
93
  }
61
- const cdnShortPath = cdnMap[path];
62
- if (!cdnShortPath) {
63
- console.warn("[App] cdn \"".concat(path, "\" is not exist."));
64
- return path;
65
- }
66
- return "https://".concat(regionMap[_regionCode], "/").concat(cdnShortPath);
94
+ return getCdnUrl(path, cdnMap, _regionCode);
67
95
  }
@@ -1,2 +1,5 @@
1
- export default function getCdnUrl(path: string, cdnMap?: Record<string, string>, region?: string): string;
2
- export declare function getCdnUrlAsync(path: string, cdnMap?: Record<string, string>, region?: string): Promise<string>;
1
+ type IRegionCode = 'AY' | 'AZ' | 'EU' | 'IN' | 'UE' | 'WE' | 'RU';
2
+ type ICdnMap = Record<string, string> | Record<IRegionCode, Record<string, string>>;
3
+ export default function getCdnUrl(path: string, cdnMap?: ICdnMap, region?: IRegionCode): string;
4
+ export declare function getCdnUrlAsync(path: string, cdnMap?: ICdnMap, region?: IRegionCode): Promise<string>;
5
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/api",
3
- "version": "1.6.9",
3
+ "version": "1.6.10",
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.9",
33
- "@ray-js/router": "1.6.9",
32
+ "@ray-js/framework": "1.6.10",
33
+ "@ray-js/router": "1.6.10",
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.9",
39
+ "@ray-js/cli": "1.6.10",
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": "443512d1bc0bda8f5b8c37b757f57581dbd8f66f"
49
+ "gitHead": "1fe082574c262f1470437db0bbd720ae9453f0fc"
50
50
  }