iota-fetch 0.0.3 → 0.0.4
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 +41 -3
- package/lib/bundle.cjs.js +13 -1
- package/lib/bundle.cjs.js.map +1 -1
- package/lib/bundle.esm.js +13 -1
- package/lib/bundle.esm.js.map +1 -1
- package/package.json +1 -1
- package/public/index.html +57 -58
- package/src/axios/fetch.js +12 -2
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
npm install iota-fetch
|
|
5
5
|
```
|
|
6
6
|
|
|
7
|
-
### 使用
|
|
7
|
+
### 使用 ①
|
|
8
8
|
|
|
9
9
|
```js
|
|
10
10
|
import createFetch from "iota-fetch"
|
|
@@ -12,6 +12,44 @@ import createFetch from "iota-fetch"
|
|
|
12
12
|
// requestConfig 请求配置项 同axios config
|
|
13
13
|
const requestConfig = {};
|
|
14
14
|
|
|
15
|
+
// 请求拦截
|
|
16
|
+
function requestIntercept() {
|
|
17
|
+
let intercept = (config) => {
|
|
18
|
+
// 在发送请求之前做些什么
|
|
19
|
+
return config;
|
|
20
|
+
}
|
|
21
|
+
let error = (error) => {
|
|
22
|
+
// 对请求错误做些什么
|
|
23
|
+
return Promise.reject(error);
|
|
24
|
+
}
|
|
25
|
+
return [intercept, error]
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// 相应拦截
|
|
29
|
+
function responseIntercept() {
|
|
30
|
+
let intercept = (response) => {
|
|
31
|
+
// 2xx 范围内的状态码都会触发该函数。
|
|
32
|
+
console.log(response, "响应拦截器")
|
|
33
|
+
return response;
|
|
34
|
+
}
|
|
35
|
+
let error = () => {
|
|
36
|
+
// 超出 2xx 范围的状态码都会触发该函数。
|
|
37
|
+
return Promise.reject(error);
|
|
38
|
+
}
|
|
39
|
+
return [intercept, error]
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let { fetch } = createFetch({ requestIntercept, responseIntercept, requestConfig })
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### 使用 ②
|
|
46
|
+
|
|
47
|
+
```tsx
|
|
48
|
+
import createFetch from "iota-fetch"
|
|
49
|
+
|
|
50
|
+
// requestConfig 请求配置项 同axios config
|
|
51
|
+
const requestConfig = {};
|
|
52
|
+
|
|
15
53
|
//loading -> loading.show() loading.hide()
|
|
16
54
|
const { fetch, instance } = createFetch({ requestConfig, loading})
|
|
17
55
|
|
|
@@ -30,17 +68,17 @@ instance.interceptors.request.use(function (config) {
|
|
|
30
68
|
// 添加响应拦截器
|
|
31
69
|
instance.interceptors.response.use(function (response) {
|
|
32
70
|
// 2xx 范围内的状态码都会触发该函数。
|
|
33
|
-
// 对响应数据做点什么
|
|
34
71
|
return response;
|
|
35
72
|
}, function (error) {
|
|
36
73
|
// 超出 2xx 范围的状态码都会触发该函数。
|
|
37
|
-
// 对响应错误做点什么
|
|
38
74
|
return Promise.reject(error);
|
|
39
75
|
});
|
|
40
76
|
|
|
41
77
|
export default fetch;
|
|
42
78
|
```
|
|
43
79
|
|
|
80
|
+
|
|
81
|
+
|
|
44
82
|
### 取消请求
|
|
45
83
|
|
|
46
84
|
```js
|
package/lib/bundle.cjs.js
CHANGED
|
@@ -3344,13 +3344,15 @@ const createInstance = ({
|
|
|
3344
3344
|
};
|
|
3345
3345
|
|
|
3346
3346
|
const createFetch = ({
|
|
3347
|
+
requestIntercept,
|
|
3348
|
+
responseIntercept,
|
|
3347
3349
|
requestConfig,
|
|
3348
3350
|
loading
|
|
3349
3351
|
}) => {
|
|
3350
3352
|
let instance = createInstance({
|
|
3351
3353
|
requestConfig
|
|
3352
3354
|
});
|
|
3353
|
-
if (!requestConfig) throw "
|
|
3355
|
+
if (!requestConfig) throw "please add requestConfig";
|
|
3354
3356
|
|
|
3355
3357
|
//将封装的数据分发下来
|
|
3356
3358
|
const fetch = ["get", "post", "delete", "put", "patch"].reduce((pre, cur) => {
|
|
@@ -3366,6 +3368,16 @@ const createFetch = ({
|
|
|
3366
3368
|
}
|
|
3367
3369
|
};
|
|
3368
3370
|
}, {});
|
|
3371
|
+
|
|
3372
|
+
// 拦截器规范
|
|
3373
|
+
if (requestIntercept) {
|
|
3374
|
+
let [requestFunc, errorFunc] = requestIntercept();
|
|
3375
|
+
instance.interceptors.request.use(requestFunc, errorFunc);
|
|
3376
|
+
}
|
|
3377
|
+
if (responseIntercept) {
|
|
3378
|
+
let [responseFunc, errorFunc] = requestIntercept();
|
|
3379
|
+
instance.interceptors.response.use(responseFunc, errorFunc);
|
|
3380
|
+
}
|
|
3369
3381
|
return {
|
|
3370
3382
|
fetch,
|
|
3371
3383
|
instance
|