@alibarbar/common 1.0.8 → 1.0.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.
Files changed (176) hide show
  1. package/dist/{chunk-O3O67R4I.js → algorithm.cjs} +0 -2
  2. package/dist/algorithm.js +130 -44
  3. package/dist/{chunk-LOAZSPGQ.js → array.cjs} +0 -2
  4. package/dist/array.js +135 -84
  5. package/dist/{chunk-Y364QIQH.js → color.cjs} +0 -2
  6. package/dist/color.js +127 -40
  7. package/dist/{chunk-DYBSRI7V.js → crypto.cjs} +0 -2
  8. package/dist/crypto.js +172 -60
  9. package/dist/{chunk-BHCRFURU.js → data-structure.cjs} +0 -2
  10. package/dist/data-structure.js +481 -32
  11. package/dist/{chunk-I3L42475.js → date.cjs} +0 -2
  12. package/dist/date.js +125 -72
  13. package/dist/{chunk-JK2SE3I2.js → dom.cjs} +0 -2
  14. package/dist/dom.js +84 -56
  15. package/dist/{chunk-7V5UQXIO.js → file.cjs} +0 -2
  16. package/dist/file.js +79 -32
  17. package/dist/{chunk-OX5PLOWB.js → i18n.cjs} +0 -2
  18. package/dist/i18n.js +78 -40
  19. package/dist/index.cjs +4132 -0
  20. package/dist/index.d.mts +3 -159
  21. package/dist/index.d.ts +3 -159
  22. package/dist/index.js +3364 -17427
  23. package/dist/{chunk-4RGXV4SJ.js → network.cjs} +0 -2
  24. package/dist/network.js +97 -28
  25. package/dist/{chunk-3FRYJPHG.js → number.cjs} +0 -2
  26. package/dist/number.js +77 -56
  27. package/dist/{chunk-27UDDVLZ.js → object.cjs} +0 -2
  28. package/dist/object.js +237 -80
  29. package/dist/{chunk-JQZBPAPO.js → performance.cjs} +0 -2
  30. package/dist/performance.js +145 -40
  31. package/dist/services.cjs +291 -0
  32. package/dist/services.d.mts +161 -0
  33. package/dist/services.d.ts +161 -0
  34. package/dist/services.js +281 -0
  35. package/dist/{chunk-WZDOPUJW.js → storage.cjs} +216 -17
  36. package/dist/storage.js +550 -31
  37. package/dist/{chunk-D7CS5EKF.js → string.cjs} +0 -2
  38. package/dist/string.js +91 -68
  39. package/dist/{chunk-56W6YECK.js → tracking.cjs} +0 -2
  40. package/dist/tracking.js +359 -52
  41. package/dist/{chunk-ZDMFMUDR.js → transform.cjs} +0 -2
  42. package/dist/transform.js +299 -32
  43. package/dist/{chunk-NJARVI6X.mjs → upload.cjs} +41 -14
  44. package/dist/upload.js +402 -20
  45. package/dist/{chunk-KGFTD255.js → url.cjs} +0 -2
  46. package/dist/url.js +91 -44
  47. package/dist/{chunk-TQN37HIN.js → validation.cjs} +0 -2
  48. package/dist/validation.js +77 -60
  49. package/package.json +12 -2
  50. package/dist/algorithm.js.map +0 -1
  51. package/dist/algorithm.mjs +0 -4
  52. package/dist/algorithm.mjs.map +0 -1
  53. package/dist/array.js.map +0 -1
  54. package/dist/array.mjs +0 -4
  55. package/dist/array.mjs.map +0 -1
  56. package/dist/chunk-27UDDVLZ.js.map +0 -1
  57. package/dist/chunk-2R2QWFJC.mjs +0 -138
  58. package/dist/chunk-2R2QWFJC.mjs.map +0 -1
  59. package/dist/chunk-3FRYJPHG.js.map +0 -1
  60. package/dist/chunk-4RGXV4SJ.js.map +0 -1
  61. package/dist/chunk-56W6YECK.js.map +0 -1
  62. package/dist/chunk-5BGSUGTI.mjs +0 -128
  63. package/dist/chunk-5BGSUGTI.mjs.map +0 -1
  64. package/dist/chunk-7E6GELHJ.mjs +0 -302
  65. package/dist/chunk-7E6GELHJ.mjs.map +0 -1
  66. package/dist/chunk-7V5UQXIO.js.map +0 -1
  67. package/dist/chunk-A4SWQXX7.mjs +0 -484
  68. package/dist/chunk-A4SWQXX7.mjs.map +0 -1
  69. package/dist/chunk-BHCRFURU.js.map +0 -1
  70. package/dist/chunk-CDSGEAOK.mjs +0 -80
  71. package/dist/chunk-CDSGEAOK.mjs.map +0 -1
  72. package/dist/chunk-D7CS5EKF.js.map +0 -1
  73. package/dist/chunk-DYBSRI7V.js.map +0 -1
  74. package/dist/chunk-FEBKPX5A.js +0 -386
  75. package/dist/chunk-FEBKPX5A.js.map +0 -1
  76. package/dist/chunk-FJ6ZGZIA.mjs +0 -39
  77. package/dist/chunk-FJ6ZGZIA.mjs.map +0 -1
  78. package/dist/chunk-HLDFI7R2.mjs +0 -175
  79. package/dist/chunk-HLDFI7R2.mjs.map +0 -1
  80. package/dist/chunk-I3L42475.js.map +0 -1
  81. package/dist/chunk-JBLX27WD.mjs +0 -240
  82. package/dist/chunk-JBLX27WD.mjs.map +0 -1
  83. package/dist/chunk-JHZ7M2MR.mjs +0 -133
  84. package/dist/chunk-JHZ7M2MR.mjs.map +0 -1
  85. package/dist/chunk-JK2SE3I2.js.map +0 -1
  86. package/dist/chunk-JQZBPAPO.js.map +0 -1
  87. package/dist/chunk-JXYGC2C5.mjs +0 -100
  88. package/dist/chunk-JXYGC2C5.mjs.map +0 -1
  89. package/dist/chunk-KGFTD255.js.map +0 -1
  90. package/dist/chunk-LBHBNPNJ.mjs +0 -148
  91. package/dist/chunk-LBHBNPNJ.mjs.map +0 -1
  92. package/dist/chunk-LF4CILQS.mjs +0 -87
  93. package/dist/chunk-LF4CILQS.mjs.map +0 -1
  94. package/dist/chunk-LOAZSPGQ.js.map +0 -1
  95. package/dist/chunk-NJARVI6X.mjs.map +0 -1
  96. package/dist/chunk-NSSDYX2U.mjs +0 -80
  97. package/dist/chunk-NSSDYX2U.mjs.map +0 -1
  98. package/dist/chunk-O3O67R4I.js.map +0 -1
  99. package/dist/chunk-OIXQ3E6W.mjs +0 -354
  100. package/dist/chunk-OIXQ3E6W.mjs.map +0 -1
  101. package/dist/chunk-OX5PLOWB.js.map +0 -1
  102. package/dist/chunk-PJ7UCTX4.mjs +0 -362
  103. package/dist/chunk-PJ7UCTX4.mjs.map +0 -1
  104. package/dist/chunk-PR4QN5HX.js +0 -44
  105. package/dist/chunk-PR4QN5HX.js.map +0 -1
  106. package/dist/chunk-QIBE7GVN.mjs +0 -81
  107. package/dist/chunk-QIBE7GVN.mjs.map +0 -1
  108. package/dist/chunk-QIOC54LQ.mjs +0 -130
  109. package/dist/chunk-QIOC54LQ.mjs.map +0 -1
  110. package/dist/chunk-TQN37HIN.js.map +0 -1
  111. package/dist/chunk-WZDOPUJW.js.map +0 -1
  112. package/dist/chunk-XJTZDXSR.mjs +0 -94
  113. package/dist/chunk-XJTZDXSR.mjs.map +0 -1
  114. package/dist/chunk-Y364QIQH.js.map +0 -1
  115. package/dist/chunk-YXM6Q4JS.mjs +0 -94
  116. package/dist/chunk-YXM6Q4JS.mjs.map +0 -1
  117. package/dist/chunk-ZDMFMUDR.js.map +0 -1
  118. package/dist/chunk-ZVJ6NQUM.mjs +0 -82
  119. package/dist/chunk-ZVJ6NQUM.mjs.map +0 -1
  120. package/dist/color.js.map +0 -1
  121. package/dist/color.mjs +0 -4
  122. package/dist/color.mjs.map +0 -1
  123. package/dist/crypto.js.map +0 -1
  124. package/dist/crypto.mjs +0 -4
  125. package/dist/crypto.mjs.map +0 -1
  126. package/dist/data-structure.js.map +0 -1
  127. package/dist/data-structure.mjs +0 -4
  128. package/dist/data-structure.mjs.map +0 -1
  129. package/dist/date.js.map +0 -1
  130. package/dist/date.mjs +0 -4
  131. package/dist/date.mjs.map +0 -1
  132. package/dist/dom.js.map +0 -1
  133. package/dist/dom.mjs +0 -4
  134. package/dist/dom.mjs.map +0 -1
  135. package/dist/file.js.map +0 -1
  136. package/dist/file.mjs +0 -4
  137. package/dist/file.mjs.map +0 -1
  138. package/dist/i18n.js.map +0 -1
  139. package/dist/i18n.mjs +0 -4
  140. package/dist/i18n.mjs.map +0 -1
  141. package/dist/index.js.map +0 -1
  142. package/dist/index.mjs +0 -17155
  143. package/dist/index.mjs.map +0 -1
  144. package/dist/network.js.map +0 -1
  145. package/dist/network.mjs +0 -4
  146. package/dist/network.mjs.map +0 -1
  147. package/dist/number.js.map +0 -1
  148. package/dist/number.mjs +0 -4
  149. package/dist/number.mjs.map +0 -1
  150. package/dist/object.js.map +0 -1
  151. package/dist/object.mjs +0 -4
  152. package/dist/object.mjs.map +0 -1
  153. package/dist/performance.js.map +0 -1
  154. package/dist/performance.mjs +0 -4
  155. package/dist/performance.mjs.map +0 -1
  156. package/dist/storage.js.map +0 -1
  157. package/dist/storage.mjs +0 -5
  158. package/dist/storage.mjs.map +0 -1
  159. package/dist/string.js.map +0 -1
  160. package/dist/string.mjs +0 -4
  161. package/dist/string.mjs.map +0 -1
  162. package/dist/tracking.js.map +0 -1
  163. package/dist/tracking.mjs +0 -4
  164. package/dist/tracking.mjs.map +0 -1
  165. package/dist/transform.js.map +0 -1
  166. package/dist/transform.mjs +0 -4
  167. package/dist/transform.mjs.map +0 -1
  168. package/dist/upload.js.map +0 -1
  169. package/dist/upload.mjs +0 -5
  170. package/dist/upload.mjs.map +0 -1
  171. package/dist/url.js.map +0 -1
  172. package/dist/url.mjs +0 -4
  173. package/dist/url.mjs.map +0 -1
  174. package/dist/validation.js.map +0 -1
  175. package/dist/validation.mjs +0 -4
  176. package/dist/validation.mjs.map +0 -1
