@superutils/fetch 1.5.1 → 1.5.3

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/index.js CHANGED
@@ -1,3 +1,12 @@
1
+ // src/index.ts
2
+ import {
3
+ ResolveError,
4
+ ResolveIgnored,
5
+ TIMEOUT_FALLBACK,
6
+ TIMEOUT_MAX as TIMEOUT_MAX2,
7
+ TimeoutPromise
8
+ } from "@superutils/promise";
9
+
1
10
  // src/createClient.ts
2
11
  import { deferredCallback } from "@superutils/promise";
3
12
 
@@ -6,10 +15,11 @@ import {
6
15
  fallbackIfFails as fallbackIfFails3,
7
16
  isError,
8
17
  isFn as isFn4,
18
+ isObj as isObj2,
9
19
  isPromise,
10
20
  isUrlValid
11
21
  } from "@superutils/core";
12
- import { timeout as PromisE_timeout } from "@superutils/promise";
22
+ import { timeout as PromisE_timeout, TIMEOUT_MAX } from "@superutils/promise";
13
23
 
14
24
  // src/executeInterceptors.ts
15
25
  import { fallbackIfFails, isFn } from "@superutils/core";
@@ -166,20 +176,25 @@ var FetchError = class _FetchError extends Error {
166
176
  }
167
177
  };
168
178
 
169
- // src/types/index.ts
170
- import {
171
- ResolveError,
172
- ResolveIgnored,
173
- TIMEOUT_FALLBACK,
174
- TIMEOUT_MAX,
175
- TimeoutPromise
176
- } from "@superutils/promise";
177
-
178
179
  // src/fetch.ts
