nuxt-custom-fetch 2.1.4 → 2.3.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 CHANGED
@@ -58,6 +58,7 @@ ajax.http({
58
58
  export declare class CustomFetch {
59
59
  baseURL: any
60
60
  immutableKey: boolean
61
+ showLogs: boolean
61
62
  params: HTTPConfig
62
63
  baseHandler: HTTPConfig['handler']
63
64
  interceptors: Interceptors
package/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nuxt-custom-fetch",
3
3
  "configKey": "customFetch",
4
- "version": "2.1.4",
4
+ "version": "2.3.0",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "0.8.4",
7
7
  "unbuild": "unknown"
@@ -1,4 +1,4 @@
1
- import type { NitroFetchRequest } from 'nitropack';
1
+ import type { NitroFetchRequest } from 'nitro';
2
2
  import type { AsyncData, AsyncDataOptions, NuxtError } from 'nuxt/app';
3
3
  import type { FetchMethod, HTTPConfig, Interceptors, KeysOf, PickFrom } from './type.js';
4
4
  type CustomFetchReturnValue<DataT, ErrorT> = AsyncData<PickFrom<DataT, KeysOf<DataT>> | null, (ErrorT extends Error | NuxtError<unknown> ? ErrorT : NuxtError<ErrorT>) | null>;
@@ -10,6 +10,7 @@ export declare class CustomFetch {
10
10
  baseHandler: HTTPConfig['handler'];
11
11
  interceptors: Interceptors;
12
12
  offline: typeof Noop;
13
+ showLogs: boolean;
13
14
  constructor(config?: HTTPConfig);
14
15
  private baseConfig;
15
16
  http<DataT, ErrorT = Error | null>(url: NitroFetchRequest, config: HTTPConfig & {
@@ -1,5 +1,5 @@
1
- import { createError, reactive, ref, toValue, unref, useAsyncData, useNuxtApp } from "#imports";
2
- import { hash } from "ohash";
1
+ import { createError, getCurrentScope, onScopeDispose, reactive, ref, toValue, unref, useAsyncData, useNuxtApp, useRequestFetch, watch } from "#imports";
2
+ import { hash, serialize } from "ohash";
3
3
  function Noop() {
4
4
  }
5
5
  function generateOptionSegments(opts) {
@@ -32,10 +32,14 @@ export class CustomFetch {
32
32
  baseHandler;
33
33
  interceptors = {};
34
34
  offline = Noop;
35
- constructor(config = { baseURL: "" }) {
35
+ showLogs = false;
36
+ constructor(config = {
37
+ baseURL: ""
38
+ }) {
36
39
  this.params = { ...config };
37
40
  this.baseURL = config.baseURL;
38
41
  this.immutableKey = config.immutableKey ?? false;
42
+ this.showLogs = config.showLogs ?? import.meta.dev;
39
43
  if (config.handler) {
40
44
  this.baseHandler = config.handler;
41
45
  }
@@ -63,6 +67,10 @@ export class CustomFetch {
63
67
  http(url, config, options = {}) {
64
68
  config.baseURL = config?.baseURL || this.baseURL;
65
69
  Object.assign(config, this.baseConfig(config));
70
+ const generateOptionSegmentsWithConfig = generateOptionSegments(config);
71
+ if (this.showLogs && import.meta.client) {
72
+ console.warn("[Custom Fetch] `Request:`", url, " `Query:`", serialize(generateOptionSegmentsWithConfig), " `Body:`", serialize(config.body));
73
+ }
66
74
  const { onRequest, onRequestError, onResponse, onResponseError, offline, handler, useHandler, immutableKey, ...restAjaxConfig } = config;
67
75
  if (import.meta.client && navigator && !navigator.onLine) {
68
76
  if (offline) {
@@ -121,24 +129,24 @@ export class CustomFetch {
121
129
  });
122
130
  }
123
131
  };
124
- const hashValue = ["custom|", url];
132
+ const hashValue = ["custom_fetch:", url];
125
133
  if (!this.immutableKey && !immutableKey) {
126
- hashValue.push(...generateOptionSegments(config));
134
+ hashValue.push(...generateOptionSegmentsWithConfig);
127
135
  }
128
- const key = hash(hashValue);
136
+ const key = hash(hashValue).replace(/[-_]/g, "").slice(0, 10);
129
137
  options.default = options.default ?? (() => null);
130
138
  const _handler = () => {
131
139
  if (_cachedController.get(key)) {
132
140
  _cachedController.get(key)?.abort?.();
133
141
  }
134
- return $fetch(url, {
142
+ return useRequestFetch()(url, {
135
143
  signal: _cachedController.get(key)?.signal,
136
144
  ...defaultOptions,
137
145
  ..._config
138
146
  });
139
147
  };
140
- const nuxtApp = useNuxtApp();
141
148
  const controller = typeof AbortController !== "undefined" ? new AbortController() : {};
149
+ const nuxtApp = useNuxtApp();
142
150
  if (import.meta.client && !nuxtApp.isHydrating) {
143
151
  const asyncData = {
144
152
  data: ref(null),
@@ -176,6 +184,16 @@ export class CustomFetch {
176
184
  asyncData.pending.value = false;
177
185
  _cachedController.delete(key);
178
186
  });
187
+ asyncData.refresh = asyncData.execute = () => _handler().then((result) => asyncData.data.value = result);
188
+ const hasScope = getCurrentScope();
189
+ if (options.watch) {
190
+ const unsub = watch(options.watch, async () => {
191
+ asyncData.refresh();
192
+ });
193
+ if (hasScope) {
194
+ onScopeDispose(unsub);
195
+ }
196
+ }
179
197
  return promise;
180
198
  }
181
199
  return useAsyncData(config.key || key, _handler, options);
@@ -6,6 +6,7 @@ export type FetchMethod = 'options' | 'GET' | 'POST' | 'get' | 'HEAD' | 'PATCH'
6
6
  export interface HTTPConfig extends Omit<FetchOptions, 'method'> {
7
7
  key?: string;
8
8
  immutableKey?: boolean;
9
+ showLogs?: boolean;
9
10
  interceptors?: Interceptors;
10
11
  baseURL?: string;
11
12
  useHandler?: boolean;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nuxt-custom-fetch",
3
3
  "type": "module",
4
- "version": "2.1.4",
4
+ "version": "2.3.0",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
@@ -36,24 +36,25 @@
36
36
  }
37
37
  },
38
38
  "dependencies": {
39
- "@nuxt/kit": "^3.15.4",
40
- "ohash": "^1.1.4"
39
+ "@nuxt/kit": "^3.16.1",
40
+ "ohash": "^2.0.11"
41
41
  },
42
42
  "devDependencies": {
43
- "@antfu/eslint-config": "^4.3.0",
44
- "@commitlint/cli": "^19.7.1",
45
- "@commitlint/config-conventional": "^19.7.1",
43
+ "@antfu/eslint-config": "^4.10.1",
44
+ "@commitlint/cli": "^19.8.0",
45
+ "@commitlint/config-conventional": "^19.8.0",
46
46
  "@nuxt/module-builder": "^0.8.4",
47
- "@nuxt/schema": "^3.15.4",
48
- "@types/node": "^22.13.5",
49
- "changelogen": "^0.5.7",
47
+ "@nuxt/schema": "^3.16.1",
48
+ "@types/node": "^22.13.10",
49
+ "changelogen": "^0.6.1",
50
50
  "commitizen": "^4.3.1",
51
- "cz-git": "^1.11.0",
52
- "eslint": "^9.21.0",
51
+ "cz-git": "^1.11.1",
52
+ "eslint": "^9.22.0",
53
53
  "husky": "^9.1.7",
54
- "lint-staged": "^15.4.3",
55
- "nuxt": "^3.15.4",
56
- "typescript": "^5.7.3"
54
+ "lint-staged": "^15.5.0",
55
+ "nitro": "npm:nitropack",
56
+ "nuxt": "^3.16.1",
57
+ "typescript": "5.8.2"
57
58
  },
58
59
  "husky": {
59
60
  "hooks": {