etherreq 1.0.23 → 1.0.24

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "etherreq",
3
- "version": "1.0.23",
3
+ "version": "1.0.24",
4
4
  "description": "A lightweight custom HTTP request library.",
5
5
  "main": "src/index.js",
6
6
  "types": "src/types/etherreq.d.ts",
package/src/etherreq.js CHANGED
@@ -12,21 +12,28 @@ export const create = (defaultConfig = {}) => {
12
12
 
13
13
  const dispatchRequest = async (config) => {
14
14
  // 请求拦截器执行
15
- for (const interceptor of interceptors.request) {
15
+ for (const interceptor of interceptors.request) {
16
16
  config = await interceptor.fulfilled(config);
17
17
  }
18
+ let response;
19
+ if (isAlreadyResolved(config)) {
20
+ return config; // 如果是缓存数据,直接返回
21
+ }
18
22
 
19
- let response;
23
+ // 否则才发起网络请求
24
+ const { url, method = 'GET', headers = {}, body } = config;
20
25
  try {
21
- const { url, method = 'GET', headers = {}, body } = config;
22
- const options = {
23
- method,
24
- headers,
25
- body: method !== 'GET' ? JSON.stringify(body) : undefined,
26
- };
26
+ const options = {
27
+ method,
28
+ headers,
29
+ body: method !== 'GET' ? JSON.stringify(body) : undefined,
30
+ };
31
+
32
+ const res = await fetch(url, options);
33
+ const data = await res.json();
34
+
35
+
27
36
 
28
- const res = await fetch(url, options);
29
- const data = await res.json();
30
37
  if (!res.ok) {
31
38
  const error = new Error(`HTTP 错误: ${res.status} - ${res.statusText}`);
32
39
  error.response = {
package/src/index.js CHANGED
@@ -25,11 +25,11 @@ const createMethod = (method) => (url, data, callback) => {
25
25
 
26
26
  const promise = request(url, options);
27
27
 
28
- if (typeof callback === 'function') {
29
- promise.then(data => callback(null, data)).catch(err => callback(err, null));
30
- } else {
31
- return promise;
32
- }
28
+ if (typeof callback === 'function') {
29
+ promise.then(data => callback(null, data)).catch(err => callback(err, null));
30
+ } else {
31
+ return promise;
32
+ }
33
33
  };
34
34
 
35
35
  export const etherreq = Object.assign(