defaultOptionsValidator-CXwrNjme.cjs 1.5 KB

123456789101112131415161718192021222324252627282930313233
  1. "use strict";
  2. const isReactNative = typeof navigator > "u" ? !1 : navigator.product === "ReactNative", defaultOptions = { timeout: isReactNative ? 6e4 : 12e4 }, processOptions = function(opts) {
  3. const options = {
  4. ...defaultOptions,
  5. ...typeof opts == "string" ? { url: opts } : opts
  6. }, { searchParams } = new URL(options.url, "http://localhost");
  7. if (options.timeout = normalizeTimeout(options.timeout), options.query) {
  8. for (const [key, value] of Object.entries(options.query))
  9. if (value !== void 0)
  10. if (Array.isArray(value))
  11. for (const v of value)
  12. searchParams.append(key, v);
  13. else
  14. searchParams.append(key, value);
  15. }
  16. const [url] = options.url.split("?"), search = searchParams.toString();
  17. return search && (options.url = `${url}?${search}`), options.method = options.body && !options.method ? "POST" : (options.method || "GET").toUpperCase(), options;
  18. };
  19. function normalizeTimeout(time) {
  20. if (time === !1 || time === 0)
  21. return !1;
  22. if (time.connect || time.socket)
  23. return time;
  24. const delay = Number(time);
  25. return isNaN(delay) ? normalizeTimeout(defaultOptions.timeout) : { connect: delay, socket: delay };
  26. }
  27. const validUrl = /^https?:\/\//i, validateOptions = function(options) {
  28. if (!validUrl.test(options.url))
  29. throw new Error(`"${options.url}" is not a valid URL`);
  30. };
  31. exports.processOptions = processOptions;
  32. exports.validateOptions = validateOptions;
  33. //# sourceMappingURL=defaultOptionsValidator-CXwrNjme.cjs.map