@ruan-cat/utils 4.3.2 → 4.4.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.2",
3
+ "version": "4.4.0",
4
4
  "description": "阮喵喵工具集合。默认提供js文件,也直接提供ts文件。",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
@@ -32,6 +32,7 @@
32
32
  "./unplugin-vue-router": "./src/unplugin-vue-router/index.ts",
33
33
  "./vite-plugin-autogeneration-import-file": "./src/vite-plugin-autogeneration-import-file/index.ts",
34
34
  "./vueuse": "./src/vueuse/index.ts",
35
+ "./vueuse/*": "./src/vueuse/*",
35
36
  "./src/*": "./src/*",
36
37
  "./dist/*": "./dist/*"
37
38
  },
@@ -1 +1,2 @@
1
1
  export * from "./useAxios/index";
2
+ export * from "./useAxios-for-01s/index";
@@ -0,0 +1,4 @@
1
+ export * from "./types/JsonVO";
2
+ export * from "./types/PageDTO";
3
+ export * from "./tools";
4
+ export * from "./main";
@@ -0,0 +1,94 @@
1
+ import type { KeyAxiosRequestConfig, UseAxiosWrapperParams } from "../useAxios/index";
2
+ import { useAxiosWrapper } from "../useAxios/index";
3
+
4
+ import type { UseAxiosOptionsBase } from "@vueuse/integrations/useAxios";
5
+ import type { PartialPick } from "type-plus";
6
+ import type { AxiosInstance } from "axios";
7
+ import axios from "axios";
8
+ import type { JsonVO } from "./types/JsonVO";
9
+
10
+ import type { AxiosRequestConfigBaseKey, HttpParamWay } from "./tools.ts";
11
+ import {
12
+ createDefaultUseAxiosOptions,
13
+ setHeaders,
14
+ UpType,
15
+ setDefaultUseAxiosOptions,
16
+ setDataByHttpParamWay,
17
+ } from "./tools";
18
+
19
+ /**
20
+ * 内部用的 axios 实例
21
+ * @description
22
+ * 主动声明类型 用于约束
23
+ * @private
24
+ */
25
+ let innerAxiosInstance: AxiosInstance = axios;
26
+
27
+ /**
28
+ * 定义具体的 AxiosInstance 实例
29
+ * @description
30
+ * 让外部设置一次内部的默认请求实例
31
+ */
32
+ export function defineAxiosInstance(axiosInstance: AxiosInstance) {
33
+ innerAxiosInstance = axiosInstance;
34
+ }
35
+
36
+ export type ParamsPathKey = AxiosRequestConfigBaseKey | "url";
37
+ export type ParamsQueryKey = AxiosRequestConfigBaseKey | "data";
38
+ export type ParamsBodyKey = AxiosRequestConfigBaseKey | "data";
39
+
40
+ type UseAxiosWrapperUseKey<T extends HttpParamWay, K extends KeyAxiosRequestConfig> =
41
+ //
42
+ T extends HttpParamWay ? Exclude<K, AxiosRequestConfigBaseKey> : never;
43
+
44
+ interface _Params<K extends KeyAxiosRequestConfig, T = any, D = any>
45
+ extends UseAxiosWrapperParams<K, T, UseAxiosOptionsBase<JsonVO<T>>, D> {
46
+ httpParamWay: HttpParamWay;
47
+ upType?: UpType;
48
+ }
49
+
50
+ /** 建议封装接口请求函数不传递 useAxios 的 instance 和 options 参数 */
51
+ type Params<K extends KeyAxiosRequestConfig, T = any, D = any> = PartialPick<_Params<K, T, D>, "instance" | "options">;
52
+
53
+ /**
54
+ * 项目内用的接口请求
55
+ * @description
56
+ * 01s项目内常用的接口请求
57
+ * @version 2
58
+ */
59
+ export function useRequestIn01s<
60
+ K extends KeyAxiosRequestConfig,
61
+ T = any,
62
+ D = any,
63
+ HttpParamWayTemp extends HttpParamWay = Params<K>["httpParamWay"],
64
+ >(
65
+ params: Params<
66
+ //
67
+ UseAxiosWrapperUseKey<HttpParamWayTemp, K>,
68
+ T,
69
+ D
70
+ >,
71
+ ) {
72
+ const {
73
+ config,
74
+ options = createDefaultUseAxiosOptions(),
75
+ instance = innerAxiosInstance,
76
+ httpParamWay,
77
+ upType,
78
+ url,
79
+ } = params;
80
+ setHeaders(config, upType);
81
+ setDefaultUseAxiosOptions(options);
82
+ setDataByHttpParamWay({
83
+ httpParamWay,
84
+ config,
85
+ });
86
+ // console.log(" instance ?", instance.defaults.baseURL);
87
+ return useAxiosWrapper<
88
+ //
89
+ UseAxiosWrapperUseKey<HttpParamWayTemp, K>,
90
+ JsonVO<T>,
91
+ UseAxiosOptionsBase<JsonVO<T>>,
92
+ D
93
+ >({ config, instance, options, url });
94
+ }
@@ -0,0 +1,185 @@
1
+ import type { UseAxiosOptionsBase } from "@vueuse/integrations/useAxios";
2
+ import type { AxiosRequestConfig } from "axios";
3
+
4
+ import { isNil, merge } from "lodash-es";
5
+ import qs from "qs";
6
+
7
+ import type { JsonVO } from "./types/JsonVO";
8
+ import { isConditionsSome } from "../../index";
9
+
10
+ /**
11
+ * http的接口传参方式
12
+ * @description
13
+ * 用于控制接口请求时的参数传递方式
14
+ * @see https://www.cnblogs.com/jinyuanya/p/13934722.html
15
+ */
16
+ export type HttpParamWay =
17
+ // 路径传参
18
+ | "path"
19
+ // query传参
20
+ | "query"
21
+ // body传参
22
+ | "body";
23
+
24
+ /**
25
+ * 基础的 axios.config 必填项字段
26
+ * @description
27
+ * 用于说明axios配置必须传递method字段
28
+ * @version 2
29
+ */
30
+ export type AxiosRequestConfigBaseKey = "method";
31
+
32
+ /**
33
+ * 数据上传数据类型
34
+ * @description
35
+ * 一个标记工具,用来控制上传数据的格式
36
+ *
37
+ * 目前没有发现axios有类似的上传类型声明
38
+ */
39
+ export enum UpType {
40
+ /** 表单类型 */
41
+ form = 0,
42
+
43
+ /** json类型 */
44
+ json = 1,
45
+
46
+ /** 文件类型 */
47
+ file = 3,
48
+
49
+ /** 文件流类型 */
50
+ stream = 4,
51
+ }
52
+
53
+ /**
54
+ * HTTP状态码
55
+ * @description
56
+ * 待优化 未来可以直接复用axios内提供的值 不需要额外地封装工具
57
+ */
58
+ export enum HttpCode {
59
+ /** 暂未登录或TOKEN已经过期 */
60
+ UNAUTHORIZED = 401,
61
+ /** 没有相关权限 */
62
+ FORBIDDEN = 403,
63
+ /** 访问页面未找到 */
64
+ NOT_FOUND = 404,
65
+ /** 服务器错误 */
66
+ SERVER_ERROR = 9994,
67
+ /** 上传参数异常 */
68
+ PARAMS_INVALID = 9995,
69
+ /** ContentType错误 */
70
+ CONTENT_TYPE_ERR = 9996,
71
+ /** 功能尚未实现 */
72
+ API_UN_IMPL = 9997,
73
+ /** 服务器繁忙 */
74
+ SERVER_BUSY = 9998,
75
+ /** 操作失败 */
76
+ FAIL = 9999,
77
+ /** 操作成功 */
78
+ SUCCESS = 10000,
79
+ }
80
+
81
+ /**
82
+ * 上传类型-请求体类型 枚举
83
+ * @description
84
+ * 待优化 未来可以直接复用axios内提供的值 不需要额外地封装工具
85
+ */
86
+ export enum MapContentTypeUpType {
87
+ "application/json;charset=UTF-8" = UpType.json,
88
+ "multipart/form-data" = UpType.file,
89
+ "application/octet-stream" = UpType.stream,
90
+ "application/x-www-form-urlencoded;charset=UTF-8" = UpType.form,
91
+ }
92
+
93
+ /** @private */
94
+ type ContentType = keyof typeof MapContentTypeUpType;
95
+
96
+ /**
97
+ * 根据数据上传类型 重设请求头类型
98
+ * @description
99
+ * 不同的数据上传数据类型 要使用不同的接口请求方式
100
+ *
101
+ * 不再有返回值
102
+ * @version 2
103
+ */
104
+ export function setHeaders(config: AxiosRequestConfig, upType: UpType = UpType.json) {
105
+ const contentType = MapContentTypeUpType[upType] as ContentType;
106
+ if (contentType) {
107
+ config = merge<AxiosRequestConfig, AxiosRequestConfig>(config, {
108
+ headers: {
109
+ "Content-Type": contentType,
110
+ },
111
+ });
112
+ }
113
+ if (!isNil(config.data) && upType === UpType.form) {
114
+ config.data = qs.stringify(config.data, { arrayFormat: "repeat" });
115
+ }
116
+ if (!isNil(config.data) && upType === UpType.file) {
117
+ const formData = new FormData();
118
+ for (const key in config.data) {
119
+ formData.append(key, config.data[key]);
120
+ }
121
+ config.data = formData;
122
+ }
123
+ }
124
+
125
+ /**
126
+ * 提供默认返回值的包装类型
127
+ * @description
128
+ * 预期会在每一个接口的包装函数内使用 用来定义 useAxios 的options参数
129
+ *
130
+ * 该类型应该成为高频使用的工具类型
131
+ *
132
+ * 被设计成一个简单的类型 而不是interface接口
133
+ * @version 2
134
+ */
135
+ export type UseAxiosOptionsJsonVO<T = any> = UseAxiosOptionsBase<JsonVO<T>>;
136
+
137
+ /** useAxios的选项配置 且默认带有immediate属性 不立刻执行 */
138
+ export interface UseAxiosOptionsImmediate<T = any> extends UseAxiosOptionsJsonVO<T> {
139
+ /**
140
+ * @default false
141
+ * @description
142
+ * 默认为 false 不立刻执行
143
+ */
144
+ immediate: boolean;
145
+ }
146
+
147
+ /** 生成默认的选项配置 */
148
+ export function createDefaultUseAxiosOptions<T = any>(): UseAxiosOptionsImmediate<T> {
149
+ return {
150
+ immediate: false,
151
+ };
152
+ }
153
+
154
+ /**
155
+ * 设置默认的 useAxiosOptions 参数值
156
+ * @description
157
+ * 设置 immediate 的值
158
+ *
159
+ * 如果外部没有传递 immediate 属性,那么就使用默认的值
160
+ *
161
+ * 每个业务接口都会传递 options 值,本函数用来完成默认的参数补全。
162
+ */
163
+ export function setDefaultUseAxiosOptions<T>(options: UseAxiosOptionsBase<T>) {
164
+ const _options = createDefaultUseAxiosOptions();
165
+ options.immediate = options?.immediate ?? _options.immediate;
166
+ }
167
+
168
+ /** @private */
169
+ interface SetDataByHttpParamWayParams {
170
+ httpParamWay: HttpParamWay;
171
+ config: AxiosRequestConfig;
172
+ }
173
+ export function setDataByHttpParamWay(params: SetDataByHttpParamWayParams) {
174
+ const { httpParamWay, config } = params;
175
+ if (
176
+ isConditionsSome([
177
+ //
178
+ () => config.method === "GET",
179
+ () => config.method === "get",
180
+ () => httpParamWay === "query",
181
+ ])
182
+ ) {
183
+ config.params = config.data;
184
+ }
185
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * 前后端数据对接数据对象
3
+ * @description
4
+ * 后端 JsonVO 泛型类的前端翻译
5
+ *
6
+ * `01s` 项目统一的数据返回格式
7
+ */
8
+ export interface JsonVO<T> {
9
+ /** 状态码 */
10
+ code: number;
11
+
12
+ /** 提示消息 */
13
+
14
+ message: string;
15
+
16
+ /** 数据对象 */
17
+ data: T;
18
+ }
@@ -0,0 +1,33 @@
1
+ /**
2
+ * 数据分页的返回对象
3
+ * @description
4
+ *
5
+ * `01s` 项目统一的数据返回格式
6
+ */
7
+ export interface PageDTO<T> {
8
+ /**
9
+ * 当前页码
10
+ */
11
+ pageIndex: number;
12
+
13
+ /**
14
+ * 每页显示最大数据条数
15
+ */
16
+
17
+ pageSize: number;
18
+
19
+ /**
20
+ * 总条数
21
+ */
22
+ total: number;
23
+
24
+ /**
25
+ * 总页数
26
+ */
27
+ pages: number;
28
+
29
+ /**
30
+ * 当前页数据列表
31
+ */
32
+ rows: T[];
33
+ }