nuxt-custom-fetch 4.6.0 → 4.6.1

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 CHANGED
@@ -17,7 +17,7 @@ Use `CustomFetch` when you specifically need an extra request layer on top of th
17
17
  - `v4`: Nuxt `^4.5.0` (Nuxt 4 only)
18
18
  - `v2`: Nuxt `3.0.0` to `3.16.x`
19
19
 
20
- The development playground targets Nuxt `4.6.2` and Vue `3.5.44`. Nuxt `4.6.2` requires Node.js `^22.22.3 || ^24.15.0 || >=26.0.0`. Nuxt 5 is still in development and is not included in this module's compatibility range.
20
+ The development playground targets Nuxt `4.7.0` and Vue `3.5.46`. Nuxt `4.7.0` requires Node.js `^22.22.3 || ^24.15.0 || >=26.0.0`.
21
21
 
22
22
  ## Current Maintenance Summary
23
23
 
@@ -27,7 +27,8 @@ The current implementation is maintained around these guarantees:
27
27
  - public method generics follow Nuxt `AsyncData` typing more closely
28
28
  - reactive `key`, `baseURL`, `params`, `query`, `headers`, `body`, and `cache` values are resolved before each request
29
29
  - generated keys are derived with Nuxt's `hashKey` (the same digest `useFetch` uses) and include both `params` and `query`, which avoids stale client reuse when only one side changes
30
- - same-key client compatibility requests share one async-data bucket, so `dedupe: 'cancel'` can abort the previous pending request
30
+ - same-key calls after mount share request state. Clearing or replacing a request prevents its result from overwriting that state, including while an asynchronous `transform` is running
31
+ - after hydration, the default cache uses prerendered data for the initial load. Changes to watched sources fetch fresh data
31
32
  - the client compatibility path mirrors Nuxt 4 async-data behavior: `pending` follows the `pendingWhenIdle` config, `refreshNuxtData()` (the `app:data:refresh` hook) re-runs fallback requests, `getCachedData` is honored and successful data is written back to `nuxtApp.payload.data` unless `serialize: false` is set
32
33
  - the client compatibility path honors Nuxt 4.5's `enabled` option (a reactive `enabled` turning `false` cancels the in-flight request) and cancels an in-flight request when its owning scope is disposed
33
34
  - on Nuxt 4.6, the client compatibility path also honors async-data `middleware` (first entry outermost, before `transform`/`pick`) and `serialize: false`, including cached results
@@ -260,7 +261,7 @@ The return value follows Nuxt's `AsyncData<...>` shape and can use the same `def
260
261
  ## References
261
262
 
262
263
  - https://github.com/nuxt/nuxt/releases/tag/v4.6.0
263
- - https://github.com/nuxt/nuxt/releases/tag/v4.6.2
264
+ - https://github.com/nuxt/nuxt/releases/tag/v4.7.0
264
265
  - https://nuxt.com/docs/4.x/api/utils/define-use-fetch-addon
265
266
  - https://nuxt.com/docs/4.x/api/composables/create-use-async-data
266
267
  - https://nuxt.com/docs/4.x/api/composables/create-use-fetch
package/dist/module.json CHANGED
@@ -4,9 +4,9 @@
4
4
  "compatibility": {
5
5
  "nuxt": "^4.5.0"
6
6
  },
7
- "version": "4.6.0",
7
+ "version": "4.6.1",
8
8
  "builder": {
9
- "@nuxt/module-builder": "1.0.3",
9
+ "@nuxt/module-builder": "1.0.4",
10
10
  "unbuild": "unknown"
11
11
  }
12
12
  }
package/dist/module.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { fileURLToPath } from 'node:url';
2
- import { defineNuxtModule, createResolver, addImports } from '@nuxt/kit';
2
+ import { defineNuxtModule, createResolver, addImports } from 'nuxt/kit';
3
3
 
