@moluoxixi/ajax-package 0.0.16 → 0.0.17-beta.1

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.
@@ -0,0 +1,142 @@
1
+ import { AxiosError, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig, default as axios } from 'axios';
2
+ import { BaseHttpClientConfig } from './_types/index.ts';
3
+ import { MessageInstance, NotificationInstance } from './_utils/index.ts';
4
+ /**
5
+ * BaseHttpClient 基础类
6
+ * 提供最基础的 HTTP 请求功能,包括:
7
+ * - 创建 axios 实例
8
+ * - 自动添加 Token
9
+ * - 基础错误处理(401、超时等)
10
+ * - HTTP 方法(get、post、put、delete、all)
11
+ * - 文件上传
12
+ */
13
+ export default class BaseHttpClient {
14
+ protected baseURL: string;
15
+ protected timeout: number;
16
+ protected onTimeout: (messageInstance: MessageInstance) => void;
17
+ protected getToken?: () => string | null;
18
+ protected onLoginRequired?: (messageInstance: MessageInstance) => void;
19
+ instance: ReturnType<typeof axios.create>;
20
+ protected messageInstance: MessageInstance;
21
+ protected notificationInstance: NotificationInstance;
22
+ /**
23
+ * 创建 BaseHttpClient 实例
24
+ * @param config - HTTP 客户端配置对象
25
+ */
26
+ constructor(config: BaseHttpClientConfig);
27
+ /**
28
+ * 处理请求配置,子类可重写此方法自定义请求配置
29
+ * @param config - 请求配置对象
30
+ * @returns 处理后的请求配置
31
+ */
32
+ processRequestConfig(config: InternalAxiosRequestConfig): InternalAxiosRequestConfig<any>;
33
+ /**
34
+ * 处理响应配置,子类可重写此方法自定义响应处理
35
+ * 按照标准 HTTP 结构处理响应
36
+ * @param response - Axios 响应对象
37
+ * @returns 解析后的响应数据
38
+ */
39
+ processResponseConfig(response: AxiosResponse): AxiosResponse['data'];
40
+ /**
41
+ * 处理 HTTP 状态码
42
+ * 子类可重写此方法来自定义 HTTP 状态码处理逻辑
43
+ * @param response - Axios 响应对象
44
+ */
45
+ protected handleHttpStatus(response: AxiosResponse): void;
46
+ /**
47
+ * 处理成功响应
48
+ * 子类可重写此方法来自定义成功响应的处理逻辑
49
+ * @param response - Axios 响应对象
50
+ * @returns 解析后的响应数据
51
+ */
52
+ protected handleSuccessResponse(response: AxiosResponse): AxiosResponse['data'];
53
+ /**
54
+ * 处理响应错误,子类可重写此方法自定义错误处理
55
+ * 按照标准 HTTP 错误结构处理错误
56
+ * @param error - Axios 错误对象
57
+ * @returns 处理后的错误对象
58
+ */
59
+ processResponseError(error: AxiosError): Promise<AxiosError>;
60
+ /**
61
+ * 处理认证错误(401 - 未授权/登录失效)
62
+ * 子类可重写此方法来自定义认证错误处理逻辑
63
+ * @param error - Axios 错误对象
64
+ */
65
+ protected handleAuthenticationError(error: AxiosError): void;
66
+ /**
67
+ * 处理超时错误
68
+ * 子类可重写此方法来自定义超时错误处理逻辑
69
+ * @param error - Axios 错误对象
70
+ */
71
+ protected handleTimeoutError(error: AxiosError): void;
72
+ /**
73
+ * 处理网络错误(其他错误)
74
+ * 子类可重写此方法来自定义网络错误处理逻辑
75
+ * @param error - Axios 错误对象
76
+ */
77
+ protected handleNetworkError(error: AxiosError): void;
78
+ /**
79
+ * 设置请求和响应拦截器
80
+ * 请求拦截器:自动添加 Token
81
+ * 响应拦截器:处理成功响应和错误响应(401、超时等)
82
+ */
83
+ private setupInterceptors;
84
+ /**
85
+ * 发送 HTTP 请求,所有 HTTP 方法最终都调用此方法
86
+ * @param config - Axios 请求配置对象
87
+ * @returns 解析后的响应数据
88
+ */
89
+ protected request<R>(config: AxiosRequestConfig): Promise<AxiosResponse['data']>;
90
+ /**
91
+ * 发送 GET 请求
92
+ * @param url - 请求 URL 路径
93
+ * @param params - 查询参数对象
94
+ * @param config - 额外的请求配置
95
+ * @returns 解析后的响应数据
96
+ */
97
+ get<R>(url: string, params?: Record<string, any>, config?: AxiosRequestConfig): Promise<AxiosResponse['data']>;
98
+ /**
99
+ * 发送 POST 请求
100
+ * @param url - 请求 URL 路径
101
+ * @param data - 请求体数据
102
+ * @param config - 额外的请求配置
103
+ * @returns 解析后的响应数据
104
+ */
105
+ post<R>(url: string, data?: Record<string, any>, config?: AxiosRequestConfig): Promise<AxiosResponse['data']>;
106
+ /**
107
+ * 发送 DELETE 请求
108
+ * @param url - 请求 URL 路径
109
+ * @param params - 查询参数对象
110
+ * @param config - 额外的请求配置
111
+ * @returns 解析后的响应数据
112
+ */
113
+ delete<R>(url: string, params?: Record<string, any>, config?: AxiosRequestConfig): Promise<AxiosResponse['data']>;
114
+ /**
115
+ * 发送 PUT 请求
116
+ * @param url - 请求 URL 路径
117
+ * @param data - 请求体数据
118
+ * @param config - 额外的请求配置
119
+ * @returns 解析后的响应数据
120
+ */
121
+ put<R>(url: string, data?: Record<string, any>, config?: AxiosRequestConfig): Promise<AxiosResponse['data']>;
122
+ /**
123
+ * 批量请求,并发发送多个请求
124
+ * @param requests - 请求配置数组或已发起的请求 Promise 数组
125
+ * @returns 所有请求的响应数据数组
126
+ */
127
+ all<R>(requests: Array<AxiosRequestConfig | Promise<AxiosResponse<R>>>): Promise<AxiosResponse['data'][]>;
128
+ /**
129
+ * 文件上传,将文件包装为 FormData 发送
130
+ * @param url - 上传地址
131
+ * @param file - 文件对象
132
+ * @param config - 额外的请求配置
133
+ * @returns 解析后的响应数据
134
+ */
135
+ uploadFile<R>(url: string, file: File | Blob, config?: AxiosRequestConfig): Promise<AxiosResponse['data']>;
136
+ /**
137
+ * 下载文件,将 Blob 对象下载到本地
138
+ * @param blob - Blob 对象
139
+ * @param filename - 文件名,如果不提供则使用时间戳
140
+ */
141
+ downloadFile(blob: Blob, filename?: string): void;
142
+ }
@@ -0,0 +1,129 @@
1
+ /**
2
+ * SystemErrorDialog 组件
3
+ * 使用 defineComponent 和 h 函数实现
4
+ */
5
+ declare const _default: import('vue').DefineComponent<import('vue').ExtractPropTypes<{
6
+ title: {
7
+ type: StringConstructor;
8
+ default: string;
9
+ };
10
+ width: {
11
+ type: (StringConstructor | NumberConstructor)[];
12
+ default: number;
13
+ };
14
+ userName: {
15
+ type: StringConstructor;
16
+ default: undefined;
17
+ };
18
+ userId: {
19
+ type: StringConstructor;
20
+ default: undefined;
21
+ };
22
+ deptName: {
23
+ type: StringConstructor;
24
+ default: undefined;
25
+ };
26
+ deptId: {
27
+ type: StringConstructor;
28
+ default: undefined;
29
+ };
30
+ clientIp: {
31
+ type: StringConstructor;
32
+ default: undefined;
33
+ };
34
+ requestUrl: {
35
+ type: StringConstructor;
36
+ default: undefined;
37
+ };
38
+ traceId: {
39
+ type: StringConstructor;
40
+ default: undefined;
41
+ };
42
+ errorMessage: {
43
+ type: StringConstructor;
44
+ default: undefined;
45
+ };
46
+ errorCode: {
47
+ type: (StringConstructor | NumberConstructor)[];
48
+ default: undefined;
49
+ };
50
+ modelValue: {
51
+ type: BooleanConstructor;
52
+ default: boolean;
53
+ };
54
+ }>, () => import('vue').VNode<import('vue').RendererNode, import('vue').RendererElement, {
55
+ [key: string]: any;
56
+ }>, {}, {}, {}, import('vue').ComponentOptionsMixin, import('vue').ComponentOptionsMixin, {
57
+ 'update:modelValue': (val: boolean) => true;
58
+ close: () => true;
59
+ confirm: (data: any) => true;
60
+ report: () => true;
61
+ }, string, import('vue').PublicProps, Readonly<import('vue').ExtractPropTypes<{
62
+ title: {
63
+ type: StringConstructor;
64
+ default: string;
65
+ };
66
+ width: {
67
+ type: (StringConstructor | NumberConstructor)[];
68
+ default: number;
69
+ };
70
+ userName: {
71
+ type: StringConstructor;
72
+ default: undefined;
73
+ };
74
+ userId: {
75
+ type: StringConstructor;
76
+ default: undefined;
77
+ };
78
+ deptName: {
79
+ type: StringConstructor;
80
+ default: undefined;
81
+ };
82
+ deptId: {
83
+ type: StringConstructor;
84
+ default: undefined;
85
+ };
86
+ clientIp: {
87
+ type: StringConstructor;
88
+ default: undefined;
89
+ };
90
+ requestUrl: {
91
+ type: StringConstructor;
92
+ default: undefined;
93
+ };
94
+ traceId: {
95
+ type: StringConstructor;
96
+ default: undefined;
97
+ };
98
+ errorMessage: {
99
+ type: StringConstructor;
100
+ default: undefined;
101
+ };
102
+ errorCode: {
103
+ type: (StringConstructor | NumberConstructor)[];
104
+ default: undefined;
105
+ };
106
+ modelValue: {
107
+ type: BooleanConstructor;
108
+ default: boolean;
109
+ };
110
+ }>> & Readonly<{
111
+ onClose?: (() => any) | undefined;
112
+ onConfirm?: ((data: any) => any) | undefined;
113
+ "onUpdate:modelValue"?: ((val: boolean) => any) | undefined;
114
+ onReport?: (() => any) | undefined;
115
+ }>, {
116
+ title: string;
117
+ width: string | number;
118
+ userName: string;
119
+ userId: string;
120
+ deptName: string;
121
+ deptId: string;
122
+ clientIp: string;
123
+ requestUrl: string;
124
+ traceId: string;
125
+ errorMessage: string;
126
+ errorCode: string | number;
127
+ modelValue: boolean;
128
+ }, {}, {}, {}, string, import('vue').ComponentProvideOptions, true, {}, any>;
129
+ export default _default;
@@ -0,0 +1,118 @@
1
+ import { AxiosRequestConfig } from 'axios';
2
+ import { App } from 'vue';
3
+ import { MessageInstance } from '../_utils/index.ts';
4
+ import { default as BaseApi } from '../class.ts';
5
+ /**
6
+ * BaseHttpClient 基础配置接口,包含最基础的 HTTP 客户端配置
7
+ */
8
+ export interface BaseHttpClientConfig {
9
+ /** API 基础地址 */
10
+ baseURL?: string;
11
+ /** 请求超时时间(毫秒),默认 5000 */
12
+ timeout?: number;
13
+ /** 请求超时回调函数,接收 messageInstance 用于显示消息提示 */
14
+ onTimeout?: (messageInstance: MessageInstance) => void;
15
+ /** 获取 token 的函数,每次请求前自动调用 */
16
+ getToken?: () => string | null;
17
+ /** 登录失效回调函数,当检测到 401 错误时调用,接收 messageInstance 用于显示消息提示 */
18
+ onLoginRequired?: (messageInstance: MessageInstance) => void;
19
+ /** 允许其他任意配置项,会直接传递给 axios.create */
20
+ [key: string]: any;
21
+ }
22
+ /**
23
+ * BaseApi 配置接口,用于配置 BaseApi 实例的所有选项
24
+ * 继承 BaseHttpClientConfig,添加响应字段映射和系统异常弹窗配置
25
+ */
26
+ export interface BaseApiConfig extends BaseHttpClientConfig {
27
+ /** 响应字段映射配置 */
28
+ responseFields?: {
29
+ /** 响应状态码字段名,默认 'Code' */
30
+ code?: string;
31
+ /** 响应消息字段名,默认 'Message' */
32
+ message?: string;
33
+ /** 响应数据字段名,默认 'data' */
34
+ data?: string;
35
+ /** 错误数组字段名 */
36
+ errors?: string;
37
+ /** 提示信息字段名 */
38
+ tips?: string;
39
+ };
40
+ /** 是否启用 code === -1 的系统异常弹窗,默认为 true */
41
+ enableSystemErrorDialog?: boolean;
42
+ }
43
+ /**
44
+ * Vue Axios 插件配置选项
45
+ */
46
+ export interface vueAxiosPluginOptionsType {
47
+ /** 默认 HTTP 服务配置 */
48
+ default?: BaseApiConfig;
49
+ /** 是否在所有组件中通过 mixin 注入 $http,默认为 true */
50
+ globalMixin?: boolean;
51
+ }
52
+ /**
53
+ * Vue HTTP 服务类型,在 Vue 应用中通过 this.$http 或 inject('$http') 获取
54
+ */
55
+ export type vueHttpServiceType = BaseApi;
56
+ /**
57
+ * Vue Axios 插件类型,定义了 Vue 插件的标准接口
58
+ */
59
+ export interface vueAxiosPluginType {
60
+ /**
61
+ * 安装插件
62
+ * @param app - Vue 应用实例
63
+ * @param options - 插件配置选项
64
+ */
65
+ install: (app: App, options?: vueAxiosPluginOptionsType) => void;
66
+ }
67
+ /**
68
+ * 通知配置选项
69
+ */
70
+ export interface NotificationOptions {
71
+ /**
72
+ * 通知标题
73
+ */
74
+ title?: string;
75
+ /**
76
+ * 通知类型
77
+ */
78
+ type?: 'success' | 'error' | 'warning' | 'info';
79
+ /**
80
+ * 通知持续时间(毫秒)
81
+ */
82
+ duration?: number;
83
+ /**
84
+ * 是否显示关闭按钮
85
+ */
86
+ showClose?: boolean;
87
+ /**
88
+ * 自定义类名
89
+ */
90
+ customClass?: string;
91
+ /**
92
+ * 自定义样式
93
+ */
94
+ customStyle?: string | Record<string, any>;
95
+ /**
96
+ * 其他通知选项
97
+ */
98
+ [key: string]: any;
99
+ }
100
+ /**
101
+ * 扩展 AxiosRequestConfig,支持自定义通知配置
102
+ */
103
+ export interface ExtendedAxiosRequestConfig extends AxiosRequestConfig {
104
+ /**
105
+ * 错误通知配置选项,用于覆盖默认的错误通知参数
106
+ */
107
+ errorNotificationOptions?: NotificationOptions;
108
+ /**
109
+ * 提示通知配置选项,用于覆盖默认的提示通知参数
110
+ */
111
+ tipsNotificationOptions?: NotificationOptions;
112
+ /**
113
+ * 是否使用自定义消息处理
114
+ * 当为 true 时,handleBusinessError、handleErrorArray、handleTips 都不会执行
115
+ * 默认值为 false
116
+ */
117
+ isCustomMessage?: boolean;
118
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * SystemErrorDialog 组件的 Emits 类型定义
3
+ */
4
+ export interface SystemErrorDialogEmitsType {
5
+ /** v-model 更新事件 */
6
+ 'update:modelValue': [val: boolean];
7
+ /** 关闭事件 */
8
+ 'close': [];
9
+ /** 确认事件 */
10
+ 'confirm': [data: any];
11
+ /** 上报事件 */
12
+ 'report': [];
13
+ }
@@ -0,0 +1,4 @@
1
+ export * from './api.ts';
2
+ export * from './emits.ts';
3
+ export type { SystemErrorDialogEmitsType } from './emits.ts';
4
+ export * from './props.ts';
@@ -0,0 +1,27 @@
1
+ /**
2
+ * SystemErrorDialog 组件的 Props 类型定义
3
+ */
4
+ export interface SystemErrorDialogPropsType {
5
+ /** 对话框标题 */
6
+ title?: string;
7
+ /** 对话框宽度 */
8
+ width?: number | string;
9
+ /** 用户名 */
10
+ userName?: string;
11
+ /** 用户ID */
12
+ userId?: string;
13
+ /** 科室名称 */
14
+ deptName?: string;
15
+ /** 科室ID */
16
+ deptId?: string;
17
+ /** 客户端IP地址 */
18
+ clientIp?: string;
19
+ /** 请求URL路径 */
20
+ requestUrl?: string;
21
+ /** 链路追踪ID */
22
+ traceId?: string;
23
+ /** 错误消息 */
24
+ errorMessage?: string;
25
+ /** 错误代码 */
26
+ errorCode?: number | string;
27
+ }
@@ -0,0 +1,3 @@
1
+ export * from './messageWrapper.ts';
2
+ export * from './notificationWrapper.ts';
3
+ export * from './systemErrorInfo.ts';
@@ -0,0 +1,39 @@
1
+ /**
2
+ * 创建消息实例的包装函数
3
+ * @returns 消息实例,支持 success、error、warning、info 方法
4
+ */
5
+ export declare function createMessageWrapper(): (import('element-plus').MessageFn & {
6
+ primary: import('element-plus').MessageTypedFn;
7
+ success: import('element-plus').MessageTypedFn;
8
+ warning: import('element-plus').MessageTypedFn;
9
+ info: import('element-plus').MessageTypedFn;
10
+ error: import('element-plus').MessageTypedFn;
11
+ } & import('vue').ObjectPlugin<any[]> & {
12
+ _context: import('vue').AppContext | null;
13
+ }) | (import('element-plus').MessageFn & {
14
+ primary: import('element-plus').MessageTypedFn;
15
+ success: import('element-plus').MessageTypedFn;
16
+ warning: import('element-plus').MessageTypedFn;
17
+ info: import('element-plus').MessageTypedFn;
18
+ error: import('element-plus').MessageTypedFn;
19
+ } & ((app: import('vue').App, ...options: any[]) => any) & Partial<import('vue').ObjectPlugin<any[]>> & {
20
+ _context: import('vue').AppContext | null;
21
+ }) | {
22
+ success: (options: string | {
23
+ message?: string;
24
+ [key: string]: any;
25
+ }) => void;
26
+ error: (options: string | {
27
+ message?: string;
28
+ [key: string]: any;
29
+ }) => void;
30
+ warning: (options: string | {
31
+ message?: string;
32
+ [key: string]: any;
33
+ }) => void;
34
+ info: (options: string | {
35
+ message?: string;
36
+ [key: string]: any;
37
+ }) => void;
38
+ };
39
+ export type MessageInstance = ReturnType<typeof createMessageWrapper>;
@@ -0,0 +1,12 @@
1
+ /**
2
+ * 创建通知实例的包装函数(用于 errors / tips 展示)
3
+ * @returns 通知实例,支持 success、error、warning、info 方法
4
+ */
5
+ export declare function createNotificationWrapper(): ((import('element-plus').Notify & import('vue').Plugin) & {
6
+ _context: import('vue').AppContext | null;
7
+ }) | ((options: string | {
8
+ message?: string;
9
+ title?: string;
10
+ type?: "success" | "error" | "warning" | "info";
11
+ }) => void);
12
+ export type NotificationInstance = ReturnType<typeof createNotificationWrapper>;
@@ -0,0 +1,27 @@
1
+ import { AxiosResponse } from 'axios';
2
+ import { SystemErrorDialogPropsType } from '../_types/index.ts';
3
+ /**
4
+ * 规范化请求参数对象
5
+ * @param payload 请求参数
6
+ * @returns 规范化后的对象
7
+ */
8
+ export declare function normalizePayload(payload: any): Record<string, any>;
9
+ /**
10
+ * 解析响应头中的 TraceId
11
+ * @param headers 响应头
12
+ * @returns TraceId 字符串
13
+ */
14
+ export declare function resolveTraceId(headers: AxiosResponse['headers'] | undefined): string;
15
+ /**
16
+ * 从 localStorage 中读取 userInfo
17
+ * @returns userInfo 对象,如果不存在则返回空对象
18
+ */
19
+ export declare function getUserInfoFromLocalStorage(): Record<string, any>;
20
+ /**
21
+ * 从 AxiosResponse 中提取系统错误信息
22
+ * @param response Axios 响应对象
23
+ * @param code 错误代码
24
+ * @param message 错误消息
25
+ * @returns 提取的错误信息
26
+ */
27
+ export declare function extractSystemErrorInfo(response: AxiosResponse, code: number, message: string): Omit<SystemErrorDialogPropsType, 'title' | 'width'>;
package/es/class.d.ts ADDED
@@ -0,0 +1,219 @@
1
+ import { AxiosError, AxiosResponse, InternalAxiosRequestConfig } from 'axios';
2
+ import { BaseApiConfig, ExtendedAxiosRequestConfig, NotificationOptions } from './_types/index.ts';
3
+ import { default as BaseHttpClient } from './BaseHttpClient.ts';
4
+ /**
5
+ * BaseApi 类
6
+ * 继承 BaseHttpClient,提供增强的响应解析和错误处理功能
7
+ * 包括:响应字段映射、错误码处理、系统异常弹窗等
8
+ */
9
+ export default class BaseApi extends BaseHttpClient {
10
+ protected responseFields: Required<BaseApiConfig['responseFields']>;
11
+ protected enableSystemErrorDialog: boolean;
12
+ /**
13
+ * 创建 BaseApi 实例
14
+ * @param config - API 配置对象
15
+ */
16
+ constructor(config: BaseApiConfig);
17
+ /**
18
+ * 处理请求配置,子类可重写此方法自定义请求配置
19
+ * 显式声明以确保类型一致性,避免打包后的类型不兼容问题
20
+ * @param config - 请求配置对象
21
+ * @returns 处理后的请求配置
22
+ */
23
+ processRequestConfig(config: InternalAxiosRequestConfig): InternalAxiosRequestConfig;
24
+ /**
25
+ * 处理响应配置,子类可重写此方法自定义响应处理
26
+ * 显式声明以确保类型一致性,避免打包后的类型不兼容问题
27
+ * @param response - Axios 响应对象
28
+ * @returns 解析后的响应数据
29
+ */
30
+ processResponseConfig(response: AxiosResponse): AxiosResponse['data'];
31
+ /**
32
+ * 处理响应错误,子类可重写此方法自定义错误处理
33
+ * 显式声明以确保类型一致性,避免打包后的类型不兼容问题
34
+ * @param error - Axios 错误对象
35
+ * @returns 处理后的错误对象
36
+ */
37
+ processResponseError(error: AxiosError): Promise<AxiosError>;
38
+ /**
39
+ * 处理 HTTP 状态码
40
+ * 重写父类方法,确保子类可以重写此方法
41
+ * @param response - Axios 响应对象
42
+ */
43
+ protected handleHttpStatus(response: AxiosResponse): void;
44
+ /**
45
+ * 处理认证错误(401 - 未授权/登录失效)
46
+ * 重写父类方法,处理 HTTP 401 错误
47
+ * 子类可重写此方法来自定义 HTTP 认证错误处理逻辑
48
+ * @param error - Axios 错误对象
49
+ */
50
+ protected handleAuthenticationError(error: AxiosError): void;
51
+ /**
52
+ * 处理超时错误
53
+ * 重写父类方法,确保子类可以重写此方法
54
+ * @param error - Axios 错误对象
55
+ */
56
+ protected handleTimeoutError(error: AxiosError): void;
57
+ /**
58
+ * 处理网络错误(其他错误)
59
+ * 重写父类方法,确保子类可以重写此方法
60
+ * @param error - Axios 错误对象
61
+ */
62
+ protected handleNetworkError(error: AxiosError): void;
63
+ /**
64
+ * 处理成功响应
65
+ * 重写父类方法,在标准 HTTP 成功响应基础上,处理业务特定的响应结构
66
+ * 支持嵌套路径解析,自动处理业务层的登录失效、系统异常等错误
67
+ * 注意:HTTP 层的错误(如 HTTP 401、超时等)由父类 BaseHttpClient 处理
68
+ * @param response - Axios 响应对象
69
+ * @returns 解析后的响应数据
70
+ */
71
+ protected handleSuccessResponse(response: AxiosResponse): AxiosResponse['data'];
72
+ /**
73
+ * 支持路径解析的辅助函数
74
+ * @param obj
75
+ * @param path
76
+ * @protected
77
+ */
78
+ protected getValueByPath(obj: any, path: string | undefined): any;
79
+ /**
80
+ * 解析响应字段,支持嵌套路径解析
81
+ * 子类可重写此方法来自定义字段解析逻辑
82
+ * @param data - 响应数据对象
83
+ * @returns 解析后的字段值对象
84
+ */
85
+ protected parseResponseFields(data: any): {
86
+ code: any;
87
+ message: any;
88
+ responseData: any;
89
+ };
90
+ /**
91
+ * 处理系统异常错误(-1 - 系统异常)
92
+ * 子类可重写此方法来自定义系统异常处理逻辑
93
+ * @param response - Axios 响应对象
94
+ * @param code - 响应状态码
95
+ * @param message - 错误消息
96
+ * @param responseData - 响应数据
97
+ */
98
+ protected handleSystemError(response: AxiosResponse, code: any, message: any, responseData: any): void;
99
+ /**
100
+ * 处理业务错误(其他非200错误码)
101
+ * 子类可重写此方法来自定义业务错误处理逻辑
102
+ * @param code - 响应状态码
103
+ * @param message - 错误消息
104
+ */
105
+ protected handleBusinessError(code: any, message: any): void;
106
+ /**
107
+ * 处理错误数组 errors(如果有配置)
108
+ * 子类可重写此方法来自定义错误数组处理逻辑
109
+ * @param httpData
110
+ * @param response
111
+ */
112
+ protected handleErrorArray(httpData: any, response: AxiosResponse): void;
113
+ /**
114
+ * 显示错误数组通知
115
+ * 子类可重写此方法来自定义错误数组通知显示方式
116
+ * @param errors - 错误数组
117
+ * @param notificationOptions
118
+ */
119
+ protected showErrorArrayNotification(errors: Array<{
120
+ code: string;
121
+ message: string;
122
+ }>, notificationOptions?: NotificationOptions): void;
123
+ /**
124
+ * 处理提示信息 tips(如果有配置)
125
+ * 子类可重写此方法来自定义提示信息处理逻辑
126
+ * @param httpData
127
+ * @param response
128
+ */
129
+ protected handleTips(httpData: any, response: AxiosResponse): void;
130
+ /**
131
+ * 显示提示信息通知
132
+ * 子类可重写此方法来自定义提示信息通知显示方式
133
+ * @param tips - 提示信息数组
134
+ * @param notificationOptions
135
+ */
136
+ protected showTipsNotification(tips: Array<{
137
+ code: string;
138
+ message: string;
139
+ }>, notificationOptions?: NotificationOptions): void;
140
+ /**
141
+ * 显示系统异常对话框,当响应状态码为 -1 时调用
142
+ * @param response - Axios 响应对象
143
+ * @param responseData - 响应数据
144
+ * @param code - 错误状态码
145
+ * @param message - 错误消息
146
+ */
147
+ private showSystemExceptionDialog;
148
+ /**
149
+ * 上报错误信息到服务器,默认实现仅显示提示,子类可重写实现真实上报
150
+ * @param errorInfo - 错误信息对象
151
+ */
152
+ protected reportError(errorInfo: any): Promise<void>;
153
+ /**
154
+ * 发送 HTTP 请求,所有 HTTP 方法最终都调用此方法
155
+ * 显式声明以确保类型一致性,子类可重写此方法
156
+ * @param config - Axios 请求配置对象
157
+ * @returns 解析后的响应数据
158
+ */
159
+ protected request<R>(config: ExtendedAxiosRequestConfig): Promise<AxiosResponse['data']>;
160
+ /**
161
+ * 发送 GET 请求
162
+ * 显式声明以确保类型一致性,子类可重写此方法
163
+ * @param url - 请求 URL 路径
164
+ * @param params - 查询参数对象
165
+ * @param config - 额外的请求配置
166
+ * @returns 解析后的响应数据
167
+ */
168
+ get<R>(url: string, params?: Record<string, any>, config?: ExtendedAxiosRequestConfig): Promise<AxiosResponse['data']>;
169
+ /**
170
+ * 发送 POST 请求
171
+ * 显式声明以确保类型一致性,子类可重写此方法
172
+ * @param url - 请求 URL 路径
173
+ * @param data - 请求体数据
174
+ * @param config - 额外的请求配置
175
+ * @returns 解析后的响应数据
176
+ */
177
+ post<R>(url: string, data?: Record<string, any>, config?: ExtendedAxiosRequestConfig): Promise<AxiosResponse['data']>;
178
+ /**
179
+ * 发送 DELETE 请求
180
+ * 显式声明以确保类型一致性,子类可重写此方法
181
+ * @param url - 请求 URL 路径
182
+ * @param params - 查询参数对象
183
+ * @param config - 额外的请求配置
184
+ * @returns 解析后的响应数据
185
+ */
186
+ delete<R>(url: string, params?: Record<string, any>, config?: ExtendedAxiosRequestConfig): Promise<AxiosResponse['data']>;
187
+ /**
188
+ * 发送 PUT 请求
189
+ * 显式声明以确保类型一致性,子类可重写此方法
190
+ * @param url - 请求 URL 路径
191
+ * @param data - 请求体数据
192
+ * @param config - 额外的请求配置
193
+ * @returns 解析后的响应数据
194
+ */
195
+ put<R>(url: string, data?: Record<string, any>, config?: ExtendedAxiosRequestConfig): Promise<AxiosResponse['data']>;
196
+ /**
197
+ * 批量请求,并发发送多个请求
198
+ * 显式声明以确保类型一致性,子类可重写此方法
199
+ * @param requests - 请求配置数组或已发起的请求 Promise 数组
200
+ * @returns 所有请求的响应数据数组
201
+ */
202
+ all<R>(requests: Array<ExtendedAxiosRequestConfig | Promise<AxiosResponse<R>>>): Promise<AxiosResponse['data'][]>;
203
+ /**
204
+ * 文件上传,将文件包装为 FormData 发送
205
+ * 显式声明以确保类型一致性,子类可重写此方法
206
+ * @param url - 上传地址
207
+ * @param file - 文件对象
208
+ * @param config - 额外的请求配置
209
+ * @returns 解析后的响应数据
210
+ */
211
+ uploadFile<R>(url: string, file: File | Blob, config?: ExtendedAxiosRequestConfig): Promise<AxiosResponse['data']>;
212
+ /**
213
+ * 下载文件,将 Blob 对象下载到本地
214
+ * 显式声明以确保类型一致性,子类可重写此方法
215
+ * @param blob - Blob 对象
216
+ * @param filename - 文件名,如果不提供则使用时间戳
217
+ */
218
+ downloadFile(blob: Blob, filename?: string): void;
219
+ }
package/es/index.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import { default as BaseHttpClient } from './BaseHttpClient.ts';
2
+ import { default as BaseApi } from './class.ts';
3
+ import { default as VueAxiosPlugin, createHttpService, getHttpService } from './netseriver.ts';
4
+ export { BaseApi, BaseHttpClient, createHttpService, getHttpService, VueAxiosPlugin, };
5
+ export type { BaseApiConfig, BaseHttpClientConfig, vueAxiosPluginOptionsType, vueAxiosPluginType, vueHttpServiceType, } from './_types/index.ts';
6
+ export default getHttpService;
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("14c6052f-e4ae-4502-9336-1f25c81ac076")) {
5
+ if (!document.getElementById("fff2b744-05a5-42b2-a548-d0942177ace6")) {
6
6
  var elementStyle = document.createElement("style");
7
- elementStyle.id = "14c6052f-e4ae-4502-9336-1f25c81ac076";
7
+ elementStyle.id = "fff2b744-05a5-42b2-a548-d0942177ace6";
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
  }
@@ -12613,8 +12613,8 @@ class BaseApi extends BaseHttpClient {
12613
12613
  code: "Code",
12614
12614
  message: "Message",
12615
12615
  data: "data",
12616
- errors: "errors",
12617
- tips: "tips",
12616
+ errors: "data.errors",
12617
+ tips: "data.tips",
12618
12618
  ...responseFields
12619
12619
  };
12620
12620
  this.enableSystemErrorDialog = enableSystemErrorDialog;
@@ -12691,12 +12691,36 @@ class BaseApi extends BaseHttpClient {
12691
12691
  const httpData = super.handleSuccessResponse(response);
12692
12692
  const parsedFields = this.parseResponseFields(httpData);
12693
12693
  const { code, message: message2, responseData } = parsedFields;
12694
+ const config = response.config;
12695
+ const isCustomMessage = (config == null ? void 0 : config.isCustomMessage) ?? false;
12694
12696
  this.handleSystemError(response, code, message2, responseData);
12695
- this.handleBusinessError(code, message2);
12696
- this.handleErrorArray(responseData);
12697
- this.handleTips(responseData);
12697
+ if (!isCustomMessage) {
12698
+ this.handleBusinessError(code, message2, response);
12699
+ this.handleErrorArray(httpData, response);
12700
+ this.handleTips(httpData, response);
12701
+ }
12698
12702
  return responseData;
12699
12703
  }
12704
+ /**
12705
+ * 支持路径解析的辅助函数
12706
+ * @param obj
12707
+ * @param path
12708
+ * @protected
12709
+ */
12710
+ getValueByPath(obj, path) {
12711
+ if (!path)
12712
+ return obj;
12713
+ const keys = path.split(".");
12714
+ let result = obj;
12715
+ for (const key of keys) {
12716
+ if (result && typeof result === "object" && key in result) {
12717
+ result = result[key];
12718
+ } else {
12719
+ return void 0;
12720
+ }
12721
+ }
12722
+ return result;
12723
+ }
12700
12724
  /**
12701
12725
  * 解析响应字段,支持嵌套路径解析
12702
12726
  * 子类可重写此方法来自定义字段解析逻辑
@@ -12705,23 +12729,9 @@ class BaseApi extends BaseHttpClient {
12705
12729
  */
12706
12730
  parseResponseFields(data) {
12707
12731
  var _a2, _b, _c;
12708
- const getValueByPath = (obj, path) => {
12709
- if (!path)
12710
- return obj;
12711
- const keys = path.split(".");
12712
- let result = obj;
12713
- for (const key of keys) {
12714
- if (result && typeof result === "object" && key in result) {
12715
- result = result[key];
12716
- } else {
12717
- return void 0;
12718
- }
12719
- }
12720
- return result;
12721
- };
12722
- const code = getValueByPath(data, (_a2 = this.responseFields) == null ? void 0 : _a2.code);
12723
- const message2 = getValueByPath(data, (_b = this.responseFields) == null ? void 0 : _b.message);
12724
- const responseData = getValueByPath(data, (_c = this.responseFields) == null ? void 0 : _c.data);
12732
+ const code = this.getValueByPath(data, (_a2 = this.responseFields) == null ? void 0 : _a2.code);
12733
+ const message2 = this.getValueByPath(data, (_b = this.responseFields) == null ? void 0 : _b.message);
12734
+ const responseData = this.getValueByPath(data, (_c = this.responseFields) == null ? void 0 : _c.data);
12725
12735
  return { code, message: message2, responseData };
12726
12736
  }
12727
12737
  /**
@@ -12761,15 +12771,18 @@ class BaseApi extends BaseHttpClient {
12761
12771
  /**
12762
12772
  * 处理错误数组 errors(如果有配置)
12763
12773
  * 子类可重写此方法来自定义错误数组处理逻辑
12764
- * @param responseData - 响应数据
12774
+ * @param httpData
12775
+ * @param response
12765
12776
  */
12766
- handleErrorArray(responseData) {
12777
+ handleErrorArray(httpData, response) {
12767
12778
  var _a2;
12768
12779
  const errorsField = (_a2 = this.responseFields) == null ? void 0 : _a2.errors;
12769
12780
  if (errorsField) {
12770
- const errors = responseData == null ? void 0 : responseData[errorsField];
12781
+ const errors = this.getValueByPath(httpData, errorsField);
12771
12782
  if (Array.isArray(errors) && errors.length) {
12772
- this.showErrorArrayNotification(errors);
12783
+ const config = response.config;
12784
+ const notificationOptions = config == null ? void 0 : config.errorNotificationOptions;
12785
+ this.showErrorArrayNotification(errors, notificationOptions);
12773
12786
  throw new Error("请求错误");
12774
12787
  }
12775
12788
  }
@@ -12778,37 +12791,47 @@ class BaseApi extends BaseHttpClient {
12778
12791
  * 显示错误数组通知
12779
12792
  * 子类可重写此方法来自定义错误数组通知显示方式
12780
12793
  * @param errors - 错误数组
12794
+ * @param notificationOptions
12781
12795
  */
12782
- showErrorArrayNotification(errors) {
12796
+ showErrorArrayNotification(errors, notificationOptions) {
12783
12797
  var _a2, _b;
12784
12798
  const html = errors.map((item) => `<div style="font-size: 14px;color:red">${item.code}:${item.message}</div>`).join("");
12799
+ const defaultOptions = {
12800
+ title: "提示",
12801
+ type: "error"
12802
+ };
12785
12803
  if (hasDocument) {
12786
- (_a2 = this.notificationInstance) == null ? void 0 : _a2.call(this, {
12787
- title: "提示",
12788
- message: html,
12789
- type: "error"
12790
- });
12804
+ const finalOptions = {
12805
+ ...defaultOptions,
12806
+ ...notificationOptions,
12807
+ message: html
12808
+ };
12809
+ (_a2 = this.notificationInstance) == null ? void 0 : _a2.call(this, finalOptions);
12791
12810
  } else {
12792
12811
  const errorMessages = errors.map((item) => `${item.code}:${item.message}`).join("\n");
12793
- (_b = this.notificationInstance) == null ? void 0 : _b.call(this, {
12794
- title: "提示",
12795
- message: errorMessages,
12796
- type: "error"
12797
- });
12812
+ const finalOptions = {
12813
+ ...defaultOptions,
12814
+ ...notificationOptions,
12815
+ message: errorMessages
12816
+ };
12817
+ (_b = this.notificationInstance) == null ? void 0 : _b.call(this, finalOptions);
12798
12818
  }
12799
12819
  }
12800
12820
  /**
12801
12821
  * 处理提示信息 tips(如果有配置)
12802
12822
  * 子类可重写此方法来自定义提示信息处理逻辑
12803
- * @param responseData - 响应数据
12823
+ * @param httpData
12824
+ * @param response
12804
12825
  */
12805
- handleTips(responseData) {
12826
+ handleTips(httpData, response) {
12806
12827
  var _a2;
12807
12828
  const tipsField = (_a2 = this.responseFields) == null ? void 0 : _a2.tips;
12808
12829
  if (tipsField) {
12809
- const tips = responseData == null ? void 0 : responseData[tipsField];
12830
+ const tips = this.getValueByPath(httpData, tipsField);
12810
12831
  if (Array.isArray(tips) && tips.length) {
12811
- this.showTipsNotification(tips);
12832
+ const config = response.config;
12833
+ const notificationOptions = config == null ? void 0 : config.tipsNotificationOptions;
12834
+ this.showTipsNotification(tips, notificationOptions);
12812
12835
  }
12813
12836
  }
12814
12837
  }
@@ -12816,23 +12839,30 @@ class BaseApi extends BaseHttpClient {
12816
12839
  * 显示提示信息通知
12817
12840
  * 子类可重写此方法来自定义提示信息通知显示方式
12818
12841
  * @param tips - 提示信息数组
12842
+ * @param notificationOptions
12819
12843
  */
12820
- showTipsNotification(tips) {
12844
+ showTipsNotification(tips, notificationOptions) {
12821
12845
  var _a2, _b;
12822
12846
  const html = tips.map((item) => `<div style="font-size: 14px;color:#E6A23C">${item.code}:${item.message}</div>`).join("");
12847
+ const defaultOptions = {
12848
+ title: "提示",
12849
+ type: "warning"
12850
+ };
12823
12851
  if (hasDocument) {
12824
- (_a2 = this.notificationInstance) == null ? void 0 : _a2.call(this, {
12825
- title: "提示",
12826
- message: html,
12827
- type: "warning"
12828
- });
12852
+ const finalOptions = {
12853
+ ...defaultOptions,
12854
+ ...notificationOptions,
12855
+ message: html
12856
+ };
12857
+ (_a2 = this.notificationInstance) == null ? void 0 : _a2.call(this, finalOptions);
12829
12858
  } else {
12830
12859
  const tipMessages = tips.map((item) => `${item.code}:${item.message}`).join("\n");
12831
- (_b = this.notificationInstance) == null ? void 0 : _b.call(this, {
12832
- title: "提示",
12833
- message: tipMessages,
12834
- type: "warning"
12835
- });
12860
+ const finalOptions = {
12861
+ ...defaultOptions,
12862
+ ...notificationOptions,
12863
+ message: tipMessages
12864
+ };
12865
+ (_b = this.notificationInstance) == null ? void 0 : _b.call(this, finalOptions);
12836
12866
  }
12837
12867
  }
12838
12868
  /**
@@ -0,0 +1,26 @@
1
+ import { BaseApiConfig, vueAxiosPluginType, vueHttpServiceType } from './_types/index.ts';
2
+ import { default as BaseApi } from './class.ts';
3
+ declare global {
4
+ interface Window {
5
+ $http?: vueHttpServiceType;
6
+ }
7
+ }
8
+ /**
9
+ * 创建 HTTP 服务实例
10
+ * @param options - API 配置对象
11
+ * @returns BaseApi 实例
12
+ */
13
+ declare function createHttpService(options?: BaseApiConfig): BaseApi;
14
+ /**
15
+ * Vue Axios 插件,用于在 Vue 应用中全局注册 HTTP 服务
16
+ * 提供 this.$http、inject('$http') 和 window.$http 三种使用方式
17
+ */
18
+ declare const VueAxiosPlugin: vueAxiosPluginType;
19
+ /**
20
+ * 获取 HTTP 服务实例,与 createHttpService 功能相同
21
+ * @param options - API 配置对象
22
+ * @returns BaseApi 实例
23
+ */
24
+ declare function getHttpService(options?: BaseApiConfig): BaseApi;
25
+ export default VueAxiosPlugin;
26
+ export { createHttpService, getHttpService };
@@ -0,0 +1,40 @@
1
+ export default service;
2
+ /**
3
+ * 创建 axios 实例
4
+ * @type {NewApi}
5
+ */
6
+ declare const service: NewApi;
7
+ /**
8
+ * 自定义 API 类,继承自 BaseApi
9
+ * 实现了原有的 axios 封装功能
10
+ */
11
+ export class NewApi {
12
+ constructor(config?: {});
13
+ /**
14
+ * 处理请求配置
15
+ * 在请求发送之前进行一些处理
16
+ * 对应旧代码的请求拦截器逻辑
17
+ */
18
+ processRequestConfig(config: any): any;
19
+ /**
20
+ * 处理 HTTP 状态码
21
+ */
22
+ handleHttpStatus(response: any): void;
23
+ /**
24
+ * 处理业务错误(重写父类方法,不抛出错误,只显示消息)
25
+ */
26
+ handleBusinessError(code: any, message: any): void;
27
+ /**
28
+ * 处理成功响应(完全重写,符合旧代码逻辑)
29
+ */
30
+ handleSuccessResponse(response: any): any;
31
+ /**
32
+ * 处理超时错误(重写父类方法)
33
+ */
34
+ handleTimeoutError(error: any): void;
35
+ /**
36
+ * 处理网络错误(重写父类方法)
37
+ */
38
+ handleNetworkError(error: any): void;
39
+ }
40
+ export function showErrorNotification(title: any, errors: any): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moluoxixi/ajax-package",
3
- "version": "0.0.16",
3
+ "version": "0.0.17-beta.1",
4
4
  "description": "AjaxPackage 组件",
5
5
  "sideEffects": [
6
6
  "*.css",