@alibarbar/common 1.1.2 → 1.1.4

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.
package/dist/services.cjs CHANGED
@@ -11,6 +11,48 @@ var Qs__default = /*#__PURE__*/_interopDefault(Qs);
11
11
  var axios__default = /*#__PURE__*/_interopDefault(axios);
12
12
 
13
13
  // src/services/index.ts
14
+ function convertRes2Blob(response) {
15
+ const contentDisposition = response.headers["content-disposition"];
16
+ let fileName = "file.xlsx";
17
+ if (contentDisposition) {
18
+ const rfc5987Match = contentDisposition.match(/filename\*=UTF-8''([^;]+)/i);
19
+ if (rfc5987Match) {
20
+ fileName = decodeURIComponent(rfc5987Match[1]);
21
+ } else {
22
+ const standardMatch = contentDisposition.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/i);
23
+ if (standardMatch) {
24
+ fileName = standardMatch[1];
25
+ fileName = fileName.replace(/^['"]|['"]$/g, "");
26
+ try {
27
+ fileName = decodeURIComponent(fileName);
28
+ } catch (e) {
29
+ try {
30
+ fileName = decodeURI(fileName);
31
+ } catch (e2) {
32
+ }
33
+ }
34
+ }
35
+ }
36
+ fileName = fileName.trim().replace(/^_+|_+$/g, "");
37
+ }
38
+ const blob = new Blob([response.data], { type: "application/vnd.ms-excel;charset=utf-8" });
39
+ if (typeof window.navigator.msSaveBlob !== "undefined") {
40
+ window.navigator.msSaveBlob(blob, fileName);
41
+ } else {
42
+ const blobURL = window.URL.createObjectURL(blob);
43
+ const tempLink = document.createElement("a");
44
+ tempLink.style.display = "none";
45
+ tempLink.href = blobURL;
46
+ tempLink.setAttribute("download", fileName);
47
+ if (typeof tempLink.download === "undefined") {
48
+ tempLink.setAttribute("target", "_blank");
49
+ }
50
+ document.body.appendChild(tempLink);
51
+ tempLink.click();
52
+ document.body.removeChild(tempLink);
53
+ window.URL.revokeObjectURL(blobURL);
54
+ }
55
+ }
14
56
  var serviceEventHandlers = {
15
57
  message: null
16
58
  };
@@ -44,11 +86,28 @@ function filterEmptyKey(params, emptyString) {
44
86
  }
45
87
  });
46
88
  }
