nuxt-custom-fetch 0.0.2-alpha.0 → 1.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/README.md +15 -3
- package/dist/module.json +1 -1
- package/dist/runtime/ajax.d.ts +4 -4
- package/dist/runtime/ajax.mjs +17 -16
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -60,6 +60,18 @@ onResponseError?(context: FetchContext & {
|
|
|
60
60
|
}): Promise<void> | void;
|
|
61
61
|
```
|
|
62
62
|
|
|
63
|
+
### AsyncDataOptions<DataT>
|
|
64
|
+
|
|
65
|
+
```
|
|
66
|
+
server?: boolean;
|
|
67
|
+
lazy?: boolean;
|
|
68
|
+
default?: () => DataT | Ref<DataT> | null;
|
|
69
|
+
transform?: Transform;
|
|
70
|
+
pick?: PickKeys;
|
|
71
|
+
watch?: MultiWatchSources;
|
|
72
|
+
immediate?: boolean;
|
|
73
|
+
```
|
|
74
|
+
|
|
63
75
|
## 使用方式
|
|
64
76
|
|
|
65
77
|
```ts
|
|
@@ -73,14 +85,14 @@ const ajax = new HTTP({
|
|
|
73
85
|
}
|
|
74
86
|
})
|
|
75
87
|
|
|
76
|
-
export const getInfo =
|
|
77
|
-
ajax.get<DataT>('/api/get-ip', { params })
|
|
88
|
+
export const getInfo = (params: FetchOptions['params']) =>
|
|
89
|
+
ajax.get<DataT>('/api/get-ip', { params }, options?: AsyncDataOptions<DataT>)
|
|
78
90
|
```
|
|
79
91
|
|
|
80
92
|
```ts
|
|
81
93
|
import { getInfo } from './ajax'
|
|
82
94
|
|
|
83
|
-
const { data, error, pending } = await getInfo
|
|
95
|
+
const { data, error, pending } = await getInfo({
|
|
84
96
|
sign: 123
|
|
85
97
|
})
|
|
86
98
|
console.log(data.value)
|
package/dist/module.json
CHANGED
package/dist/runtime/ajax.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { FetchOptions } from 'ofetch';
|
|
2
|
-
import type { _AsyncData } from 'nuxt/dist/app/composables/asyncData';
|
|
2
|
+
import type { _AsyncData, AsyncDataOptions } from 'nuxt/dist/app/composables/asyncData';
|
|
3
3
|
export declare const ajax: <DataT, ErrorT = Error | null>(url: string, key: string, config?: FetchOptions & {
|
|
4
4
|
extraParams?: string[];
|
|
5
5
|
offline?: () => void;
|
|
6
|
-
}) => Promise<_AsyncData<DataT, ErrorT>>;
|
|
6
|
+
}, options?: AsyncDataOptions<DataT, import("nuxt/dist/app/composables/asyncData")._Transform<DataT, DataT>, import("nuxt/dist/app/composables/asyncData").KeyOfRes<import("nuxt/dist/app/composables/asyncData")._Transform<DataT, DataT>>> | undefined) => Promise<_AsyncData<DataT, ErrorT>>;
|
|
7
7
|
type HTTPConfig = {
|
|
8
8
|
baseURL?: string;
|
|
9
9
|
extraParams?: string[];
|
|
@@ -14,7 +14,7 @@ export declare class HTTP {
|
|
|
14
14
|
params: HTTPConfig;
|
|
15
15
|
constructor(config?: HTTPConfig);
|
|
16
16
|
baseConfig(config: FetchOptions): FetchOptions;
|
|
17
|
-
get<DataT, ErrorT = Error | null>(url: string, config?: FetchOptions): Promise<_AsyncData<DataT, ErrorT>>;
|
|
18
|
-
post<DataT, ErrorT = Error | null>(url: string, config?: FetchOptions): Promise<_AsyncData<DataT, ErrorT>>;
|
|
17
|
+
get<DataT, ErrorT = Error | null>(url: string, config?: FetchOptions, options?: AsyncDataOptions<DataT>): Promise<_AsyncData<DataT, ErrorT>>;
|
|
18
|
+
post<DataT, ErrorT = Error | null>(url: string, config?: FetchOptions, options?: AsyncDataOptions<DataT>): Promise<_AsyncData<DataT, ErrorT>>;
|
|
19
19
|
}
|
|
20
20
|
export {};
|
package/dist/runtime/ajax.mjs
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { hash } from "ohash";
|
|
2
2
|
import { createError } from "h3";
|
|
3
3
|
import { useAsyncData } from "#imports";
|
|
4
|
-
export const ajax = (url, key, config = {}) => {
|
|
4
|
+
export const ajax = (url, key, config = {}, options) => {
|
|
5
5
|
if (process.client && navigator && !navigator.onLine) {
|
|
6
6
|
config.offline && config.offline();
|
|
7
7
|
}
|
|
8
8
|
const defaultOptions = {
|
|
9
9
|
key,
|
|
10
10
|
onRequest({
|
|
11
|
-
options
|
|
11
|
+
options: options2
|
|
12
12
|
}) {
|
|
13
|
-
|
|
13
|
+
options2.headers = options2.headers || {};
|
|
14
14
|
},
|
|
15
15
|
onRequestError({
|
|
16
16
|
error
|
|
@@ -40,9 +40,10 @@ export const ajax = (url, key, config = {}) => {
|
|
|
40
40
|
return useAsyncData(key, () => $fetch(url, {
|
|
41
41
|
...defaultOptions,
|
|
42
42
|
...config
|
|
43
|
-
}));
|
|
43
|
+
}), options);
|
|
44
44
|
};
|
|
45
|
-
function getKey(url,
|
|
45
|
+
function getKey(url, config, extraParams = []) {
|
|
46
|
+
const params = (config.query ? config.query : config.params) || {};
|
|
46
47
|
const restConfig = {};
|
|
47
48
|
for (const key2 in params) {
|
|
48
49
|
if (Object.prototype.hasOwnProperty.call(params, key2) && !extraParams.includes(key2)) {
|
|
@@ -61,31 +62,31 @@ export class HTTP {
|
|
|
61
62
|
}
|
|
62
63
|
baseConfig(config) {
|
|
63
64
|
const { paramsHandler } = this.params;
|
|
64
|
-
const params = config.params || {};
|
|
65
|
+
const params = (config.query ? config.query : config.params) || {};
|
|
65
66
|
if (paramsHandler && typeof paramsHandler === "function") {
|
|
66
67
|
return paramsHandler({ ...params });
|
|
67
68
|
}
|
|
68
69
|
return { ...params };
|
|
69
70
|
}
|
|
70
|
-
get(url, config = {}) {
|
|
71
|
-
const key = getKey(url, config
|
|
72
|
-
const
|
|
71
|
+
get(url, config = {}, options) {
|
|
72
|
+
const key = getKey(url, config, this.params.extraParams);
|
|
73
|
+
const query = this.baseConfig(config);
|
|
73
74
|
return ajax(url, key, {
|
|
74
75
|
baseURL: this.params.baseURL,
|
|
75
76
|
method: "GET",
|
|
76
77
|
...config,
|
|
77
|
-
|
|
78
|
-
});
|
|
78
|
+
query
|
|
79
|
+
}, options);
|
|
79
80
|
}
|
|
80
|
-
post(url, config = {}) {
|
|
81
|
-
const key = getKey(url, config
|
|
82
|
-
const
|
|
81
|
+
post(url, config = {}, options) {
|
|
82
|
+
const key = getKey(url, config, this.params.extraParams);
|
|
83
|
+
const query = this.baseConfig(config);
|
|
83
84
|
return ajax(url, key, {
|
|
84
85
|
baseURL: this.params.baseURL,
|
|
85
86
|
method: "POST",
|
|
86
87
|
...config,
|
|
87
|
-
|
|
88
|
+
query,
|
|
88
89
|
offline: this.params.offline
|
|
89
|
-
});
|
|
90
|
+
}, options);
|
|
90
91
|
}
|
|
91
92
|
}
|