@varlet/axle 0.0.26 → 0.1.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/README.md CHANGED
@@ -1,7 +1,5 @@
1
1
  <span>English</span> |
2
- <a href="https://github.com/varletjs/axle/blob/dev/README.zh-CN.md">中文</a>
3
-
4
- ---
2
+ <a href="https://github.com/varletjs/axle/blob/main/README.zh-CN.md">中文</a>
5
3
 
6
4
  ### Intro
7
5
 
@@ -37,71 +35,6 @@ axle.get('/url', { current: 1, pageSize: 10 }, { headers: {} })
37
35
  axle.post('/url', { name: 'Axle' }, { headers: {} })
38
36
  ```
39
37
 
40
- #### Vue Composition API
41
-
42
- Axle provides the usage of Vue Composition API style, which encapsulates the `loading status`, `error status`, `upload and download progress` of the request, `return data`, `error retry`, `lifecycle`, etc., And inherit all the configuration of `axios`.
43
-
44
- ```html
45
- <script setup>
46
- import { createAxle } from '@varlet/axle'
47
- import { createUseAxle } from '@varlet/axle/use'
48
-
49
- const axle = createAxle(/** @see https://axios-http.com **/)
50
- const useAxle = createUseAxle(/** Unstable **/)
51
-
52
- const [users, getUsers, { loading, error, uploadProgress, downloadProgress }] = useAxle({
53
- // Request initial data
54
- data: [],
55
- // Request runner
56
- runner: axle.get,
57
- // Request url
58
- url: '/user',
59
- // Whether to send the request immediately, defaults false
60
- immediate: true,
61
- // The number of retries after a failed request, defaults 0
62
- retry: 3,
63
- // Request params, defaults {}
64
- params: { current: 1, pageSize: 10 },
65
- // Axios config, see https://axios-http.com
66
- config: { headers: {} },
67
- // lifecycle
68
- onBefore(refs) {
69
- const { data, loading, error, uploadProgress, downloadProgress } = refs
70
- console.log(
71
- data.value,
72
- loading.value,
73
- error.value,
74
- uploadProgress.value,
75
- downloadProgress.value
76
- )
77
- // Do request before
78
- },
79
- onTransform(response, refs) {
80
- // Handle data transform
81
- return response.data
82
- },
83
- onSuccess(response, refs) {
84
- // Do request success
85
- },
86
- onError(error, refs) {
87
- // Do request error
88
- },
89
- onAfter(refs) {
90
- // Do request after
91
- }
92
- })
93
- </script>
94
-
95
- <template>
96
- <span>{{ users }}</span>
97
- <span>{{ loading }}</span>
98
- <span>{{ error }}</span>
99
- <span>{{ uploadProgress }}</span>
100
- <span>{{ downloadProgress }}</span>
101
- <button @click="getUsers">Send Request</button>
102
- </template>
103
- ```
104
-
105
38
  ### Config
106
39
 
107
40
  Axle fully supports all configuration abilities of axios.
@@ -197,7 +130,7 @@ axios.get('/url', { params: { id: 1 }, responseType: 'stream' })
197
130
  axle.getStream('/url', { id: 1 })
198
131
  ```
199
132
 
200
- ## post
133
+ ### post
201
134
 
202
135
  #### JSON
203
136
 
