@ray-js/api 1.7.50 → 1.7.52

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.
@@ -1908,6 +1908,7 @@ export type Audio = {
1908
1908
  export type FileInfo = {
1909
1909
  objectKey: string;
1910
1910
  fileName: string;
1911
+ fileSize: number;
1911
1912
  };
1912
1913
  export type FileSign = {
1913
1914
  action: string;
@@ -1,5 +1,23 @@
1
1
  type IRegionCode = 'AY' | 'AZ' | 'EU' | 'IN' | 'UE' | 'WE' | 'RU' | 'SG';
2
2
  type ICdnMap = Record<string, string> | Record<IRegionCode, Record<string, string>>;
3
+ interface IImageProcessOptions {
4
+ crop?: {
5
+ h?: number;
6
+ w?: number;
7
+ x?: number;
8
+ y?: number;
9
+ };
10
+ resize?: {
11
+ h?: number;
12
+ w?: number;
13
+ };
14
+ rotate?: {
15
+ value: 90 | 180 | 270 | 360;
16
+ };
17
+ format?: {
18
+ value: string;
19
+ };
20
+ }
3
21
  /**
4
22
  * 获取 CDN 地址
5
23
  * @description 基础库 2.26.0 及以上版本推荐使用此方法,可以准确获取区域码。
@@ -8,9 +26,8 @@ type ICdnMap = Record<string, string> | Record<IRegionCode, Record<string, strin
8
26
  * @param cdnMap - CDN 地址映射表,若未提供,则使用内置 regionMap 直接与 path 进行拼接
9
27
  * @param region - 区域代码,若未提供,则使用内置方法获取,EU 兜底
10
28
  * @returns CDN 完整地址
11
- * @deprecated - 推荐使用 getCdnUrlAsync 方法
12
29
  */
13
- export default function getCdnUrl(path: string, cdnMap?: ICdnMap, region?: IRegionCode): string;
30
+ export default function getCdnUrl(path: string, cdnMap?: ICdnMap, region?: IRegionCode, process?: IImageProcessOptions): string;
14
31
  /**
15
32
  * 获取 CDN 地址
16
33
  * @description 基础库 2.26.0 以下版本请使用此方法,以避免区域码获取不准确的问题。
@@ -6,7 +6,6 @@
6
6
  * @param cdnMap - CDN 地址映射表,若未提供,则使用内置 regionMap 直接与 path 进行拼接
7
7
  * @param region - 区域代码,若未提供,则使用内置方法获取,EU 兜底
8
8
  * @returns CDN 完整地址
9
- * @deprecated - 推荐使用 getCdnUrlAsync 方法
10
9
  */
11
10
  export default function getCdnUrl(path) {
12
11
  return path;
@@ -1,5 +1,23 @@
1
1
  type IRegionCode = 'AY' | 'AZ' | 'EU' | 'IN' | 'UE' | 'WE' | 'RU' | 'SG';
2
2
  type ICdnMap = Record<string, string> | Record<IRegionCode, Record<string, string>>;
3
+ interface IImageProcessOptions {
4
+ crop?: {
5
+ h?: number;
6
+ w?: number;
7
+ x?: number;
8
+ y?: number;
9
+ };
10
+ resize?: {
11
+ h?: number;
12
+ w?: number;
13
+ };
14
+ rotate?: {
15
+ value: 90 | 180 | 270 | 360;
16
+ };
17
+ format?: {
18
+ value: string;
19
+ };
20
+ }
3
21
  export declare function getRegionCode(): Promise<IRegionCode>;
4
22
  /**
5
23
  * 获取 CDN 地址
@@ -8,9 +26,10 @@ export declare function getRegionCode(): Promise<IRegionCode>;
8
26
  * @param path - 图片路径
9
27
  * @param cdnMap - CDN 地址映射表,若未提供,则使用内置 regionMap 直接与 path 进行拼接
10
28
  * @param region - 区域代码,若未提供,则使用内置方法获取,EU 兜底
29
+ * @param process - 图片处理参数对象(可选)
11
30
  * @returns CDN 完整地址
12
31
  */
13
- export default function getCdnUrl(path: string, cdnMap?: ICdnMap, region?: IRegionCode): string;
32
+ export default function getCdnUrl(path: string, cdnMap?: ICdnMap, region?: IRegionCode, process?: IImageProcessOptions): string;
14
33
  /**
15
34
  * 获取 CDN 地址
16
35
  * @description 基础库 2.26.0 以下版本请使用此方法,以避免区域码获取不准确的问题。
@@ -9,6 +9,9 @@ const regionMap = {
9
9
  WE: 'https://weimages57vdxnr.cdn5th.com',
10
10
  UE: 'https://ueimages58yny2.cdn5th.com'
11
11
  };
12
+
13
+ // 国外有处理参数时统一使用该域名
14
+ const unifiedProcessDomain = 'https://d395y9yyizm1zb.cdn5th.com';
12
15
  let _globalRegionCode;
13
16
  const getInternalRegionCode = () => {
14
17
  if (!_globalRegionCode) {
@@ -51,6 +54,58 @@ const isEmptyOrUndefined = obj => {
51
54
  return !obj || Object.keys(obj).length === 0;
52
55
  };
53
56
 
57
+ // 构建裸操作串 image/crop,.../resize,.../rotate,.../format,... (调整顺序:crop → resize)
58
+ const buildOperationChain = opts => {
59
+ if (!opts || Object.keys(opts).length === 0) return '';
60
+ const seg = ['image'];
61
+ if (opts.crop && !isEmptyOrUndefined(opts.crop)) {
62
+ const {
63
+ w,
64
+ h,
65
+ x,
66
+ y
67
+ } = opts.crop;
68
+ const cropParts = ['crop'];
69
+ if (w != null) cropParts.push(`w_${w}`);
70
+ if (h != null) cropParts.push(`h_${h}`);
71
+ if (x != null) cropParts.push(`x_${x}`);
72
+ if (y != null) cropParts.push(`y_${y}`);
73
+ seg.push(cropParts.join(','));
74
+ }
75
+ if (opts.resize && !isEmptyOrUndefined(opts.resize)) {
76
+ const {
77
+ w,
78
+ h
79
+ } = opts.resize;
80
+ const resizeParts = ['resize'];
81
+ if (w != null) resizeParts.push(`w_${w}`);
82
+ if (h != null) resizeParts.push(`h_${h}`);
83
+ seg.push(resizeParts.join(','));
84
+ }
85
+ if (opts.rotate && opts.rotate.value) {
86
+ seg.push(`rotate,${opts.rotate.value}`);
87
+ }
88
+ if (opts.format && opts.format.value) {
89
+ seg.push(`format,${opts.format.value}`);
90
+ }
91
+ if (seg.length === 1) return '';
92
+ return seg.join('/');
93
+ };
94
+
95
+ // 根据区域生成查询键名与完整查询片段
96
+ const buildProcessQuery = (regionCode, opts) => {
97
+ if (!opts || isEmptyOrUndefined(opts)) return '';
98
+ const chain = buildOperationChain(opts);
99
+ if (!chain) return '';
100
+ const key = regionCode === 'AY' ? 'x-oss-process' : 'process';
101
+ return `${key}=${chain}`;
102
+ };
103
+ const appendQuery = (url, query) => {
104
+ if (!query) return url;
105
+ if (url.indexOf('?') === -1) return `${url}?${query}`;
106
+ return url.endsWith('?') || url.endsWith('&') ? `${url}${query}` : `${url}&${query}`;
107
+ };
108
+
54
109
  /**
55
110
  * 获取 CDN 地址
56
111
  * @description 基础库 2.26.0 及以上版本推荐使用此方法,可以准确获取区域码。
@@ -58,28 +113,46 @@ const isEmptyOrUndefined = obj => {
58
113
  * @param path - 图片路径
59
114
  * @param cdnMap - CDN 地址映射表,若未提供,则使用内置 regionMap 直接与 path 进行拼接
60
115
  * @param region - 区域代码,若未提供,则使用内置方法获取,EU 兜底
116
+ * @param process - 图片处理参数对象(可选)
61
117
  * @returns CDN 完整地址
62
118
  */
63
- export default function getCdnUrl(path, cdnMap, region) {
119
+ export default function getCdnUrl(path, cdnMap, region, process) {
64
120
  const regionCode = region || getInternalRegionCode();
121
+ const hasProcess = !!process && !isEmptyOrUndefined(process);
122
+ const queryPart = buildProcessQuery(regionCode, process);
65
123
  if (isEmptyOrUndefined(cdnMap)) {
66
- // 如果未提供 cdnMap,则使用 regionMap 直接与 path 进行拼接
67
- return concatRegionOrigin(regionCode, path);
124
+ if (hasProcess && regionCode !== 'AY') {
125
+ const url = path.indexOf('/') === 0 ? `${unifiedProcessDomain}${path}` : `${unifiedProcessDomain}/${path}`;
126
+ return appendQuery(url, queryPart);
127
+ }
128
+ return appendQuery(concatRegionOrigin(regionCode, path), queryPart);
68
129
  }
69
130
 
70
131
  // 总是使用 EU 进行兜底
71
132
  const cdnShortPath = get(cdnMap, [regionCode, path]) || get(cdnMap, ['EU', path]) || get(cdnMap, [path]);
72
133
  if (!cdnShortPath) {
73
134
  console.warn(`[App] cdn "${path}" is not exist.`);
74
- return path;
135
+ if (hasProcess && regionCode !== 'AY') {
136
+ const url = path.indexOf('/') === 0 ? `${unifiedProcessDomain}${path}` : `${unifiedProcessDomain}/${path}`;
137
+ return appendQuery(url, queryPart);
138
+ }
139
+ return appendQuery(path, queryPart);
75
140
  }
141
+ let finalUrl;
76
142
  if (cdnShortPath.indexOf('http') === 0 || cdnShortPath.indexOf('//') === 0) {
77
- return cdnShortPath;
143
+ finalUrl = cdnShortPath;
144
+ } else if (cdnShortPath.indexOf('/') === 0) {
145
+ finalUrl = `${regionMap[regionCode]}${cdnShortPath}`;
146
+ } else {
147
+ finalUrl = `${regionMap[regionCode]}/${cdnShortPath}`;
78
148
  }
79
- if (cdnShortPath.indexOf('/') === 0) {
80
- return `${regionMap[regionCode]}${cdnShortPath}`;
149
+ if (hasProcess && regionCode !== 'AY') {
150
+ const originPrefix = regionMap[regionCode];
151
+ if (finalUrl.startsWith(originPrefix)) {
152
+ finalUrl = finalUrl.replace(originPrefix, unifiedProcessDomain);
153
+ }
81
154
  }
82
- return `${regionMap[regionCode]}/${cdnShortPath}`;
155
+ return appendQuery(finalUrl, queryPart);
83
156
  }
84
157
 
85
158
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/api",
3
- "version": "1.7.50",
3
+ "version": "1.7.52",
4
4
  "description": "Ray universal api",
5
5
  "keywords": [
6
6
  "ray"
@@ -29,8 +29,8 @@
29
29
  "watch": "ray start --type=component"
30
30
  },
31
31
  "dependencies": {
32
- "@ray-js/framework": "1.7.50",
33
- "@ray-js/router": "1.7.50",
32
+ "@ray-js/framework": "1.7.52",
33
+ "@ray-js/router": "1.7.52",
34
34
  "base64-browser": "^1.0.1",
35
35
  "query-string": "^7.1.3"
36
36
  },
@@ -38,7 +38,7 @@
38
38
  "@ray-js/wechat": "^0.3.13"
39
39
  },
40
40
  "devDependencies": {
41
- "@ray-js/cli": "1.7.50",
41
+ "@ray-js/cli": "1.7.52",
42
42
  "art-template": "^4.13.4",
43
43
  "fs-extra": "^10.1.0",
44
44
  "miniprogram-api-typings": "^3.12.3",
@@ -48,5 +48,5 @@
48
48
  "access": "public",
49
49
  "registry": "https://registry.npmjs.org"
50
50
  },
51
- "gitHead": "f518f3b1907c30d12f9cad69790609530c68f44f"
51
+ "gitHead": "14ced414dd88e28032bde51c350b8dae60fe6892"
52
52
  }