@varlet/axle 0.2.6 → 0.2.7-alpha.1701536208987
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/README.md +117 -121
- package/README.zh-CN.md +116 -120
- package/es/chunk-KLNSLHAC.js +24 -0
- package/es/index.d.ts +29 -2
- package/es/index.js +214 -2
- package/es/instance-8e68a761.d.ts +64 -0
- package/es/use.d.ts +19 -12
- package/es/use.js +164 -88
- package/package.json +36 -7
- package/es/instance.d.ts +0 -61
- package/es/instance.js +0 -105
- package/es/interceptors/index.d.ts +0 -3
- package/es/interceptors/index.js +0 -3
- package/es/interceptors/requestHeadersInterceptor.d.ts +0 -7
- package/es/interceptors/requestHeadersInterceptor.js +0 -18
- package/es/interceptors/responseBlobInterceptor.d.ts +0 -7
- package/es/interceptors/responseBlobInterceptor.js +0 -15
- package/es/interceptors/responseTimeoutInterceptor.d.ts +0 -7
- package/es/interceptors/responseTimeoutInterceptor.js +0 -15
- package/es/useAverageProgress.d.ts +0 -2
- package/es/useAverageProgress.js +0 -9
- package/es/useHasLoading.d.ts +0 -2
- package/es/useHasLoading.js +0 -8
- package/es/useValues.d.ts +0 -2
- package/es/useValues.js +0 -11
package/es/index.js
CHANGED
|
@@ -1,2 +1,214 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
__spreadValues
|
|
3
|
+
} from "./chunk-KLNSLHAC.js";
|
|
4
|
+
|
|
5
|
+
// src/instance.ts
|
|
6
|
+
import axios from "axios";
|
|
7
|
+
import qs from "qs";
|
|
8
|
+
import { inBrowser } from "@varlet/shared";
|
|
9
|
+
function createFetchRunner(service, method, responseType) {
|
|
10
|
+
return function(url, params, config) {
|
|
11
|
+
return service[method](url, __spreadValues({
|
|
12
|
+
params,
|
|
13
|
+
responseType
|
|
14
|
+
}, config));
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function createModifyRunner(service, method, contentType) {
|
|
18
|
+
return function(url, params, config) {
|
|
19
|
+
if (contentType === "application/x-www-form-urlencoded") {
|
|
20
|
+
params = qs.stringify(params);
|
|
21
|
+
}
|
|
22
|
+
if (contentType === "multipart/form-data") {
|
|
23
|
+
const formData = new FormData();
|
|
24
|
+
Object.keys(params).forEach((key) => formData.append(key, params[key]));
|
|
25
|
+
params = formData;
|
|
26
|
+
}
|
|
27
|
+
return service[method](url, params, __spreadValues({
|
|
28
|
+
headers: {
|
|
29
|
+
"Content-Type": contentType
|
|
30
|
+
}
|
|
31
|
+
}, config));
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function download(url, filename) {
|
|
35
|
+
if (!inBrowser()) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const a = document.createElement("a");
|
|
39
|
+
a.download = filename;
|
|
40
|
+
a.style.display = "none";
|
|
41
|
+
a.href = typeof url === "string" ? url : URL.createObjectURL(url);
|
|
42
|
+
document.body.appendChild(a);
|
|
43
|
+
a.click();
|
|
44
|
+
URL.revokeObjectURL(a.href);
|
|
45
|
+
document.body.removeChild(a);
|
|
46
|
+
}
|
|
47
|
+
function createAxle(config = {}) {
|
|
48
|
+
const service = axios.create(config);
|
|
49
|
+
function getHeaders() {
|
|
50
|
+
return service.defaults.headers.common;
|
|
51
|
+
}
|
|
52
|
+
function setHeader(key, value) {
|
|
53
|
+
;
|
|
54
|
+
service.defaults.headers.common[key] = value;
|
|
55
|
+
}
|
|
56
|
+
function removeHeader(key) {
|
|
57
|
+
if (typeof key === "string") {
|
|
58
|
+
Reflect.deleteProperty(service.defaults.headers.common, key);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
key.forEach((k) => Reflect.deleteProperty(service.defaults.headers.common, k));
|
|
62
|
+
}
|
|
63
|
+
function useRequestInterceptor(...interceptors) {
|
|
64
|
+
interceptors.forEach((interceptor) => {
|
|
65
|
+
service.interceptors.request.use(interceptor.onFulfilled, interceptor.onRejected, interceptor.options);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
function useResponseInterceptor(...interceptors) {
|
|
69
|
+
interceptors.forEach((interceptor) => {
|
|
70
|
+
service.interceptors.response.use(interceptor.onFulfilled, interceptor.onRejected, interceptor.options);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
get: createFetchRunner(service, "get", "json"),
|
|
75
|
+
getBlob: createFetchRunner(service, "get", "blob"),
|
|
76
|
+
getDocument: createFetchRunner(service, "get", "document"),
|
|
77
|
+
getArrayBuffer: createFetchRunner(service, "get", "arraybuffer"),
|
|
78
|
+
getText: createFetchRunner(service, "get", "text"),
|
|
79
|
+
getStream: createFetchRunner(service, "get", "stream"),
|
|
80
|
+
head: createFetchRunner(service, "head", "json"),
|
|
81
|
+
headBlob: createFetchRunner(service, "head", "blob"),
|
|
82
|
+
headDocument: createFetchRunner(service, "head", "document"),
|
|
83
|
+
headArrayBuffer: createFetchRunner(service, "head", "arraybuffer"),
|
|
84
|
+
headText: createFetchRunner(service, "head", "text"),
|
|
85
|
+
headStream: createFetchRunner(service, "head", "stream"),
|
|
86
|
+
options: createFetchRunner(service, "options", "json"),
|
|
87
|
+
optionsBlob: createFetchRunner(service, "options", "blob"),
|
|
88
|
+
optionsDocument: createFetchRunner(service, "options", "document"),
|
|
89
|
+
optionsArrayBuffer: createFetchRunner(service, "options", "arraybuffer"),
|
|
90
|
+
optionsText: createFetchRunner(service, "options", "text"),
|
|
91
|
+
optionsStream: createFetchRunner(service, "options", "stream"),
|
|
92
|
+
delete: createFetchRunner(service, "delete", "json"),
|
|
93
|
+
deleteBlob: createFetchRunner(service, "delete", "blob"),
|
|
94
|
+
deleteDocument: createFetchRunner(service, "delete", "document"),
|
|
95
|
+
deleteArrayBuffer: createFetchRunner(service, "delete", "arraybuffer"),
|
|
96
|
+
deleteText: createFetchRunner(service, "delete", "text"),
|
|
97
|
+
deleteStream: createFetchRunner(service, "delete", "stream"),
|
|
98
|
+
post: createModifyRunner(service, "post", "application/json"),
|
|
99
|
+
postUrlEncode: createModifyRunner(service, "post", "application/x-www-form-urlencoded"),
|
|
100
|
+
postMultipart: createModifyRunner(service, "post", "multipart/form-data"),
|
|
101
|
+
put: createModifyRunner(service, "put", "application/json"),
|
|
102
|
+
putUrlEncode: createModifyRunner(service, "put", "application/x-www-form-urlencoded"),
|
|
103
|
+
putMultipart: createModifyRunner(service, "put", "multipart/form-data"),
|
|
104
|
+
patch: createModifyRunner(service, "patch", "application/json"),
|
|
105
|
+
patchUrlEncode: createModifyRunner(service, "patch", "application/x-www-form-urlencoded"),
|
|
106
|
+
patchMultipart: createModifyRunner(service, "patch", "multipart/form-data"),
|
|
107
|
+
getHeaders,
|
|
108
|
+
setHeader,
|
|
109
|
+
removeHeader,
|
|
110
|
+
useRequestInterceptor,
|
|
111
|
+
useResponseInterceptor,
|
|
112
|
+
axios: service
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// src/interceptors/requestHeadersInterceptor.ts
|
|
117
|
+
import { isFunction } from "@varlet/shared";
|
|
118
|
+
|
|
119
|
+
// src/matcher.ts
|
|
120
|
+
import { minimatch } from "minimatch";
|
|
121
|
+
function matchPattern(pattern, method, url) {
|
|
122
|
+
return pattern.startsWith("method:") ? pattern.replace("method:", "").trim() === method : minimatch(url != null ? url : "", pattern);
|
|
123
|
+
}
|
|
124
|
+
function createMatcher(include, exclude) {
|
|
125
|
+
function matcher(method, url) {
|
|
126
|
+
if (!include && !exclude) {
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
const isExclude = (exclude != null ? exclude : []).some((pattern) => matchPattern(pattern, method, url));
|
|
130
|
+
if (isExclude) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
if (!include) {
|
|
134
|
+
return true;
|
|
135
|
+
}
|
|
136
|
+
const isInclude = (include != null ? include : []).some((pattern) => matchPattern(pattern, method, url));
|
|
137
|
+
return isInclude;
|
|
138
|
+
}
|
|
139
|
+
return matcher;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// src/interceptors/requestHeadersInterceptor.ts
|
|
143
|
+
function requestHeadersInterceptor(options = {}) {
|
|
144
|
+
const { headers: headersOrGetter } = options;
|
|
145
|
+
return {
|
|
146
|
+
onFulfilled(config) {
|
|
147
|
+
var _a, _b, _c;
|
|
148
|
+
const matcher = createMatcher(options.include, options.exclude);
|
|
149
|
+
if (!matcher((_a = config.method) != null ? _a : "", (_b = config.url) != null ? _b : "")) {
|
|
150
|
+
return config;
|
|
151
|
+
}
|
|
152
|
+
const headers = (_c = isFunction(headersOrGetter) ? headersOrGetter() : headersOrGetter) != null ? _c : {};
|
|
153
|
+
Object.entries(headers).forEach(([key, value]) => {
|
|
154
|
+
config.headers[key] = value;
|
|
155
|
+
});
|
|
156
|
+
return config;
|
|
157
|
+
},
|
|
158
|
+
onRejected(error) {
|
|
159
|
+
return Promise.reject(error);
|
|
160
|
+
},
|
|
161
|
+
options: options.axiosInterceptorOptions
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// src/interceptors/responseBlobInterceptor.ts
|
|
166
|
+
function responseBlobInterceptor(options = {}) {
|
|
167
|
+
return {
|
|
168
|
+
onFulfilled(response) {
|
|
169
|
+
var _a, _b, _c, _d;
|
|
170
|
+
const matcher = createMatcher(options.include, options.exclude);
|
|
171
|
+
if (!matcher((_a = response.config.method) != null ? _a : "", (_b = response.config.url) != null ? _b : "")) {
|
|
172
|
+
return response;
|
|
173
|
+
}
|
|
174
|
+
if (response.request.responseType === "blob") {
|
|
175
|
+
return (_d = (_c = options.onResponse) == null ? void 0 : _c.call(options, response)) != null ? _d : response;
|
|
176
|
+
}
|
|
177
|
+
return response;
|
|
178
|
+
},
|
|
179
|
+
onRejected(error) {
|
|
180
|
+
return Promise.reject(error);
|
|
181
|
+
},
|
|
182
|
+
options: options.axiosInterceptorOptions
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// src/interceptors/responseTimeoutInterceptor.ts
|
|
187
|
+
function responseTimeoutInterceptor(options = {}) {
|
|
188
|
+
return {
|
|
189
|
+
onFulfilled(response) {
|
|
190
|
+
return response;
|
|
191
|
+
},
|
|
192
|
+
onRejected(error) {
|
|
193
|
+
var _a, _b, _c;
|
|
194
|
+
const matcher = createMatcher(options.include, options.exclude);
|
|
195
|
+
if (!matcher((_a = error.config.method) != null ? _a : "", (_b = error.config.url) != null ? _b : "")) {
|
|
196
|
+
return Promise.reject(error);
|
|
197
|
+
}
|
|
198
|
+
if (error.code === "ECONNABORTED" && error.message.includes("timeout") || error.code === "ETIMEDOUT") {
|
|
199
|
+
error.code = (_c = options == null ? void 0 : options.normalizeErrorCode) != null ? _c : "TIMEOUT";
|
|
200
|
+
}
|
|
201
|
+
return Promise.reject(error);
|
|
202
|
+
},
|
|
203
|
+
options: options.axiosInterceptorOptions
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
export {
|
|
207
|
+
createAxle,
|
|
208
|
+
createFetchRunner,
|
|
209
|
+
createModifyRunner,
|
|
210
|
+
download,
|
|
211
|
+
requestHeadersInterceptor,
|
|
212
|
+
responseBlobInterceptor,
|
|
213
|
+
responseTimeoutInterceptor
|
|
214
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { AxiosRequestConfig, AxiosResponse, AxiosInterceptorOptions, InternalAxiosRequestConfig, HeadersDefaults, AxiosInstance, ResponseType } from 'axios';
|
|
2
|
+
|
|
3
|
+
type AxleRequestConfig = AxiosRequestConfig & {
|
|
4
|
+
exceptionMessage?: any;
|
|
5
|
+
};
|
|
6
|
+
type FetchRunner = <T = any, R = AxiosResponse<T>>(url: string, params?: any, config?: AxleRequestConfig) => Promise<R>;
|
|
7
|
+
type ModifyRunner = <T = any, R = AxiosResponse<T>>(url: string, params?: any, config?: AxleRequestConfig) => Promise<R>;
|
|
8
|
+
type FetchMethod = 'get' | 'delete' | 'options' | 'head';
|
|
9
|
+
type ModifyMethod = 'post' | 'put' | 'patch';
|
|
10
|
+
type RunnerMethod = keyof Omit<AxleInstance, 'axios' | 'getHeaders' | 'setHeader' | 'removeHeader' | 'useRequestInterceptor' | 'useResponseInterceptor'>;
|
|
11
|
+
interface Interceptor<V> {
|
|
12
|
+
onFulfilled?: ((value: V) => any) | null;
|
|
13
|
+
onRejected?: ((error: any) => any) | null;
|
|
14
|
+
options?: AxiosInterceptorOptions;
|
|
15
|
+
}
|
|
16
|
+
type RequestInterceptor = Interceptor<InternalAxiosRequestConfig>;
|
|
17
|
+
type ResponseInterceptor<V = AxiosResponse<any, any>> = Interceptor<V>;
|
|
18
|
+
type AxleInstance = {
|
|
19
|
+
get: FetchRunner;
|
|
20
|
+
getBlob: FetchRunner;
|
|
21
|
+
getDocument: FetchRunner;
|
|
22
|
+
getText: FetchRunner;
|
|
23
|
+
getArrayBuffer: FetchRunner;
|
|
24
|
+
getStream: FetchRunner;
|
|
25
|
+
head: FetchRunner;
|
|
26
|
+
headBlob: FetchRunner;
|
|
27
|
+
headDocument: FetchRunner;
|
|
28
|
+
headText: FetchRunner;
|
|
29
|
+
headArrayBuffer: FetchRunner;
|
|
30
|
+
headStream: FetchRunner;
|
|
31
|
+
options: FetchRunner;
|
|
32
|
+
optionsBlob: FetchRunner;
|
|
33
|
+
optionsDocument: FetchRunner;
|
|
34
|
+
optionsText: FetchRunner;
|
|
35
|
+
optionsArrayBuffer: FetchRunner;
|
|
36
|
+
optionsStream: FetchRunner;
|
|
37
|
+
delete: FetchRunner;
|
|
38
|
+
deleteBlob: FetchRunner;
|
|
39
|
+
deleteDocument: FetchRunner;
|
|
40
|
+
deleteText: FetchRunner;
|
|
41
|
+
deleteArrayBuffer: FetchRunner;
|
|
42
|
+
deleteStream: FetchRunner;
|
|
43
|
+
post: ModifyRunner;
|
|
44
|
+
postUrlEncode: ModifyRunner;
|
|
45
|
+
postMultipart: ModifyRunner;
|
|
46
|
+
put: ModifyRunner;
|
|
47
|
+
putUrlEncode: ModifyRunner;
|
|
48
|
+
putMultipart: ModifyRunner;
|
|
49
|
+
patch: ModifyRunner;
|
|
50
|
+
patchUrlEncode: ModifyRunner;
|
|
51
|
+
patchMultipart: ModifyRunner;
|
|
52
|
+
getHeaders(): HeadersDefaults['common'];
|
|
53
|
+
setHeader(key: string, value: string): void;
|
|
54
|
+
removeHeader(key: string | string[]): void;
|
|
55
|
+
useRequestInterceptor(...interceptors: RequestInterceptor[]): void;
|
|
56
|
+
useResponseInterceptor(...interceptors: ResponseInterceptor[]): void;
|
|
57
|
+
axios: AxiosInstance;
|
|
58
|
+
};
|
|
59
|
+
declare function createFetchRunner(service: AxiosInstance, method: FetchMethod, responseType: ResponseType): <T, R = AxiosResponse<T, any>>(url: string, params?: any, config?: AxleRequestConfig) => Promise<R>;
|
|
60
|
+
declare function createModifyRunner(service: AxiosInstance, method: ModifyMethod, contentType: 'application/json' | 'multipart/form-data' | 'application/x-www-form-urlencoded'): <T, R = AxiosResponse<T, any>>(url: string, params?: any, config?: AxleRequestConfig) => Promise<R>;
|
|
61
|
+
declare function download(url: string | Blob, filename: string): void;
|
|
62
|
+
declare function createAxle(config?: AxiosRequestConfig): AxleInstance;
|
|
63
|
+
|
|
64
|
+
export { AxleRequestConfig as A, FetchRunner as F, Interceptor as I, ModifyRunner as M, RequestInterceptor as R, ResponseInterceptor as a, FetchMethod as b, ModifyMethod as c, RunnerMethod as d, AxleInstance as e, createFetchRunner as f, createModifyRunner as g, download as h, createAxle as i };
|
package/es/use.d.ts
CHANGED
|
@@ -1,21 +1,29 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import { Ref, WritableComputedRef } from 'vue';
|
|
2
|
+
import { A as AxleRequestConfig, d as RunnerMethod, e as AxleInstance } from './instance-8e68a761.js';
|
|
3
|
+
import 'axios';
|
|
4
|
+
|
|
5
|
+
declare function useValues<T extends Ref<any>[]>(...valueCollection: T): WritableComputedRef<T[number]['value'][]>;
|
|
6
|
+
|
|
7
|
+
declare function useHasLoading(...loadings: Ref<boolean>[]): Ref<boolean>;
|
|
8
|
+
|
|
9
|
+
declare function useAverageProgress(...progressCollection: Ref<number>[]): Ref<number>;
|
|
10
|
+
|
|
11
|
+
interface RunOptions<P> {
|
|
4
12
|
url?: string;
|
|
5
13
|
params?: P;
|
|
6
14
|
config?: AxleRequestConfig;
|
|
7
15
|
resetValue?: boolean;
|
|
8
16
|
_retryCount?: number;
|
|
9
17
|
}
|
|
10
|
-
|
|
11
|
-
|
|
18
|
+
type Run<R, P> = (options?: RunOptions<P>) => Promise<R>;
|
|
19
|
+
interface UseAxleRefs<V> {
|
|
12
20
|
value: Ref<V>;
|
|
13
21
|
loading: Ref<boolean>;
|
|
14
22
|
error: Ref<Error | undefined>;
|
|
15
23
|
uploadProgress: Ref<number>;
|
|
16
24
|
downloadProgress: Ref<number>;
|
|
17
25
|
}
|
|
18
|
-
|
|
26
|
+
interface UseAxleOptions<V = any, R = any, P = Record<string, any>> {
|
|
19
27
|
url: string;
|
|
20
28
|
method: RunnerMethod;
|
|
21
29
|
value?: V;
|
|
@@ -30,7 +38,7 @@ export interface UseAxleOptions<V = any, R = any, P = Record<string, any>> {
|
|
|
30
38
|
onSuccess?(response: R, refs: UseAxleRefs<V>): void;
|
|
31
39
|
onError?(error: Error, refs: UseAxleRefs<V>): void;
|
|
32
40
|
}
|
|
33
|
-
|
|
41
|
+
type UseAxleInstance<V, R, P> = [
|
|
34
42
|
value: Ref<V>,
|
|
35
43
|
run: Run<R, P>,
|
|
36
44
|
extra: {
|
|
@@ -41,11 +49,10 @@ export type UseAxleInstance<V, R, P> = [
|
|
|
41
49
|
abort(): void;
|
|
42
50
|
}
|
|
43
51
|
];
|
|
44
|
-
|
|
52
|
+
interface CreateUseAxleOptions {
|
|
45
53
|
axle: AxleInstance;
|
|
46
54
|
onTransform?(response: any, refs: any): any;
|
|
47
55
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
export
|
|
51
|
-
export * from './useAverageProgress.js';
|
|
56
|
+
declare function createUseAxle(options: CreateUseAxleOptions): <V = any, R = any, P = Record<string, any>>(options: UseAxleOptions<V, R, P>) => UseAxleInstance<V, R, P>;
|
|
57
|
+
|
|
58
|
+
export { CreateUseAxleOptions, Run, RunOptions, UseAxleInstance, UseAxleOptions, UseAxleRefs, createUseAxle, useAverageProgress, useHasLoading, useValues };
|
package/es/use.js
CHANGED
|
@@ -1,92 +1,168 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
1
|
+
import {
|
|
2
|
+
__spreadProps,
|
|
3
|
+
__spreadValues
|
|
4
|
+
} from "./chunk-KLNSLHAC.js";
|
|
5
|
+
|
|
6
|
+
// src/use.ts
|
|
7
|
+
import { ref as ref3 } from "vue";
|
|
8
|
+
import { isFunction } from "@varlet/shared";
|
|
9
|
+
|
|
10
|
+
// src/useValues.ts
|
|
11
|
+
import { computed } from "vue";
|
|
12
|
+
function useValues(...valueCollection) {
|
|
13
|
+
return computed({
|
|
14
|
+
get: () => valueCollection.map((value) => value.value),
|
|
15
|
+
set(values) {
|
|
16
|
+
valueCollection.forEach((value, index) => {
|
|
17
|
+
value.value = values[index];
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// src/useHasLoading.ts
|
|
24
|
+
import { ref, watch } from "vue";
|
|
25
|
+
function useHasLoading(...loadings) {
|
|
26
|
+
const hasLoading = ref(false);
|
|
27
|
+
watch(
|
|
28
|
+
() => loadings.map((loading) => loading.value),
|
|
29
|
+
(newLoadings) => {
|
|
30
|
+
hasLoading.value = newLoadings.some(Boolean);
|
|
31
|
+
},
|
|
32
|
+
{ immediate: true }
|
|
33
|
+
);
|
|
34
|
+
return hasLoading;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// src/useAverageProgress.ts
|
|
38
|
+
import { ref as ref2, watch as watch2 } from "vue";
|
|
39
|
+
function useAverageProgress(...progressCollection) {
|
|
40
|
+
const averageProgress = ref2(0);
|
|
41
|
+
watch2(
|
|
42
|
+
() => progressCollection.map((progress) => progress.value),
|
|
43
|
+
(newProgressCollection) => {
|
|
44
|
+
const total = newProgressCollection.reduce((total2, progress) => total2 + progress, 0);
|
|
45
|
+
averageProgress.value = total / progressCollection.length;
|
|
46
|
+
},
|
|
47
|
+
{ immediate: true }
|
|
48
|
+
);
|
|
49
|
+
return averageProgress;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// src/use.ts
|
|
53
|
+
function createUseAxle(options) {
|
|
54
|
+
const { axle, onTransform: defaultOnTransform } = options;
|
|
55
|
+
const useAxle = (options2) => {
|
|
56
|
+
const {
|
|
57
|
+
url,
|
|
58
|
+
method,
|
|
59
|
+
immediate,
|
|
60
|
+
value: initialValue,
|
|
61
|
+
resetValue: initialResetValue,
|
|
62
|
+
params: initialParamsOrGetter,
|
|
63
|
+
config: initialConfig,
|
|
64
|
+
retry = 0,
|
|
65
|
+
onBefore = () => {
|
|
66
|
+
},
|
|
67
|
+
onAfter = () => {
|
|
68
|
+
},
|
|
69
|
+
onTransform = defaultOnTransform != null ? defaultOnTransform : (response) => response,
|
|
70
|
+
onSuccess = () => {
|
|
71
|
+
},
|
|
72
|
+
onError = () => {
|
|
73
|
+
}
|
|
74
|
+
} = options2;
|
|
75
|
+
const initialUrl = url;
|
|
76
|
+
const value = ref3(initialValue);
|
|
77
|
+
const loading = ref3(false);
|
|
78
|
+
const error = ref3();
|
|
79
|
+
const downloadProgress = ref3(0);
|
|
80
|
+
const uploadProgress = ref3(0);
|
|
81
|
+
const shouldRetry = retry > 0;
|
|
82
|
+
const refs = {
|
|
83
|
+
value,
|
|
84
|
+
loading,
|
|
85
|
+
error,
|
|
86
|
+
downloadProgress,
|
|
87
|
+
uploadProgress
|
|
88
|
+
};
|
|
89
|
+
let controller = new AbortController();
|
|
90
|
+
const run = async (options3 = {}) => {
|
|
91
|
+
var _a, _b, _c;
|
|
92
|
+
if (controller.signal.aborted) {
|
|
93
|
+
controller = new AbortController();
|
|
94
|
+
}
|
|
95
|
+
const resetValue = (_b = (_a = options3.resetValue) != null ? _a : initialResetValue) != null ? _b : false;
|
|
96
|
+
if (resetValue === true) {
|
|
97
|
+
value.value = initialValue;
|
|
98
|
+
}
|
|
99
|
+
uploadProgress.value = 0;
|
|
100
|
+
downloadProgress.value = 0;
|
|
101
|
+
const url2 = (_c = options3.url) != null ? _c : initialUrl;
|
|
102
|
+
onBefore(refs);
|
|
103
|
+
loading.value = true;
|
|
104
|
+
try {
|
|
105
|
+
const getterParams = isFunction(initialParamsOrGetter) ? initialParamsOrGetter() : {};
|
|
106
|
+
const response = await axle[method](
|
|
107
|
+
url2,
|
|
108
|
+
__spreadValues(__spreadValues({}, getterParams), options3.params),
|
|
109
|
+
__spreadValues({
|
|
110
|
+
signal: controller.signal,
|
|
111
|
+
onUploadProgress(event) {
|
|
112
|
+
var _a2;
|
|
113
|
+
uploadProgress.value = (_a2 = event.progress) != null ? _a2 : 0;
|
|
114
|
+
},
|
|
115
|
+
onDownloadProgress(event) {
|
|
116
|
+
var _a2;
|
|
117
|
+
downloadProgress.value = (_a2 = event.progress) != null ? _a2 : 0;
|
|
64
118
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
119
|
+
}, options3.config)
|
|
120
|
+
);
|
|
121
|
+
value.value = onTransform(response, refs);
|
|
122
|
+
error.value = void 0;
|
|
123
|
+
onSuccess(response, refs);
|
|
124
|
+
loading.value = false;
|
|
125
|
+
onAfter(refs);
|
|
126
|
+
return response;
|
|
127
|
+
} catch (responseError) {
|
|
128
|
+
const currentRetryCount = options3._retryCount == null ? 0 : options3._retryCount;
|
|
129
|
+
if (shouldRetry && currentRetryCount < retry) {
|
|
130
|
+
return run(__spreadProps(__spreadValues({}, options3), { _retryCount: currentRetryCount + 1 }));
|
|
75
131
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
},
|
|
86
|
-
];
|
|
132
|
+
error.value = responseError;
|
|
133
|
+
onError(responseError, refs);
|
|
134
|
+
loading.value = false;
|
|
135
|
+
onAfter(refs);
|
|
136
|
+
throw responseError;
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
const abort = () => {
|
|
140
|
+
controller.abort();
|
|
87
141
|
};
|
|
88
|
-
|
|
142
|
+
if (immediate) {
|
|
143
|
+
run({
|
|
144
|
+
url: initialUrl,
|
|
145
|
+
params: isFunction(initialParamsOrGetter) ? {} : initialParamsOrGetter,
|
|
146
|
+
config: initialConfig
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
return [
|
|
150
|
+
value,
|
|
151
|
+
run,
|
|
152
|
+
{
|
|
153
|
+
loading,
|
|
154
|
+
error,
|
|
155
|
+
uploadProgress,
|
|
156
|
+
downloadProgress,
|
|
157
|
+
abort
|
|
158
|
+
}
|
|
159
|
+
];
|
|
160
|
+
};
|
|
161
|
+
return useAxle;
|
|
89
162
|
}
|
|
90
|
-
export
|
|
91
|
-
|
|
92
|
-
|
|
163
|
+
export {
|
|
164
|
+
createUseAxle,
|
|
165
|
+
useAverageProgress,
|
|
166
|
+
useHasLoading,
|
|
167
|
+
useValues
|
|
168
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@varlet/axle",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.7-alpha.1701536208987",
|
|
4
4
|
"description": "Progressive request tool based on axios",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "es/index.js",
|
|
@@ -24,21 +24,45 @@
|
|
|
24
24
|
"keywords": [
|
|
25
25
|
"axle",
|
|
26
26
|
"axios",
|
|
27
|
-
"helpers"
|
|
27
|
+
"helpers",
|
|
28
|
+
"vue3",
|
|
29
|
+
"composition api"
|
|
28
30
|
],
|
|
29
31
|
"author": "haoziqaq <357229046@qq.com>",
|
|
30
32
|
"license": "MIT",
|
|
33
|
+
"simple-git-hooks": {
|
|
34
|
+
"pre-commit": "pnpm exec nano-staged --allow-empty --concurrent false",
|
|
35
|
+
"commit-msg": "pnpm exec commitlint -e $1"
|
|
36
|
+
},
|
|
37
|
+
"nano-staged": {
|
|
38
|
+
"*.{vue,ts,tsx,js,jsx,less,css}": "pnpm format",
|
|
39
|
+
"*.{vue,ts,tsx,js,jsx}": "pnpm lint"
|
|
40
|
+
},
|
|
31
41
|
"dependencies": {
|
|
32
42
|
"@varlet/shared": "^2.19.2",
|
|
33
43
|
"axios": "^1.4.0",
|
|
44
|
+
"micromatch": "^4.0.5",
|
|
45
|
+
"minimatch": "^9.0.3",
|
|
34
46
|
"qs": "^6.11.0"
|
|
35
47
|
},
|
|
48
|
+
"peerDependencies": {
|
|
49
|
+
"vue": "^3.2.0"
|
|
50
|
+
},
|
|
36
51
|
"devDependencies": {
|
|
37
|
-
"
|
|
38
|
-
"
|
|
52
|
+
"tsup": "7.2.0",
|
|
53
|
+
"nano-staged": "0.8.0",
|
|
39
54
|
"rimraf": "^5.0.1",
|
|
55
|
+
"simple-git-hooks": "^2.8.1",
|
|
40
56
|
"typescript": "^5.1.5",
|
|
41
|
-
"vue": "3.3.4"
|
|
57
|
+
"vue": "3.3.4",
|
|
58
|
+
"eslint": "^8.43.0",
|
|
59
|
+
"prettier": "^2.8.8",
|
|
60
|
+
"@varlet/release": "0.0.3",
|
|
61
|
+
"@varlet/eslint-config": "^2.19.2",
|
|
62
|
+
"@commitlint/cli": "^17.6.5",
|
|
63
|
+
"@commitlint/config-conventional": "^17.6.5",
|
|
64
|
+
"@types/minimatch": "^5.1.2",
|
|
65
|
+
"@types/qs": "^6.9.7"
|
|
42
66
|
},
|
|
43
67
|
"repository": {
|
|
44
68
|
"type": "git",
|
|
@@ -52,9 +76,14 @@
|
|
|
52
76
|
"pnpm": ">=8.0"
|
|
53
77
|
},
|
|
54
78
|
"scripts": {
|
|
79
|
+
"preinstall": "npx only-allow pnpm",
|
|
80
|
+
"postinstall": "simple-git-hooks",
|
|
55
81
|
"bootstrap": "pnpm i && pnpm build && pnpm --filter ./playground i",
|
|
56
|
-
"dev": "
|
|
57
|
-
"build": "
|
|
82
|
+
"dev": "tsup src/index.ts src/use.ts --format esm --out-dir=es --watch --dts",
|
|
83
|
+
"build": "tsup src/index.ts src/use.ts --format esm --out-dir=es --dts --clean",
|
|
84
|
+
"dev:play": "pnpm --dir ./playground dev",
|
|
85
|
+
"lint": "eslint . --fix",
|
|
86
|
+
"format": "prettier --write .",
|
|
58
87
|
"release": "pnpm build && vr release"
|
|
59
88
|
}
|
|
60
89
|
}
|