179
180
  var fetch = (url, options = {}) => {
180
181
  var _a, _b, _c;
182
+ if (!isObj2(options)) options = {};
183
+ let fromPostClient = false;
184
+ if (options.fromPostClient) {
185
+ delete options.fromPostClient;
186
+ fromPostClient = true;
187
+ }
181
188
  let response;
182
- const opts = mergeOptions_default(fetch.defaults, options);
189
+ const opts = mergeOptions_default(
190
+ {
191
+ abortOnEarlyFinalize: fetch.defaults.abortOnEarlyFinalize,
192
+ errMsgs: fetch.defaults.errMsgs,
193
+ timeout: TIMEOUT_MAX,
194
+ validateUrl: false
195
+ },
196
+ options
197
+ );
183
198
  opts.abortCtrl = opts.abortCtrl instanceof AbortController ? opts.abortCtrl : new AbortController();
184
199
  (_a = opts.as) != null ? _a : opts.as = "response" /* response */;
185
200
  (_b = opts.method) != null ? _b : opts.method = "get";
@@ -210,29 +225,32 @@ var fetch = (url, options = {}) => {
210
225
  return PromisE_timeout(opts, async () => {
211
226
  var _a2, _b2, _c2, _d, _e;
212
227
  try {
213
- let contentType = headers.get("content-type");
214
- if (!contentType) {
215
- headers.set("content-type", ContentType.APPLICATION_JSON);
216
- contentType = ContentType.APPLICATION_JSON;
217
- }
228
+ opts.body = await fallbackIfFails3(
229
+ opts.body,
230
+ [],
231
+ (err) => Promise.reject(err)
232
+ );
218
233
  url = await executeInterceptors_default(
219
234
  url,
220
235
  abortCtrl.signal,
221
236
  (_a2 = opts.interceptors) == null ? void 0 : _a2.request,
222
237
  opts
223
238
  );
224
- const { body, errMsgs, validateUrl = true } = opts;
239
+ const { body, errMsgs, validateUrl = false } = opts;
225
240
  (_b2 = opts.signal) != null ? _b2 : opts.signal = abortCtrl.signal;
226
241
  if (validateUrl && !isUrlValid(url, false))
227
242
  throw new Error(errMsgs.invalidUrl);
228
- const shouldStringifyBody = [
229
- ContentType.APPLICATION_JSON,
230
- ContentType.APPLICATION_X_WWW_FORM_URLENCODED
231
- ].find((x) => contentType.includes(x)) && !["undefined", "string"].includes(typeof body);
232
- if (shouldStringifyBody)
233
- opts.body = JSON.stringify(
234
- isFn4(body) ? fallbackIfFails3(body, [], void 0) : body
235
- );
243
+ if (fromPostClient) {
244
+ let contentType = headers.get("content-type");
245
+ if (!contentType) {
246
+ headers.set("content-type", ContentType.APPLICATION_JSON);
247
+ contentType = ContentType.APPLICATION_JSON;
248
+ }
249
+ const shouldStringifyBody = ["delete", "patch", "post", "put"].includes(
250
+ `${opts.method}`.toLowerCase()
251
+ ) && !["undefined", "string"].includes(typeof body) && isObj2(body, true) && contentType === ContentType.APPLICATION_JSON;
252
+ if (shouldStringifyBody) opts.body = JSON.stringify(opts.body);
253
+ }
236
254
  response = await getResponse_default(url, opts);
237
255
  response = await executeInterceptors_default(
238
256
  response,
@@ -298,8 +316,8 @@ fetch.defaults = {
298
316
  response: [],
299
317
  result: []
300
318
  },
301
- timeout: 3e4,
302
- validateUrl: true
319
+ timeout: 6e4,
320
+ validateUrl: false
303
321
  };
304
322
  var interceptErr = async (err, url, options, response) => {
305
323
  var _a, _b, _c;
@@ -325,6 +343,7 @@ var createClient = (fixedOptions, commonOptions, commonDeferOptions) => {
325
343
  function client(url, options) {
326
344
  var _a;
327
345
  const mergedOptions = mergeOptions_default(
346
+ fetch_default.defaults,
328
347
  commonOptions,
329
348
  options,
330
349
  fixedOptions
@@ -338,6 +357,7 @@ var createClient = (fixedOptions, commonOptions, commonDeferOptions) => {
338
357
  const fetchCb = (...args) => {
339
358
  var _a, _b, _c;
340
359
  const mergedOptions = (_a = mergeOptions_default(
360
+ fetch_default.defaults,
341
361
  commonOptions,
342
362
  defaultOptions,
343
363
  defaultUrl === void 0 ? args[1] : args[0],
@@ -367,14 +387,16 @@ var createPostClient = (fixedOptions, commonOptions, commonDeferOptions) => {
367
387
  function client(url, data, options) {
368
388
  var _a, _b;
369
389
  const mergedOptions = mergeOptions_default(
390
+ fetch_default.defaults,
370
391
  commonOptions,
371
392
  options,
372
393
  fixedOptions
373
394
  // fixed options will always override other options
374
395
  );
375
396
  (_a = mergedOptions.as) != null ? _a : mergedOptions.as = "json" /* json */;
376
- mergedOptions.body = data;
397
+ mergedOptions.body = data != null ? data : mergedOptions.body;
377
398
  (_b = mergedOptions.method) != null ? _b : mergedOptions.method = "post";
399
+ mergedOptions.fromPostClient = true;
378
400
  return fetch_default(url, mergedOptions);
379
401
  }
380
402
  client.deferred = (deferOptions, defaultUrl, defaultData, defaultOptions) => {
@@ -384,6 +406,7 @@ var createPostClient = (fixedOptions, commonOptions, commonDeferOptions) => {
384
406
  if (defaultUrl !== void 0) args.splice(0, 0, defaultUrl);
385
407
  if (defaultData !== void 0) args.splice(1, 0, defaultData);
386
408
  const mergedOptions = (_a = mergeOptions_default(
409
+ fetch_default.defaults,
387
410
  commonOptions,
388
411
  defaultOptions,
389
412
  args[2],
@@ -395,6 +418,7 @@ var createPostClient = (fixedOptions, commonOptions, commonDeferOptions) => {
395
418
  _abortCtrl = new AbortController();
396
419
  mergedOptions.body = (_d = args[1]) != null ? _d : mergedOptions.body;
397
420
  (_e = mergedOptions.method) != null ? _e : mergedOptions.method = "post";
421
+ mergedOptions.fromPostClient = true;
398
422
  return fetch_default(args[0], mergedOptions);
399
423
  };
400
424
  return deferredCallback2(postCb, {
@@ -439,7 +463,7 @@ export {
439
463
  ResolveError,
440
464
  ResolveIgnored,
441
465
  TIMEOUT_FALLBACK,
442
- TIMEOUT_MAX,
466
+ TIMEOUT_MAX2 as TIMEOUT_MAX,
443
467
  TimeoutPromise,
444
468
  createClient,
445
469
  createPostClient,
package/package.json CHANGED
@@ -5,8 +5,8 @@
5
5
  },
6
6
  "description": "A lightweight `fetch` wrapper for browsers and Node.js, designed to simplify data fetching and reduce boilerplate.",
7
7
  "dependencies": {
8
- "@superutils/core": "^1.2.3",
9
- "@superutils/promise": "^1.3.1"
8
+ "@superutils/core": "^1.2.5",
9
+ "@superutils/promise": "^1.3.3"
10
10
  },
11
11
  "files": [
12
12
  "dist",
@@ -53,6 +53,6 @@
53
53
  "module": "./dist/index.js",
54
54
  "type": "module",
55
55
  "types": "./dist/index.d.ts",
56
- "version": "1.5.1",
57
- "gitHead": "08b5530ef047409e70c4187b749615f6811405a9"
56
+ "version": "1.5.3",
57
+ "gitHead": "da664cdcf3ce784d931e94a73627a9b259ae6381"
58
58
  }