@@ -0,0 +1,161 @@
1
+ import { AxiosRequestConfig, AxiosInstance } from 'axios';
2
+
3
+ /**
4
+ * 扩展 Navigator 接口以支持 msSaveBlob
5
+ */
6
+ declare global {
7
+ interface Navigator {
8
+ msSaveBlob?: (blob: Blob, fileName: string) => boolean;
9
+ }
10
+ }
11
+ /**
12
+ * 消息事件类型
13
+ */
14
+ type MessageEventType = 'info' | 'success' | 'warn' | 'warning' | 'error';
15
+ /**
16
+ * 消息事件载荷
17
+ */
18
+ interface MessageEventPayload {
19
+ /** 消息类型 */
20
+ type: MessageEventType;
21
+ /** 消息内容 */
22
+ message: string;
23
+ /** 错误代码(可选) */
24
+ code?: number;
25
+ /** HTTP 状态码(可选) */
26
+ status?: number;
27
+ /** 请求配置(可选) */
28
+ config?: AxiosRequestConfig;
29
+ /** 错误对象(可选) */
30
+ error?: unknown;
31
+ }
32
+ /**
33
+ * 消息事件处理器
34
+ */
35
+ type MessageEventHandler = (payload: MessageEventPayload) => void;
36
+ /**
37
+ * 服务事件处理器配置
38
+ */
39
+ interface ServiceEventHandlers {
40
+ /** 统一消息/错误事件处理器 */
41
+ message?: MessageEventHandler | null;
42
+ }
43
+ /**
44
+ * 扩展的 Axios 请求配置
45
+ * 只声明扩展的属性,继承的属性不需要重复声明
46
+ */
47
+ interface ExtendedAxiosRequestConfig extends AxiosRequestConfig {
48
+ /** 是否隐藏错误提示 */
49
+ hidden?: boolean;
50
+ /** 操作名称(用于错误提示) */
51
+ action?: string;
52
+ /** 是否清空空参数 */
53
+ emptyParams?: boolean;
54
+ /** 是否为文件下载 */
55
+ file?: boolean;
56
+ /** 重试计数(内部使用) */
57
+ _retryCount?: number;
58
+ /** 是否禁用自动重试 */
59
+ _noAutoRetry?: boolean;
60
+ /** 是否禁用自动添加时间戳(仅对 GET 请求有效) */
61
+ disableTimestamp?: boolean;
62
+ }
63
+ /**
64
+ * 服务响应数据类型
65
+ */
66
+ interface ServiceResponseData<T = unknown> {
67
+ success?: boolean | string;
68
+ code?: number;
69
+ errorCode?: number;
70
+ errorMessage?: string;
71
+ message?: string;
72
+ msg?: string;
73
+ data?: T;
74
+ }
75
+ /**
76
+ * 错误响应类型
77
+ */
78
+ interface ServiceError {
79
+ code: number;
80
+ message: string;
81
+ }
82
+ /**
83
+ * 设置 service 的事件处理函数
84
+ * @param handlers - 事件处理器配置
85
+ */
86
+ declare function setServiceEventHandlers(handlers?: Partial<ServiceEventHandlers>): void;
87
+ declare const service: AxiosInstance & ServiceInstance;
88
+ /**
89
+ * Service 实例扩展接口
90
+ */
91
+ interface ServiceInstance extends AxiosInstance {
92
+ /**
93
+ * GET 请求
94
+ * @param url - 请求 URL
95
+ * @param config - 请求配置
96
+ */
97
+ get<TResponse = unknown>(url: string, config?: ExtendedAxiosRequestConfig): Promise<TResponse>;
98
+ /**
99
+ * POST 请求
100
+ * @param url - 请求 URL
101
+ * @param data - 请求数据
102
+ * @param config - 请求配置
103
+ */
104
+ post<TResponse = unknown, TData = unknown>(url: string, data?: TData, config?: ExtendedAxiosRequestConfig): Promise<TResponse>;
105
+ /**
106
+ * DELETE 请求
107
+ * @param url - 请求 URL
108
+ * @param config - 请求配置
109
+ */
110
+ delete<TResponse = unknown>(url: string, config?: ExtendedAxiosRequestConfig): Promise<TResponse>;
111
+ /**
112
+ * PATCH 请求
113
+ * @param url - 请求 URL
114
+ * @param data - 请求数据
115
+ * @param config - 请求配置
116
+ */
117
+ patch<TResponse = unknown, TData = unknown>(url: string, data?: TData, config?: ExtendedAxiosRequestConfig): Promise<TResponse>;
118
+ /**
119
+ * HEAD 请求
120
+ * @param url - 请求 URL
121
+ * @param config - 请求配置
122
+ */
123
+ head<TResponse = unknown>(url: string, config?: ExtendedAxiosRequestConfig): Promise<TResponse>;
124
+ /**
125
+ * OPTIONS 请求
126
+ * @param url - 请求 URL
127
+ * @param config - 请求配置
128
+ */
129
+ options<TResponse = unknown>(url: string, config?: ExtendedAxiosRequestConfig): Promise<TResponse>;
130
+ /**
131
+ * JSON 格式 POST 请求
132
+ * @param url - 请求 URL
133
+ * @param params - 请求参数
134
+ * @param config - 请求配置
135
+ */
136
+ json<TResponse = unknown, TParams = unknown>(url: string, params?: TParams | TParams[], config?: ExtendedAxiosRequestConfig): Promise<TResponse>;
137
+ /**
138
+ * PUT 请求(JSON 格式)
139
+ * @param url - 请求 URL
140
+ * @param params - 请求参数
141
+ * @param config - 请求配置
142
+ */
143
+ put<TResponse = unknown, TParams = unknown>(url: string, params?: TParams | TParams[], config?: ExtendedAxiosRequestConfig): Promise<TResponse>;
144
+ /**
145
+ * 数组格式 GET 请求
146
+ * @param url - 请求 URL
147
+ * @param config - 请求配置
148
+ */
149
+ arrayGet<TResponse = unknown>(url: string, config?: ExtendedAxiosRequestConfig): Promise<TResponse>;
150
+ /**
151
+ * 文件下载
152
+ * @param url - 下载 URL
153
+ * @param config - 下载配置
154
+ */
155
+ download<TParams = unknown>(url: string, config?: {
156
+ type?: 'get' | 'json';
157
+ params?: TParams;
158
+ } & ExtendedAxiosRequestConfig): Promise<void>;
159
+ }
160
+
161
+ export { type ExtendedAxiosRequestConfig, type MessageEventHandler, type MessageEventPayload, type MessageEventType, type ServiceError, type ServiceEventHandlers, type ServiceInstance, type ServiceResponseData, service as default, setServiceEventHandlers };
@@ -0,0 +1,281 @@
1
+ import Qs from 'qs';
2
+ import axios from 'axios';
3
+
4
+ // src/services/index.ts
5
+ var serviceEventHandlers = {
6
+ message: null
7
+ };
8
+ function setServiceEventHandlers(handlers = {}) {
9
+ Object.assign(serviceEventHandlers, handlers);
10
+ }
11
+ function emitMessageEvent(payload) {
12
+ const handler = serviceEventHandlers.message;
13
+ if (typeof handler === "function") {
14
+ handler(payload);
15
+ return;
16
+ }
17
+ const { type = "error", message, code, status } = payload;
18
+ const prefix = `[service][${type}]`;
19
+ const extra = [];
20
+ if (typeof code !== "undefined") extra.push(`code=${code}`);
21
+ if (typeof status !== "undefined") extra.push(`status=${status}`);
22
+ const suffix = extra.length ? ` (${extra.join(", ")})` : "";
23
+ if (type === "info" || type === "success") {
24
+ console.info(`${prefix} ${message}${suffix}`);
25
+ } else if (type === "warn" || type === "warning") {
26
+ console.warn(`${prefix} ${message}${suffix}`);
27
+ } else {
28
+ console.error(`${prefix} ${message}${suffix}`);
29
+ }
30
+ }
31
+ function filterEmptyKey(params, emptyString) {
32
+ Object.keys(params).forEach((key) => {
33
+ if (params[key] === null || emptyString && params[key] === "" || emptyString && Array.isArray(params[key]) && params[key].length <= 0) {
34
+ delete params[key];
35
+ }
36
+ });
37
+ }
38
+ function getTransformRequest(contentType) {
39
+ if (!contentType) {
40
+ return (data) => Qs.stringify(data);
41
+ }
42
+ const normalizedType = contentType.toLowerCase().trim();
43
+ if (normalizedType.startsWith("application/json")) {
44
+ return (data) => JSON.stringify(data);
45
+ }
46
+ if (normalizedType.startsWith("application/x-www-form-urlencoded")) {
47
+ return (data) => Qs.stringify(data);
48
+ }
49
+ return void 0;
50
+ }
51
+ var service = axios.create({
52
+ baseURL: "",
53
+ timeout: 6e4,
54
+ // 不设置默认 transformRequest,根据 Content-Type 自动选择
55
+ headers: {
56
+ "Cache-Control": "no-cache",
57
+ "Content-Type": "application/x-www-form-urlencoded",
58
+ "X-Requested-With": "XMLHttpRequest"
59
+ }
60
+ });
61
+ service.interceptors.request.use(
62
+ async (config) => {
63
+ const contentType = config.headers?.["Content-Type"] || config.headers?.["content-type"];
64
+ if (!config.transformRequest && contentType) {
65
+ const transformRequest = getTransformRequest(
66
+ typeof contentType === "string" ? contentType : void 0
67
+ );
68
+ if (transformRequest) {
69
+ config.transformRequest = transformRequest;
70
+ }
71
+ } else if (!config.transformRequest) {
72
+ config.transformRequest = (data) => Qs.stringify(data);
73
+ }
74
+ 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;
78
+ } else if (config.method === "get" || config.method === "delete") {
79
+ if (!config.disableTimestamp) {
80
+ config.params = {
81
+ _t: Math.floor(Date.now() / 1e3),
82
+ ...config.params || {}
83
+ };
84
+ }
85
+ filterEmptyKey(config.params, true);
86
+ }
87
+ return config;
88
+ },
89
+ (error) => Promise.reject(error)
90
+ );
91
+ function extractErrorMessage(responseData) {
92
+ return responseData?.errorMessage || responseData?.message || responseData?.msg || "Unknown server error";
93
+ }
94
+ function handle401Error(config, error) {
95
+ emitMessageEvent({
96
+ type: "info",
97
+ message: "Authentication required",
98
+ status: 401,
99
+ config,
100
+ error
101
+ });
102
+ }
103
+ function isServiceResponseData(data) {
104
+ return typeof data === "object" && data !== null && !(data instanceof Blob) && ("success" in data || "code" in data || "data" in data || "message" in data || "msg" in data);
105
+ }
106
+ function hasDataField(responseData) {
107
+ return "data" in responseData && responseData.data !== void 0;
108
+ }
109
+ function extractResponseData(response) {
110
+ const { data } = response;
111
+ if (data instanceof Blob) {
112
+ return data;
113
+ }
114
+ if (!isServiceResponseData(data)) {
115
+ return data;
116
+ }
117
+ if (hasDataField(data)) {
118
+ return data.data;
119
+ }
120
+ return data;
121
+ }
122
+ service.interceptors.response.use(
123
+ (res) => {
124
+ const { data, config } = res;
125
+ const extendedConfig = config;
126
+ if (extendedConfig.file && res.status === 200 && data instanceof Blob) {
127
+ convertRes2Blob(res);
128
+ return void 0;
129
+ }
130
+ if (!isServiceResponseData(data)) {
131
+ return data;
132
+ }
133
+ const responseData = data;
134
+ if ((!responseData.success || responseData.success === "false") && responseData.code !== 200 && responseData.code !== 0) {
135
+ const msg = extractErrorMessage(responseData);
136
+ const code = responseData.errorCode || responseData.code || -1e3;
137
+ if (!extendedConfig.hidden) {
138
+ emitMessageEvent({
139
+ type: code === 401 ? "info" : "error",
140
+ message: `${extendedConfig.action || "Request"} failed: ${msg}`,
141
+ code,
142
+ status: res.status,
143
+ config: extendedConfig
144
+ });
145
+ }
146
+ return Promise.reject({ code, message: msg });
147
+ }
148
+ const extractedData = extractResponseData(res);
149
+ return extractedData;
150
+ },
151
+ (error) => {
152
+ try {
153
+ const axiosError = error;
154
+ const config = axiosError.config || {};
155
+ const status = axiosError.response?.status;
156
+ const messageText = String(axiosError?.message || "");
157
+ const isAborted = axiosError.code === "ECONNABORTED";
158
+ if (status === 401) {
159
+ handle401Error(config, error);
160
+ }
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) {
174
+ let friendly = messageText;
175
+ if (status === 403) friendly = "No permission";
176
+ else if (status === 502) friendly = "System is upgrading or unavailable";
177
+ else if (status === 504) friendly = "System timeout";
178
+ emitMessageEvent({
179
+ type: status === 401 ? "info" : "error",
180
+ message: `${config.action || "Request"} failed: ${friendly}`,
181
+ status,
182
+ config,
183
+ error
184
+ });
185
+ }
186
+ } catch (e) {
187
+ console.error("error in response error handler", e);
188
+ }
189
+ return Promise.reject(error);
190
+ }
191
+ );
192
+ service.json = function(url, params, config) {
193
+ const newConfig = {
194
+ headers: { "Content-Type": "application/json", ...config?.headers },
195
+ ...config
196
+ };
197
+ return service.post(
198
+ url,
199
+ Array.isArray(params) ? params : params,
200
+ newConfig
201
+ );
202
+ };
203
+ service.put = function(url, params, config) {
204
+ const newConfig = {
205
+ headers: { "Content-Type": "application/json", ...config?.headers },
206
+ ...config
207
+ };
208
+ return service.put(url, Array.isArray(params) ? params : params, newConfig).then((res) => res);
209
+ };
210
+ service.arrayGet = function(url, config) {
211
+ if (config) {
212
+ config.paramsSerializer = (params) => Qs.stringify(params, { arrayFormat: "repeat" });
213
+ }
214
+ return service.get(url, config);
215
+ };
216
+ service.download = function(url, config = {}) {
217
+ const { type = "get", params } = config;
218
+ const newConfig = {
219
+ headers: {
220
+ Accept: "application/json",
221
+ "Content-Type": "application/json; charset=utf-8",
222
+ ...config.headers
223
+ },
224
+ withCredentials: true,
225
+ file: true,
226
+ responseType: "blob",
227
+ ...config
228
+ };
229
+ if (type === "get") {
230
+ newConfig.params = params;
231
+ return service.get(url, newConfig);
232
+ } else {
233
+ newConfig.params = {};
234
+ return service.post(url, params, newConfig).then(() => void 0);
235
+ }
236
+ };
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
+ var services_default = service;
280
+
281
+ export { services_default as default, setServiceEventHandlers };