@varlet/axle 0.0.25 → 0.0.26

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,8 +1,13 @@
1
+ <span>English</span> |
2
+ <a href="https://github.com/varletjs/axle/blob/dev/README.zh-CN.md">中文</a>
3
+
4
+ ---
5
+
1
6
  ### Intro
2
7
 
3
- A tool library based on axios to simplify the development process
8
+ Progressive request tool based on axios. It does not destroy the original abilities of axios, and helps you process requests more easily.
4
9
 
5
- ### Quickstart
10
+ ### QuickStart
6
11
 
7
12
  #### Install
8
13
 
@@ -19,22 +24,22 @@ yarn add @varlet/axle
19
24
  pnpm add @varlet/axle
20
25
  ```
21
26
 
22
- #### Request Call
27
+ #### Send Request
28
+
29
+ Axle normalizes the parameters of the request function, and expands more request functions for different interface requirements. Here is a simple example.
23
30
 
24
31
  ```js
25
32
  import { createAxle } from '@varlet/axle'
26
33
 
27
- // create axle instance
28
34
  const axle = createAxle(/** @see https://axios-http.com **/)
29
35
 
30
- // The built-in axios of the axle, the usage is exactly the same as that of axios, and shares the configuration with the axle.
31
- const { axios } = axle
32
-
33
- const response = await axle.get('/user', { current: 1, pageSize: 10 }, { headers: {} })
34
- const response = await axle.post('/user', { name: 'Axle' }, { headers: {} })
36
+ axle.get('/url', { current: 1, pageSize: 10 }, { headers: {} })
37
+ axle.post('/url', { name: 'Axle' }, { headers: {} })
35
38
  ```
36
39
 
37
- ### Vue Composition Api
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`.
38
43
 
39
44
  ```html
40
45
  <script setup>
@@ -42,26 +47,215 @@ import { createAxle } from '@varlet/axle'
42
47
  import { createUseAxle } from '@varlet/axle/use'
43
48
 
44
49
  const axle = createAxle(/** @see https://axios-http.com **/)
45
- const useAxle = createUseAxle(/** @see https://github.com/varletjs/axle/blob/main/src/use.ts?plain=1#L5 **/)
50
+ const useAxle = createUseAxle(/** Unstable **/)
46
51
 
47
- // default immediate request
48
- const [users, getUsers, loading, { error }] = useAxle({
49
- /** @see https://github.com/varletjs/axle/blob/main/src/use.ts?plain=1#L26 **/
52
+ const [users, getUsers, { loading, error, uploadProgress, downloadProgress }] = useAxle({
53
+ // Request initial data
54
+ data: [],
55
+ // Request runner
50
56
  runner: axle.get,
57
+ // Request url
51
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 {}
52
64
  params: { current: 1, pageSize: 10 },
53
- config: { headers: {} }
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
+ }
54
92
  })
55
93
  </script>
56
94
 
57
95
  <template>
58
- <span>{{ users }}</span>
59
- <span>{{ loading }}</span>
60
- <span>{{ error }}</span>
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>
61
102
  </template>
62
103
  ```
63
104
 