89
+ function isFileUpload(data) {
90
+ if (!data) return false;
91
+ if (data instanceof FormData) return true;
92
+ if (data instanceof File) return true;
93
+ if (data instanceof Blob) return true;
94
+ if (data instanceof ArrayBuffer) return true;
95
+ if (typeof data === "object" && data !== null && !Array.isArray(data)) {
96
+ const obj = data;
97
+ return Object.values(obj).some(
98
+ (value) => value instanceof File || value instanceof Blob || value instanceof FormData
99
+ );
100
+ }
101
+ return false;
102
+ }
47
103
  function getTransformRequest(contentType) {
48
104
  if (!contentType) {
49
105
  return (data) => Qs__default.default.stringify(data);
50
106
  }
51
107
  const normalizedType = contentType.toLowerCase().trim();
108
+ if (normalizedType.startsWith("multipart/form-data")) {
109
+ return void 0;
110
+ }
52
111
  if (normalizedType.startsWith("application/json")) {
53
112
  return (data) => JSON.stringify(data);
54
113
  }
@@ -69,6 +128,15 @@ var service = axios__default.default.create({
69
128
  });
70
129
  service.interceptors.request.use(
71
130
  async (config) => {
131
+ const isFileUploadRequest = isFileUpload(config.data);
132
+ if (isFileUploadRequest) {
133
+ if (config.headers) {
134
+ delete config.headers["Content-Type"];
135
+ delete config.headers["content-type"];
136
+ }
137
+ config.transformRequest = void 0;
138
+ return config;
139
+ }
72
140
  const contentType = config.headers?.["Content-Type"] || config.headers?.["content-type"];
73
141
  if (!config.transformRequest && contentType) {
74
142
  const transformRequest = getTransformRequest(
@@ -81,9 +149,11 @@ service.interceptors.request.use(
81
149
  config.transformRequest = (data) => Qs__default.default.stringify(data);
82
150
  }
83
151
  if (config.method === "post" || config.method === "put" || config.method === "patch") {
84
- const params = { ...config.data || {} };
85
- filterEmptyKey(params, config.emptyParams ?? false);
86
- config.data = params;
152
+ if (config.data && typeof config.data === "object" && !isFileUploadRequest) {
153
+ const params = { ...config.data || {} };
154
+ filterEmptyKey(params, config.emptyParams ?? false);
155
+ config.data = params;
156
+ }
87
157
  } else if (config.method === "get" || config.method === "delete") {
88
158
  if (!config.disableTimestamp) {
89
159
  config.params = {
@@ -167,19 +237,7 @@ service.interceptors.response.use(
167
237
  if (status === 401) {
168
238
  handle401Error(config, error);
169
239
  }
170
- if (isAborted && config && !config._noAutoRetry) {
171
- config._retryCount = config._retryCount || 0;
172
- if (config._retryCount < 2) {
173
- config._retryCount++;
174
- console.debug(
175
- `[request] aborted, retry attempt #${config._retryCount} -> ${config.url || ""}`
176
- );
177
- return new Promise((resolve) => setTimeout(resolve, 300)).then(
178
- () => service.request(config)
179
- );
180
- }
181
- }
182
- if (!config?.hidden && !isAborted) {
240
+ if (!config?.hidden && !isAborted && status !== 401) {
183
241
  let friendly = messageText;
184
242
  if (status === 403) friendly = "No permission";
185
243
  else if (status === 502) friendly = "System is upgrading or unavailable";
@@ -205,7 +263,7 @@ service.json = function(url, params, config) {
205
263
  };
206
264
  return service.post(
207
265
  url,
208
- Array.isArray(params) ? params : params,
266
+ params,
209
267
  newConfig
210
268
  );
211
269
  };
@@ -214,7 +272,13 @@ service.put = function(url, params, config) {
214
272
  headers: { "Content-Type": "application/json", ...config?.headers },
215
273
  ...config
216
274
  };
217
- return service.put(url, Array.isArray(params) ? params : params, newConfig).then((res) => res);
275
+ const data = Array.isArray(params) ? params : params;
276
+ return service.request({
277
+ url,
278
+ method: "put",
279
+ data,
280
+ ...newConfig
281
+ }).then((res) => res);
218
282
  };
219
283
  service.arrayGet = function(url, config) {
220
284
  if (config) {
@@ -243,48 +307,6 @@ service.download = function(url, config = {}) {
243
307
  return service.post(url, params, newConfig).then(() => void 0);
244
308
  }
245
309
  };
246
- function convertRes2Blob(response) {
247
- const contentDisposition = response.headers["content-disposition"];
248
- let fileName = "file.xlsx";
249
- if (contentDisposition) {
250
- const rfc5987Match = contentDisposition.match(/filename\*=UTF-8''([^;]+)/i);
251
- if (rfc5987Match) {
252
- fileName = decodeURIComponent(rfc5987Match[1]);
253
- } else {
254
- const standardMatch = contentDisposition.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/i);
255
- if (standardMatch) {
256
- fileName = standardMatch[1];
257
- fileName = fileName.replace(/^['"]|['"]$/g, "");
258
- try {
259
- fileName = decodeURIComponent(fileName);
260
- } catch (e) {
261
- try {
262
- fileName = decodeURI(fileName);
263
- } catch (e2) {
264
- }
265
- }
266
- }
267
- }
268
- fileName = fileName.trim().replace(/^_+|_+$/g, "");
269
- }
270
- const blob = new Blob([response.data], { type: "application/vnd.ms-excel;charset=utf-8" });
271
- if (typeof window.navigator.msSaveBlob !== "undefined") {
272
- window.navigator.msSaveBlob(blob, fileName);
273
- } else {
274
- const blobURL = window.URL.createObjectURL(blob);
275
- const tempLink = document.createElement("a");
276
- tempLink.style.display = "none";
277
- tempLink.href = blobURL;
278
- tempLink.setAttribute("download", fileName);
279
- if (typeof tempLink.download === "undefined") {
280
- tempLink.setAttribute("target", "_blank");
281
- }
282
- document.body.appendChild(tempLink);
283
- tempLink.click();
284
- document.body.removeChild(tempLink);
285
- window.URL.revokeObjectURL(blobURL);
286
- }
287
- }
288
310
  var services_default = service;
289
311
 
290
312
  exports.default = services_default;
@@ -84,7 +84,7 @@ interface ServiceError {
84
84
  * @param handlers - 事件处理器配置
85
85
  */
86
86
  declare function setServiceEventHandlers(handlers?: Partial<ServiceEventHandlers>): void;
87
- declare const service: AxiosInstance & ServiceInstance;
87
+ declare const service: ServiceInstance;
88
88
  /**
89
89
  * Service 实例扩展接口
90
90
  */
@@ -84,7 +84,7 @@ interface ServiceError {
84
84
  * @param handlers - 事件处理器配置
85
85
  */
86
86
  declare function setServiceEventHandlers(handlers?: Partial<ServiceEventHandlers>): void;
87
- declare const service: AxiosInstance & ServiceInstance;
87
+ declare const service: ServiceInstance;
88
88
  /**
89
89
  * Service 实例扩展接口
90
90
  */
package/dist/services.js CHANGED
@@ -2,6 +2,48 @@ import Qs from 'qs';
2
2
  import axios from 'axios';
3
3
 
4
4
  // src/services/index.ts
5
+ function convertRes2Blob(response) {
6
+ const contentDisposition = response.headers["content-disposition"];
7
+ let fileName = "file.xlsx";
8
+ if (contentDisposition) {
9
+ const rfc5987Match = contentDisposition.match(/filename\*=UTF-8''([^;]+)/i);
10
+ if (rfc5987Match) {
11
+ fileName = decodeURIComponent(rfc5987Match[1]);
12
+ } else {
13
+ const standardMatch = contentDisposition.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/i);
14
+ if (standardMatch) {
15
+ fileName = standardMatch[1];
16
+ fileName = fileName.replace(/^['"]|['"]$/g, "");
17
+ try {
18
+ fileName = decodeURIComponent(fileName);
19
+ } catch (e) {
20
+ try {
21
+ fileName = decodeURI(fileName);
22
+ } catch (e2) {
23
+ }
24
+ }
25
+ }
26
+ }
27
+ fileName = fileName.trim().replace(/^_+|_+$/g, "");
28
+ }
29
+ const blob = new Blob([response.data], { type: "application/vnd.ms-excel;charset=utf-8" });
30
+ if (typeof window.navigator.msSaveBlob !== "undefined") {
31
+ window.navigator.msSaveBlob(blob, fileName);
32
+ } else {
33
+ const blobURL = window.URL.createObjectURL(blob);
34
+ const tempLink = document.createElement("a");
35
+ tempLink.style.display = "none";
36
+ tempLink.href = blobURL;
37
+ tempLink.setAttribute("download", fileName);
38
+ if (typeof tempLink.download === "undefined") {
39
+ tempLink.setAttribute("target", "_blank");
40
+ }
41
+ document.body.appendChild(tempLink);
42
+ tempLink.click();
43
+ document.body.removeChild(tempLink);
44
+ window.URL.revokeObjectURL(blobURL);
45
+ }
46
+ }
5
47
  var serviceEventHandlers = {
6
48
  message: null
7
49
  };
@@ -35,11 +77,28 @@ function filterEmptyKey(params, emptyString) {
35
77
  }
36
78
  });
37
79
  }
80
+ function isFileUpload(data) {
81
+ if (!data) return false;
82
+ if (data instanceof FormData) return true;
83
+ if (data instanceof File) return true;
84
+ if (data instanceof Blob) return true;
85
+ if (data instanceof ArrayBuffer) return true;
86
+ if (typeof data === "object" && data !== null && !Array.isArray(data)) {
87
+ const obj = data;
88
+ return Object.values(obj).some(
89
+ (value) => value instanceof File || value instanceof Blob || value instanceof FormData
90
+ );
91
+ }
92
+ return false;
93
+ }
38
94
  function getTransformRequest(contentType) {
39
95
  if (!contentType) {
40
96
  return (data) => Qs.stringify(data);
41
97
  }
42
98
  const normalizedType = contentType.toLowerCase().trim();
99
+ if (normalizedType.startsWith("multipart/form-data")) {
100
+ return void 0;
101
+ }
43
102
  if (normalizedType.startsWith("application/json")) {
44
103
  return (data) => JSON.stringify(data);
45
104
  }
@@ -60,6 +119,15 @@ var service = axios.create({
60
119
  });
61
120
  service.interceptors.request.use(
62
121
  async (config) => {
122
+ const isFileUploadRequest = isFileUpload(config.data);
123
+ if (isFileUploadRequest) {
124
+ if (config.headers) {
125
+ delete config.headers["Content-Type"];
126
+ delete config.headers["content-type"];
127
+ }
128
+ config.transformRequest = void 0;
129
+ return config;
130
+ }
63
131
  const contentType = config.headers?.["Content-Type"] || config.headers?.["content-type"];
64
132
  if (!config.transformRequest && contentType) {
65
133
  const transformRequest = getTransformRequest(
@@ -72,9 +140,11 @@ service.interceptors.request.use(
72
140
  config.transformRequest = (data) => Qs.stringify(data);
73
141
  }
74
142
  if (config.method === "post" || config.method === "put" || config.method === "patch") {
75
- const params = { ...config.data || {} };
76
- filterEmptyKey(params, config.emptyParams ?? false);
77
- config.data = params;
143
+ if (config.data && typeof config.data === "object" && !isFileUploadRequest) {
144
+ const params = { ...config.data || {} };
145
+ filterEmptyKey(params, config.emptyParams ?? false);
146
+ config.data = params;
147
+ }
78
148
  } else if (config.method === "get" || config.method === "delete") {
79
149
  if (!config.disableTimestamp) {
80
150
  config.params = {
@@ -158,19 +228,7 @@ service.interceptors.response.use(
158
228
  if (status === 401) {
159
229
  handle401Error(config, error);
160
230
  }
161
- if (isAborted && config && !config._noAutoRetry) {
162
- config._retryCount = config._retryCount || 0;
163
- if (config._retryCount < 2) {
164
- config._retryCount++;
165
- console.debug(
166
- `[request] aborted, retry attempt #${config._retryCount} -> ${config.url || ""}`
167
- );
168
- return new Promise((resolve) => setTimeout(resolve, 300)).then(
169
- () => service.request(config)
170
- );
171
- }
172
- }
173
- if (!config?.hidden && !isAborted) {
231
+ if (!config?.hidden && !isAborted && status !== 401) {
174
232
  let friendly = messageText;
175
233
  if (status === 403) friendly = "No permission";
176
234
  else if (status === 502) friendly = "System is upgrading or unavailable";
@@ -196,7 +254,7 @@ service.json = function(url, params, config) {
196
254
  };
197
255
  return service.post(
198
256
  url,
199
- Array.isArray(params) ? params : params,
257
+ params,
200
258
  newConfig
201
259
  );
202
260
  };
@@ -205,7 +263,13 @@ service.put = function(url, params, config) {
205
263
  headers: { "Content-Type": "application/json", ...config?.headers },
206
264
  ...config
207
265
  };
208
- return service.put(url, Array.isArray(params) ? params : params, newConfig).then((res) => res);
266
+ const data = Array.isArray(params) ? params : params;
267
+ return service.request({
268
+ url,
269
+ method: "put",
270
+ data,
271
+ ...newConfig
272
+ }).then((res) => res);
209
273
  };
210
274
  service.arrayGet = function(url, config) {
211
275
  if (config) {
@@ -234,48 +298,6 @@ service.download = function(url, config = {}) {
234
298
  return service.post(url, params, newConfig).then(() => void 0);
235
299
  }
236
300
  };
237
- function convertRes2Blob(response) {
238
- const contentDisposition = response.headers["content-disposition"];
239
- let fileName = "file.xlsx";
240
- if (contentDisposition) {
241
- const rfc5987Match = contentDisposition.match(/filename\*=UTF-8''([^;]+)/i);
242
- if (rfc5987Match) {
243
- fileName = decodeURIComponent(rfc5987Match[1]);
244
- } else {
245
- const standardMatch = contentDisposition.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/i);
246
- if (standardMatch) {
247
- fileName = standardMatch[1];
248
- fileName = fileName.replace(/^['"]|['"]$/g, "");
249
- try {
250
- fileName = decodeURIComponent(fileName);
251
- } catch (e) {
252
- try {
253
- fileName = decodeURI(fileName);
254
- } catch (e2) {
255
- }
256
- }
257
- }
258
- }
259
- fileName = fileName.trim().replace(/^_+|_+$/g, "");
260
- }
261
- const blob = new Blob([response.data], { type: "application/vnd.ms-excel;charset=utf-8" });
262
- if (typeof window.navigator.msSaveBlob !== "undefined") {
263
- window.navigator.msSaveBlob(blob, fileName);
264
- } else {
265
- const blobURL = window.URL.createObjectURL(blob);
266
- const tempLink = document.createElement("a");
267
- tempLink.style.display = "none";
268
- tempLink.href = blobURL;
269
- tempLink.setAttribute("download", fileName);
270
- if (typeof tempLink.download === "undefined") {
271
- tempLink.setAttribute("target", "_blank");
272
- }
273
- document.body.appendChild(tempLink);
274
- tempLink.click();
275
- document.body.removeChild(tempLink);
276
- window.URL.revokeObjectURL(blobURL);
277
- }
278
- }
279
301
  var services_default = service;
280
302
 
281
303
  export { services_default as default, setServiceEventHandlers };