nuxt-custom-fetch 2.0.2 → 2.0.4

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/dist/module.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "nuxt-custom-fetch",
3
3
  "configKey": "customFetch",
4
- "version": "2.0.2"
4
+ "version": "2.0.4"
5
5
  }
@@ -4,6 +4,7 @@ import type { HTTPConfig, Interceptors, FetchMethod, KeysOf, PickFrom } from './
4
4
  type CustomFetchReturnValue<DataT, ErrorT> = AsyncData<PickFrom<DataT, KeysOf<DataT>> | null, (ErrorT extends Error | NuxtError<unknown> ? ErrorT : NuxtError<ErrorT>) | null>;
5
5
  export declare class CustomFetch {
6
6
  baseURL: any;
7
+ immutableKey: boolean;
7
8
  params: HTTPConfig;
8
9
  baseHandler: HTTPConfig['handler'];
9
10
  interceptors: Interceptors;
@@ -1,5 +1,5 @@
1
1
  import { hash } from "ohash";
2
- import { createError, ref, toValue, unref, useAsyncData, useNuxtApp } from "#imports";
2
+ import { createError, reactive, ref, toValue, unref, useAsyncData, useNuxtApp } from "#imports";
3
3
  const Noop = () => {
4
4
  };
5
5
  function generateOptionSegments(opts) {
@@ -30,6 +30,7 @@ function pick(obj, keys) {
30
30
  const _cachedController = /* @__PURE__ */ new Map();
31
31
  export class CustomFetch {
32
32
  baseURL;
33
+ immutableKey = false;
33
34
  params = {};
34
35
  baseHandler;
35
36
  interceptors = {};
@@ -39,6 +40,7 @@ export class CustomFetch {
39
40
  ...config
40
41
  };
41
42
  this.baseURL = config.baseURL;
43
+ this.immutableKey = config.immutableKey ?? false;
42
44
  if (config.handler) {
43
45
  this.baseHandler = config.handler;
44
46
  }
@@ -53,15 +55,22 @@ export class CustomFetch {
53
55
  baseConfig(config) {
54
56
  const { useHandler = true, handler, query = {}, params = {} } = config;
55
57
  const baseHandler = handler || this.baseHandler;
56
- const mergeObj = { ...params, ...query };
58
+ const _name = Object.keys(query).length ? "query" : "params";
59
+ const mergeObj = { ...query, ...params };
57
60
  if (useHandler && baseHandler && typeof baseHandler === "function") {
58
- return baseHandler({ ...mergeObj });
61
+ return {
62
+ [_name]: baseHandler(mergeObj)
63
+ };
59
64
  }
60
- return { ...mergeObj };
65
+ return {
66
+ [_name]: {
67
+ ...mergeObj
68
+ }
69
+ };
61
70
  }
62
- http(url, config, options) {
71
+ http(url, config, options = {}) {
63
72
  config.baseURL = config?.baseURL || this.baseURL;
64
- config.query = this.baseConfig(config);
73
+ Object.assign(config, this.baseConfig(config));
65
74
  if (process.client && navigator && !navigator.onLine) {
66
75
  config.offline && config.offline();
67
76
  }
@@ -72,8 +81,14 @@ export class CustomFetch {
72
81
  onResponse,
73
82
  onResponseError,
74
83
  offline,
84
+ handler,
85
+ useHandler,
86
+ immutableKey,
75
87
  ...restAjaxConfig
76
88
  } = config;
89
+ const _config = reactive({
90
+ ...restAjaxConfig
91
+ });
77
92
  const defaultOptions = {
78
93
  onRequest(ctx) {
79
94
  [interceptors.onRequest, onRequest].map((fn) => fn && fn(ctx));
@@ -105,15 +120,20 @@ export class CustomFetch {
105
120
  });
106
121
  }
107
122
  };
108
- const key = hash(["custom|", url, ...generateOptionSegments(config)]);
109
- const handler = () => {
123
+ const hashValue = ["custom|", url];
124
+ if (!this.immutableKey || !immutableKey) {
125
+ hashValue.push(...generateOptionSegments(config));
126
+ }
127
+ const key = hash(hashValue);
128
+ options.default = options.default ?? (() => null);
129
+ const _handler = () => {
110
130
  if (_cachedController.get(key)) {
111
131
  _cachedController.get(key)?.abort?.();
112
132
  }
113
133
  return $fetch(url, {
114
134
  signal: _cachedController.get(key)?.signal,
115
135
  ...defaultOptions,
116
- ...restAjaxConfig
136
+ ..._config
117
137
  });
118
138
  };
119
139
  const nuxtApp = useNuxtApp();
@@ -129,7 +149,7 @@ export class CustomFetch {
129
149
  asyncData.status.value = "pending";
130
150
  const promise = new Promise((resolve, reject) => {
131
151
  try {
132
- resolve(handler());
152
+ resolve(_handler());
133
153
  _cachedController.set(key, controller);
134
154
  } catch (err) {
135
155
  reject(err);
@@ -157,7 +177,7 @@ export class CustomFetch {
157
177
  });
158
178
  return promise;
159
179
  }
160
- return useAsyncData(config.key || key, handler, options);
180
+ return useAsyncData(config.key || key, _handler, options);
161
181
  }
162
182
  get(url, config = {}, options) {
163
183
  return this.http(url, { ...config, method: "GET" }, options);
@@ -23,6 +23,7 @@ export interface OnResponseErrorType {
23
23
  export type FetchMethod = 'options' | 'GET' | 'POST' | 'get' | 'HEAD' | 'PATCH' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'post' | 'head' | 'patch' | 'put' | 'delete' | 'connect' | 'trace' | undefined;
24
24
  export interface HTTPConfig extends Omit<FetchOptions, 'method'> {
25
25
  key?: string;
26
+ immutableKey?: boolean;
26
27
  baseURL?: string;
27
28
  useHandler?: boolean;
28
29
  handler?: (params: Record<string, any>) => Record<string, any>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-custom-fetch",
3
- "version": "2.0.2",
3
+ "version": "2.0.4",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {