nuxt-custom-fetch 2.3.1 → 4.0.0-alpha.10
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 +35 -30
- package/dist/module.d.mts +2 -1
- package/dist/module.json +2 -2
- package/dist/module.mjs +2 -1
- package/dist/runtime/ajax.d.ts +13 -13
- package/dist/runtime/ajax.js +73 -69
- package/dist/runtime/type.d.ts +13 -6
- package/dist/runtime/utils.d.ts +6 -0
- package/dist/runtime/utils.js +53 -0
- package/dist/types.d.mts +3 -1
- package/package.json +17 -13
package/README.md
CHANGED
|
@@ -1,22 +1,24 @@
|
|
|
1
1
|
# Nuxt Module
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
simple wrapper useAsyncData
|
|
4
4
|
|
|
5
|
-
`version
|
|
5
|
+
`version >= 3.17.0`
|
|
6
6
|
|
|
7
|
-
##
|
|
7
|
+
## Break Change
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
1. `http` method is removed, use `request` instead
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
## Attention
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
1. Invalid request in onMounted
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
- Execute in `nextTick`, or set `{server: false}` to watch `status` changes
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
2. When using `refresh` and `execute`, it should also be noted that the parameters are not responsive, so they will always be the parameters requested for the first time
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
3. If a reactive object is passed and using `watch`, it will automatically re request
|
|
20
|
+
|
|
21
|
+
## Install
|
|
20
22
|
|
|
21
23
|
```bash
|
|
22
24
|
pnpm add nuxt-custom-fetch
|
|
@@ -29,26 +31,26 @@ export default defineNuxtConfig({
|
|
|
29
31
|
})
|
|
30
32
|
```
|
|
31
33
|
|
|
32
|
-
##
|
|
34
|
+
## Usage
|
|
33
35
|
|
|
34
|
-
|
|
36
|
+
global CustomFetch
|
|
35
37
|
|
|
36
38
|
```ts
|
|
37
39
|
const ajax = new CustomFetch({
|
|
38
40
|
baseURL: ''
|
|
39
41
|
})
|
|
40
42
|
|
|
41
|
-
// `
|
|
43
|
+
// `request|get|post`
|
|
42
44
|
ajax.get('/api/user')
|
|
43
45
|
|
|
44
46
|
ajax.post('/api/user')
|
|
45
47
|
|
|
46
|
-
ajax.
|
|
47
|
-
method: '
|
|
48
|
+
ajax.request({
|
|
49
|
+
method: 'DELETE'
|
|
48
50
|
})
|
|
49
51
|
```
|
|
50
52
|
|
|
51
|
-
##
|
|
53
|
+
## Types
|
|
52
54
|
|
|
53
55
|
### useAsyncDataType
|
|
54
56
|
|
|
@@ -56,29 +58,32 @@ ajax.http({
|
|
|
56
58
|
|
|
57
59
|
```ts
|
|
58
60
|
export declare class CustomFetch {
|
|
59
|
-
baseURL:
|
|
61
|
+
baseURL: string
|
|
60
62
|
immutableKey: boolean
|
|
61
|
-
showLogs: boolean
|
|
62
|
-
params: HTTPConfig
|
|
63
|
-
baseHandler: HTTPConfig['handler']
|
|
64
|
-
interceptors: Interceptors
|
|
65
63
|
offline: typeof Noop
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
64
|
+
showLogs: boolean
|
|
65
|
+
constructor (config: CustomFetchOptions)
|
|
66
|
+
request<DataT, NuxtErrorDataT = Error | null>(url: NitroFetchRequest, config: CustomFetchOptions & {
|
|
69
67
|
method: FetchMethod
|
|
70
|
-
}, options?: AsyncDataOptions<DataT>): CustomFetchReturnValue<DataT,
|
|
71
|
-
get<DataT,
|
|
72
|
-
post<DataT,
|
|
68
|
+
}, options?: AsyncDataOptions<DataT>): CustomFetchReturnValue<DataT, NuxtErrorDataT>
|
|
69
|
+
get<DataT, NuxtErrorDataT = Error | null>(url: NitroFetchRequest, config?: CustomFetchOptions, options?: AsyncDataOptions<DataT>): CustomFetchReturnValue<DataT, NuxtErrorDataT>
|
|
70
|
+
post<DataT, NuxtErrorDataT = Error | null>(url: NitroFetchRequest, config?: CustomFetchOptions, options?: AsyncDataOptions<DataT>): CustomFetchReturnValue<DataT, NuxtErrorDataT>
|
|
73
71
|
}
|
|
74
72
|
|
|
75
73
|
export type FetchMethod = 'options' | 'GET' | 'POST' | 'get' | 'HEAD' | 'PATCH' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'post' | 'head' | 'patch' | 'put' | 'delete' | 'connect' | 'trace' | undefined
|
|
76
|
-
export interface
|
|
74
|
+
export interface CustomFetchOptions extends Omit<FetchOptions, 'method'> {
|
|
75
|
+
/** unique key for fetch */
|
|
77
76
|
key?: string
|
|
77
|
+
/** hash key for fetch, with [customFetch: + url] without query */
|
|
78
78
|
immutableKey?: boolean
|
|
79
|
+
/** show logs */
|
|
80
|
+
showLogs?: boolean
|
|
79
81
|
baseURL?: string
|
|
82
|
+
/** is use handler to deal with query or pramas */
|
|
80
83
|
useHandler?: boolean
|
|
81
|
-
handler
|
|
84
|
+
/** handler to deal with query or pramas */
|
|
85
|
+
handler?: (mergedObject: FetchOptions['params'] & FetchOptions['query']) => Record<string, any>
|
|
86
|
+
/** offline handler */
|
|
82
87
|
offline?: () => void
|
|
83
88
|
}
|
|
84
89
|
export interface Interceptors {
|
|
@@ -89,7 +94,7 @@ export interface Interceptors {
|
|
|
89
94
|
}
|
|
90
95
|
```
|
|
91
96
|
|
|
92
|
-
##
|
|
97
|
+
## Example
|
|
93
98
|
|
|
94
99
|
```ts
|
|
95
100
|
// api.ts
|
|
@@ -100,7 +105,7 @@ export const getInfo = (params: Record<string, any>) =>
|
|
|
100
105
|
```ts
|
|
101
106
|
// index.vue
|
|
102
107
|
import { getInfo } from './ajax'
|
|
103
|
-
const { data, error
|
|
108
|
+
const { data, error } = await getInfo({
|
|
104
109
|
sign: 123
|
|
105
110
|
})
|
|
106
111
|
console.log(data.value)
|
package/dist/module.d.mts
CHANGED
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -4,7 +4,8 @@ import { defineNuxtModule, createResolver, addImports } from '@nuxt/kit';
|
|
|
4
4
|
const module = defineNuxtModule({
|
|
5
5
|
meta: {
|
|
6
6
|
name: "nuxt-custom-fetch",
|
|
7
|
-
configKey: "customFetch"
|
|
7
|
+
configKey: "customFetch",
|
|
8
|
+
version: ">=3.17.0"
|
|
8
9
|
},
|
|
9
10
|
defaults: {},
|
|
10
11
|
setup(options, nuxt) {
|
package/dist/runtime/ajax.d.ts
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
import type { NitroFetchRequest } from 'nitro';
|
|
1
|
+
import type { NitroFetchRequest } from 'nitro/types';
|
|
2
2
|
import type { AsyncData, AsyncDataOptions, NuxtError } from 'nuxt/app';
|
|
3
|
-
import type {
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
import type { CustomFetchOptions, FetchMethod, Interceptors, KeysOf, PickFrom } from './type.js';
|
|
4
|
+
import { Noop } from './utils.js';
|
|
5
|
+
type CustomFetchReturnValue<DataT, NuxtErrorDataT> = AsyncData<PickFrom<DataT, KeysOf<DataT>> | undefined, (NuxtErrorDataT extends Error | NuxtError<unknown> ? NuxtErrorDataT : NuxtError<NuxtErrorDataT>) | undefined>;
|
|
6
6
|
export declare class CustomFetch {
|
|
7
|
-
baseURL: string
|
|
7
|
+
baseURL: string;
|
|
8
8
|
immutableKey: boolean;
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
_config: CustomFetchOptions;
|
|
10
|
+
_baseHandler: CustomFetchOptions['handler'];
|
|
11
|
+
_interceptors: Interceptors;
|
|
12
12
|
offline: typeof Noop;
|
|
13
13
|
showLogs: boolean;
|
|
14
|
-
constructor(config
|
|
14
|
+
constructor(config: CustomFetchOptions);
|
|
15
15
|
private baseConfig;
|
|
16
|
-
|
|
16
|
+
request<DataT, NuxtErrorDataT = Error | null>(url: NitroFetchRequest, config: CustomFetchOptions & {
|
|
17
17
|
method: FetchMethod;
|
|
18
|
-
}, options?: AsyncDataOptions<DataT>): CustomFetchReturnValue<DataT,
|
|
19
|
-
get<DataT,
|
|
20
|
-
post<DataT,
|
|
18
|
+
}, options?: AsyncDataOptions<DataT>): CustomFetchReturnValue<DataT, NuxtErrorDataT>;
|
|
19
|
+
get<DataT, NuxtErrorDataT = Error | null>(url: NitroFetchRequest, config?: CustomFetchOptions, options?: AsyncDataOptions<DataT>): CustomFetchReturnValue<DataT, NuxtErrorDataT>;
|
|
20
|
+
post<DataT, NuxtErrorDataT = Error | null>(url: NitroFetchRequest, config?: CustomFetchOptions, options?: AsyncDataOptions<DataT>): CustomFetchReturnValue<DataT, NuxtErrorDataT>;
|
|
21
21
|
}
|
|
22
22
|
export {};
|
package/dist/runtime/ajax.js
CHANGED
|
@@ -1,50 +1,29 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { asyncDataDefaults, pendingWhenIdle } from "#build/nuxt.config.mjs";
|
|
2
|
+
import { clearNuxtData, computed, createError, getCurrentScope, onScopeDispose, reactive, ref, shallowRef, toValue, unref, useAsyncData, useNuxtApp, useRequestFetch, useRuntimeConfig, watch } from "#imports";
|
|
2
3
|
import { hash, serialize } from "ohash";
|
|
3
|
-
|
|
4
|
-
}
|
|
5
|
-
function generateOptionSegments(opts) {
|
|
6
|
-
const segments = [toValue(opts.method)?.toUpperCase() || "GET", toValue(opts.baseURL)];
|
|
7
|
-
for (const _obj of [opts.params || opts.query]) {
|
|
8
|
-
const obj = toValue(_obj);
|
|
9
|
-
if (!obj) {
|
|
10
|
-
continue;
|
|
11
|
-
}
|
|
12
|
-
const unwrapped = {};
|
|
13
|
-
for (const [key, value] of Object.entries(obj)) {
|
|
14
|
-
unwrapped[toValue(key)] = toValue(value);
|
|
15
|
-
}
|
|
16
|
-
segments.push(unwrapped);
|
|
17
|
-
}
|
|
18
|
-
return segments;
|
|
19
|
-
}
|
|
20
|
-
function pick(obj, keys) {
|
|
21
|
-
const newObj = {};
|
|
22
|
-
for (const key of keys) {
|
|
23
|
-
newObj[key] = obj[key];
|
|
24
|
-
}
|
|
25
|
-
return newObj;
|
|
26
|
-
}
|
|
4
|
+
import { generateOptionSegments, Noop, pick } from "./utils.js";
|
|
27
5
|
const _cachedController = /* @__PURE__ */ new Map();
|
|
28
6
|
export class CustomFetch {
|
|
29
7
|
baseURL;
|
|
30
8
|
immutableKey = false;
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
9
|
+
_config = {};
|
|
10
|
+
_baseHandler;
|
|
11
|
+
_interceptors = {};
|
|
34
12
|
offline = Noop;
|
|
35
13
|
showLogs = false;
|
|
36
|
-
constructor(config
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
this.
|
|
40
|
-
this.baseURL = config.baseURL;
|
|
14
|
+
constructor(config) {
|
|
15
|
+
const { handler, offline, ...restConfig } = config;
|
|
16
|
+
this._config = restConfig;
|
|
17
|
+
this.baseURL = config.baseURL || "";
|
|
41
18
|
this.immutableKey = config.immutableKey ?? false;
|
|
42
|
-
this.showLogs = config.showLogs ?? import.meta.dev;
|
|
43
|
-
if (
|
|
44
|
-
this.
|
|
19
|
+
this.showLogs = config.showLogs ?? import.meta.dev ?? false;
|
|
20
|
+
if (handler) {
|
|
21
|
+
this._baseHandler = handler || Noop;
|
|
22
|
+
}
|
|
23
|
+
if (offline) {
|
|
24
|
+
this.offline = offline;
|
|
45
25
|
}
|
|
46
|
-
this.
|
|
47
|
-
this.interceptors = {
|
|
26
|
+
this._interceptors = {
|
|
48
27
|
onRequest: config.onRequest || Noop,
|
|
49
28
|
onRequestError: config.onRequestError || Noop,
|
|
50
29
|
onResponse: config.onResponse || Noop,
|
|
@@ -53,19 +32,20 @@ export class CustomFetch {
|
|
|
53
32
|
}
|
|
54
33
|
baseConfig(config) {
|
|
55
34
|
const { useHandler = true, handler, query = {}, params = {} } = config;
|
|
56
|
-
const baseHandler = handler || this.
|
|
35
|
+
const baseHandler = handler || this._baseHandler;
|
|
57
36
|
const _name = Object.keys(query).length ? "query" : "params";
|
|
58
37
|
const mergeObj = {
|
|
59
|
-
...
|
|
60
|
-
...
|
|
38
|
+
...params,
|
|
39
|
+
...query
|
|
61
40
|
};
|
|
62
41
|
if (useHandler && baseHandler && typeof baseHandler === "function") {
|
|
63
42
|
return { [_name]: baseHandler(mergeObj) };
|
|
64
43
|
}
|
|
65
44
|
return { [_name]: { ...mergeObj } };
|
|
66
45
|
}
|
|
67
|
-
|
|
68
|
-
|
|
46
|
+
request(url, config, options = {}) {
|
|
47
|
+
const app = useRuntimeConfig().app;
|
|
48
|
+
config.baseURL ??= this.baseURL || app.baseURL;
|
|
69
49
|
Object.assign(config, this.baseConfig(config));
|
|
70
50
|
const generateOptionSegmentsWithConfig = generateOptionSegments(config);
|
|
71
51
|
if (this.showLogs && import.meta.client) {
|
|
@@ -75,16 +55,14 @@ export class CustomFetch {
|
|
|
75
55
|
} catch (error) {
|
|
76
56
|
console.warn("[Custom Fetch] couldn't serialize `Body:`", error);
|
|
77
57
|
}
|
|
78
|
-
console.warn("[Custom Fetch] `Request:`", url, "
|
|
58
|
+
console.warn("[Custom Fetch] `Request:`", url, "`Query:`", serialize(generateOptionSegmentsWithConfig), " `Body:`", bodyLogs);
|
|
79
59
|
}
|
|
80
|
-
const { onRequest, onRequestError, onResponse, onResponseError, offline, handler, useHandler, showLogs, immutableKey, ...
|
|
60
|
+
const { onRequest, onRequestError, onResponse, onResponseError, offline, handler, useHandler, showLogs, immutableKey, ...asyncDataOptions } = config;
|
|
81
61
|
if (import.meta.client && navigator && !navigator.onLine) {
|
|
82
|
-
|
|
83
|
-
offline();
|
|
84
|
-
}
|
|
62
|
+
this.offline();
|
|
85
63
|
}
|
|
86
|
-
const interceptors = this.
|
|
87
|
-
const _config = reactive({ ...
|
|
64
|
+
const interceptors = this._interceptors;
|
|
65
|
+
const _config = reactive({ ...asyncDataOptions });
|
|
88
66
|
const defaultOptions = {
|
|
89
67
|
onRequest(ctx) {
|
|
90
68
|
[interceptors.onRequest, onRequest].forEach((fns) => {
|
|
@@ -136,17 +114,29 @@ export class CustomFetch {
|
|
|
136
114
|
}
|
|
137
115
|
};
|
|
138
116
|
const hashValue = ["custom_fetch:", url];
|
|
117
|
+
if (import.meta.dev && !config.key && (this.immutableKey || immutableKey)) {
|
|
118
|
+
console.warn("[Custom Fetch] immutableKey is enabled, the key will be generated by hash([custom_fetch:, url])");
|
|
119
|
+
}
|
|
139
120
|
if (!this.immutableKey && !immutableKey) {
|
|
140
121
|
hashValue.push(...generateOptionSegmentsWithConfig);
|
|
141
122
|
}
|
|
142
|
-
const
|
|
143
|
-
|
|
123
|
+
const hashKey = hash(hashValue).replace(/[-_]/g, "").slice(0, 10);
|
|
124
|
+
const key = computed(() => toValue(config.key) || hashKey);
|
|
125
|
+
watch(key, (key2, oldKey) => {
|
|
126
|
+
if (oldKey) {
|
|
127
|
+
_cachedController.get(oldKey)?.abort?.();
|
|
128
|
+
_cachedController.delete(oldKey);
|
|
129
|
+
} else {
|
|
130
|
+
const controller2 = typeof AbortController !== "undefined" ? new AbortController() : {};
|
|
131
|
+
_cachedController.set(key2, controller2);
|
|
132
|
+
}
|
|
133
|
+
});
|
|
144
134
|
const _handler = () => {
|
|
145
|
-
if (_cachedController.get(key)) {
|
|
146
|
-
_cachedController.get(key)?.abort?.();
|
|
135
|
+
if (_cachedController.get(key.value)) {
|
|
136
|
+
_cachedController.get(key.value)?.abort?.();
|
|
147
137
|
}
|
|
148
138
|
return useRequestFetch()(url, {
|
|
149
|
-
signal: _cachedController.get(key)?.signal,
|
|
139
|
+
signal: _cachedController.get(key.value)?.signal,
|
|
150
140
|
...defaultOptions,
|
|
151
141
|
..._config
|
|
152
142
|
});
|
|
@@ -154,18 +144,30 @@ export class CustomFetch {
|
|
|
154
144
|
const controller = typeof AbortController !== "undefined" ? new AbortController() : {};
|
|
155
145
|
const nuxtApp = useNuxtApp();
|
|
156
146
|
if (import.meta.client && !nuxtApp.isHydrating) {
|
|
147
|
+
if (nuxtApp._asyncData[key.value]?._deps) {
|
|
148
|
+
nuxtApp._asyncData[key.value].execute({
|
|
149
|
+
cause: "initial",
|
|
150
|
+
dedupe: options.dedupe
|
|
151
|
+
});
|
|
152
|
+
return nuxtApp._asyncData[key.value];
|
|
153
|
+
}
|
|
154
|
+
const _ref = options.deep ? ref : shallowRef;
|
|
155
|
+
options.default ??= () => asyncDataDefaults.value;
|
|
157
156
|
const asyncData = {
|
|
158
|
-
data:
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
157
|
+
data: _ref(null),
|
|
158
|
+
error: _ref(null),
|
|
159
|
+
status: _ref("idle"),
|
|
160
|
+
pending: _ref(false),
|
|
161
|
+
clear: () => clearNuxtData(key.value)
|
|
162
162
|
};
|
|
163
|
-
|
|
163
|
+
if (pendingWhenIdle) {
|
|
164
|
+
asyncData.pending.value = true;
|
|
165
|
+
}
|
|
164
166
|
asyncData.status.value = "pending";
|
|
165
167
|
const promise = new Promise((resolve, reject) => {
|
|
166
168
|
try {
|
|
167
169
|
resolve(_handler());
|
|
168
|
-
_cachedController.set(key, controller);
|
|
170
|
+
_cachedController.set(key.value, controller);
|
|
169
171
|
} catch (err) {
|
|
170
172
|
reject(err);
|
|
171
173
|
}
|
|
@@ -178,40 +180,42 @@ export class CustomFetch {
|
|
|
178
180
|
result = pick(result, options.pick);
|
|
179
181
|
}
|
|
180
182
|
asyncData.data.value = result;
|
|
181
|
-
asyncData.error.value =
|
|
183
|
+
asyncData.error.value = asyncDataDefaults.errorValue;
|
|
182
184
|
asyncData.status.value = "success";
|
|
183
185
|
return asyncData;
|
|
184
186
|
}).catch((error) => {
|
|
185
187
|
asyncData.error.value = createError(error);
|
|
186
|
-
asyncData.data.value = unref(options
|
|
188
|
+
asyncData.data.value = unref(options.default());
|
|
187
189
|
asyncData.status.value = "error";
|
|
188
190
|
return asyncData;
|
|
189
191
|
}).finally(() => {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
+
if (pendingWhenIdle) {
|
|
193
|
+
asyncData.pending.value = false;
|
|
194
|
+
}
|
|
195
|
+
_cachedController.delete(key.value);
|
|
192
196
|
});
|
|
193
197
|
asyncData.refresh = asyncData.execute = () => _handler().then((result) => asyncData.data.value = result);
|
|
194
198
|
const hasScope = getCurrentScope();
|
|
195
199
|
if (options.watch) {
|
|
196
200
|
const unsub = watch(options.watch, async () => {
|
|
197
201
|
asyncData.refresh();
|
|
198
|
-
});
|
|
202
|
+
}, { flush: "post" });
|
|
199
203
|
if (hasScope) {
|
|
200
204
|
onScopeDispose(unsub);
|
|
201
205
|
}
|
|
202
206
|
}
|
|
203
207
|
return promise;
|
|
204
208
|
}
|
|
205
|
-
return useAsyncData(
|
|
209
|
+
return useAsyncData(key, _handler, options);
|
|
206
210
|
}
|
|
207
211
|
get(url, config = {}, options) {
|
|
208
|
-
return this.
|
|
212
|
+
return this.request(url, {
|
|
209
213
|
...config,
|
|
210
214
|
method: "GET"
|
|
211
215
|
}, options);
|
|
212
216
|
}
|
|
213
217
|
post(url, config = {}, options) {
|
|
214
|
-
return this.
|
|
218
|
+
return this.request(url, {
|
|
215
219
|
...config,
|
|
216
220
|
method: "POST"
|
|
217
221
|
}, options);
|
package/dist/runtime/type.d.ts
CHANGED
|
@@ -1,16 +1,23 @@
|
|
|
1
|
+
import type { MaybeRefOrGetter } from '#imports';
|
|
1
2
|
import type { FetchOptions } from 'ofetch';
|
|
2
3
|
export type { FetchContext, FetchResponse } from 'ofetch';
|
|
3
|
-
|
|
4
|
-
export type
|
|
4
|
+
/** https://github.com/nuxt/nuxt/blob/edeb0759a455dbafd030f0b5ae1d5a39ad0dfc2a/packages/nuxt/src/app/composables/asyncData.ts#L20 */
|
|
5
|
+
export type PickFrom<T, K extends Array<string>> = T extends Array<any> ? T : T extends Record<string, any> ? keyof T extends K[number] ? T : K[number] extends never ? T : Pick<T, K[number]> : T;
|
|
6
|
+
export type KeysOf<T> = Array<T extends T ? keyof T extends string ? keyof T : never : never>;
|
|
5
7
|
export type FetchMethod = 'options' | 'GET' | 'POST' | 'get' | 'HEAD' | 'PATCH' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'post' | 'head' | 'patch' | 'put' | 'delete' | 'connect' | 'trace' | undefined;
|
|
6
|
-
export interface
|
|
7
|
-
key
|
|
8
|
+
export interface CustomFetchOptions extends Omit<FetchOptions, 'method'> {
|
|
9
|
+
/** unique key for fetch */
|
|
10
|
+
key?: MaybeRefOrGetter<string>;
|
|
11
|
+
/** hash key for fetch, with [customFetch: + url] without query */
|
|
8
12
|
immutableKey?: boolean;
|
|
13
|
+
/** show logs */
|
|
9
14
|
showLogs?: boolean;
|
|
10
|
-
interceptors?: Interceptors;
|
|
11
15
|
baseURL?: string;
|
|
16
|
+
/** is use handler to deal with query or pramas */
|
|
12
17
|
useHandler?: boolean;
|
|
13
|
-
handler
|
|
18
|
+
/** handler to deal with query or pramas */
|
|
19
|
+
handler?: (mergedObject: FetchOptions['params'] & FetchOptions['query']) => Record<string, any>;
|
|
20
|
+
/** offline handler */
|
|
14
21
|
offline?: () => void;
|
|
15
22
|
}
|
|
16
23
|
export interface Interceptors {
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { CustomFetchOptions, FetchMethod } from './type.js';
|
|
2
|
+
export declare function Noop(): void;
|
|
3
|
+
export declare function generateOptionSegments<_ResT>(opts: CustomFetchOptions & {
|
|
4
|
+
method: FetchMethod;
|
|
5
|
+
}): (string | Record<string, string> | undefined)[];
|
|
6
|
+
export declare function pick(obj: Record<string, any>, keys: string[]): any;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { reactive, toValue } from "#imports";
|
|
2
|
+
import { isPlainObject } from "@vue/shared";
|
|
3
|
+
import { hash } from "ohash";
|
|
4
|
+
export function Noop() {
|
|
5
|
+
}
|
|
6
|
+
export function generateOptionSegments(opts) {
|
|
7
|
+
const segments = [
|
|
8
|
+
toValue(opts.method)?.toUpperCase() || "GET",
|
|
9
|
+
toValue(opts.baseURL)
|
|
10
|
+
];
|
|
11
|
+
for (const _obj of [opts.params || opts.query]) {
|
|
12
|
+
const obj = toValue(_obj);
|
|
13
|
+
if (!obj) {
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
const unwrapped = {};
|
|
17
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
18
|
+
unwrapped[toValue(key)] = toValue(value);
|
|
19
|
+
}
|
|
20
|
+
segments.push(unwrapped);
|
|
21
|
+
}
|
|
22
|
+
if (opts.body) {
|
|
23
|
+
const value = toValue(opts.body);
|
|
24
|
+
if (!value) {
|
|
25
|
+
segments.push(hash(value));
|
|
26
|
+
} else if (value instanceof ArrayBuffer) {
|
|
27
|
+
segments.push(hash(Object.fromEntries([...new Uint8Array(value).entries()].map(([k, v]) => [k, v.toString()]))));
|
|
28
|
+
} else if (value instanceof FormData) {
|
|
29
|
+
const obj = {};
|
|
30
|
+
for (const entry of value.entries()) {
|
|
31
|
+
const [key, val] = entry;
|
|
32
|
+
obj[key] = val instanceof File ? val.name : val;
|
|
33
|
+
}
|
|
34
|
+
segments.push(hash(obj));
|
|
35
|
+
} else if (isPlainObject(value)) {
|
|
36
|
+
segments.push(hash(reactive(value)));
|
|
37
|
+
} else {
|
|
38
|
+
try {
|
|
39
|
+
segments.push(hash(value));
|
|
40
|
+
} catch {
|
|
41
|
+
console.warn("[Custom Fetch] Failed to hash body", value);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return segments;
|
|
46
|
+
}
|
|
47
|
+
export function pick(obj, keys) {
|
|
48
|
+
const newObj = {};
|
|
49
|
+
for (const key of keys) {
|
|
50
|
+
newObj[key] = obj[key];
|
|
51
|
+
}
|
|
52
|
+
return newObj;
|
|
53
|
+
}
|
package/dist/types.d.mts
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nuxt-custom-fetch",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "4.0.0-alpha.10",
|
|
5
|
+
"packageManager": "pnpm@8.15.3+sha1.64838798f519c18029c1e8a1310e16101fc2eda0",
|
|
5
6
|
"license": "MIT",
|
|
6
7
|
"repository": {
|
|
7
8
|
"type": "git",
|
|
@@ -27,6 +28,7 @@
|
|
|
27
28
|
"lint:fix": "eslint . --fix",
|
|
28
29
|
"prepare": "husky",
|
|
29
30
|
"commit": "git-cz",
|
|
31
|
+
"prerelease": "changelogen --prerelease && pnpm prepack && npm publish --tag alpha",
|
|
30
32
|
"release": "changelogen --release && pnpm prepack && npm publish && git push --follow-tags"
|
|
31
33
|
},
|
|
32
34
|
"config": {
|
|
@@ -35,24 +37,26 @@
|
|
|
35
37
|
}
|
|
36
38
|
},
|
|
37
39
|
"dependencies": {
|
|
38
|
-
"@
|
|
39
|
-
"ohash": "^2.0.11"
|
|
40
|
+
"@vue/shared": "^3.5.16",
|
|
41
|
+
"ohash": "^2.0.11",
|
|
42
|
+
"vue": "^3.5.16"
|
|
40
43
|
},
|
|
41
44
|
"devDependencies": {
|
|
42
|
-
"@antfu/eslint-config": "^4.
|
|
43
|
-
"@commitlint/cli": "^19.8.
|
|
44
|
-
"@commitlint/config-conventional": "^19.8.
|
|
45
|
-
"@nuxt/
|
|
46
|
-
"@nuxt/
|
|
47
|
-
"@
|
|
45
|
+
"@antfu/eslint-config": "^4.13.2",
|
|
46
|
+
"@commitlint/cli": "^19.8.1",
|
|
47
|
+
"@commitlint/config-conventional": "^19.8.1",
|
|
48
|
+
"@nuxt/kit": "3.17.4",
|
|
49
|
+
"@nuxt/module-builder": "^1.0.1",
|
|
50
|
+
"@nuxt/schema": "3.17.4",
|
|
51
|
+
"@types/node": "^22.15.26",
|
|
48
52
|
"changelogen": "^0.6.1",
|
|
49
53
|
"commitizen": "^4.3.1",
|
|
50
54
|
"cz-git": "^1.11.1",
|
|
51
|
-
"eslint": "^9.
|
|
55
|
+
"eslint": "^9.27.0",
|
|
52
56
|
"husky": "^9.1.7",
|
|
53
|
-
"lint-staged": "^
|
|
54
|
-
"nitro": "npm:nitropack",
|
|
55
|
-
"nuxt": "
|
|
57
|
+
"lint-staged": "^16.1.0",
|
|
58
|
+
"nitro": "npm:nitropack@^2.11.12",
|
|
59
|
+
"nuxt": "3.17.4",
|
|
56
60
|
"typescript": "5.8.3"
|
|
57
61
|
},
|
|
58
62
|
"husky": {
|