nuxt-custom-fetch 1.1.2 → 1.1.3
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 +0 -6
- package/dist/module.json +1 -1
- package/dist/runtime/ajax.mjs +17 -10
- package/dist/runtime/type.d.ts +0 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,7 +10,6 @@
|
|
|
10
10
|
{
|
|
11
11
|
baseURL?: string
|
|
12
12
|
paramsHandler?: (params: FetchOptions['params']) => FetchOptions & HTTPConfig
|
|
13
|
-
extraParams?: string[]
|
|
14
13
|
offline?: () => void
|
|
15
14
|
}
|
|
16
15
|
```
|
|
@@ -23,11 +22,6 @@
|
|
|
23
22
|
|
|
24
23
|
处理 params 的自定义函数
|
|
25
24
|
|
|
26
|
-
### extraParams
|
|
27
|
-
|
|
28
|
-
排除在 params 中的 key(用于生成\$fetch 所需要的唯一 key)
|
|
29
|
-
类型: string[]
|
|
30
|
-
|
|
31
25
|
### 请求-响应拦截
|
|
32
26
|
|
|
33
27
|
```ts
|
package/dist/module.json
CHANGED
package/dist/runtime/ajax.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { hash } from "ohash";
|
|
2
2
|
import { createError } from "h3";
|
|
3
|
-
import { useAsyncData } from "#imports";
|
|
3
|
+
import { toValue, useAsyncData } from "#imports";
|
|
4
4
|
export const ajax = (url, key, config, options) => {
|
|
5
5
|
if (process.client && navigator && !navigator.onLine) {
|
|
6
6
|
config.offline && config.offline();
|
|
@@ -52,16 +52,23 @@ export const ajax = (url, key, config, options) => {
|
|
|
52
52
|
options
|
|
53
53
|
);
|
|
54
54
|
};
|
|
55
|
-
function
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
55
|
+
function generateOptionSegments(opts) {
|
|
56
|
+
const segments = [
|
|
57
|
+
toValue(opts.method)?.toUpperCase() || "GET",
|
|
58
|
+
toValue(opts.baseURL)
|
|
59
|
+
];
|
|
60
|
+
for (const _obj of [opts.params || opts.query]) {
|
|
61
|
+
const obj = toValue(_obj);
|
|
62
|
+
if (!obj) {
|
|
63
|
+
continue;
|
|
61
64
|
}
|
|
65
|
+
const unwrapped = {};
|
|
66
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
67
|
+
unwrapped[toValue(key)] = toValue(value);
|
|
68
|
+
}
|
|
69
|
+
segments.push(unwrapped);
|
|
62
70
|
}
|
|
63
|
-
|
|
64
|
-
return key;
|
|
71
|
+
return segments;
|
|
65
72
|
}
|
|
66
73
|
const Noop = () => {
|
|
67
74
|
};
|
|
@@ -97,7 +104,7 @@ export class CustomFetch {
|
|
|
97
104
|
return { ...params };
|
|
98
105
|
}
|
|
99
106
|
http(url, config = {}, options) {
|
|
100
|
-
const key = config.key ? config.key :
|
|
107
|
+
const key = config.key ? config.key : hash([...generateOptionSegments(config)]);
|
|
101
108
|
const baseURL = config?.baseURL || this.baseURL;
|
|
102
109
|
const query = this.baseConfig(config);
|
|
103
110
|
const offline = config?.offline || this.offline;
|
package/dist/runtime/type.d.ts
CHANGED
|
@@ -23,7 +23,6 @@ export interface OnResponseErrorType {
|
|
|
23
23
|
export type FetchMethod = 'options' | 'GET' | 'POST' | 'get' | 'HEAD' | 'PATCH' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'post' | 'head' | 'patch' | 'put' | 'delete' | 'connect' | 'trace' | undefined;
|
|
24
24
|
export interface HTTPConfig extends Omit<FetchOptions, 'method'> {
|
|
25
25
|
baseURL?: string;
|
|
26
|
-
extraParams?: string[];
|
|
27
26
|
useParamsHandler?: boolean;
|
|
28
27
|
paramsHandler?: (params: FetchOptions['params']) => HTTPConfig;
|
|
29
28
|
offline?: () => void;
|