@moluoxixi/ajax-package 0.0.12 → 0.0.13

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.
@@ -4,16 +4,21 @@ import { MessageInstance, NotificationInstance } from './_utils/index.ts';
4
4
  export default class BaseHttpClient {
5
5
  protected baseURL: string;
6
6
  protected timeout: number;
7
- protected onTimeout: () => void;
7
+ protected onTimeout: (messageInstance: MessageInstance) => void;
8
8
  protected getToken?: () => string | null;
9
- protected onLoginRequired?: () => void;
9
+ protected onLoginRequired?: (messageInstance: MessageInstance) => void;
10
10
  instance: ReturnType<typeof axios.create>;
11
11
  protected messageInstance: MessageInstance;
12
12
  protected notificationInstance: NotificationInstance;
13
13
  constructor(config: BaseHttpClientConfig);
14
14
  processRequestConfig(config: InternalAxiosRequestConfig): InternalAxiosRequestConfig<any>;
15
15
  processResponseConfig(response: AxiosResponse): AxiosResponse['data'];
16
+ protected handleHttpStatus(response: AxiosResponse): void;
17
+ protected handleSuccessResponse(response: AxiosResponse): AxiosResponse['data'];
16
18
  processResponseError(error: AxiosError): Promise<AxiosError>;
19
+ protected handleAuthenticationError(error: AxiosError): void;
20
+ protected handleTimeoutError(error: AxiosError): void;
21
+ protected handleNetworkError(error: AxiosError): void;
17
22
  private setupInterceptors;
18
23
  protected request<R>(config: AxiosRequestConfig): Promise<AxiosResponse['data']>;
19
24
  get<R>(url: string, params?: Record<string, any>, config?: AxiosRequestConfig): Promise<AxiosResponse['data']>;
@@ -22,4 +27,5 @@ export default class BaseHttpClient {
22
27
  put<R>(url: string, data?: Record<string, any>, config?: AxiosRequestConfig): Promise<AxiosResponse['data']>;
23
28
  all<R>(requests: Array<AxiosRequestConfig | Promise<AxiosResponse<R>>>): Promise<AxiosResponse['data'][]>;
24
29
  uploadFile<R>(url: string, file: File | Blob, config?: AxiosRequestConfig): Promise<AxiosResponse['data']>;
30
+ downloadFile(blob: Blob, filename?: string): void;
25
31
  }
@@ -4,7 +4,7 @@ declare const _default: import('vue').DefineComponent<import('vue').ExtractPropT
4
4
  default: string;
5
5
  };
6
6
  width: {
7
- type: (NumberConstructor | StringConstructor)[];
7
+ type: (StringConstructor | NumberConstructor)[];
8
8
  default: number;
9
9
  };
10
10
  userName: {
@@ -40,7 +40,7 @@ declare const _default: import('vue').DefineComponent<import('vue').ExtractPropT
40
40
  default: undefined;
41
41
  };
42
42
  errorCode: {
43
- type: (NumberConstructor | StringConstructor)[];
43
+ type: (StringConstructor | NumberConstructor)[];
44
44
  default: undefined;
45
45
  };
46
46
  modelValue: {
@@ -60,7 +60,7 @@ declare const _default: import('vue').DefineComponent<import('vue').ExtractPropT
60
60
  default: string;
61
61
  };
62
62
  width: {
63
- type: (NumberConstructor | StringConstructor)[];
63
+ type: (StringConstructor | NumberConstructor)[];
64
64
  default: number;
65
65
  };
66
66
  userName: {
@@ -96,7 +96,7 @@ declare const _default: import('vue').DefineComponent<import('vue').ExtractPropT
96
96
  default: undefined;
97
97
  };
98
98
  errorCode: {
99
- type: (NumberConstructor | StringConstructor)[];
99
+ type: (StringConstructor | NumberConstructor)[];
100
100
  default: undefined;
101
101
  };
