nuxt-custom-fetch 1.0.7 → 1.0.8
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 +6 -2
- package/dist/module.json +3 -3
- package/dist/module.mjs +3 -3
- package/dist/runtime/ajax.d.ts +4 -1
- package/dist/runtime/ajax.mjs +21 -14
- package/dist/runtime/type.d.ts +1 -0
- package/dist/types.d.ts +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
## HTTPOptions
|
|
6
6
|
|
|
7
|
-
实例化全局
|
|
7
|
+
实例化全局 CustomFetch
|
|
8
8
|
|
|
9
9
|
```
|
|
10
10
|
baseURL?: string
|
|
@@ -85,7 +85,10 @@ const key = hash(JSON.stringify(restConfig) + url)
|
|
|
85
85
|
|
|
86
86
|
```
|
|
87
87
|
baseURL?: string;
|
|
88
|
+
key?: string;
|
|
88
89
|
body?: RequestInit["body"] | Record<string, any>;
|
|
90
|
+
useParamsHandler: HTTPConfig['paramsHandler'];
|
|
91
|
+
paramsHandler: HTTPConfig['paramsHandler'];
|
|
89
92
|
params?: SearchParameters;
|
|
90
93
|
query?: SearchParameters;
|
|
91
94
|
parseResponse?: (responseText: string) => any;
|
|
@@ -121,8 +124,9 @@ immediate?: boolean;
|
|
|
121
124
|
```ts
|
|
122
125
|
// ajax.ts文件
|
|
123
126
|
import type { FetchOptions } from 'ofetch'
|
|
124
|
-
const ajax = new
|
|
127
|
+
const ajax = new CustomFetch({
|
|
125
128
|
baseURL: '',
|
|
129
|
+
// 全局处理query\params的方法
|
|
126
130
|
paramsHandler: (params = {}) => {
|
|
127
131
|
params.aa = 111
|
|
128
132
|
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
3
|
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>>;
|
|
4
|
-
export declare class
|
|
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,48 @@ 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 {
|
|
54
56
|
constructor(config = { baseURL: "" }) {
|
|
55
57
|
this.params = {};
|
|
56
58
|
this.interceptors = {};
|
|
59
|
+
this.offline = Noop;
|
|
57
60
|
this.params = {
|
|
58
61
|
...config
|
|
59
62
|
};
|
|
63
|
+
this.baseURL = config.baseURL;
|
|
64
|
+
if (config.paramsHandler) {
|
|
65
|
+
this.baseParamsHandler = config.paramsHandler;
|
|
66
|
+
}
|
|
67
|
+
this.offline = config?.offline || Noop;
|
|
60
68
|
this.interceptors = {
|
|
61
|
-
onRequest: config.onRequest ||
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
onResponse: config.onResponse || function() {
|
|
66
|
-
},
|
|
67
|
-
onResponseError: config.onResponseError || function() {
|
|
68
|
-
}
|
|
69
|
+
onRequest: config.onRequest || Noop,
|
|
70
|
+
onRequestError: config.onRequestError || Noop,
|
|
71
|
+
onResponse: config.onResponse || Noop,
|
|
72
|
+
onResponseError: config.onResponseError || Noop
|
|
69
73
|
};
|
|
70
74
|
}
|
|
71
75
|
baseConfig(config) {
|
|
72
|
-
const { paramsHandler } =
|
|
76
|
+
const { useParamsHandler = true, paramsHandler } = config;
|
|
77
|
+
const handler = paramsHandler || this.baseParamsHandler;
|
|
73
78
|
const params = (config.query ? config.query : config.params) || {};
|
|
74
|
-
if (
|
|
75
|
-
return
|
|
79
|
+
if (useParamsHandler && handler && typeof handler === "function") {
|
|
80
|
+
return handler({ ...params });
|
|
76
81
|
}
|
|
77
82
|
return { ...params };
|
|
78
83
|
}
|
|
79
84
|
http(url, config = {}, options) {
|
|
80
85
|
const key = config.key ? config.key : getKey(url, config, this.params.extraParams);
|
|
86
|
+
const baseURL = config?.baseURL || this.baseURL;
|
|
81
87
|
const query = this.baseConfig(config);
|
|
88
|
+
const offline = config?.offline || this.offline;
|
|
82
89
|
return ajax(url, key, {
|
|
83
|
-
baseURL
|
|
90
|
+
baseURL,
|
|
84
91
|
...config,
|
|
85
92
|
query,
|
|
86
93
|
interceptors: this.interceptors,
|
|
87
|
-
offline
|
|
94
|
+
offline
|
|
88
95
|
}, options);
|
|
89
96
|
}
|
|
90
97
|
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
|
|