nuxt-custom-fetch 2.0.3 → 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.3"
4
+ "version": "2.0.4"
5
5
  }
@@ -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) {
@@ -55,15 +55,22 @@ export class CustomFetch {
55
55
  baseConfig(config) {
56
56
  const { useHandler = true, handler, query = {}, params = {} } = config;
57
57
  const baseHandler = handler || this.baseHandler;
58
- const mergeObj = { ...params, ...query };
58
+ const _name = Object.keys(query).length ? "query" : "params";
59
+ const mergeObj = { ...query, ...params };
59
60
  if (useHandler && baseHandler && typeof baseHandler === "function") {
60
- return baseHandler({ ...mergeObj });
61
+ return {
62
+ [_name]: baseHandler(mergeObj)
63
+ };
61
64
  }
62
- return { ...mergeObj };
65
+ return {
66
+ [_name]: {
67
+ ...mergeObj
68
+ }
69
+ };
63
70
  }
64
- http(url, config, options) {
71
+ http(url, config, options = {}) {
65
72
  config.baseURL = config?.baseURL || this.baseURL;
66
- config.query = this.baseConfig(config);
73
+ Object.assign(config, this.baseConfig(config));
67
74
  if (process.client && navigator && !navigator.onLine) {
68
75
  config.offline && config.offline();
69
76
  }
@@ -74,8 +81,14 @@ export class CustomFetch {
74
81
  onResponse,
75
82
  onResponseError,
76
83
  offline,
84
+ handler,
85
+ useHandler,
86
+ immutableKey,
77
87
  ...restAjaxConfig
78
88
  } = config;
89
+ const _config = reactive({
90
+ ...restAjaxConfig
91
+ });
79
92
  const defaultOptions = {
80
93
  onRequest(ctx) {
81
94
  [interceptors.onRequest, onRequest].map((fn) => fn && fn(ctx));
@@ -108,18 +121,19 @@ export class CustomFetch {
108
121
  }
109
122
  };
110
123
  const hashValue = ["custom|", url];
111
- if (!this.immutableKey) {
124
+ if (!this.immutableKey || !immutableKey) {
112
125
  hashValue.push(...generateOptionSegments(config));
113
126
  }
114
127
  const key = hash(hashValue);
115
- const handler = () => {
128
+ options.default = options.default ?? (() => null);
129
+ const _handler = () => {
116
130
  if (_cachedController.get(key)) {
117
131
  _cachedController.get(key)?.abort?.();
118
132
  }
119
133
  return $fetch(url, {
120
134
  signal: _cachedController.get(key)?.signal,
121
135
  ...defaultOptions,
122
- ...restAjaxConfig
136
+ ..._config
123
137
  });
124
138
  };
125
139
  const nuxtApp = useNuxtApp();
@@ -135,7 +149,7 @@ export class CustomFetch {
135
149
  asyncData.status.value = "pending";
136
150
  const promise = new Promise((resolve, reject) => {
137
151
  try {
138
- resolve(handler());
152
+ resolve(_handler());
139
153
  _cachedController.set(key, controller);
140
154
  } catch (err) {
141
155
  reject(err);
@@ -163,7 +177,7 @@ export class CustomFetch {
163
177
  });
164
178
  return promise;
165
179
  }
166
- return useAsyncData(config.key || key, handler, options);
180
+ return useAsyncData(config.key || key, _handler, options);
167
181
  }
168
182
  get(url, config = {}, options) {
169
183
  return this.http(url, { ...config, method: "GET" }, options);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-custom-fetch",
3
- "version": "2.0.3",
3
+ "version": "2.0.4",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {