iota-fetch 0.0.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/babel.config.cjs +11 -0
- package/lib/bundle.cjs.js +3377 -0
- package/lib/bundle.cjs.js.map +1 -0
- package/lib/bundle.esm.js +3372 -0
- package/lib/bundle.esm.js.map +1 -0
- package/package.json +30 -0
- package/public/index.html +65 -0
- package/rollup.config.js +56 -0
- package/src/axios/fetch.js +22 -0
- package/src/axios/fetchFun.js +40 -0
- package/src/axios/instance.js +8 -0
- package/src/axios/resQueue.js +39 -0
- package/src/main.js +5 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import fetchData from "./fetchFun";
|
|
2
|
+
import createInstance from "./instance";
|
|
3
|
+
|
|
4
|
+
const createFetch = ({ requestConfig, loading }) => {
|
|
5
|
+
let instance = createInstance({ requestConfig });
|
|
6
|
+
|
|
7
|
+
if (!requestConfig) throw "需要 requestConfig";
|
|
8
|
+
|
|
9
|
+
//将封装的数据分发下来
|
|
10
|
+
const fetch = ["get", "post", "delete", "put", "patch"].reduce((pre, cur) => {
|
|
11
|
+
return {
|
|
12
|
+
...pre,
|
|
13
|
+
[cur](url, data, config) {
|
|
14
|
+
return fetchData(instance, { method: cur, url, data, config }, loading); // 请求方式
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
}, {});
|
|
18
|
+
|
|
19
|
+
return { fetch, instance };
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export default createFetch;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import requestQueue from "./resQueue";
|
|
2
|
+
|
|
3
|
+
const { createMapKey, queue } = requestQueue;
|
|
4
|
+
|
|
5
|
+
const fetchData = (instance, { url, method, data, config }, loading) => {
|
|
6
|
+
const controller = new AbortController();
|
|
7
|
+
let httpOpts = {
|
|
8
|
+
url,
|
|
9
|
+
method,
|
|
10
|
+
...config,
|
|
11
|
+
signal: controller.signal,
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
// 注册 请求队列
|
|
15
|
+
let key = createMapKey(method, url, data);
|
|
16
|
+
queue.set(key, controller);
|
|
17
|
+
|
|
18
|
+
if (["get", "delete"].includes(method)) {
|
|
19
|
+
httpOpts = { ...httpOpts, params: data };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (["patch", "put", "post"].includes(method)) {
|
|
23
|
+
httpOpts = { ...httpOpts, data };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return new Promise((resolve, rejects) => {
|
|
27
|
+
config?.loading && loading?.show();
|
|
28
|
+
instance(httpOpts)
|
|
29
|
+
.then((res) => resolve(res.data))
|
|
30
|
+
.catch((err) => rejects(err))
|
|
31
|
+
.finally(() => {
|
|
32
|
+
// 每次请求完要对Resqueue 删除
|
|
33
|
+
queue.has(key) && queue.delete(key);
|
|
34
|
+
// 如果有load show 需要关闭
|
|
35
|
+
config?.loading && loading?.hide();
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export default fetchData;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
class RequestQueue {
|
|
2
|
+
queue = new Map();
|
|
3
|
+
|
|
4
|
+
constructor() {
|
|
5
|
+
if (RequestQueue.instance) {
|
|
6
|
+
return RequestQueue.instance;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
RequestQueue.instance = this;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// 获取queque
|
|
13
|
+
getQueue() {
|
|
14
|
+
return this.queue;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// 生成队列的key
|
|
18
|
+
createMapKey(method, path, data) {
|
|
19
|
+
let params = data ? `-${JSON.stringify(data)}` : "";
|
|
20
|
+
return `${method}-${path}` + params;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// 中断key
|
|
24
|
+
abortive(key) {
|
|
25
|
+
const controller = this.queue.get(key);
|
|
26
|
+
controller?.abort();
|
|
27
|
+
this.queue.delete(key); // 中断后移除该key
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// 中断所有请求
|
|
31
|
+
removeResAll() {
|
|
32
|
+
this.queue.forEach((controller) => controller.abort());
|
|
33
|
+
this.queue.clear(); // 清除队列
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
let requestQueue = new RequestQueue();
|
|
38
|
+
|
|
39
|
+
export default requestQueue;
|