@ruan-cat/utils 4.5.0 → 4.7.0
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ruan-cat/utils",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.7.0",
|
|
4
4
|
"description": "阮喵喵工具集合。默认提供js文件,也直接提供ts文件。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
"tsconfig.json"
|
|
61
61
|
],
|
|
62
62
|
"dependencies": {
|
|
63
|
-
"@vueuse/integrations": "^
|
|
63
|
+
"@vueuse/integrations": "^13.3.0",
|
|
64
64
|
"axios": "^1.8.1",
|
|
65
65
|
"consola": "^3.4.0",
|
|
66
66
|
"lodash-es": "^4.17.21"
|
|
@@ -43,6 +43,7 @@ export interface StrictUseAxiosReturn<
|
|
|
43
43
|
K extends KeyAxiosRequestConfig<D>,
|
|
44
44
|
R,
|
|
45
45
|
D,
|
|
46
|
+
O extends UseAxiosOptions = UseAxiosOptions<T>,
|
|
46
47
|
> extends UseAxiosReturn<T, R, D> {
|
|
47
48
|
/**
|
|
48
49
|
* Manually call the axios request
|
|
@@ -50,7 +51,7 @@ export interface StrictUseAxiosReturn<
|
|
|
50
51
|
execute: (
|
|
51
52
|
url?: string | CreateAxiosRequestConfig<K, D>,
|
|
52
53
|
config?: CreateAxiosRequestConfig<K, D>,
|
|
53
|
-
) => Promise<StrictUseAxiosReturn<T, K, R, D>>;
|
|
54
|
+
) => Promise<StrictUseAxiosReturn<T, K, R, D, O>>;
|
|
54
55
|
}
|
|
55
56
|
|
|
56
57
|
declare module "@vueuse/integrations/useAxios" {
|
|
@@ -65,12 +66,13 @@ declare module "@vueuse/integrations/useAxios" {
|
|
|
65
66
|
K extends KeyAxiosRequestConfig<D> = "url",
|
|
66
67
|
R = AxiosResponse<T>,
|
|
67
68
|
D = any,
|
|
69
|
+
O extends UseAxiosOptionsBase<T> = UseAxiosOptionsBase<T>,
|
|
68
70
|
>(
|
|
69
71
|
url: string,
|
|
70
72
|
config: AxiosRequestConfig<D>,
|
|
71
73
|
instance: AxiosInstance,
|
|
72
|
-
options
|
|
73
|
-
): StrictUseAxiosReturn<T, K, R, D> & Promise<StrictUseAxiosReturn<T, K, R, D>>;
|
|
74
|
+
options: O,
|
|
75
|
+
): StrictUseAxiosReturn<T, K, R, D, O> & Promise<StrictUseAxiosReturn<T, K, R, D, O>>;
|
|
74
76
|
}
|
|
75
77
|
|
|
76
78
|
/** 包装器的参数 @version 2 */
|
|
@@ -142,10 +144,11 @@ export function useAxiosWrapper<
|
|
|
142
144
|
T,
|
|
143
145
|
UseAxiosOptionsLike extends UseAxiosOptionsBase,
|
|
144
146
|
D = any,
|
|
147
|
+
O extends UseAxiosOptionsBase<T> = UseAxiosOptionsBase<T>,
|
|
145
148
|
>(params: UseAxiosWrapperParams<K, T, UseAxiosOptionsLike, D>) {
|
|
146
|
-
const { config = {}, instance, options = {} } = params;
|
|
149
|
+
const { config = {}, instance, options = {} as O } = params;
|
|
147
150
|
const url = params.url || "";
|
|
148
|
-
return useAxios<T, K, AxiosResponse<T>, D>(url, config, instance, options);
|
|
151
|
+
return useAxios<T, K, AxiosResponse<T>, D, O>(url, config, instance, options as O);
|
|
149
152
|
}
|
|
150
153
|
|
|
151
154
|
export * from "./v1.js";
|
|
@@ -8,13 +8,7 @@ import axios from "axios";
|
|
|
8
8
|
import type { JsonVO } from "./types/JsonVO";
|
|
9
9
|
|
|
10
10
|
import type { AxiosRequestConfigBaseKey, HttpParamWay } from "./tools.ts";
|
|
11
|
-
import {
|
|
12
|
-
createDefaultUseAxiosOptions,
|
|
13
|
-
setHeaders,
|
|
14
|
-
UpType,
|
|
15
|
-
setDefaultUseAxiosOptions,
|
|
16
|
-
setDataByHttpParamWay,
|
|
17
|
-
} from "./tools";
|
|
11
|
+
import { createDefaultUseAxiosOptions, setHeaders, UpType, setDefaultUseAxiosOptions } from "./tools";
|
|
18
12
|
|
|
19
13
|
/**
|
|
20
14
|
* 内部用的 axios 实例
|
|
@@ -34,7 +28,7 @@ export function defineAxiosInstance(axiosInstance: AxiosInstance) {
|
|
|
34
28
|
}
|
|
35
29
|
|
|
36
30
|
export type ParamsPathKey = AxiosRequestConfigBaseKey | "url";
|
|
37
|
-
export type ParamsQueryKey = AxiosRequestConfigBaseKey | "
|
|
31
|
+
export type ParamsQueryKey = AxiosRequestConfigBaseKey | "params";
|
|
38
32
|
export type ParamsBodyKey = AxiosRequestConfigBaseKey | "data";
|
|
39
33
|
|
|
40
34
|
type UseAxiosWrapperUseKey<T extends HttpParamWay, K extends KeyAxiosRequestConfig> =
|
|
@@ -61,6 +55,7 @@ export function useRequestIn01s<
|
|
|
61
55
|
T = any,
|
|
62
56
|
D = any,
|
|
63
57
|
HttpParamWayTemp extends HttpParamWay = Params<K>["httpParamWay"],
|
|
58
|
+
O extends UseAxiosOptionsBase<JsonVO<T>> = UseAxiosOptionsBase<JsonVO<T>>,
|
|
64
59
|
>(
|
|
65
60
|
params: Params<
|
|
66
61
|
//
|
|
@@ -79,16 +74,15 @@ export function useRequestIn01s<
|
|
|
79
74
|
} = params;
|
|
80
75
|
setHeaders(config, upType);
|
|
81
76
|
setDefaultUseAxiosOptions(options);
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
config,
|
|
85
|
-
});
|
|
77
|
+
|
|
78
|
+
// console.log(" 最后请求前,看一下数据? ", config);
|
|
86
79
|
// console.log(" instance ?", instance.defaults.baseURL);
|
|
87
80
|
return useAxiosWrapper<
|
|
88
81
|
//
|
|
89
82
|
UseAxiosWrapperUseKey<HttpParamWayTemp, K>,
|
|
90
83
|
JsonVO<T>,
|
|
91
84
|
UseAxiosOptionsBase<JsonVO<T>>,
|
|
92
|
-
D
|
|
85
|
+
D,
|
|
86
|
+
O
|
|
93
87
|
>({ config, instance, options, url });
|
|
94
88
|
}
|
|
@@ -12,6 +12,15 @@ import { isConditionsSome } from "../../index";
|
|
|
12
12
|
* @description
|
|
13
13
|
* 用于控制接口请求时的参数传递方式
|
|
14
14
|
* @see https://www.cnblogs.com/jinyuanya/p/13934722.html
|
|
15
|
+
*
|
|
16
|
+
* @description
|
|
17
|
+
* 警告 该配置目前失去意义
|
|
18
|
+
*
|
|
19
|
+
* 该配置目前不再被使用了 不会被任何函数使用 配置起来属于无意义内容
|
|
20
|
+
*
|
|
21
|
+
* 未来会被删除 并重新整理对应的接口生成成果
|
|
22
|
+
*
|
|
23
|
+
* TODO: 准备删除该工具
|
|
15
24
|
*/
|
|
16
25
|
export type HttpParamWay =
|
|
17
26
|
// 路径传参
|
|
@@ -78,6 +87,44 @@ export enum HttpCode {
|
|
|
78
87
|
SUCCESS = 10000,
|
|
79
88
|
}
|
|
80
89
|
|
|
90
|
+
/**
|
|
91
|
+
* 请求结果分类
|
|
92
|
+
* @description
|
|
93
|
+
* 成功 失败 错误
|
|
94
|
+
*/
|
|
95
|
+
export type HttpStatus = "success" | "fail" | "error";
|
|
96
|
+
|
|
97
|
+
/** 接口请求状态是否是成功的? */
|
|
98
|
+
export function isHttpStatusSuccess(status: HttpStatus): status is "success" {
|
|
99
|
+
return status === "success";
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* http码的映射存储值
|
|
104
|
+
* @description
|
|
105
|
+
* 根据业务类型存储不同的值
|
|
106
|
+
*/
|
|
107
|
+
export interface HttpCodeMessageMapValue {
|
|
108
|
+
/** 接口提示文本 */
|
|
109
|
+
message: string;
|
|
110
|
+
/** 接口请求状态 */
|
|
111
|
+
type: HttpStatus;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** 请求码与请求响应文本的映射表 */
|
|
115
|
+
export const HttpCodeMessageMap: Record<HttpCode, HttpCodeMessageMapValue> = {
|
|
116
|
+
[HttpCode.UNAUTHORIZED]: { message: "未登录或登录已过期,请重新登录", type: "error" },
|
|
117
|
+
[HttpCode.FORBIDDEN]: { message: "没有相关权限", type: "error" },
|
|
118
|
+
[HttpCode.NOT_FOUND]: { message: "访问页面未找到", type: "error" },
|
|
119
|
+
[HttpCode.SERVER_ERROR]: { message: "服务器错误", type: "error" },
|
|
120
|
+
[HttpCode.PARAMS_INVALID]: { message: "上传参数异常", type: "error" },
|
|
121
|
+
[HttpCode.CONTENT_TYPE_ERR]: { message: "ContentType错误", type: "error" },
|
|
122
|
+
[HttpCode.API_UN_IMPL]: { message: "功能尚未实现", type: "error" },
|
|
123
|
+
[HttpCode.SERVER_BUSY]: { message: "服务器繁忙", type: "error" },
|
|
124
|
+
[HttpCode.FAIL]: { message: "操作失败", type: "fail" },
|
|
125
|
+
[HttpCode.SUCCESS]: { message: "操作成功", type: "success" },
|
|
126
|
+
};
|
|
127
|
+
|
|
81
128
|
/**
|
|
82
129
|
* 上传类型-请求体类型 枚举
|
|
83
130
|
* @description
|
|
@@ -165,12 +212,29 @@ export function setDefaultUseAxiosOptions<T>(options: UseAxiosOptionsBase<T>) {
|
|
|
165
212
|
options.immediate = options?.immediate ?? _options.immediate;
|
|
166
213
|
}
|
|
167
214
|
|
|
168
|
-
/**
|
|
215
|
+
/**
|
|
216
|
+
* @private
|
|
217
|
+
* @deprecated
|
|
218
|
+
* 废弃该功能
|
|
219
|
+
*
|
|
220
|
+
* 该函数没意义 重设请求前的参数 没有意义
|
|
221
|
+
*
|
|
222
|
+
* 重新设置的参数 没办法及时传递到真正执行接口请求的函数内
|
|
223
|
+
*/
|
|
169
224
|
interface SetDataByHttpParamWayParams {
|
|
170
225
|
httpParamWay: HttpParamWay;
|
|
171
226
|
config: AxiosRequestConfig;
|
|
172
227
|
}
|
|
173
|
-
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* @deprecated
|
|
231
|
+
* 废弃该功能
|
|
232
|
+
*
|
|
233
|
+
* 该函数没意义 重设请求前的参数 没有意义
|
|
234
|
+
*
|
|
235
|
+
* 重新设置的参数 没办法及时传递到真正执行接口请求的函数内
|
|
236
|
+
*/
|
|
237
|
+
function setDataByHttpParamWay(params: SetDataByHttpParamWayParams) {
|
|
174
238
|
const { httpParamWay, config } = params;
|
|
175
239
|
if (
|
|
176
240
|
isConditionsSome([
|