@@ -240,12 +173,14 @@ axios.post('/url', formData, {
240
173
  axle.postMultipart('/url', { name: 'foo', file: new File() })
241
174
  ```
242
175
 
243
- ### Utils
176
+ ## Utils
244
177
 
245
178
  #### Notify the browser to download the file
246
179
 
247
180
  ```js
248
- axle.download(await axle.getBlob('/url', { id: 1 }), 'filename')
181
+ import { download } from '@varlet/axle'
182
+
183
+ download(await axle.getBlob('/url', { id: 1 }), 'filename')
249
184
  ```
250
185
 
251
186
  #### Common Header Operate
@@ -256,6 +191,128 @@ axle.setHeader('TOKEN', TOKEN)
256
191
  axle.removeHeader('TOKEN')
257
192
  ```
258
193
 
194
+ ## Vue Composition API
195
+
196
+ Axle provides the usage of Vue Composition API style, which encapsulates the `loading status`, `error status`, `upload and download progress` of the request, `return data`, `error retry`, `lifecycle`, etc., And inherit all the configuration of `axios`.
197
+
198
+ ```html
199
+ <script setup>
200
+ import { createAxle } from '@varlet/axle'
201
+ import { createUseAxle } from '@varlet/axle/use'
202
+
203
+ const axle = createAxle(/** @see https://axios-http.com **/)
204
+ const useAxle = createUseAxle({
205
+ // Optional value: Default onTransform of the useAxle
206
+ onTransform: response => response
207
+ })
208
+
209
+ const [users, getUsers, { loading, error, uploadProgress, downloadProgress, abort }] = useAxle({
210
+ // Request initial data
211
+ data: [],
212
+ // Request runner
213
+ runner: axle.get,
214
+ // Request url
215
+ url: '/user',
216
+ // Whether to send the request immediately, defaults false
217
+ immediate: true,
218
+ // The number of retries after a failed request, defaults 0
219
+ retry: 3,
220
+ // Request params, defaults {}
221
+ params: { current: 1, pageSize: 10 },
222
+ // Axios config, see https://axios-http.com
223
+ config: { headers: {} },
224
+ // lifecycle
225
+ onBefore(refs) {
226
+ const { data, loading, error, uploadProgress, downloadProgress } = refs
227
+ console.log(
228
+ data.value,
229
+ loading.value,
230
+ error.value,
231
+ uploadProgress.value,
232
+ downloadProgress.value
233
+ )
234
+ // Do request before
235
+ },
236
+ onTransform(response, refs) {
237
+ // Handle data transform, The transformed data will be the value of users.
238
+ return response.data
239
+ },
240
+ onSuccess(response, refs) {
241
+ // Do request success
242
+ },
243
+ onError(error, refs) {
244
+ // Do request error
245
+ },
246
+ onAfter(refs) {
247
+ // Do request after
248
+ }
249
+ })
250
+ </script>
251
+
252
+ <template>
253
+ <span>{{ users }}</span>
254
+ <span>{{ loading }}</span>
255
+ <span>{{ error }}</span>
256
+ <span>{{ uploadProgress }}</span>
257
+ <span>{{ downloadProgress }}</span>
258
+ <button @click="getUsers">Send Request</button>
259
+ <button @click="abort">Abort Request</button>
260
+ </template>
261
+ ```
262
+
263
+ ### Parallel Utils
264
+
265
+ Axle provides some parallel request processing tools, please refer to the following examples.
266
+
267
+ ```html
268
+ <script setup>
269
+ import { createAxle } from '@varlet/axle'
270
+ import { createUseAxle, useAllData, useAverageProgress, useHasLoading } from '@varlet/axle/use'
271
+
272
+ const axle = createAxle(/** @see https://axios-http.com **/)
273
+ const useAxle = createUseAxle()
274
+
275
+ const [users, getUsers, { loading: isUsersLoading, downloadProgress: usersDownloadProgress }] = useAxle({
276
+ data: [],
277
+ runner: axle.get,
278
+ url: '/user',
279
+ })
280
+
281
+ const [roles, getRoles, { loading: isRolesLoading, downloadProgress: rolesDownloadProgress }] = useAxle({
282
+ data: [],
283
+ runner: axle.get,
284
+ url: '/role',
285
+ })
286
+
287
+ // At the end of all requests, loading is false
288
+ const loading = useHasLoading(isUsersLoading, isRolesLoading)
289
+ // At the end of all requests, downloadProgress is 1
290
+ const downloadProgress = useAverageProgress(usersDownloadProgress, rolesDownloadProgress)
291
+ // Ref<[
292
+ // [{ name: 'foo' }, { name: 'bar' }],
293
+ // [{ role: 'admin' }, { role: 'user' }]
294
+ // ]> <-
295
+ // [
296
+ // Ref<[{ name: 'foo' }, { name: 'bar' }]>,
297
+ // Ref<[{ role: 'admin' }, { role: 'user' }]>
298
+ // ]
299
+ const usersRoles = useAllData(users, roles)
300
+
301
+ function sendAllRequest() {
302
+ // parallel
303
+ getUsers()
304
+ getRoles()
305
+ }
306
+ </script>
307
+
308
+ <template>
309
+ <span>{{ usersRoles }}</span>
310
+ <span>{{ loading }}</span>
311
+ <span>{{ downloadProgress }}</span>
312
+ <button @click="sendAllRequest">Send All Request</button>
313
+ </template>
314
+ ```
315
+
259
316
 
260
317
 
261
318
 
package/README.zh-CN.md CHANGED
@@ -1,8 +1,6 @@
1
- <a href="https://github.com/varletjs/axle/blob/dev/README.md">English</a> |
1
+ <a href="https://github.com/varletjs/axle/blob/main/README.md">English</a> |
2
2
  <span>中文</span>
3
3
 
4
- ---
5
-
6
4
  ### 介绍
7
5
 
8
6
  基于 [axios](https://axios-http.com/) 的渐进式请求工具。 它不会破坏 `axios` 原有的能力,帮助您更轻松地处理请求。
@@ -37,71 +35,6 @@ axle.get('/url', { current: 1, pageSize: 10 }, { headers: {} })
37
35
  axle.post('/url', { name: 'Axle' }, { headers: {} })
38
36
  ```
39
37
 
40
- #### Vue 组合式 API
41
-
42
- Axle 提供了 Vue Composition API 风格的用法,封装了请求的 `加载状态`, `错误状态`, `请求的上下行进度`,`返回数据`,`错误重试`,`生命周期` 等等,并继承了 `axios` 的所有配置。
43
-
44
- ```html
45
- <script setup>
46
- import { createAxle } from '@varlet/axle'
47
- import { createUseAxle } from '@varlet/axle/use'
48
-
49
- const axle = createAxle(/** @see https://axios-http.com **/)
50
- const useAxle = createUseAxle()
51
-
52
- const [users, getUsers, { loading, error, uploadProgress, downloadProgress }] = useAxle({
53
- // 请求初始化数据
54
- data: [],
55
- // 请求函数
56
- runner: axle.get,
57
- // 请求地址
58
- url: '/user',
59
- // 是否立即发送请求, 默认值: false
60
- immediate: true,
61
- // 错误重试次数, 默认值: 0
62
- retry: 3,
63
- // 请求参数, 默认值: {}
64
- params: { current: 1, pageSize: 10 },
65
- // Axios 配置, see https://axios-http.com
66
- config: { headers: {} },
67
- // 生命周期
68
- onBefore(refs) {
69
- const { data, loading, error, uploadProgress, downloadProgress } = refs
70
- console.log(
71
- data.value,
72
- loading.value,
73
- error.value,
74
- uploadProgress.value,
75
- downloadProgress.value
76
- )
77
- // 处理请求前逻辑
78
- },
79
- onTransform(response, refs) {
80
- // 处理数据转换,转换后的数据将成为 users 的值。
81
- return response.data
82
- },
83
- onSuccess(response, refs) {
84
- // 处理请求成功逻辑
85
- },
86
- onError(error, refs) {
87
- // 处理请求错误逻辑
88
- },
89
- onAfter(refs) {
90
- // 处理请求结束逻辑,无论成功失败都会触发。
91
- }
92
- })
93
- </script>
94
-
95
- <template>
96
- <span>{{ users }}</span>
97
- <span>{{ loading }}</span>
98
- <span>{{ error }}</span>
99
- <span>{{ uploadProgress }}</span>
100
- <span>{{ downloadProgress }}</span>
101
- <button @click="getUsers">发送请求</button>
102
- </template>
103
- ```
104
-
105
38
  ### 配置
106
39
 
107
40
  `Axle` 完全支持 `axios` 的所有配置能力。
@@ -197,7 +130,7 @@ axios.get('/url', { params: { id: 1 }, responseType: 'stream' })
197
130
  axle.getStream('/url', { id: 1 })
198
131
  ```
199
132
 
200
- ## post
133
+ ### post
201
134
 
202
135
  #### JSON
203
136
 
@@ -240,12 +173,14 @@ axios.post('/url', formData, {
240
173
  axle.postMultipart('/url', { name: 'foo', file: new File() })
241
174
  ```
242
175
 
243
- ### 实用工具
176
+ ## 实用工具
244
177
 
245
178
  #### 通知浏览器下载文件
246
179
 
247
180
  ```js
248
- axle.download(await axle.getBlob('/url', { id: 1 }), 'filename')
181
+ import { download } from '@varlet/axle'
182
+
183
+ download(await axle.getBlob('/url', { id: 1 }), 'filename')
249
184
  ```
250
185
 
251
186
  #### 公共 header 操作
@@ -256,6 +191,128 @@ axle.setHeader('TOKEN', TOKEN)
256
191
  axle.removeHeader('TOKEN')
257
192
  ```
258
193
 
194
+ ## Vue 组合式 API
195
+
196
+ Axle 提供了 Vue Composition API 风格的用法,封装了请求的 `加载状态`, `错误状态`, `请求的上下行进度`,`返回数据`,`错误重试`,`生命周期` 等等,并继承了 `axios` 的所有配置。
197
+
198
+ ```html
199
+ <script setup>
200
+ import { createAxle } from '@varlet/axle'
201
+ import { createUseAxle } from '@varlet/axle/use'
202
+
203
+ const axle = createAxle(/** @see https://axios-http.com **/)
204
+ const useAxle = createUseAxle({
205
+ // 可选项: useAxle 的默认 onTransform
206
+ onTransform: response => response
207
+ })
208
+
209
+ const [users, getUsers, { loading, error, uploadProgress, downloadProgress, abort }] = useAxle({
210
+ // 请求初始化数据
211
+ data: [],
212
+ // 请求函数
213
+ runner: axle.get,
214
+ // 请求地址
215
+ url: '/user',
216
+ // 是否立即发送请求, 默认值: false
217
+ immediate: true,
218
+ // 错误重试次数, 默认值: 0
219
+ retry: 3,
220
+ // 请求参数, 默认值: {}
221
+ params: { current: 1, pageSize: 10 },
222
+ // Axios 配置, see https://axios-http.com
223
+ config: { headers: {} },
224
+ // 生命周期
225
+ onBefore(refs) {
226
+ const { data, loading, error, uploadProgress, downloadProgress } = refs
227
+ console.log(
228
+ data.value,
229
+ loading.value,
230
+ error.value,
231
+ uploadProgress.value,
232
+ downloadProgress.value
233
+ )
234
+ // 处理请求前逻辑
235
+ },
236
+ onTransform(response, refs) {
237
+ // 处理数据转换,转换后的数据将成为 users 的值。
238
+ return response.data
239
+ },
240
+ onSuccess(response, refs) {
241
+ // 处理请求成功逻辑
242
+ },
243
+ onError(error, refs) {
244
+ // 处理请求错误逻辑
245
+ },
246
+ onAfter(refs) {
247
+ // 处理请求结束逻辑,无论成功失败都会触发。
248
+ }
249
+ })
250
+ </script>
251
+
252
+ <template>
253
+ <span>{{ users }}</span>
254
+ <span>{{ loading }}</span>
255
+ <span>{{ error }}</span>
256
+ <span>{{ uploadProgress }}</span>
257
+ <span>{{ downloadProgress }}</span>
258
+ <button @click="getUsers">发送请求</button>
259
+ <button @click="abort">中断请求</button>
260
+ </template>
261
+ ```
262
+
263
+ ### 并行请求实用工具
264
+
265
+ Axle 提供了一些并行请求处理工具,请参考以下示例。
266
+
267
+ ```html
268
+ <script setup>
269
+ import { createAxle } from '@varlet/axle'
270
+ import { createUseAxle, useAllData, useAverageProgress, useHasLoading } from '@varlet/axle/use'
271
+
272
+ const axle = createAxle(/** @see https://axios-http.com **/)
273
+ const useAxle = createUseAxle()
274
+
275
+ const [users, getUsers, { loading: isUsersLoading, downloadProgress: usersDownloadProgress }] = useAxle({
276
+ data: [],
277
+ runner: axle.get,
278
+ url: '/user',
279
+ })
280
+
281
+ const [roles, getRoles, { loading: isRolesLoading, downloadProgress: rolesDownloadProgress }] = useAxle({
282
+ data: [],
283
+ runner: axle.get,
284
+ url: '/role',
285
+ })
286
+
287
+ // 所有请求结束时,loading 为 false
288
+ const loading = useHasLoading(isUsersLoading, isRolesLoading)
289
+ // 所有请求结束时,downloadProgress 为 1
290
+ const downloadProgress = useAverageProgress(usersDownloadProgress, rolesDownloadProgress)
291
+ // Ref<[
292
+ // [{ name: 'foo' }, { name: 'bar' }],
293
+ // [{ role: 'admin' }, { role: 'user' }]
294
+ // ]> <-
295
+ // [
296
+ // Ref<[{ name: 'foo' }, { name: 'bar' }]>,
297
+ // Ref<[{ role: 'admin' }, { role: 'user' }]>
298
+ // ]
299
+ const usersRoles = useAllData(users, roles)
300
+
301
+ function sendAllRequest() {
302
+ // parallel
303
+ getUsers()
304
+ getRoles()
305
+ }
306
+ </script>
307
+
308
+ <template>
309
+ <span>{{ usersRoles }}</span>
310
+ <span>{{ loading }}</span>
311
+ <span>{{ downloadProgress }}</span>
312
+ <button @click="sendAllRequest">发送全部请求</button>
313
+ </template>
314
+ ```
315
+
259
316
 
260
317
 
261
318
 
package/es/instance.d.ts CHANGED
@@ -40,7 +40,6 @@ export type AxleInstance = {
40
40
  patch: ModifyHelper;
41
41
  patchUrlEncode: ModifyHelper;
42
42
  patchMultipart: ModifyHelper;
43
- download(blob: string | Blob, filename: string): void;
44
43
  getHeaders(): HeadersDefaults['common'];
45
44
  setHeader(key: string, value: string): void;
46
45
  removeHeader(key: string | string[]): void;
package/es/instance.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import axios from 'axios';
2
2
  import qs from 'qs';
3
+ import { inBrowser } from '@varlet/shared';
3
4
  export function createFetchHelper(service, method, responseType) {
4
5
  return function (url, params, config) {
5
6
  return service[method](url, Object.assign({ params,
@@ -22,6 +23,9 @@ export function createModifyHelper(service, method, contentType) {
22
23
  };
23
24
  }
24
25
  export function download(url, filename) {
26
+ if (!inBrowser()) {
27
+ return;
28
+ }
25
29
  const a = document.createElement('a');
26
30
  a.download = filename;
27
31
  a.style.display = 'none';
@@ -81,7 +85,6 @@ export function createAxle(config = {}) {
81
85
  patch: createModifyHelper(service, 'patch', 'application/json'),
82
86
  patchUrlEncode: createModifyHelper(service, 'patch', 'application/x-www-form-urlencoded'),
83
87
  patchMultipart: createModifyHelper(service, 'patch', 'multipart/form-data'),
84
- download,
85
88
  getHeaders,
86
89
  setHeader,
87
90
  removeHeader,
package/es/use.d.ts CHANGED
@@ -43,3 +43,6 @@ export interface CreateUseAxleOptions {
43
43
  onTransform?(response: any, refs: any): any;
44
44
  }
45
45
  export declare function createUseAxle(options?: CreateUseAxleOptions): <D = any, R = any, P = Record<string, any>>(options: UseAxleOptions<D, R, P>) => UseAxleInstance<D, R, P>;
46
+ export * from './useAllData.js';
47
+ export * from './useHasLoading.js';
48
+ export * from './useAverageProgress.js';
package/es/use.js CHANGED
@@ -16,7 +16,7 @@ export function createUseAxle(options = {}) {
16
16
  loading,
17
17
  error,
18
18
  downloadProgress,
19
- uploadProgress
19
+ uploadProgress,
20
20
  };
21
21
  let controller = new AbortController();
22
22
  const run = async (options = {}) => {
@@ -81,3 +81,6 @@ export function createUseAxle(options = {}) {
81
81
  };
82
82
  return useAxle;
83
83
  }
84
+ export * from './useAllData.js';
85
+ export * from './useHasLoading.js';
86
+ export * from './useAverageProgress.js';
@@ -0,0 +1 @@
1
+ export declare function useAllData<DC extends any[]>(...dataCollection: DC): import("vue").WritableComputedRef<any[]>;
@@ -0,0 +1,11 @@
1
+ import { computed } from 'vue';
2
+ export function useAllData(...dataCollection) {
3
+ return computed({
4
+ get: () => dataCollection.map((data) => data.value),
5
+ set(values) {
6
+ dataCollection.forEach((data, index) => {
7
+ data.value = values[index];
8
+ });
9
+ },
10
+ });
11
+ }
@@ -0,0 +1,2 @@
1
+ import { type Ref } from "vue";
2
+ export declare function useAverageProgress(...progressCollection: Ref<number>[]): Ref<number>;
@@ -0,0 +1,9 @@
1
+ import { ref, watch } from "vue";
2
+ export function useAverageProgress(...progressCollection) {
3
+ const averageProgress = ref(0);
4
+ watch(() => progressCollection.map(progress => progress.value), (newProgressCollection) => {
5
+ const total = newProgressCollection.reduce((total, progress) => total + progress, 0);
6
+ averageProgress.value = total / progressCollection.length;
7
+ }, { immediate: true });
8
+ return averageProgress;
9
+ }
@@ -0,0 +1,2 @@
1
+ import { type Ref } from "vue";
2
+ export declare function useHasLoading(...loadings: Ref<boolean>[]): Ref<boolean>;
@@ -0,0 +1,8 @@
1
+ import { ref, watch } from "vue";
2
+ export function useHasLoading(...loadings) {
3
+ const hasLoading = ref(false);
4
+ watch(() => loadings.map(loading => loading.value), (newLoadings) => {
5
+ hasLoading.value = newLoadings.some(Boolean);
6
+ }, { immediate: true });
7
+ return hasLoading;
8
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@varlet/axle",
3
- "version": "0.0.26",
4
- "description": "A useful axios helper",
3
+ "version": "0.1.1",
4
+ "description": "Progressive request tool based on axios",
5
5
  "type": "module",
6
6
  "main": "es/index.js",
7
7
  "module": "es/index.js",
@@ -26,7 +26,7 @@
26
26
  "axios",
27
27
  "helpers"
28
28
  ],
29
- "author": "Joker <13255238145@163.com>",
29
+ "author": "haoziqaq <357229046@qq.com>",
30
30
  "license": "MIT",
31
31
  "dependencies": {
32
32
  "@varlet/shared": "^2.11.8",
@@ -35,7 +35,7 @@
35
35
  },
36
36
  "devDependencies": {
37
37
  "@types/qs": "^6.9.7",
38
- "@varlet/cli": "^2.11.8",
38
+ "@varlet/cli": "^2.13.1",
39
39
  "rimraf": "^5.0.1",
40
40
  "typescript": "^5.1.5",
41
41
  "vue": "3.3.4"