@ruan-cat/utils 1.3.0 → 1.3.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.
- package/package.json +20 -4
- package/readme.md +2 -0
- package/src/index.ts +1 -0
- package/src/vueuse/useAxios.ts +126 -0
- package/tsconfig.json +4 -9
package/package.json
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ruan-cat/utils",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.1",
|
|
4
4
|
"description": "阮喵喵工具集合。一个纯typescript库。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
7
7
|
"types": "./src/index.ts",
|
|
8
|
-
"homepage": "https://
|
|
8
|
+
"homepage": "https://utils.ruancat6312.top",
|
|
9
9
|
"bugs": {
|
|
10
10
|
"url": "https://github.com/ruan-cat/vercel-monorepo-test/issues"
|
|
11
11
|
},
|
|
12
12
|
"repository": {
|
|
13
13
|
"type": "git",
|
|
14
14
|
"url": "git+https://github.com/ruan-cat/vercel-monorepo-test.git",
|
|
15
|
-
"directory": "utils"
|
|
15
|
+
"directory": "packages/utils"
|
|
16
16
|
},
|
|
17
17
|
"exports": {
|
|
18
18
|
".": {
|
|
@@ -35,17 +35,26 @@
|
|
|
35
35
|
},
|
|
36
36
|
"files": [
|
|
37
37
|
"src",
|
|
38
|
+
"!src/tests",
|
|
38
39
|
"tsconfig.json"
|
|
39
40
|
],
|
|
40
41
|
"devDependencies": {
|
|
41
42
|
"@antfu/utils": "^0.7.10",
|
|
43
|
+
"@types/qs": "^6.9.17",
|
|
44
|
+
"@vueuse/integrations": "^12.1.0",
|
|
45
|
+
"axios": "^1.7.9",
|
|
46
|
+
"qs": "^6.13.1",
|
|
47
|
+
"tsup": "^8.3.5",
|
|
48
|
+
"type-plus": "^7.6.2",
|
|
42
49
|
"typedoc": "^0.27.0",
|
|
43
50
|
"typedoc-plugin-markdown": "^4.3.0",
|
|
44
51
|
"typescript": "5.7.2",
|
|
45
52
|
"unplugin-vue-router": "^0.10.8",
|
|
46
|
-
"@ruan-cat/vuepress-preset-config": "^0.1.
|
|
53
|
+
"@ruan-cat/vuepress-preset-config": "^0.1.14"
|
|
47
54
|
},
|
|
48
55
|
"peerDependencies": {
|
|
56
|
+
"@vueuse/integrations": "^12",
|
|
57
|
+
"axios": "^1.7.x",
|
|
49
58
|
"typescript": "5.7.2",
|
|
50
59
|
"unplugin-vue-router": "^0.10.8"
|
|
51
60
|
},
|
|
@@ -55,6 +64,12 @@
|
|
|
55
64
|
},
|
|
56
65
|
"unplugin-vue-router": {
|
|
57
66
|
"optional": true
|
|
67
|
+
},
|
|
68
|
+
"@vueuse/integrations": {
|
|
69
|
+
"optional": true
|
|
70
|
+
},
|
|
71
|
+
"axios": {
|
|
72
|
+
"optional": true
|
|
58
73
|
}
|
|
59
74
|
},
|
|
60
75
|
"scripts": {
|
|
@@ -73,6 +88,7 @@
|
|
|
73
88
|
"generate:type-3": "vue-tsc -p tsconfig.types.json",
|
|
74
89
|
"get-type": "pnpm clean:type && pnpm generate:type-3",
|
|
75
90
|
"build:-try-1": "pnpm clean:type && vue-tsc -p tsconfig.build.json",
|
|
91
|
+
"build:tsup": "tsup",
|
|
76
92
|
"rm:node_modules": "rimraf node_modules"
|
|
77
93
|
}
|
|
78
94
|
}
|
package/readme.md
CHANGED
package/src/index.ts
CHANGED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import type { RequiredPick } from "type-plus";
|
|
2
|
+
import type { AxiosRequestConfig, AxiosResponse, AxiosInstance } from "axios";
|
|
3
|
+
import type { UseAxiosOptions, UseAxiosReturn } from "@vueuse/integrations/useAxios";
|
|
4
|
+
|
|
5
|
+
/** 拓展的类型参数 用于约束必填的字段 */
|
|
6
|
+
export type KeyAxiosRequestConfig<D = any> = keyof AxiosRequestConfig<D>;
|
|
7
|
+
|
|
8
|
+
/** 填写key值的帮助类型 */
|
|
9
|
+
export type KeyHelper<K extends KeyAxiosRequestConfig> = K;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 创建 AxiosRequestConfig 的各种变种类型
|
|
13
|
+
* @description
|
|
14
|
+
* 目前需要给 AxiosRequestConfig 添加必填属性
|
|
15
|
+
*
|
|
16
|
+
* 故需要本工具创建各种变种类型
|
|
17
|
+
*
|
|
18
|
+
* @example CreateAxiosRequestConfig<"url", D>
|
|
19
|
+
*/
|
|
20
|
+
export type CreateAxiosRequestConfig<K extends keyof Target, D = any, Target = AxiosRequestConfig<D>> = RequiredPick<
|
|
21
|
+
Target,
|
|
22
|
+
K
|
|
23
|
+
>;
|
|
24
|
+
|
|
25
|
+
/** 拓展K泛型后的类型 */
|
|
26
|
+
export interface StrictUseAxiosReturn<
|
|
27
|
+
T,
|
|
28
|
+
/**
|
|
29
|
+
* 拓展的类型参数 用于约束必填的字段
|
|
30
|
+
* @description
|
|
31
|
+
* 这里不需要提供默认的取值
|
|
32
|
+
*/
|
|
33
|
+
K extends KeyAxiosRequestConfig<D>,
|
|
34
|
+
R,
|
|
35
|
+
D,
|
|
36
|
+
> extends UseAxiosReturn<T, R, D> {
|
|
37
|
+
/**
|
|
38
|
+
* Manually call the axios request
|
|
39
|
+
*/
|
|
40
|
+
execute: (
|
|
41
|
+
url?: string | CreateAxiosRequestConfig<K, D>,
|
|
42
|
+
config?: CreateAxiosRequestConfig<K, D>,
|
|
43
|
+
) => Promise<StrictUseAxiosReturn<T, K, R, D>>;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 拓展类型参数后的 useAxios 函数
|
|
48
|
+
* @description
|
|
49
|
+
* 在我们的封装中 使用本类型
|
|
50
|
+
*/
|
|
51
|
+
export declare function useAxios<
|
|
52
|
+
T = any,
|
|
53
|
+
/** 拓展的类型参数 用于约束必填的字段 */
|
|
54
|
+
K extends KeyAxiosRequestConfig<D> = "url",
|
|
55
|
+
R = AxiosResponse<T>,
|
|
56
|
+
D = any,
|
|
57
|
+
>(
|
|
58
|
+
url: string,
|
|
59
|
+
config: AxiosRequestConfig<D>,
|
|
60
|
+
instance: AxiosInstance,
|
|
61
|
+
options?: UseAxiosOptions,
|
|
62
|
+
): StrictUseAxiosReturn<T, K, R, D> & Promise<StrictUseAxiosReturn<T, K, R, D>>;
|
|
63
|
+
|
|
64
|
+
/** 包装器的参数 */
|
|
65
|
+
export interface UseAxiosWrapperParams<
|
|
66
|
+
/**
|
|
67
|
+
* 业务数据类型
|
|
68
|
+
* @description
|
|
69
|
+
* 必须先填写业务类型
|
|
70
|
+
*/
|
|
71
|
+
T = any,
|
|
72
|
+
/**
|
|
73
|
+
* AxiosRequestConfig 默认必填的字段
|
|
74
|
+
* @description
|
|
75
|
+
* 用于约束其他类型的字段
|
|
76
|
+
*
|
|
77
|
+
* 然后才能填写必传的参数类型
|
|
78
|
+
*
|
|
79
|
+
* 默认为 必填url请求地址的 config 请求配置
|
|
80
|
+
*/
|
|
81
|
+
K extends KeyAxiosRequestConfig<D> = "url",
|
|
82
|
+
/**
|
|
83
|
+
* UseAxiosOptions 的派生类型
|
|
84
|
+
*/
|
|
85
|
+
UseAxiosOptionsLike extends UseAxiosOptions = UseAxiosOptions,
|
|
86
|
+
/**
|
|
87
|
+
* AxiosRequestConfig 用的类型
|
|
88
|
+
* @description
|
|
89
|
+
* 最后才可以传递此类型
|
|
90
|
+
*/
|
|
91
|
+
D = any,
|
|
92
|
+
> {
|
|
93
|
+
/**
|
|
94
|
+
* axios的配置类型
|
|
95
|
+
* @description
|
|
96
|
+
* 默认为 必填url请求地址的 config 请求配置
|
|
97
|
+
*/
|
|
98
|
+
config: CreateAxiosRequestConfig<K, D>;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* axios实例
|
|
102
|
+
* @description
|
|
103
|
+
* 对于包装器函数而言 必须传递有意义的请求实例
|
|
104
|
+
*/
|
|
105
|
+
instance: AxiosInstance;
|
|
106
|
+
|
|
107
|
+
/** useAxios 的选项配置 */
|
|
108
|
+
options: UseAxiosOptionsLike;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* useAxios 的包装函数
|
|
113
|
+
* @description
|
|
114
|
+
* 其本质是对 useAxios 函数的封装,仅仅是包装了参数层
|
|
115
|
+
*
|
|
116
|
+
* 预期设计成一个万能的 通用的请求函数
|
|
117
|
+
*/
|
|
118
|
+
export function useAxiosWrapper<T, K extends KeyAxiosRequestConfig, D = any>(params: UseAxiosWrapperParams) {
|
|
119
|
+
const {
|
|
120
|
+
config: { url },
|
|
121
|
+
config,
|
|
122
|
+
instance,
|
|
123
|
+
options,
|
|
124
|
+
} = params;
|
|
125
|
+
return useAxios<T, K, AxiosResponse<T>, D>(url, config, instance, options);
|
|
126
|
+
}
|
package/tsconfig.json
CHANGED
|
@@ -33,21 +33,16 @@
|
|
|
33
33
|
"typedoc",
|
|
34
34
|
"typedoc-plugin-markdown",
|
|
35
35
|
],
|
|
36
|
-
"paths": {
|
|
37
|
-
"@/*": [
|
|
38
|
-
"./src/*"
|
|
39
|
-
]
|
|
40
|
-
}
|
|
41
36
|
},
|
|
42
37
|
"include": [
|
|
43
38
|
"./src/**/*.ts",
|
|
44
|
-
"./tests/**/*.ts",
|
|
45
39
|
"./src/**/*.js",
|
|
46
|
-
"./tests/**/*.
|
|
47
|
-
//
|
|
48
|
-
"./tests/with-types/types/CanNewTypeByClass.d.ts",
|
|
40
|
+
// "./tests/**/*.ts",
|
|
41
|
+
// "./tests/**/*.js",
|
|
49
42
|
],
|
|
50
43
|
"exclude": [
|
|
44
|
+
// 测试用例的全部文件 交由本项目根目录下的 tsconfig.json 处理
|
|
45
|
+
"src/tests/**/*.ts",
|
|
51
46
|
"notuse-typedoc.config.cjs"
|
|
52
47
|
]
|
|
53
48
|
}
|