nuxt-custom-fetch 1.0.7 → 1.1.0
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 +65 -37
- package/dist/module.json +3 -3
- package/dist/module.mjs +3 -3
- package/dist/runtime/ajax.d.ts +5 -2
- package/dist/runtime/ajax.mjs +25 -16
- package/dist/runtime/type.d.ts +1 -0
- package/dist/types.d.ts +2 -2
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -4,13 +4,15 @@
|
|
|
4
4
|
|
|
5
5
|
## HTTPOptions
|
|
6
6
|
|
|
7
|
-
实例化全局
|
|
7
|
+
实例化全局 CustomFetch
|
|
8
8
|
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
```ts
|
|
10
|
+
{
|
|
11
|
+
baseURL?: string
|
|
12
|
+
paramsHandler?: (params: FetchOptions['params']) => FetchOptions & HTTPConfig
|
|
13
|
+
extraParams?: string[]
|
|
14
|
+
offline?: () => void
|
|
15
|
+
}
|
|
14
16
|
```
|
|
15
17
|
|
|
16
18
|
### baseURL
|
|
@@ -28,8 +30,9 @@ offline?: () => void
|
|
|
28
30
|
|
|
29
31
|
### 请求-响应拦截
|
|
30
32
|
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
+
```ts
|
|
34
|
+
{
|
|
35
|
+
onRequest?: ({ request, options }: {
|
|
33
36
|
request: FetchRequest
|
|
34
37
|
options: FetchOptions
|
|
35
38
|
}) => void
|
|
@@ -60,6 +63,7 @@ onRequest?: ({ request, options }: {
|
|
|
60
63
|
options: FetchOptions
|
|
61
64
|
response: FetchResponse<any>
|
|
62
65
|
}) => void
|
|
66
|
+
}
|
|
63
67
|
```
|
|
64
68
|
|
|
65
69
|
## 实例方法
|
|
@@ -83,46 +87,70 @@ const key = hash(JSON.stringify(restConfig) + url)
|
|
|
83
87
|
|
|
84
88
|
### fetchOptions
|
|
85
89
|
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
90
|
+
```ts
|
|
91
|
+
interface fetchOptions {
|
|
92
|
+
baseURL?: string
|
|
93
|
+
key?: string
|
|
94
|
+
body?: RequestInit['body'] | Record<string, any>
|
|
95
|
+
useParamsHandler: HTTPConfig['paramsHandler']
|
|
96
|
+
paramsHandler: HTTPConfig['paramsHandler']
|
|
97
|
+
params?: SearchParameters
|
|
98
|
+
query?: SearchParameters
|
|
99
|
+
parseResponse?: (responseText: string) => any
|
|
100
|
+
responseType?: R
|
|
101
|
+
response?: boolean
|
|
102
|
+
retry?: number | false
|
|
103
|
+
onRequest?(context: FetchContext): Promise<void> | void
|
|
104
|
+
onRequestError?(
|
|
105
|
+
context: FetchContext & {
|
|
106
|
+
error: Error
|
|
107
|
+
}
|
|
108
|
+
): Promise<void> | void
|
|
109
|
+
onResponse?(
|
|
110
|
+
context: FetchContext & {
|
|
111
|
+
response: FetchResponse<R>
|
|
112
|
+
}
|
|
113
|
+
): Promise<void> | void
|
|
114
|
+
onResponseError?(
|
|
115
|
+
context: FetchContext & {
|
|
116
|
+
response: FetchResponse<R>
|
|
117
|
+
}
|
|
118
|
+
): Promise<void> | void
|
|
119
|
+
}
|
|
105
120
|
```
|
|
106
121
|
|
|
107
|
-
### AsyncDataOptions
|
|
122
|
+
### AsyncDataOptions
|
|
108
123
|
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
124
|
+
```ts
|
|
125
|
+
// AsyncDataOptions<DataT>
|
|
126
|
+
{
|
|
127
|
+
server?: boolean;
|
|
128
|
+
lazy?: boolean;
|
|
129
|
+
default?: () => DataT | Ref<DataT> | null;
|
|
130
|
+
transform?: Transform;
|
|
131
|
+
pick?: PickKeys;
|
|
132
|
+
watch?: MultiWatchSources;
|
|
133
|
+
immediate?: boolean;
|
|
134
|
+
}
|
|
117
135
|
```
|
|
118
136
|
|
|
119
137
|
## 使用方式
|
|
120
138
|
|
|
139
|
+
```bash
|
|
140
|
+
pnpm add nuxt-custom-fetch
|
|
141
|
+
```
|
|
142
|
+
|
|
121
143
|
```ts
|
|
144
|
+
// nuxt.config.ts
|
|
145
|
+
export default defineNuxtConfig({
|
|
146
|
+
modules: ['nuxt-custom-fetch']
|
|
147
|
+
})
|
|
148
|
+
|
|
122
149
|
// ajax.ts文件
|
|
123
150
|
import type { FetchOptions } from 'ofetch'
|
|
124
|
-
const ajax = new
|
|
151
|
+
const ajax = new CustomFetch({
|
|
125
152
|
baseURL: '',
|
|
153
|
+
// 全局处理query\params的方法
|
|
126
154
|
paramsHandler: (params = {}) => {
|
|
127
155
|
params.aa = 111
|
|
128
156
|
return params
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -3,8 +3,8 @@ import { defineNuxtModule, createResolver, addImports } from '@nuxt/kit';
|
|
|
3
3
|
|
|
4
4
|
const module = defineNuxtModule({
|
|
5
5
|
meta: {
|
|
6
|
-
name: "nuxt-
|
|
7
|
-
configKey: "
|
|
6
|
+
name: "nuxt-custom-fetch",
|
|
7
|
+
configKey: "customFetch"
|
|
8
8
|
},
|
|
9
9
|
defaults: {},
|
|
10
10
|
setup(options, nuxt) {
|
|
@@ -13,7 +13,7 @@ const module = defineNuxtModule({
|
|
|
13
13
|
nuxt.options.build.transpile.push(runtimeDir);
|
|
14
14
|
addImports({
|
|
15
15
|
from: resolve(runtimeDir, "ajax"),
|
|
16
|
-
name: "
|
|
16
|
+
name: "CustomFetch"
|
|
17
17
|
});
|
|
18
18
|
}
|
|
19
19
|
});
|
package/dist/runtime/ajax.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import type { _AsyncData, AsyncDataOptions } from 'nuxt/dist/app/composables/asyncData';
|
|
2
2
|
import { HTTPConfig, Interceptors, AjaxConfig } from './type';
|
|
3
|
-
export declare const ajax: <DataT, ErrorT = Error | null>(url: string, key: string, config: AjaxConfig, options?: AsyncDataOptions<DataT,
|
|
4
|
-
export declare class
|
|
3
|
+
export declare const ajax: <DataT, ErrorT = Error | null>(url: string, key: string, config: AjaxConfig, options?: AsyncDataOptions<DataT, DataT, import("nuxt/dist/app/composables/asyncData").KeysOf<DataT>, null> | undefined) => Promise<_AsyncData<DataT, ErrorT>>;
|
|
4
|
+
export declare class CustomFetch {
|
|
5
|
+
baseURL: any;
|
|
5
6
|
params: HTTPConfig;
|
|
7
|
+
baseParamsHandler: HTTPConfig['paramsHandler'];
|
|
6
8
|
interceptors: Interceptors;
|
|
9
|
+
offline: () => void;
|
|
7
10
|
constructor(config?: HTTPConfig);
|
|
8
11
|
private baseConfig;
|
|
9
12
|
http<DataT, ErrorT = Error | null>(url: string, config?: HTTPConfig & {
|
package/dist/runtime/ajax.mjs
CHANGED
|
@@ -50,41 +50,50 @@ function getKey(url, config, extraParams = []) {
|
|
|
50
50
|
const key = hash(JSON.stringify(restConfig) + url);
|
|
51
51
|
return key;
|
|
52
52
|
}
|
|
53
|
-
|
|
53
|
+
const Noop = () => {
|
|
54
|
+
};
|
|
55
|
+
export class CustomFetch {
|
|
56
|
+
baseURL;
|
|
57
|
+
params = {};
|
|
58
|
+
baseParamsHandler;
|
|
59
|
+
interceptors = {};
|
|
60
|
+
offline = Noop;
|
|
54
61
|
constructor(config = { baseURL: "" }) {
|
|
55
|
-
this.params = {};
|
|
56
|
-
this.interceptors = {};
|
|
57
62
|
this.params = {
|
|
58
63
|
...config
|
|
59
64
|
};
|
|
65
|
+
this.baseURL = config.baseURL;
|
|
66
|
+
if (config.paramsHandler) {
|
|
67
|
+
this.baseParamsHandler = config.paramsHandler;
|
|
68
|
+
}
|
|
69
|
+
this.offline = config?.offline || Noop;
|
|
60
70
|
this.interceptors = {
|
|
61
|
-
onRequest: config.onRequest ||
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
onResponse: config.onResponse || function() {
|
|
66
|
-
},
|
|
67
|
-
onResponseError: config.onResponseError || function() {
|
|
68
|
-
}
|
|
71
|
+
onRequest: config.onRequest || Noop,
|
|
72
|
+
onRequestError: config.onRequestError || Noop,
|
|
73
|
+
onResponse: config.onResponse || Noop,
|
|
74
|
+
onResponseError: config.onResponseError || Noop
|
|
69
75
|
};
|
|
70
76
|
}
|
|
71
77
|
baseConfig(config) {
|
|
72
|
-
const { paramsHandler } =
|
|
78
|
+
const { useParamsHandler = true, paramsHandler } = config;
|
|
79
|
+
const handler = paramsHandler || this.baseParamsHandler;
|
|
73
80
|
const params = (config.query ? config.query : config.params) || {};
|
|
74
|
-
if (
|
|
75
|
-
return
|
|
81
|
+
if (useParamsHandler && handler && typeof handler === "function") {
|
|
82
|
+
return handler({ ...params });
|
|
76
83
|
}
|
|
77
84
|
return { ...params };
|
|
78
85
|
}
|
|
79
86
|
http(url, config = {}, options) {
|
|
80
87
|
const key = config.key ? config.key : getKey(url, config, this.params.extraParams);
|
|
88
|
+
const baseURL = config?.baseURL || this.baseURL;
|
|
81
89
|
const query = this.baseConfig(config);
|
|
90
|
+
const offline = config?.offline || this.offline;
|
|
82
91
|
return ajax(url, key, {
|
|
83
|
-
baseURL
|
|
92
|
+
baseURL,
|
|
84
93
|
...config,
|
|
85
94
|
query,
|
|
86
95
|
interceptors: this.interceptors,
|
|
87
|
-
offline
|
|
96
|
+
offline
|
|
88
97
|
}, options);
|
|
89
98
|
}
|
|
90
99
|
get(url, config = {}, options) {
|
package/dist/runtime/type.d.ts
CHANGED
|
@@ -22,6 +22,7 @@ export type FetchMethod = 'options' | 'GET' | 'POST' | 'get' | 'HEAD' | 'PATCH'
|
|
|
22
22
|
export interface HTTPConfig extends Omit<FetchOptions, 'method'> {
|
|
23
23
|
baseURL?: string;
|
|
24
24
|
extraParams?: string[];
|
|
25
|
+
useParamsHandler?: boolean;
|
|
25
26
|
paramsHandler?: (params: FetchOptions['params']) => HTTPConfig;
|
|
26
27
|
offline?: () => void;
|
|
27
28
|
method?: FetchMethod;
|
package/dist/types.d.ts
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
import { ModuleOptions } from './module'
|
|
3
3
|
|
|
4
4
|
declare module '@nuxt/schema' {
|
|
5
|
-
interface NuxtConfig { ['
|
|
6
|
-
interface NuxtOptions { ['
|
|
5
|
+
interface NuxtConfig { ['customFetch']?: Partial<ModuleOptions> }
|
|
6
|
+
interface NuxtOptions { ['customFetch']?: ModuleOptions }
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nuxt-custom-fetch",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -25,13 +25,13 @@
|
|
|
25
25
|
"dev:prepare": "nuxt-module-build --stub && nuxi prepare playground"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@nuxt/kit": "^3.
|
|
28
|
+
"@nuxt/kit": "^3.8.1"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@nuxt/module-builder": "^0.2.1",
|
|
32
|
-
"@nuxt/schema": "^3.
|
|
33
|
-
"@nuxtjs/eslint-config-typescript": "^12.
|
|
34
|
-
"eslint": "^8.
|
|
35
|
-
"nuxt": "^3.
|
|
32
|
+
"@nuxt/schema": "^3.8.1",
|
|
33
|
+
"@nuxtjs/eslint-config-typescript": "^12.1.0",
|
|
34
|
+
"eslint": "^8.53.0",
|
|
35
|
+
"nuxt": "^3.8.1"
|
|
36
36
|
}
|
|
37
37
|
}
|