@lingxiteam/ebe-utils 0.2.15 → 0.2.17

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.
@@ -25,27 +25,85 @@ type InterceptorResponseTypeCtx = {
25
25
  hookCtx: Record<string, any>;
26
26
  };
27
27
 
28
- type InterceptorResponseType = (url: string, ctx: InterceptorResponseTypeCtx) => any;
28
+ type InterceptorResponseType = (
29
+ url: string,
30
+ ctx: InterceptorResponseTypeCtx,
31
+ ) => any;
29
32
 
30
- export const defaultResponense: ResponseInterceptor = async (response, options) => {
33
+ export const defaultResponense: ResponseInterceptor = async (
34
+ response:Response,
35
+ options:RequestOptionsInit,
36
+ ) => {
31
37
  const { url } = response;
32
- const data = await response.clone().json();
38
+ const contentType: string = response.headers.get('content-type') || 'text/plain';
39
+ let data;
40
+ if(contentType === 'application/json'){
41
+ data = await response.clone().json();
42
+ }
33
43
  if (url.indexOf('/static') !== -1) {
34
- if (typeof data === 'object' && Object.hasOwnProperty.call(data, 'resultCode') && +data.resultCode !== 0) {
44
+ if (
45
+ typeof data === 'object' &&
46
+ Object.hasOwnProperty.call(data, 'resultCode') &&
47
+ +data.resultCode !== 0
48
+ ) {
35
49
  return Promise.reject(data.resultMsg);
36
50
  }
37
51
  return data;
38
52
  }
39
53
 
40
- // 下载文件
41
- if (Object.prototype.toString.call(data) === '[object Blob]' || url.indexOf('/app/file/download') !== -1) {
54
+ // responseType指定Blob时直接返回umi-request处理后的data
55
+ if (Object.prototype.toString.call(data) === '[object Blob]') {
42
56
  return data;
43
57
  }
44
58
 
59
+ // 如果内容为非json类型并没有配置指定响应内容类型时,根据content-type进行二次处理
60
+ if (contentType !== 'application/json' && !options.responseType) {
61
+ // Blob解析
62
+ if (url.indexOf('/app/file/download') !== -1 || [
63
+ /^application\/octet-stream/,
64
+ /^application\/(zip|x-rar-compressed|x-7z-compressed)/,
65
+ /^application\/vnd\./,
66
+ /^application\/pdf/,
67
+ /^application\/msword/,
68
+ /^image\//,
69
+ /^audio\//,
70
+ /^video\//,
71
+ ].find(regType => regType.test(contentType))) {
72
+ return response.blob().then((resBlob:any) => {
73
+ // 从请求头上获取文件名称
74
+ const disposition = response.headers.get('content-disposition');
75
+ let fileName: string;
76
+ if (disposition) {
77
+ const result = disposition.match(/(?:.*filename\*|filename)=(?:([^'"]*)''|("))([^;]+)\2(?:[;`\n]|$)/);
78
+ if (result && result[3]) {
79
+ fileName = decodeURIComponent(result[3].replace(/['"]/g, '').replace(/[+]/g, '%20'));
80
+ }
81
+ }
82
+ // 兼容文件类型服务编排出参字段
83
+ Object.defineProperty(resBlob, 'fileName', {
84
+ get() {
85
+ return fileName;
86
+ },
87
+ });
88
+ Object.defineProperty(resBlob, 'fileContent', {
89
+ get() {
90
+ return resBlob;
91
+ },
92
+ });
93
+
94
+ return resBlob;
95
+ });
96
+ }
97
+ }
45
98
  if (data.resultCode === '0') {
46
99
  const successData = data.resultData || data.resultObject;
47
100
  // 如果resultObject中有错误码抛出异常
48
- if (successData && successData.error && typeof successData.status === 'number' && isErrorStatus(successData.status)) {
101
+ if (
102
+ successData &&
103
+ successData.error &&
104
+ typeof successData.status === 'number' &&
105
+ isErrorStatus(successData.status)
106
+ ) {
49
107
  // eslint-disable-next-line prefer-promise-reject-errors
50
108
  return Promise.reject({ response: successData });
51
109
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lingxiteam/ebe-utils",
3
- "version": "0.2.15",
3
+ "version": "0.2.17",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "author": "",
@@ -19,7 +19,7 @@
19
19
  "@babel/types": "^7.12.12",
20
20
  "cac": "^6.7.14",
21
21
  "fs-extra": "9.x",
22
- "@lingxiteam/ebe": "0.2.15"
22
+ "@lingxiteam/ebe": "0.2.17"
23
23
  },
24
24
  "publishConfig": {
25
25
  "access": "public"