102
102
  modelValue: {
@@ -110,7 +110,6 @@ declare const _default: import('vue').DefineComponent<import('vue').ExtractPropT
110
110
  onReport?: (() => any) | undefined;
111
111
  }>, {
112
112
  title: string;
113
- modelValue: boolean;
114
113
  width: string | number;
115
114
  userName: string;
116
115
  userId: string;
@@ -121,5 +120,6 @@ declare const _default: import('vue').DefineComponent<import('vue').ExtractPropT
121
120
  traceId: string;
122
121
  errorMessage: string;
123
122
  errorCode: string | number;
123
+ modelValue: boolean;
124
124
  }, {}, {}, {}, string, import('vue').ComponentProvideOptions, true, {}, any>;
125
125
  export default _default;
@@ -1,11 +1,12 @@
1
1
  import { App } from 'vue';
2
+ import { MessageInstance } from '../_utils/index.ts';
2
3
  import { default as BaseApi } from '../class.ts';
3
4
  export interface BaseHttpClientConfig {
4
5
  baseURL?: string;
5
6
  timeout?: number;
6
- onTimeout?: () => void;
7
+ onTimeout?: (messageInstance: MessageInstance) => void;
7
8
  getToken?: () => string | null;
8
- onLoginRequired?: () => void;
9
+ onLoginRequired?: (messageInstance: MessageInstance) => void;
9
10
  [key: string]: any;
10
11
  }