4
4
  const nuxtCustomFetch = defineNuxtModule({
5
5
  meta: {
@@ -6,6 +6,10 @@ import { generateOptionSegments, Noop, pick, resolveReactiveValue } from "./util
6
6
  const _cachedController = /* @__PURE__ */ new Map();
7
7
  const _cachedClientAsyncData = /* @__PURE__ */ new Map();
8
8
  const MAX_UNSCOPED_CLIENT_ASYNC_DATA_ENTRIES = 50;
9
+ const DISABLED_ABORT_MESSAGE = "AsyncData request cancelled by `enabled: false`";
10
+ function readOwnEntry(entries, key) {
11
+ return entries && Object.hasOwn(entries, key) ? entries[key] : void 0;
12
+ }
9
13
  function createAbortController() {
10
14
  return typeof AbortController !== "undefined" ? new AbortController() : void 0;
11
15
  }
@@ -17,7 +21,9 @@ function linkAbortSignal(signal, controller) {
17
21
  controller.abort(signal.reason);
18
22
  return;
19
23
  }
20
- signal.addEventListener("abort", () => controller.abort(signal.reason), { once: true });
24
+ const onAbort = () => controller.abort(signal.reason);
25
+ signal.addEventListener("abort", onAbort, { once: true });
26
+ return () => signal.removeEventListener("abort", onAbort);
21
27
  }
22
28
  function createMergedSignal(signals, timeout) {
23
29
  const controller = createAbortController();
@@ -28,6 +34,9 @@ function createMergedSignal(signals, timeout) {
28
34
  if (typeof timeout === "number" && timeout >= 0 && typeof AbortSignal !== "undefined" && typeof AbortSignal.timeout === "function") {
29
35
  sources.push(AbortSignal.timeout(timeout));
30
36
  }
37
+ if (typeof AbortSignal !== "undefined" && typeof AbortSignal.any === "function") {
38
+ return AbortSignal.any(sources);
39
+ }
31
40
  for (const signal of sources) {
32
41
  linkAbortSignal(signal, controller);
33
42
  }
@@ -273,16 +282,26 @@ export class CustomFetch {
273
282
  return options.getCachedData(key.value, nuxtApp, { cause });
274
283
  }
275
284
  if (nuxtApp.isHydrating) {
276
- return nuxtApp.payload?.data?.[key.value];
285
+ return readOwnEntry(nuxtApp.payload?.data, key.value);
277
286
  }
278
- if (cause !== "refresh:manual" && cause !== "refresh:hook") {
279
- return nuxtApp.static?.data?.[key.value];
287
+ if (cause === "initial") {
288
+ return readOwnEntry(nuxtApp.static?.data, key.value);
280
289
  }
281
290
  return void 0;
282
291
  };
283
292
  const writePayloadData = (value) => {
284
- if (fallbackOptions.serialize !== false && nuxtApp.payload?.data) {
285
- nuxtApp.payload.data[key.value] = value;
293
+ const payload = nuxtApp.payload?.data;
294
+ if (fallbackOptions.serialize !== false && payload) {
295
+ if (key.value === "__proto__" && !Object.hasOwn(payload, key.value)) {
296
+ Object.defineProperty(payload, key.value, {
297
+ configurable: true,
298
+ enumerable: true,
299
+ writable: true,
300
+ value
301
+ });
302
+ } else {
303
+ payload[key.value] = value;
304
+ }
286
305
  }
287
306
  };
288
307
  const isKeyReactive = isRef(resolvedConfig.key) || typeof resolvedConfig.key === "function";
@@ -303,6 +322,22 @@ export class CustomFetch {
303
322
  pendingRef.value = value;
304
323
  }
305
324
  };
325
+ const abortActiveRequest = (reason) => {
326
+ requestId++;
327
+ activeController?.abort(reason);
328
+ activeRequest = void 0;
329
+ activeController = void 0;
330
+ _cachedController.delete(key.value);
331
+ };
332
+ const cancelPendingRequest = (message) => {
333
+ if (activeRequest) {
334
+ abortActiveRequest(new DOMException(message, "AbortError"));
335
+ if (statusRef.value === "pending") {
336
+ statusRef.value = "idle";
337
+ }
338
+ setPending(false);
339
+ }
340
+ };
306
341
  const stopWatchers = () => {
307
342
  stopKeyWatch?.();
308
343
  stopKeyWatch = void 0;
@@ -320,8 +355,7 @@ export class CustomFetch {
320
355
  pending: pendingRef,
321
356
  clear: () => {
322
357
  stopWatchers();
323
- activeController?.abort();
324
- _cachedController.delete(key.value);
358
+ abortActiveRequest();
325
359
  if (_cachedClientAsyncData.get(key.value) === asyncData) {
326
360
  _cachedClientAsyncData.delete(key.value);
327
361
  }
@@ -338,31 +372,33 @@ export class CustomFetch {
338
372
  execute: async (executeOptions) => {
339
373
  const cause = executeOptions?.cause;
340
374
  const dedupe = executeOptions?.dedupe ?? options.dedupe ?? "cancel";
341
- if (activeRequest) {
342
- if (dedupe === "defer") {
343
- return activeRequest;
344
- }
345
- activeController?.abort();
375
+ if (activeRequest && dedupe === "defer") {
376
+ return activeRequest;
346
377
  }
347
378
  if (granularCachedData || cause === "initial" || nuxtApp.isHydrating) {
348
379
  const cachedData = resolveCachedData(cause);
349
380
  if (cachedData !== void 0) {
381
+ if (cause !== "initial" && activeRequest) {
382
+ abortActiveRequest(new DOMException("AsyncData request cancelled by cache hit", "AbortError"));
383
+ }
350
384
  writePayloadData(cachedData);
351
385
  hasData = true;
352
386
  asyncData.data.value = cachedData;
353
387
  asyncData.error.value = asyncDataDefaults.errorValue;
354
388
  asyncData.status.value = "success";
355
- setPending(false);
389
+ setPending(activeRequest !== void 0);
356
390
  return;
357
391
  }
358
392
  }
359
393
  if (toValue(options.enabled) === false) {
394
+ cancelPendingRequest(DISABLED_ABORT_MESSAGE);
360
395
  return;
361
396
  }
362
397
  const currentRequestId = ++requestId;
398
+ activeController?.abort();
363
399
  const controller = createAbortController();
364
400
  activeController = controller;
365
- linkAbortSignal(executeOptions?.signal, controller);
401
+ const removeExternalAbortListener = linkAbortSignal(executeOptions?.signal, controller);
366
402
  if (controller) {
367
403
  _cachedController.set(key.value, controller);
368
404
  }
@@ -379,30 +415,32 @@ export class CustomFetch {
379
415
  const next = run;
380
416
  run = () => Promise.resolve(middleware(next, { signal }));
381
417
  }
382
- let request;
383
418
  let removeAbortListener;
384
- try {
385
- request = fallbackOptions.middleware?.length ? new Promise((resolve, reject) => {
386
- const onAbort = () => reject(signal?.reason ?? new DOMException("Aborted", "AbortError"));
387
- if (signal?.aborted) {
388
- onAbort();
389
- return;
390
- }
391
- signal?.addEventListener("abort", onAbort, { once: true });
392
- removeAbortListener = () => signal?.removeEventListener("abort", onAbort);
393
- Promise.resolve(run()).then(resolve, reject);
394
- }) : run();
395
- } catch (error) {
396
- request = Promise.reject(error);
397
- }
398
- activeRequest = request.then(async (result) => {
399
- if (currentRequestId !== requestId) {
419
+ const request = new Promise((resolve, reject) => {
420
+ const onAbort = () => reject(signal?.reason ?? new DOMException("Aborted", "AbortError"));
421
+ if (signal?.aborted) {
422
+ onAbort();
400
423
  return;
401
424
  }
402
- let data = result;
425
+ signal?.addEventListener("abort", onAbort, { once: true });
426
+ removeAbortListener = () => signal?.removeEventListener("abort", onAbort);
427
+ const result = run();
403
428
  if (options.transform) {
404
- data = await options.transform(result);
429
+ result.then((value) => {
430
+ if (currentRequestId !== requestId || signal?.aborted) {
431
+ return value;
432
+ }
433
+ return options.transform(value);
434
+ }).then(resolve, reject);
435
+ } else {
436
+ result.then(resolve, reject);
405
437
  }
438
+ });
439
+ activeRequest = request.then((result) => {
440
+ if (currentRequestId !== requestId || signal?.aborted) {
441
+ return;
442
+ }
443
+ const data = result;
406
444
  let finalData = data;
407
445
  if (options.pick) {
408
446
  finalData = pick(data, options.pick);
@@ -423,6 +461,7 @@ export class CustomFetch {
423
461
  asyncData.status.value = "error";
424
462
  }).finally(() => {
425
463
  removeAbortListener?.();
464
+ removeExternalAbortListener?.();
426
465
  if (currentRequestId !== requestId) {
427
466
  return;
428
467
  }
@@ -437,20 +476,6 @@ export class CustomFetch {
437
476
  await activeRequest;
438
477
  }
439
478
  };
440
- const cancelActiveRequest = (reason) => {
441
- if (!activeRequest) {
442
- return;
443
- }
444
- requestId++;
445
- activeController?.abort(new DOMException(reason, "AbortError"));
446
- activeRequest = void 0;
447
- activeController = void 0;
448
- _cachedController.delete(key.value);
449
- if (asyncData.status.value === "pending") {
450
- asyncData.status.value = "idle";
451
- }
452
- setPending(false);
453
- };
454
479
  _cachedClientAsyncData.set(key.value, asyncData);
455
480
  stopRefreshHook = nuxtApp.hook?.("app:data:refresh", async (keys) => {
456
481
  if (!keys || keys.includes(key.value)) {
@@ -494,7 +519,7 @@ export class CustomFetch {
494
519
  if (isRef(options.enabled) || typeof options.enabled === "function") {
495
520
  stopEnabledWatch = watch(() => toValue(options.enabled), (isEnabled) => {
496
521
  if (!isEnabled) {
497
- cancelActiveRequest("AsyncData request cancelled by `enabled: false`");
522
+ cancelPendingRequest(DISABLED_ABORT_MESSAGE);
498
523
  }
499
524
  });
500
525
  }
@@ -504,7 +529,7 @@ export class CustomFetch {
504
529
  if (hasScope) {
505
530
  onScopeDispose(() => {
506
531
  stopWatchers();
507
- cancelActiveRequest("AsyncData request cancelled by unmount");
532
+ cancelPendingRequest("AsyncData request cancelled by unmount");
508
533
  });
509
534
  }
510
535
  if (options.immediate === false) {
@@ -512,7 +537,7 @@ export class CustomFetch {
512
537
  }
513
538
  return asyncData.execute({ cause: "initial" }).then(() => asyncData);
514
539
  };
515
- const sharedAsyncData = nuxtApp._asyncData?.[key.value];
540
+ const sharedAsyncData = readOwnEntry(nuxtApp._asyncData, key.value);
516
541
  const cachedClientAsyncData = _cachedClientAsyncData.get(key.value);
517
542
  const instance = getCurrentInstance();
518
543
  if (import.meta.client && !nuxtApp.isHydrating && !nuxtApp._processingMiddleware && (!instance || instance.isMounted)) {
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "nuxt-custom-fetch",
3
3
  "type": "module",
4
- "version": "4.6.0",
4
+ "version": "4.6.1",
5
+ "packageManager": "pnpm@11.7.0",
5
6
  "license": "MIT",
6
7
  "repository": {
7
8
  "type": "git",
@@ -17,6 +18,23 @@
17
18
  "files": [
18
19
  "dist"
19
20
  ],
21
+ "scripts": {
22
+ "prepack": "nuxt-module-build build",
23
+ "dev": "nuxi dev playground",
24
+ "dev:build": "nuxi build playground",
25
+ "dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground",
26
+ "echo:husky": "echo 'npx nano-staged' > .husky/pre-commit && echo 'npx --no-install commitlint --edit \"$1\"' > .husky/commit-msg",
27
+ "lint": "eslint .",
28
+ "lint:fix": "eslint . --fix",
29
+ "test": "vitest run",
30
+ "test:watch": "vitest",
31
+ "test:coverage": "vitest run --coverage",
32
+ "test:types": "tsc -p tsconfig.json --noEmit",
33
+ "prepare": "husky",
34
+ "commit": "git-cz",
35
+ "release:alpha": "changelogen --release --prerelease && pnpm prepack && npm publish --tag alpha && git push --follow-tags",
36
+ "release": "changelogen --release && pnpm prepack && npm publish && git push --follow-tags"
37
+ },
20
38
  "config": {
21
39
  "commitizen": {
22
40
  "path": "node_modules/cz-git"
@@ -27,16 +45,16 @@
27
45
  "vue": "^3.5.30"
28
46
  },
29
47
  "dependencies": {
30
- "@vue/shared": "^3.5.44",
48
+ "@vue/shared": "^3.5.46",
31
49
  "ohash": "^2.0.12"
32
50
  },
33
51
  "devDependencies": {
34
52
  "@antfu/eslint-config": "^9.0.0",
35
53
  "@commitlint/cli": "^21.0.2",
36
54
  "@commitlint/config-conventional": "^21.0.2",
37
- "@nuxt/kit": "4.6.2",
38
- "@nuxt/module-builder": "^1.0.3",
39
- "@nuxt/schema": "4.6.2",
55
+ "@nuxt/kit": "4.7.0",
56
+ "@nuxt/module-builder": "^1.0.4",
57
+ "@nuxt/schema": "4.7.0",
40
58
  "@types/node": "^25.5.0",
41
59
  "@vitest/coverage-v8": "^4.1.9",
42
60
  "changelogen": "^0.6.2",
@@ -45,11 +63,11 @@
45
63
  "eslint": "^10.0.3",
46
64
  "husky": "^9.1.7",
47
65
  "nano-staged": "^1.0.2",
48
- "nitropack": "^2.13.5",
49
- "nuxt": "4.6.2",
66
+ "nitropack": "^2.14.0",
67
+ "nuxt": "4.7.0",
50
68
  "typescript": "6.0.3",
51
69
  "vitest": "^4.1.9",
52
- "vue": "^3.5.44"
70
+ "vue": "^3.5.46"
53
71
  },
54
72
  "husky": {
55
73
  "hooks": {
@@ -58,20 +76,5 @@
58
76
  },
59
77
  "nano-staged": {
60
78
  "*": "eslint --fix"
61
- },
62
- "scripts": {
63
- "dev": "nuxi dev playground",
64
- "dev:build": "nuxi build playground",
65
- "dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground",
66
- "echo:husky": "echo 'npx nano-staged' > .husky/pre-commit && echo 'npx --no-install commitlint --edit \"$1\"' > .husky/commit-msg",
67
- "lint": "eslint .",
68
- "lint:fix": "eslint . --fix",
69
- "test": "vitest run",
70
- "test:watch": "vitest",
71
- "test:coverage": "vitest run --coverage",
72
- "test:types": "tsc -p tsconfig.json --noEmit",
73
- "commit": "git-cz",
74
- "release:alpha": "changelogen --release --prerelease && pnpm prepack && npm publish --tag alpha && git push --follow-tags",
75
- "release": "changelogen --release && pnpm prepack && npm publish && git push --follow-tags"
76
79
  }
77
- }
80
+ }