nuxt-custom-fetch 4.0.0-alpha.4 → 4.0.0-alpha.6
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 +8 -4
- package/dist/runtime/ajax.d.ts +1 -1
- package/dist/runtime/ajax.js +25 -15
- package/dist/runtime/type.d.ts +4 -3
- package/package.json +9 -8
package/README.md
CHANGED
|
@@ -4,6 +4,10 @@ simple wrapper useAsyncData
|
|
|
4
4
|
|
|
5
5
|
`version >= 3.17.0`
|
|
6
6
|
|
|
7
|
+
## Break Change
|
|
8
|
+
|
|
9
|
+
1. `http` method is removed, use `request` instead
|
|
10
|
+
|
|
7
11
|
## Attention
|
|
8
12
|
|
|
9
13
|
1. Invalid request in onMounted
|
|
@@ -36,13 +40,13 @@ const ajax = new CustomFetch({
|
|
|
36
40
|
baseURL: ''
|
|
37
41
|
})
|
|
38
42
|
|
|
39
|
-
// `
|
|
43
|
+
// `request|get|post`
|
|
40
44
|
ajax.get('/api/user')
|
|
41
45
|
|
|
42
46
|
ajax.post('/api/user')
|
|
43
47
|
|
|
44
|
-
ajax.
|
|
45
|
-
method: '
|
|
48
|
+
ajax.request({
|
|
49
|
+
method: 'DELETE'
|
|
46
50
|
})
|
|
47
51
|
```
|
|
48
52
|
|
|
@@ -59,7 +63,7 @@ export declare class CustomFetch {
|
|
|
59
63
|
offline: typeof Noop
|
|
60
64
|
showLogs: boolean
|
|
61
65
|
constructor (config: CustomFetchOptions)
|
|
62
|
-
|
|
66
|
+
request<DataT, NuxtErrorDataT = Error | null>(url: NitroFetchRequest, config: CustomFetchOptions & {
|
|
63
67
|
method: FetchMethod
|
|
64
68
|
}, options?: AsyncDataOptions<DataT>): CustomFetchReturnValue<DataT, NuxtErrorDataT>
|
|
65
69
|
get<DataT, NuxtErrorDataT = Error | null>(url: NitroFetchRequest, config?: CustomFetchOptions, options?: AsyncDataOptions<DataT>): CustomFetchReturnValue<DataT, NuxtErrorDataT>
|
package/dist/runtime/ajax.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ export declare class CustomFetch {
|
|
|
13
13
|
showLogs: boolean;
|
|
14
14
|
constructor(config: CustomFetchOptions);
|
|
15
15
|
private baseConfig;
|
|
16
|
-
|
|
16
|
+
request<DataT, NuxtErrorDataT = Error | null>(url: NitroFetchRequest, config: CustomFetchOptions & {
|
|
17
17
|
method: FetchMethod;
|
|
18
18
|
}, options?: AsyncDataOptions<DataT>): CustomFetchReturnValue<DataT, NuxtErrorDataT>;
|
|
19
19
|
get<DataT, NuxtErrorDataT = Error | null>(url: NitroFetchRequest, config?: CustomFetchOptions, options?: AsyncDataOptions<DataT>): CustomFetchReturnValue<DataT, NuxtErrorDataT>;
|
package/dist/runtime/ajax.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { asyncDataDefaults, pendingWhenIdle } from "#build/nuxt.config.mjs";
|
|
2
|
-
import { clearNuxtData, createError, getCurrentScope, onScopeDispose, reactive, ref, shallowRef, unref, useAsyncData, useNuxtApp, useRequestFetch, useRuntimeConfig, watch } from "#imports";
|
|
2
|
+
import { clearNuxtData, computed, createError, getCurrentScope, onScopeDispose, reactive, ref, shallowRef, toValue, unref, useAsyncData, useNuxtApp, useRequestFetch, useRuntimeConfig, watch } from "#imports";
|
|
3
3
|
import { hash, serialize } from "ohash";
|
|
4
4
|
import { generateOptionSegments, Noop, pick } from "./utils.js";
|
|
5
5
|
const _cachedController = /* @__PURE__ */ new Map();
|
|
@@ -43,7 +43,7 @@ export class CustomFetch {
|
|
|
43
43
|
}
|
|
44
44
|
return { [_name]: { ...mergeObj } };
|
|
45
45
|
}
|
|
46
|
-
|
|
46
|
+
request(url, config, options = {}) {
|
|
47
47
|
const app = useRuntimeConfig().app;
|
|
48
48
|
config.baseURL ??= this.baseURL || app.baseURL;
|
|
49
49
|
Object.assign(config, this.baseConfig(config));
|
|
@@ -55,7 +55,7 @@ export class CustomFetch {
|
|
|
55
55
|
} catch (error) {
|
|
56
56
|
console.warn("[Custom Fetch] couldn't serialize `Body:`", error);
|
|
57
57
|
}
|
|
58
|
-
console.warn("[Custom Fetch] `Request:`", url, "
|
|
58
|
+
console.warn("[Custom Fetch] `Request:`", url, "`Query:`", serialize(generateOptionSegmentsWithConfig), " `Body:`", bodyLogs);
|
|
59
59
|
}
|
|
60
60
|
const { onRequest, onRequestError, onResponse, onResponseError, offline, handler, useHandler, showLogs, immutableKey, ...asyncDataOptions } = config;
|
|
61
61
|
if (import.meta.client && navigator && !navigator.onLine) {
|
|
@@ -113,7 +113,6 @@ export class CustomFetch {
|
|
|
113
113
|
});
|
|
114
114
|
}
|
|
115
115
|
};
|
|
116
|
-
let key = config.key;
|
|
117
116
|
const hashValue = ["custom_fetch:", url];
|
|
118
117
|
if (import.meta.dev && !config.key && (this.immutableKey || immutableKey)) {
|
|
119
118
|
console.warn("[Custom Fetch] immutableKey is enabled, the key will be generated by hash([custom_fetch:, url])");
|
|
@@ -121,13 +120,23 @@ export class CustomFetch {
|
|
|
121
120
|
if (!this.immutableKey && !immutableKey) {
|
|
122
121
|
hashValue.push(...generateOptionSegmentsWithConfig);
|
|
123
122
|
}
|
|
124
|
-
|
|
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
|
+
});
|
|
125
134
|
const _handler = () => {
|
|
126
|
-
if (_cachedController.get(key)) {
|
|
127
|
-
_cachedController.get(key)?.abort?.();
|
|
135
|
+
if (_cachedController.get(key.value)) {
|
|
136
|
+
_cachedController.get(key.value)?.abort?.();
|
|
128
137
|
}
|
|
129
138
|
return useRequestFetch()(url, {
|
|
130
|
-
signal: _cachedController.get(key)?.signal,
|
|
139
|
+
signal: _cachedController.get(key.value)?.signal,
|
|
131
140
|
...defaultOptions,
|
|
132
141
|
..._config
|
|
133
142
|
});
|
|
@@ -135,11 +144,12 @@ export class CustomFetch {
|
|
|
135
144
|
const controller = typeof AbortController !== "undefined" ? new AbortController() : {};
|
|
136
145
|
const nuxtApp = useNuxtApp();
|
|
137
146
|
if (import.meta.client && !nuxtApp.isHydrating) {
|
|
138
|
-
if (nuxtApp._asyncData[key]) {
|
|
139
|
-
|
|
147
|
+
if (nuxtApp._asyncData[key.value]) {
|
|
148
|
+
nuxtApp._asyncData[key.value].execute({
|
|
140
149
|
cause: "initial",
|
|
141
150
|
dedupe: options.dedupe
|
|
142
151
|
});
|
|
152
|
+
return nuxtApp._asyncData[key.value];
|
|
143
153
|
}
|
|
144
154
|
const _ref = options.deep ? ref : shallowRef;
|
|
145
155
|
options.default ??= () => asyncDataDefaults.value;
|
|
@@ -148,7 +158,7 @@ export class CustomFetch {
|
|
|
148
158
|
error: _ref(null),
|
|
149
159
|
status: _ref("idle"),
|
|
150
160
|
pending: _ref(false),
|
|
151
|
-
clear: () => clearNuxtData(key)
|
|
161
|
+
clear: () => clearNuxtData(key.value)
|
|
152
162
|
};
|
|
153
163
|
if (pendingWhenIdle) {
|
|
154
164
|
asyncData.pending.value = true;
|
|
@@ -157,7 +167,7 @@ export class CustomFetch {
|
|
|
157
167
|
const promise = new Promise((resolve, reject) => {
|
|
158
168
|
try {
|
|
159
169
|
resolve(_handler());
|
|
160
|
-
_cachedController.set(key, controller);
|
|
170
|
+
_cachedController.set(key.value, controller);
|
|
161
171
|
} catch (err) {
|
|
162
172
|
reject(err);
|
|
163
173
|
}
|
|
@@ -182,7 +192,7 @@ export class CustomFetch {
|
|
|
182
192
|
if (pendingWhenIdle) {
|
|
183
193
|
asyncData.pending.value = false;
|
|
184
194
|
}
|
|
185
|
-
_cachedController.delete(key);
|
|
195
|
+
_cachedController.delete(key.value);
|
|
186
196
|
});
|
|
187
197
|
asyncData.refresh = asyncData.execute = () => _handler().then((result) => asyncData.data.value = result);
|
|
188
198
|
const hasScope = getCurrentScope();
|
|
@@ -199,13 +209,13 @@ export class CustomFetch {
|
|
|
199
209
|
return useAsyncData(key, _handler, options);
|
|
200
210
|
}
|
|
201
211
|
get(url, config = {}, options) {
|
|
202
|
-
return this.
|
|
212
|
+
return this.request(url, {
|
|
203
213
|
...config,
|
|
204
214
|
method: "GET"
|
|
205
215
|
}, options);
|
|
206
216
|
}
|
|
207
217
|
post(url, config = {}, options) {
|
|
208
|
-
return this.
|
|
218
|
+
return this.request(url, {
|
|
209
219
|
...config,
|
|
210
220
|
method: "POST"
|
|
211
221
|
}, options);
|
package/dist/runtime/type.d.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
+
import type { MaybeRefOrGetter } from '#imports';
|
|
1
2
|
import type { FetchOptions } from 'ofetch';
|
|
2
3
|
export type { FetchContext, FetchResponse } from 'ofetch';
|
|
3
4
|
/** https://github.com/nuxt/nuxt/blob/edeb0759a455dbafd030f0b5ae1d5a39ad0dfc2a/packages/nuxt/src/app/composables/asyncData.ts#L20 */
|
|
4
|
-
export type PickFrom<T, K extends Array<string>> = T extends Array<any> ? T : T extends Record<string, any> ?
|
|
5
|
-
export type KeysOf<T> = Array<T extends T ?
|
|
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>;
|
|
6
7
|
export type FetchMethod = 'options' | 'GET' | 'POST' | 'get' | 'HEAD' | 'PATCH' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'post' | 'head' | 'patch' | 'put' | 'delete' | 'connect' | 'trace' | undefined;
|
|
7
8
|
export interface CustomFetchOptions extends Omit<FetchOptions, 'method'> {
|
|
8
9
|
/** unique key for fetch */
|
|
9
|
-
key?: string
|
|
10
|
+
key?: MaybeRefOrGetter<string>;
|
|
10
11
|
/** hash key for fetch, with [customFetch: + url] without query */
|
|
11
12
|
immutableKey?: boolean;
|
|
12
13
|
/** show logs */
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nuxt-custom-fetch",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "4.0.0-alpha.
|
|
4
|
+
"version": "4.0.0-alpha.6",
|
|
5
5
|
"packageManager": "pnpm@8.15.3+sha1.64838798f519c18029c1e8a1310e16101fc2eda0",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
|
@@ -37,24 +37,25 @@
|
|
|
37
37
|
}
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"ohash": "^2.0.11"
|
|
40
|
+
"ohash": "^2.0.11",
|
|
41
|
+
"vue": "^3.5.13"
|
|
41
42
|
},
|
|
42
43
|
"devDependencies": {
|
|
43
44
|
"@antfu/eslint-config": "^4.12.0",
|
|
44
45
|
"@commitlint/cli": "^19.8.0",
|
|
45
46
|
"@commitlint/config-conventional": "^19.8.0",
|
|
46
|
-
"@nuxt/kit": "
|
|
47
|
+
"@nuxt/kit": "3.17.0",
|
|
47
48
|
"@nuxt/module-builder": "^1.0.1",
|
|
48
|
-
"@nuxt/schema": "
|
|
49
|
-
"@types/node": "^22.
|
|
49
|
+
"@nuxt/schema": "3.17.0",
|
|
50
|
+
"@types/node": "^22.15.2",
|
|
50
51
|
"changelogen": "^0.6.1",
|
|
51
52
|
"commitizen": "^4.3.1",
|
|
52
53
|
"cz-git": "^1.11.1",
|
|
53
|
-
"eslint": "^9.
|
|
54
|
+
"eslint": "^9.25.1",
|
|
54
55
|
"husky": "^9.1.7",
|
|
55
56
|
"lint-staged": "^15.5.1",
|
|
56
|
-
"nitro": "npm:
|
|
57
|
-
"nuxt": "
|
|
57
|
+
"nitro": "npm:nitropack@latest",
|
|
58
|
+
"nuxt": "3.17.0",
|
|
58
59
|
"typescript": "5.8.3"
|
|
59
60
|
},
|
|
60
61
|
"husky": {
|