64
- ### Repo Status
105
+ ### Config
106
+
107
+ Axle fully supports all configuration abilities of axios.
108
+
109
+ ```js
110
+ const axle = createAxle(/** @see https://axios-http.com **/)
111
+ // The built-in axios of the axle, the usage is exactly the same as that of axios, and shares the configuration with the axle.
112
+ const { axios } = axle
113
+
114
+ axios.defaults.baseURL = 'https://api.example.com'
115
+ axios.defaults.headers.common['TOKEN'] = TOKEN
116
+ axios.defaults.timeout = 2500
117
+
118
+ // Add a request interceptor
119
+ axios.interceptors.request.use((config) => {
120
+ // Do something before request is sent
121
+ return config
122
+ }, (error) => {
123
+ // Do something with request error
124
+ return Promise.reject(error)
125
+ })
126
+
127
+ // Add a response interceptor
128
+ axios.interceptors.response.use((response) => {
129
+ // Any status code that lie within the range of 2xx cause this function to trigger
130
+ // Do something with response data
131
+ return response
132
+ }, (error) => {
133
+ // Any status codes that falls outside the range of 2xx cause this function to trigger
134
+ // Do something with response error
135
+ return Promise.reject(error)
136
+ })
137
+ ```
138
+
139
+ ## Axle & Axios Request Functions
140
+
141
+ The request function provided by Axle can help you send requests more easily. Here are some examples including comparison with axios.
142
+ Tips: Take `get` and `post` as examples below, Axle also supports `options`, `head`, `delete`, `patch` , `put` methods.
143
+
144
+ ### get
145
+
146
+ #### JSON
147
+
148
+ ```js
149
+ // axios
150
+ axios.get('/url', { params: { id: 1 } })
151
+ // axle
152
+ axle.get('/url', { id: 1 })
153
+ ```
154
+
155
+ #### Blob
156
+
157
+ ```js
158
+ // axios
159
+ axios.get('/url', { params: { id: 1 }, responseType: 'blob' })
160
+ // axle
161
+ axle.getBlob('/url', { id: 1 })
162
+ ```
163
+
164
+ #### Text
165
+
166
+ ```js
167
+ // axios
168
+ axios.get('/url', { params: { id: 1 }, responseType: 'text' })
169
+ // axle
170
+ axle.getText('/url', { id: 1 })
171
+ ```
172
+
173
+ #### Document
174
+
175
+ ```js
176
+ // axios
177
+ axios.get('/url', { params: { id: 1 }, responseType: 'document' })
178
+ // axle
179
+ axle.getDocument('/url', { id: 1 })
180
+ ```
181
+
182
+ #### ArrayBuffer
183
+
184
+ ```js
185
+ // axios
186
+ axios.get('/url', { params: { id: 1 }, responseType: 'arraybuffer' })
187
+ // axle
188
+ axle.getArrayBuffer('/url', { id: 1 })
189
+ ```
190
+
191
+ #### Stream
192
+
193
+ ```js
194
+ // axios
195
+ axios.get('/url', { params: { id: 1 }, responseType: 'stream' })
196
+ // axle
197
+ axle.getStream('/url', { id: 1 })
198
+ ```
199
+
200
+ ## post
201
+
202
+ #### JSON
203
+
204
+ Same with axios.
205
+
206
+ ```js
207
+ // axios
208
+ axios.post('/url', { name: 'foo' })
209
+ // axle
210
+ axle.post('/url', { name: 'foo' })
211
+ ```
212
+
213
+ #### application/x-www-form-urlencoded
214
+
215
+ ```js
216
+ // axios
217
+ axios.post('/url', qs.stringify({ name: 'foo' }), {
218
+ headers: {
219
+ 'Content-Type': 'application/x-www-form-urlencoded'
220
+ }
221
+ })
222
+ // axle
223
+ axle.postUrlEncode('/url', { name: 'foo' })
224
+ ```
225
+
226
+ #### multipart/form-data
227
+
228
+ ```js
229
+ // axios
230
+ const formData = new FormData()
231
+ formData.append('name', 'foo')
232
+ formData.append('file', new File())
233
+
234
+ axios.post('/url', formData, {
235
+ headers: {
236
+ 'Content-Type': 'multipart/form-data'
237
+ }
238
+ })
239
+ // axle
240
+ axle.postMultipart('/url', { name: 'foo', file: new File() })
241
+ ```
242
+
243
+ ### Utils
244
+
245
+ #### Notify the browser to download the file
246
+
247
+ ```js
248
+ axle.download(await axle.getBlob('/url', { id: 1 }), 'filename')
249
+ ```
250
+
251
+ #### Common Header Operate
252
+
253
+ ```js
254
+ const headers = axle.getHeaders()
255
+ axle.setHeader('TOKEN', TOKEN)
256
+ axle.removeHeader('TOKEN')
257
+ ```
258
+
259
+
65
260
 
66
- WIP and Trying
67
261
 