11
12
  export interface BaseApiConfig extends BaseHttpClientConfig {
package/es/class.d.ts CHANGED
@@ -7,15 +7,28 @@ export default class BaseApi extends BaseHttpClient {
7
7
  constructor(config: BaseApiConfig);
8
8
  processRequestConfig(config: InternalAxiosRequestConfig): InternalAxiosRequestConfig;
9
9
  processResponseError(error: AxiosError): Promise<AxiosError>;
10
- processResponseConfig(response: AxiosResponse): AxiosResponse['data'];
10
+ protected handleHttpStatus(response: AxiosResponse): void;
11
+ protected handleAuthenticationError(error: AxiosError): void;
12
+ protected handleTimeoutError(error: AxiosError): void;
13
+ protected handleNetworkError(error: AxiosError): void;
14
+ protected handleSuccessResponse(response: AxiosResponse): AxiosResponse['data'];
11
15
  protected parseResponseFields(data: any): {
12
16
  code: any;
13
17
  message: any;
14
18
  responseData: any;
15
19
  };
16
- protected handleErrorCode(response: AxiosResponse, code: any, message: any, responseData: any): void;
17
- protected handleErrors(responseData: any): void;
20
+ protected handleSystemError(response: AxiosResponse, code: any, message: any, responseData: any): void;
21
+ protected handleBusinessError(code: any, message: any): void;
22
+ protected handleErrorArray(responseData: any): void;
23
+ protected showErrorArrayNotification(errors: Array<{
24
+ code: string;
25
+ message: string;
26
+ }>): void;
18
27
  protected handleTips(responseData: any): void;
28
+ protected showTipsNotification(tips: Array<{
29
+ code: string;
30
+ message: string;
31
+ }>): void;
19
32
  private showSystemExceptionDialog;
20
33
  protected reportError(errorInfo: any): Promise<void>;
21
34
  }
package/es/index.mjs CHANGED
@@ -2,9 +2,9 @@
2
2
  "use strict";
3
3
  try {
4
4
  if (typeof document !== "undefined") {
5
- if (!document.getElementById("c6a35fc5-e19d-40c8-8129-fe6481835a30")) {
5
+ if (!document.getElementById("065a78da-34dc-4a45-874d-4fb480012cd9")) {
6
6
  var elementStyle = document.createElement("style");
7
- elementStyle.id = "c6a35fc5-e19d-40c8-8129-fe6481835a30";
7
+ elementStyle.id = "065a78da-34dc-4a45-874d-4fb480012cd9";
8
8
  elementStyle.appendChild(document.createTextNode("._root_11p33_1 .el-dialog__header {\n padding: 0 12px 12px;\n}\n\n._root_11p33_1 .el-dialog__body {\n border-top: 1px solid #e5e7eb;\n border-bottom: 1px solid #e5e7eb;\n padding: 0 12px;\n}\n\n._root_11p33_1 .el-dialog__footer {\n padding: 0 12px;\n}"));
9
9
  document.head.appendChild(elementStyle);
10
10
  }
@@ -12170,7 +12170,11 @@ function extractSystemErrorInfo(response, code, message2) {
12170
12170
  errorMessage: message2
12171
12171
  };
12172
12172
  }
12173
- function defaultOnLoginRequired() {
12173
+ function defaultOnLoginRequired(messageInstance) {
12174
+ messageInstance == null ? void 0 : messageInstance.error({
12175
+ message: "登录已过期,请重新登录",
12176
+ duration: 5 * 1e3
12177
+ });
12174
12178
  if (typeof window !== "undefined") {
12175
12179
  window.location.href = `/login?redirect=${encodeURIComponent(window.location.href)}`;
12176
12180
  }
@@ -12215,11 +12219,45 @@ class BaseHttpClient {
12215
12219
  return config;
12216
12220
  }
12217
12221
  processResponseConfig(response) {
12222
+ this.handleHttpStatus(response);
12223
+ return this.handleSuccessResponse(response);
12224
+ }
12225
+ handleHttpStatus(response) {
12226
+ var _a2;
12227
+ if (response.status !== 200) {
12228
+ throw new Error(((_a2 = response.data) == null ? void 0 : _a2.message) || `HTTP Error: ${response.status}`);
12229
+ }
12230
+ }
12231
+ handleSuccessResponse(response) {
12218
12232
  return response.data;
12219
12233
  }
12220
12234
  async processResponseError(error) {
12235
+ this.handleAuthenticationError(error);
12236
+ this.handleTimeoutError(error);
12237
+ this.handleNetworkError(error);
12221
12238
  return error;
12222
12239
  }
12240
+ handleAuthenticationError(error) {
12241
+ var _a2, _b;
12242
+ if (((_a2 = error.response) == null ? void 0 : _a2.status) === 401) {
12243
+ (_b = this.onLoginRequired) == null ? void 0 : _b.call(this, this.messageInstance);
12244
+ }
12245
+ }
12246
+ handleTimeoutError(error) {
12247
+ if (error.code === "ECONNABORTED" && error.message.includes("timeout")) {
12248
+ this.onTimeout(this.messageInstance);
12249
+ }
12250
+ }
12251
+ handleNetworkError(error) {
12252
+ var _a2, _b, _c;
12253
+ if (((_a2 = error.response) == null ? void 0 : _a2.status) !== 401 && error.code !== "ECONNABORTED") {
12254
+ const fallbackError = error;
12255
+ (_c = this.messageInstance) == null ? void 0 : _c.error({
12256
+ message: ((_b = fallbackError.response) == null ? void 0 : _b.data) || fallbackError.message || "网络错误",
12257
+ duration: 5 * 1e3
12258
+ });
12259
+ }
12260
+ }
12223
12261
  setupInterceptors() {
12224
12262
  this.instance.interceptors.request.use(
12225
12263
  (config) => {
@@ -12238,34 +12276,10 @@ class BaseHttpClient {
12238
12276
  );
12239
12277
  this.instance.interceptors.response.use(
12240
12278
  (res) => {
12241
- var _a2;
12242
- if (res.status !== 200) {
12243
- return Promise.reject(new Error(((_a2 = res.data) == null ? void 0 : _a2.message) || "Error"));
12244
- }
12245
12279
  return this.processResponseConfig(res);
12246
12280
  },
12247
12281
  async (error) => {
12248
- var _a2, _b, _c, _d, _e, _f;
12249
12282
  await this.processResponseError(error);
12250
- if (((_a2 = error.response) == null ? void 0 : _a2.status) === 401) {
12251
- (_b = this.onLoginRequired) == null ? void 0 : _b.call(this);
12252
- (_c = this.messageInstance) == null ? void 0 : _c.error({
12253
- message: "登录已过期,请重新登录",
12254
- duration: 5 * 1e3
12255
- });
12256
- } else if (error.code === "ECONNABORTED" && error.message.includes("timeout")) {
12257
- this.onTimeout();
12258
- (_d = this.messageInstance) == null ? void 0 : _d.error({
12259
- message: "请求超时,请检查网络连接或稍后重试",
12260
- duration: 5 * 1e3
12261
- });
12262
- } else {
12263
- const fallbackError = error;
12264
- (_f = this.messageInstance) == null ? void 0 : _f.error({
12265
- message: ((_e = fallbackError.response) == null ? void 0 : _e.data) || fallbackError.message || "网络错误",
12266
- duration: 5 * 1e3
12267
- });
12268
- }
12269
12283
  return Promise.reject(error);
12270
12284
  }
12271
12285
  );
@@ -12308,6 +12322,20 @@ class BaseHttpClient {
12308
12322
  }
12309
12323
  });
12310
12324
  }
12325
+ downloadFile(blob, filename) {
12326
+ if (typeof window === "undefined") {
12327
+ console.warn("downloadFile: 非浏览器环境,无法下载文件");
12328
+ return;
12329
+ }
12330
+ const url = window.URL.createObjectURL(blob);
12331
+ const link = document.createElement("a");
12332
+ link.href = url;
12333
+ link.download = filename || `download-${Date.now()}`;
12334
+ document.body.appendChild(link);
12335
+ link.click();
12336
+ document.body.removeChild(link);
12337
+ window.URL.revokeObjectURL(url);
12338
+ }
12311
12339
  }
12312
12340
  async function dynamicImports(modulePromise, exportNames) {
12313
12341
  const module = await modulePromise;
@@ -12488,11 +12516,25 @@ class BaseApi extends BaseHttpClient {
12488
12516
  async processResponseError(error) {
12489
12517
  return super.processResponseError(error);
12490
12518
  }
12491
- processResponseConfig(response) {
12492
- const data = response.data;
12493
- const { code, message: message2, responseData } = this.parseResponseFields(data);
12494
- this.handleErrorCode(response, code, message2, responseData);
12495
- this.handleErrors(responseData);
12519
+ handleHttpStatus(response) {
12520
+ return super.handleHttpStatus(response);
12521
+ }
12522
+ handleAuthenticationError(error) {
12523
+ super.handleAuthenticationError(error);
12524
+ }
12525
+ handleTimeoutError(error) {
12526
+ return super.handleTimeoutError(error);
12527
+ }
12528
+ handleNetworkError(error) {
12529
+ return super.handleNetworkError(error);
12530
+ }
12531
+ handleSuccessResponse(response) {
12532
+ const httpData = super.handleSuccessResponse(response);
12533
+ const parsedFields = this.parseResponseFields(httpData);
12534
+ const { code, message: message2, responseData } = parsedFields;
12535
+ this.handleSystemError(response, code, message2, responseData);
12536
+ this.handleBusinessError(code, message2);
12537
+ this.handleErrorArray(responseData);
12496
12538
  this.handleTips(responseData);
12497
12539
  return responseData;
12498
12540
  }
@@ -12517,11 +12559,7 @@ class BaseApi extends BaseHttpClient {
12517
12559
  const responseData = getValueByPath(data, (_c = this.responseFields) == null ? void 0 : _c.data);
12518
12560
  return { code, message: message2, responseData };
12519
12561
  }
12520
- handleErrorCode(response, code, message2, responseData) {
12521
- var _a2;
12522
- if (code === 401) {
12523
- throw new Error("登录失效,请重新登录");
12524
- }
12562
+ handleSystemError(response, code, message2, responseData) {
12525
12563
  if (code === -1) {
12526
12564
  if (this.enableSystemErrorDialog) {
12527
12565
  this.showSystemExceptionDialog(response, responseData, code, message2).catch((error) => {
@@ -12530,6 +12568,9 @@ class BaseApi extends BaseHttpClient {
12530
12568
  }
12531
12569
  throw new Error(message2 || "系统异常");
12532
12570
  }
12571
+ }
12572
+ handleBusinessError(code, message2) {
12573
+ var _a2;
12533
12574
  if (code && code !== 200) {
12534
12575
  (_a2 = this.messageInstance) == null ? void 0 : _a2.error({
12535
12576
  message: message2 || "请求失败",
@@ -12538,55 +12579,63 @@ class BaseApi extends BaseHttpClient {
12538
12579
  throw new Error(message2 || "请求失败");
12539
12580
  }
12540
12581
  }
12541
- handleErrors(responseData) {
12542
- var _a2, _b, _c;
12582
+ handleErrorArray(responseData) {
12583
+ var _a2;
12543
12584
  const errorsField = (_a2 = this.responseFields) == null ? void 0 : _a2.errors;
12544
12585
  if (errorsField) {
12545
12586
  const errors = responseData == null ? void 0 : responseData[errorsField];
12546
12587
  if (Array.isArray(errors) && errors.length) {
12547
- const html = errors.map((item) => `<div style="font-size: 14px;color:red">${item.code}:${item.message}</div>`).join("");
12548
- if (hasDocument) {
12549
- (_b = this.notificationInstance) == null ? void 0 : _b.call(this, {
12550
- title: "提示",
12551
- message: html,
12552
- type: "error"
12553
- });
12554
- } else {
12555
- const errorMessages = errors.map((item) => `${item.code}:${item.message}`).join("\n");
12556
- (_c = this.notificationInstance) == null ? void 0 : _c.call(this, {
12557
- title: "提示",
12558
- message: errorMessages,
12559
- type: "error"
12560
- });
12561
- }
12588
+ this.showErrorArrayNotification(errors);
12562
12589
  throw new Error("请求错误");
12563
12590
  }
12564
12591
  }
12565
12592
  }
12593
+ showErrorArrayNotification(errors) {
12594
+ var _a2, _b;
12595
+ const html = errors.map((item) => `<div style="font-size: 14px;color:red">${item.code}:${item.message}</div>`).join("");
12596
+ if (hasDocument) {
12597
+ (_a2 = this.notificationInstance) == null ? void 0 : _a2.call(this, {
12598
+ title: "提示",
12599
+ message: html,
12600
+ type: "error"
12601
+ });
12602
+ } else {
12603
+ const errorMessages = errors.map((item) => `${item.code}:${item.message}`).join("\n");
12604
+ (_b = this.notificationInstance) == null ? void 0 : _b.call(this, {
12605
+ title: "提示",
12606
+ message: errorMessages,
12607
+ type: "error"
12608
+ });
12609
+ }
12610
+ }
12566
12611
  handleTips(responseData) {
12567
- var _a2, _b, _c;
12612
+ var _a2;
12568
12613
  const tipsField = (_a2 = this.responseFields) == null ? void 0 : _a2.tips;
12569
12614
  if (tipsField) {
12570
12615
  const tips = responseData == null ? void 0 : responseData[tipsField];
12571
12616
  if (Array.isArray(tips) && tips.length) {
12572
- const html = tips.map((item) => `<div style="font-size: 14px;color:#E6A23C">${item.code}:${item.message}</div>`).join("");
12573
- if (hasDocument) {
12574
- (_b = this.notificationInstance) == null ? void 0 : _b.call(this, {
12575
- title: "提示",
12576
- message: html,
12577
- type: "warning"
12578
- });
12579
- } else {
12580
- const tipMessages = tips.map((item) => `${item.code}:${item.message}`).join("\n");
12581
- (_c = this.notificationInstance) == null ? void 0 : _c.call(this, {
12582
- title: "提示",
12583
- message: tipMessages,
12584
- type: "warning"
12585
- });
12586
- }
12617
+ this.showTipsNotification(tips);
12587
12618
  }
12588
12619
  }
12589
12620
  }
12621
+ showTipsNotification(tips) {
12622
+ var _a2, _b;
12623
+ const html = tips.map((item) => `<div style="font-size: 14px;color:#E6A23C">${item.code}:${item.message}</div>`).join("");
12624
+ if (hasDocument) {
12625
+ (_a2 = this.notificationInstance) == null ? void 0 : _a2.call(this, {
12626
+ title: "提示",
12627
+ message: html,
12628
+ type: "warning"
12629
+ });
12630
+ } else {
12631
+ const tipMessages = tips.map((item) => `${item.code}:${item.message}`).join("\n");
12632
+ (_b = this.notificationInstance) == null ? void 0 : _b.call(this, {
12633
+ title: "提示",
12634
+ message: tipMessages,
12635
+ type: "warning"
12636
+ });
12637
+ }
12638
+ }
12590
12639
  async showSystemExceptionDialog(response, responseData, code, message2) {
12591
12640
  if (!hasDocument) {
12592
12641
  console.error("系统异常信息:", responseData);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moluoxixi/ajax-package",
3
- "version": "0.0.12",
3
+ "version": "0.0.13",
4
4
  "description": "AjaxPackage 组件",
5
5
  "sideEffects": [
6
6
  "*.css",