nuxt-custom-fetch 1.0.0 → 1.0.2
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 +55 -3
- package/dist/module.json +1 -1
- package/dist/runtime/ajax.d.ts +7 -15
- package/dist/runtime/ajax.mjs +50 -36
- package/dist/runtime/type.d.ts +49 -0
- package/dist/runtime/type.mjs +0 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -26,6 +26,42 @@ offline?: () => void
|
|
|
26
26
|
排除在 params 中的 key(用于生成\$fetch 所需要的唯一 key)
|
|
27
27
|
类型: string[]
|
|
28
28
|
|
|
29
|
+
### 请求-响应拦截
|
|
30
|
+
|
|
31
|
+
```
|
|
32
|
+
onRequest?: ({ request, options }: {
|
|
33
|
+
request: FetchRequest
|
|
34
|
+
options: FetchOptions
|
|
35
|
+
}) => void
|
|
36
|
+
onRequestError?: ({
|
|
37
|
+
request,
|
|
38
|
+
options,
|
|
39
|
+
error
|
|
40
|
+
}: {
|
|
41
|
+
request: FetchRequest
|
|
42
|
+
options: FetchOptions
|
|
43
|
+
error: FetchError
|
|
44
|
+
}) => void
|
|
45
|
+
onResponse?: ({
|
|
46
|
+
request,
|
|
47
|
+
response,
|
|
48
|
+
options
|
|
49
|
+
}: {
|
|
50
|
+
request: FetchRequest
|
|
51
|
+
options: FetchOptions
|
|
52
|
+
response: FetchResponse<any>
|
|
53
|
+
}) => FetchResponse<ResponseType>
|
|
54
|
+
onResponseError?: ({
|
|
55
|
+
request,
|
|
56
|
+
response,
|
|
57
|
+
options
|
|
58
|
+
}: {
|
|
59
|
+
request: FetchRequest
|
|
60
|
+
options: FetchOptions
|
|
61
|
+
response: FetchResponse<any>
|
|
62
|
+
}) => void
|
|
63
|
+
```
|
|
64
|
+
|
|
29
65
|
```js
|
|
30
66
|
import { hash } from 'ohash'
|
|
31
67
|
const key = hash(JSON.stringify(restConfig) + url)
|
|
@@ -60,6 +96,18 @@ onResponseError?(context: FetchContext & {
|
|
|
60
96
|
}): Promise<void> | void;
|
|
61
97
|
```
|
|
62
98
|
|
|
99
|
+
### AsyncDataOptions<DataT>
|
|
100
|
+
|
|
101
|
+
```
|
|
102
|
+
server?: boolean;
|
|
103
|
+
lazy?: boolean;
|
|
104
|
+
default?: () => DataT | Ref<DataT> | null;
|
|
105
|
+
transform?: Transform;
|
|
106
|
+
pick?: PickKeys;
|
|
107
|
+
watch?: MultiWatchSources;
|
|
108
|
+
immediate?: boolean;
|
|
109
|
+
```
|
|
110
|
+
|
|
63
111
|
## 使用方式
|
|
64
112
|
|
|
65
113
|
```ts
|
|
@@ -73,19 +121,23 @@ const ajax = new HTTP({
|
|
|
73
121
|
}
|
|
74
122
|
})
|
|
75
123
|
|
|
76
|
-
export const getInfo =
|
|
77
|
-
ajax.get<DataT>('/api/get-ip', { params })
|
|
124
|
+
export const getInfo = (params: FetchOptions['params']) =>
|
|
125
|
+
ajax.get<DataT>('/api/get-ip', { params }, options?: AsyncDataOptions<DataT>)
|
|
78
126
|
```
|
|
79
127
|
|
|
80
128
|
```ts
|
|
81
129
|
import { getInfo } from './ajax'
|
|
82
130
|
|
|
83
|
-
const { data, error, pending } = await getInfo
|
|
131
|
+
const { data, error, pending } = await getInfo({
|
|
84
132
|
sign: 123
|
|
85
133
|
})
|
|
86
134
|
console.log(data.value)
|
|
87
135
|
```
|
|
88
136
|
|
|
137
|
+
## 注意
|
|
138
|
+
|
|
139
|
+
客户端请求,需要设置{server: false},在 onMounted 中请求无效(version > 3.0.0)
|
|
140
|
+
|
|
89
141
|
## Development
|
|
90
142
|
|
|
91
143
|
- Run `npm run dev:prepare` to generate type stubs.
|
package/dist/module.json
CHANGED
package/dist/runtime/ajax.d.ts
CHANGED
|
@@ -1,20 +1,12 @@
|
|
|
1
1
|
import type { FetchOptions } from 'ofetch';
|
|
2
|
-
import type { _AsyncData } from 'nuxt/dist/app/composables/asyncData';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
offline?: () => void;
|
|
6
|
-
}) => Promise<_AsyncData<DataT, ErrorT>>;
|
|
7
|
-
type HTTPConfig = {
|
|
8
|
-
baseURL?: string;
|
|
9
|
-
extraParams?: string[];
|
|
10
|
-
paramsHandler?: (params: FetchOptions['params']) => FetchOptions & HTTPConfig;
|
|
11
|
-
offline?: () => void;
|
|
12
|
-
};
|
|
2
|
+
import type { _AsyncData, AsyncDataOptions } from 'nuxt/dist/app/composables/asyncData';
|
|
3
|
+
import { HTTPConfig, Interceptors, AjaxConfig } from './type';
|
|
4
|
+
export declare const ajax: <DataT, ErrorT = Error | null>(url: string, key: string, config: AjaxConfig, 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>>;
|
|
13
5
|
export declare class HTTP {
|
|
14
6
|
params: HTTPConfig;
|
|
7
|
+
interceptors: Interceptors;
|
|
15
8
|
constructor(config?: HTTPConfig);
|
|
16
|
-
baseConfig
|
|
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>>;
|
|
9
|
+
private baseConfig;
|
|
10
|
+
get<DataT, ErrorT = Error | null>(url: string, config?: FetchOptions, options?: AsyncDataOptions<DataT>): Promise<_AsyncData<DataT, ErrorT>>;
|
|
11
|
+
post<DataT, ErrorT = Error | null>(url: string, config?: FetchOptions, options?: AsyncDataOptions<DataT>): Promise<_AsyncData<DataT, ErrorT>>;
|
|
19
12
|
}
|
|
20
|
-
export {};
|
package/dist/runtime/ajax.mjs
CHANGED
|
@@ -1,48 +1,47 @@
|
|
|
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
|
+
const { onRequest, onRequestError, onResponse, onResponseError, interceptors, ...restAjaxConfig } = config;
|
|
8
9
|
const defaultOptions = {
|
|
9
10
|
key,
|
|
10
|
-
onRequest({
|
|
11
|
-
|
|
12
|
-
}) {
|
|
13
|
-
options.headers = options.headers || {};
|
|
11
|
+
onRequest(ctx) {
|
|
12
|
+
[interceptors.onRequest, onRequest].map((fn) => fn && fn(ctx));
|
|
14
13
|
},
|
|
15
|
-
onRequestError({
|
|
16
|
-
|
|
17
|
-
}) {
|
|
14
|
+
onRequestError(ctx) {
|
|
15
|
+
[interceptors.onRequestError, onRequestError].map((fn) => fn && fn(ctx));
|
|
18
16
|
throw createError({
|
|
19
|
-
statusCode: error.statusCode,
|
|
20
|
-
statusMessage: error.
|
|
21
|
-
message: error.message
|
|
17
|
+
statusCode: ctx.error.statusCode,
|
|
18
|
+
statusMessage: ctx.error.message,
|
|
19
|
+
message: ctx.error.message,
|
|
20
|
+
fatal: true
|
|
22
21
|
});
|
|
23
22
|
},
|
|
24
|
-
onResponse({
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
return response
|
|
23
|
+
onResponse(ctx) {
|
|
24
|
+
const resp1 = interceptors.onResponse && interceptors.onResponse(ctx);
|
|
25
|
+
const resp2 = onResponse && onResponse({ ...ctx, response: resp1 || ctx.response });
|
|
26
|
+
return resp2 || resp1 || ctx.response;
|
|
28
27
|
},
|
|
29
|
-
onResponseError({
|
|
30
|
-
|
|
31
|
-
response
|
|
32
|
-
}) {
|
|
28
|
+
onResponseError(ctx) {
|
|
29
|
+
[interceptors.onResponseError, onResponseError].map((fn) => fn && fn(ctx));
|
|
33
30
|
throw createError({
|
|
34
|
-
statusCode:
|
|
35
|
-
statusMessage: request.toString(),
|
|
36
|
-
message: response._data
|
|
31
|
+
statusCode: ctx.response.status,
|
|
32
|
+
statusMessage: ctx.request.toString(),
|
|
33
|
+
message: ctx.response._data,
|
|
34
|
+
fatal: true
|
|
37
35
|
});
|
|
38
36
|
}
|
|
39
37
|
};
|
|
40
38
|
return useAsyncData(key, () => $fetch(url, {
|
|
41
39
|
...defaultOptions,
|
|
42
|
-
...
|
|
43
|
-
}));
|
|
40
|
+
...restAjaxConfig
|
|
41
|
+
}), options);
|
|
44
42
|
};
|
|
45
|
-
function getKey(url,
|
|
43
|
+
function getKey(url, config, extraParams = []) {
|
|
44
|
+
const params = (config.query ? config.query : config.params) || {};
|
|
46
45
|
const restConfig = {};
|
|
47
46
|
for (const key2 in params) {
|
|
48
47
|
if (Object.prototype.hasOwnProperty.call(params, key2) && !extraParams.includes(key2)) {
|
|
@@ -55,37 +54,52 @@ function getKey(url, params = {}, extraParams = []) {
|
|
|
55
54
|
export class HTTP {
|
|
56
55
|
constructor(config = { baseURL: "" }) {
|
|
57
56
|
this.params = {};
|
|
57
|
+
this.interceptors = {};
|
|
58
58
|
this.params = {
|
|
59
59
|
...config
|
|
60
60
|
};
|
|
61
|
+
this.interceptors = {
|
|
62
|
+
onRequest: config.onRequest || function() {
|
|
63
|
+
},
|
|
64
|
+
onRequestError: config.onRequestError || function() {
|
|
65
|
+
},
|
|
66
|
+
onResponse: config.onResponse || function({ response }) {
|
|
67
|
+
return response;
|
|
68
|
+
},
|
|
69
|
+
onResponseError: config.onResponseError || function() {
|
|
70
|
+
}
|
|
71
|
+
};
|
|
61
72
|
}
|
|
62
73
|
baseConfig(config) {
|
|
63
74
|
const { paramsHandler } = this.params;
|
|
64
|
-
const params = config.params || {};
|
|
75
|
+
const params = (config.query ? config.query : config.params) || {};
|
|
65
76
|
if (paramsHandler && typeof paramsHandler === "function") {
|
|
66
77
|
return paramsHandler({ ...params });
|
|
67
78
|
}
|
|
68
79
|
return { ...params };
|
|
69
80
|
}
|
|
70
|
-
get(url, config = {}) {
|
|
71
|
-
const key = getKey(url, config
|
|
72
|
-
const
|
|
81
|
+
get(url, config = {}, options) {
|
|
82
|
+
const key = getKey(url, config, this.params.extraParams);
|
|
83
|
+
const query = this.baseConfig(config);
|
|
73
84
|
return ajax(url, key, {
|
|
74
85
|
baseURL: this.params.baseURL,
|
|
75
86
|
method: "GET",
|
|
76
87
|
...config,
|
|
77
|
-
|
|
78
|
-
|
|
88
|
+
query,
|
|
89
|
+
interceptors: this.interceptors,
|
|
90
|
+
offline: this.params.offline
|
|
91
|
+
}, options);
|
|
79
92
|
}
|
|
80
|
-
post(url, config = {}) {
|
|
81
|
-
const key = getKey(url, config
|
|
82
|
-
const
|
|
93
|
+
post(url, config = {}, options) {
|
|
94
|
+
const key = getKey(url, config, this.params.extraParams);
|
|
95
|
+
const query = this.baseConfig(config);
|
|
83
96
|
return ajax(url, key, {
|
|
84
97
|
baseURL: this.params.baseURL,
|
|
85
98
|
method: "POST",
|
|
86
99
|
...config,
|
|
87
|
-
|
|
100
|
+
query,
|
|
101
|
+
interceptors: this.interceptors,
|
|
88
102
|
offline: this.params.offline
|
|
89
|
-
});
|
|
103
|
+
}, options);
|
|
90
104
|
}
|
|
91
105
|
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { FetchRequest, FetchOptions, FetchError, FetchResponse } from 'ofetch';
|
|
2
|
+
interface ResponseMap {
|
|
3
|
+
blob: Blob;
|
|
4
|
+
text: string;
|
|
5
|
+
arrayBuffer: ArrayBuffer;
|
|
6
|
+
stream: ReadableStream<Uint8Array>;
|
|
7
|
+
}
|
|
8
|
+
declare type ResponseType = keyof ResponseMap | 'json';
|
|
9
|
+
export interface OnRequestType {
|
|
10
|
+
request: FetchRequest;
|
|
11
|
+
options: FetchOptions;
|
|
12
|
+
}
|
|
13
|
+
export interface OnRequestErrorType {
|
|
14
|
+
request: FetchRequest;
|
|
15
|
+
options: FetchOptions;
|
|
16
|
+
error: FetchError;
|
|
17
|
+
}
|
|
18
|
+
export interface OnResponseType {
|
|
19
|
+
request: FetchRequest;
|
|
20
|
+
options: FetchOptions;
|
|
21
|
+
response: FetchResponse<any>;
|
|
22
|
+
}
|
|
23
|
+
export interface OnResponseErrorType {
|
|
24
|
+
request: FetchRequest;
|
|
25
|
+
options: FetchOptions;
|
|
26
|
+
response: FetchResponse<any>;
|
|
27
|
+
}
|
|
28
|
+
export type HTTPConfig = {
|
|
29
|
+
baseURL?: string;
|
|
30
|
+
extraParams?: string[];
|
|
31
|
+
paramsHandler?: (params: FetchOptions['params']) => FetchOptions & HTTPConfig;
|
|
32
|
+
offline?: () => void;
|
|
33
|
+
onRequest?: ({ request, options }: OnRequestType) => void;
|
|
34
|
+
onRequestError?: ({ request, options, error }: OnRequestErrorType) => void;
|
|
35
|
+
onResponse?: ({ request, response, options }: OnResponseType) => FetchResponse<ResponseType>;
|
|
36
|
+
onResponseError?: ({ request, response, options }: OnResponseErrorType) => void;
|
|
37
|
+
};
|
|
38
|
+
export interface Interceptors {
|
|
39
|
+
onRequest?: ({ request, options }: OnRequestType) => void;
|
|
40
|
+
onRequestError?: ({ request, options, error }: OnRequestErrorType) => void;
|
|
41
|
+
onResponse?: ({ request, response, options }: OnResponseType) => FetchResponse<ResponseType>;
|
|
42
|
+
onResponseError?: ({ request, response, options }: OnResponseErrorType) => void;
|
|
43
|
+
}
|
|
44
|
+
export type AjaxConfig = FetchOptions & {
|
|
45
|
+
extraParams?: string[];
|
|
46
|
+
offline?: () => void;
|
|
47
|
+
interceptors: Interceptors;
|
|
48
|
+
};
|
|
49
|
+
export {};
|
|
File without changes
|