@@ -0,0 +1,261 @@
1
+ <a href="https://github.com/varletjs/axle/blob/dev/README.md">English</a> |
2
+ <span>中文</span>
3
+
4
+ ---
5
+
6
+ ### 介绍
7
+
8
+ 基于 [axios](https://axios-http.com/) 的渐进式请求工具。 它不会破坏 `axios` 原有的能力,帮助您更轻松地处理请求。
9
+
10
+ ### 快速开始
11
+
12
+ #### 安装
13
+
14
+ ```shell
15
+ # 通过 npm, yarn, pnpm 安装
16
+
17
+ # npm
18
+ npm i @varlet/axle -S
19
+
20
+ #yarn
21
+ yarn add @varlet/axle
22
+
23
+ #pnpm
24
+ pnpm add @varlet/axle
25
+ ```
26
+
27
+ #### 发送请求
28
+
29
+ `Axle` 归一化了请求函数的参数,并针对不同的业务需求,扩展了更多请求函数,下面是一个简单的例子。
30
+
31
+ ```js
32
+ import { createAxle } from '@varlet/axle'
33
+
34
+ const axle = createAxle(/** @see https://axios-http.com **/)
35
+
36
+ axle.get('/url', { current: 1, pageSize: 10 }, { headers: {} })
37
+ axle.post('/url', { name: 'Axle' }, { headers: {} })
38
+ ```
39
+
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
+ ### 配置
106
+
107
+ `Axle` 完全支持 `axios` 的所有配置能力。
108
+
109
+ ```js
110
+ const axle = createAxle(/** @see https://axios-http.com **/)
111
+ // axle 内置的 axios ,用法和 axios 一模一样,并且和 axle 共享配置。
112
+ const { axios } = axle
113
+
114
+ axios.defaults.baseURL = 'https://api.example.com'
115
+ axios.defaults.headers.common['TOKEN'] = TOKEN
116
+ axios.defaults.timeout = 2500
117
+
118
+ // 添加请求前拦截器
119
+ axios.interceptors.request.use((config) => {
120
+ // 请求前处理
121
+ return config
122
+ }, (error) => {
123
+ // 请求错误处理
124
+ return Promise.reject(error)
125
+ })
126
+
127
+ // 添加请求后返回拦截器
128
+ axios.interceptors.response.use((response) => {
129
+ // 任何位于 2xx 范围内的状态码都会导致该函数触发
130
+ // 对响应数据做一些事情
131
+ return response
132
+ }, (error) => {
133
+ // 任何超出 2xx 范围的状态代码都会导致此函数触发
134
+ // 对响应错误做一些事情
135
+ return Promise.reject(error)
136
+ })
137
+ ```
138
+
139
+ ## Axle & Axios 请求函数
140
+
141
+ `Axle` 提供的请求函数可以帮助您更轻松地发送请求。 以下是一些示例,包括与 `axios` 的比较。
142
+ 提示:下面只是以 `get` 和 `post` 为例,除此之外 Axle 还支持 `options`、`head`、`delete`、`patch` 、`put` 方法。
143
+
144
+ ### get
145
+
146
+ #### JSON
147
+
148
+ ```js
149
+ // axios
150
+ axios.get('/url', { params: { id: 1 } })
151
+ // axle
152
+ axle.get('/url', { id: 1 })
153
+ ```
154
+
155
+ #### Blob
156
+
157
+ ```js
158
+ // axios
159
+ axios.get('/url', { params: { id: 1 }, responseType: 'blob' })
160
+ // axle
161
+ axle.getBlob('/url', { id: 1 })
162
+ ```
163
+
164
+ #### Text
165
+
166
+ ```js
167
+ // axios
168
+ axios.get('/url', { params: { id: 1 }, responseType: 'text' })
169
+ // axle
170
+ axle.getText('/url', { id: 1 })
171
+ ```
172
+
173
+ #### Document
174
+
175
+ ```js
176
+ // axios
177
+ axios.get('/url', { params: { id: 1 }, responseType: 'document' })
178
+ // axle
179
+ axle.getDocument('/url', { id: 1 })
180
+ ```
181
+
182
+ #### ArrayBuffer
183
+
184
+ ```js
185
+ // axios
186
+ axios.get('/url', { params: { id: 1 }, responseType: 'arraybuffer' })
187
+ // axle
188
+ axle.getArrayBuffer('/url', { id: 1 })
189
+ ```
190
+
191
+ #### Stream
192
+
193
+ ```js
194
+ // axios
195
+ axios.get('/url', { params: { id: 1 }, responseType: 'stream' })
196
+ // axle
197
+ axle.getStream('/url', { id: 1 })
198
+ ```
199
+
200
+ ## post
201
+
202
+ #### JSON
203
+
204
+ 和 axios 一致。
205
+
206
+ ```js
207
+ // axios
208
+ axios.post('/url', { name: 'foo' })
209
+ // axle
210
+ axle.post('/url', { name: 'foo' })
211
+ ```
212
+
213
+ #### application/x-www-form-urlencoded
214
+
215
+ ```js
216
+ // axios
217
+ axios.post('/url', qs.stringify({ name: 'foo' }), {
218
+ headers: {
219
+ 'Content-Type': 'application/x-www-form-urlencoded'
220
+ }
221
+ })
222
+ // axle
223
+ axle.postUrlEncode('/url', { name: 'foo' })
224
+ ```
225
+
226
+ #### multipart/form-data
227
+
228
+ ```js
229
+ // axios
230
+ const formData = new FormData()
231
+ formData.append('name', 'foo')
232
+ formData.append('file', new File())
233
+
234
+ axios.post('/url', formData, {
235
+ headers: {
236
+ 'Content-Type': 'multipart/form-data'
237
+ }
238
+ })
239
+ // axle
240
+ axle.postMultipart('/url', { name: 'foo', file: new File() })
241
+ ```
242
+
243
+ ### 实用工具
244
+
245
+ #### 通知浏览器下载文件
246
+
247
+ ```js
248
+ axle.download(await axle.getBlob('/url', { id: 1 }), 'filename')
249
+ ```
250
+
251
+ #### 公共 header 操作
252
+
253
+ ```js
254
+ const headers = axle.getHeaders()
255
+ axle.setHeader('TOKEN', TOKEN)
256
+ axle.removeHeader('TOKEN')
257
+ ```
258
+
259
+
260
+
261
+
package/es/instance.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { AxiosInstance, AxiosRequestConfig, AxiosResponse, ResponseType } from 'axios';
1
+ import type { AxiosInstance, AxiosRequestConfig, AxiosResponse, HeadersDefaults, ResponseType } from 'axios';
2
2
  export type AxleRequestConfig = AxiosRequestConfig & {
3
3
  exceptionMessage?: any;
4
4
  };
@@ -32,15 +32,16 @@ export type AxleInstance = {
32
32
  deleteArrayBuffer: FetchHelper;
33
33
  deleteStream: FetchHelper;
34
34
  post: ModifyHelper;
35
- postJSON: ModifyHelper;
35
+ postUrlEncode: ModifyHelper;
36
36
  postMultipart: ModifyHelper;
37
37
  put: ModifyHelper;
38
- putJSON: ModifyHelper;
38
+ putUrlEncode: ModifyHelper;
39
39
  putMultipart: ModifyHelper;
40
40
  patch: ModifyHelper;
41
- patchJSON: ModifyHelper;
41
+ patchUrlEncode: ModifyHelper;
42
42
  patchMultipart: ModifyHelper;
43
43
  download(blob: string | Blob, filename: string): void;
44
+ getHeaders(): HeadersDefaults['common'];
44
45
  setHeader(key: string, value: string): void;
45
46
  removeHeader(key: string | string[]): void;
46
47
  axios: AxiosInstance;
package/es/instance.js CHANGED
@@ -33,6 +33,9 @@ export function download(url, filename) {
33
33
  }
34
34
  export function createAxle(config = {}) {
35
35
  const service = axios.create(config);
36
+ function getHeaders() {
37
+ return service.defaults.headers['common'];
38
+ }
36
39
  function setHeader(key, value) {
37
40
  ;
38
41
  service.defaults.headers['common'][key] = value;
@@ -69,16 +72,17 @@ export function createAxle(config = {}) {
69
72
  deleteArrayBuffer: createFetchHelper(service, 'delete', 'arraybuffer'),
70
73
  deleteText: createFetchHelper(service, 'delete', 'text'),
71
74
  deleteStream: createFetchHelper(service, 'delete', 'stream'),
72
- post: createModifyHelper(service, 'post', 'application/x-www-form-urlencoded'),
73
- postJSON: createModifyHelper(service, 'post', 'application/json'),
75
+ post: createModifyHelper(service, 'post', 'application/json'),
76
+ postUrlEncode: createModifyHelper(service, 'post', 'application/x-www-form-urlencoded'),
74
77
  postMultipart: createModifyHelper(service, 'post', 'multipart/form-data'),
75
- put: createModifyHelper(service, 'put', 'application/x-www-form-urlencoded'),
76
- putJSON: createModifyHelper(service, 'put', 'application/json'),
78
+ put: createModifyHelper(service, 'put', 'application/json'),
79
+ putUrlEncode: createModifyHelper(service, 'put', 'application/x-www-form-urlencoded'),
77
80
  putMultipart: createModifyHelper(service, 'put', 'multipart/form-data'),
78
- patch: createModifyHelper(service, 'patch', 'application/x-www-form-urlencoded'),
79
- patchJSON: createModifyHelper(service, 'patch', 'application/json'),
81
+ patch: createModifyHelper(service, 'patch', 'application/json'),
82
+ patchUrlEncode: createModifyHelper(service, 'patch', 'application/x-www-form-urlencoded'),
80
83
  patchMultipart: createModifyHelper(service, 'patch', 'multipart/form-data'),
81
84
  download,
85
+ getHeaders,
82
86
  setHeader,
83
87
  removeHeader,
84
88
  axios: service,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@varlet/axle",
3
- "version": "0.0.25",
3
+ "version": "0.0.26",
4
4
  "description": "A useful axios helper",
5
5
  "type": "module",
6
6
  "main": "es/index.js",