etherreq 1.0.4 → 1.0.6
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/package.json +1 -1
- package/src/cache.js +56 -0
- package/src/myaxios.js +13 -0
- package/src/request.js +22 -2
package/package.json
CHANGED
package/src/cache.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// src/cache.js
|
|
2
|
+
const cacheStore = new Map();
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 获取缓存 key
|
|
6
|
+
* @param {string} url 请求地址
|
|
7
|
+
* @param {Object} options 请求参数
|
|
8
|
+
* @returns {string}
|
|
9
|
+
*/
|
|
10
|
+
function getCacheKey(url, options) {
|
|
11
|
+
const params = new URLSearchParams(options.params || {});
|
|
12
|
+
return `${options.method}:${url}?${params.toString()}`;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const requestCache = {
|
|
16
|
+
/**
|
|
17
|
+
* 设置缓存
|
|
18
|
+
* @param {string} key 缓存 key
|
|
19
|
+
* @param {*} value 缓存值
|
|
20
|
+
* @param {number} ttl 过期时间(毫秒)
|
|
21
|
+
*/
|
|
22
|
+
set(key, value, ttl = 5 * 60 * 1000) {
|
|
23
|
+
const expireAt = Date.now() + ttl;
|
|
24
|
+
cacheStore.set(key, { value, expireAt });
|
|
25
|
+
},
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 获取缓存
|
|
29
|
+
* @param {string} key 缓存 key
|
|
30
|
+
* @returns {*}
|
|
31
|
+
*/
|
|
32
|
+
get(key) {
|
|
33
|
+
const entry = cacheStore.get(key);
|
|
34
|
+
if (entry && entry.expireAt > Date.now()) {
|
|
35
|
+
return entry.value;
|
|
36
|
+
}
|
|
37
|
+
return null;
|
|
38
|
+
},
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 删除指定缓存
|
|
42
|
+
* @param {string} key
|
|
43
|
+
*/
|
|
44
|
+
delete(key) {
|
|
45
|
+
cacheStore.delete(key);
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 清空所有缓存
|
|
50
|
+
*/
|
|
51
|
+
clear() {
|
|
52
|
+
cacheStore.clear();
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export { getCacheKey };
|
package/src/myaxios.js
CHANGED
|
@@ -30,6 +30,18 @@ export const create = (defaultConfig = {}) => {
|
|
|
30
30
|
const res = await fetch(url, options);
|
|
31
31
|
const data = await res.json();
|
|
32
32
|
|
|
33
|
+
// 手动处理 HTTP 错误状态
|
|
34
|
+
if (!res.ok) {
|
|
35
|
+
const error = new Error(`HTTP 错误: ${res.status} - ${res.statusText}`);
|
|
36
|
+
error.response = {
|
|
37
|
+
data,
|
|
38
|
+
status: res.status,
|
|
39
|
+
statusText: res.statusText,
|
|
40
|
+
headers: res.headers,
|
|
41
|
+
};
|
|
42
|
+
throw error;
|
|
43
|
+
}
|
|
44
|
+
|
|
33
45
|
response = {
|
|
34
46
|
data,
|
|
35
47
|
status: res.status,
|
|
@@ -71,5 +83,6 @@ export const create = (defaultConfig = {}) => {
|
|
|
71
83
|
},
|
|
72
84
|
};
|
|
73
85
|
|
|
86
|
+
|
|
74
87
|
return instance;
|
|
75
88
|
};
|
package/src/request.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
// src/request.js
|
|
2
2
|
import { create } from './myaxios';
|
|
3
|
+
import { requestCache, getCacheKey } from './cache';
|
|
3
4
|
|
|
4
5
|
// 创建一个带有默认配置的请求实例
|
|
5
6
|
const instance = create({
|
|
6
7
|
baseURL: 'https://api.example.com', // 默认基础 URL
|
|
7
8
|
});
|
|
8
9
|
|
|
9
|
-
// 请求拦截器:自动注入 token
|
|
10
|
+
// 请求拦截器:自动注入 token、Content-Type 和缓存处理
|
|
10
11
|
instance.interceptors.request.use((config) => {
|
|
11
12
|
const token = localStorage.getItem('token');
|
|
12
13
|
|
|
@@ -21,15 +22,34 @@ instance.interceptors.request.use((config) => {
|
|
|
21
22
|
headers['Content-Type'] = 'application/json';
|
|
22
23
|
}
|
|
23
24
|
|
|
25
|
+
// GET 请求启用缓存(可选:添加 disableCache: true 配置跳过缓存)
|
|
26
|
+
if (config.method === 'GET' && !config.disableCache) {
|
|
27
|
+
const cacheKey = getCacheKey(config.url, config);
|
|
28
|
+
const cached = requestCache.get(cacheKey);
|
|
29
|
+
|
|
30
|
+
if (cached) {
|
|
31
|
+
// 返回缓存数据,中断后续请求流程
|
|
32
|
+
return Promise.resolve(cached);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
24
36
|
return {
|
|
25
37
|
...config,
|
|
26
38
|
headers,
|
|
27
39
|
};
|
|
28
40
|
});
|
|
29
41
|
|
|
30
|
-
// 响应拦截器:自动提取 response.data
|
|
42
|
+
// 响应拦截器:自动提取 response.data 并写入缓存
|
|
31
43
|
instance.interceptors.response.use(
|
|
32
44
|
(response) => {
|
|
45
|
+
const config = response.config;
|
|
46
|
+
|
|
47
|
+
// 如果是 GET 请求且未禁用缓存,则写入缓存
|
|
48
|
+
if (config && config.method === 'GET' && !config.disableCache) {
|
|
49
|
+
const cacheKey = getCacheKey(config.url, config);
|
|
50
|
+
requestCache.set(cacheKey, response); // 缓存完整响应对象
|
|
51
|
+
}
|
|
52
|
+
|
|
33
53
|
return response.data; // 返回 data 字段作为结果
|
|
34
54
|
},
|
|
35
55
|
(error) => {
|