Comparing version 8.4.13 to 8.4.14-canary.0
@@ -5,5 +5,6 @@ const isReactNative = typeof navigator > "u" ? !1 : navigator.product === "ReactNative", defaultOptions = { timeout: isReactNative ? 6e4 : 12e4 }, processOptions = function(opts) { | ||
...typeof opts == "string" ? { url: opts } : opts | ||
}, { searchParams } = new URL(options.url, "http://localhost"); | ||
}; | ||
if (options.timeout = normalizeTimeout(options.timeout), options.query) { | ||
for (const [key, value] of Object.entries(options.query)) | ||
const { url, searchParams } = splitUrl(options.url); | ||
for (const [key, value] of Object.entries(options.query)) { | ||
if (value !== void 0) | ||
@@ -15,6 +16,29 @@ if (Array.isArray(value)) | ||
searchParams.append(key, value); | ||
const search = searchParams.toString(); | ||
search && (options.url = `${url}?${search}`); | ||
} | ||
} | ||
const [url] = options.url.split("?"), search = searchParams.toString(); | ||
return search && (options.url = `${url}?${search}`), options.method = options.body && !options.method ? "POST" : (options.method || "GET").toUpperCase(), options; | ||
return options.method = options.body && !options.method ? "POST" : (options.method || "GET").toUpperCase(), options; | ||
}; | ||
function splitUrl(url) { | ||
const qIndex = url.indexOf("?"); | ||
if (qIndex === -1) | ||
return { url, searchParams: new URLSearchParams() }; | ||
const base = url.slice(0, qIndex), qs = url.slice(qIndex + 1), searchParams = new URLSearchParams(qs); | ||
if (typeof searchParams.set == "function") | ||
return { url: base, searchParams }; | ||
if (typeof decodeURIComponent != "function") | ||
throw new Error( | ||
"Broken `URLSearchParams` implementation, and `decodeURIComponent` is not defined" | ||
); | ||
const params = new URLSearchParams(); | ||
for (const pair of qs.split("&")) { | ||
const [key, value] = pair.split("="); | ||
key && params.append(decodeQueryParam(key), decodeQueryParam(value || "")); | ||
} | ||
return { url: base, searchParams: params }; | ||
} | ||
function decodeQueryParam(value) { | ||
return decodeURIComponent(value.replace(/\+/g, " ")); | ||
} | ||
function normalizeTimeout(time) { | ||
@@ -21,0 +45,0 @@ if (time === !1 || time === 0) |
{ | ||
"name": "get-it", | ||
"version": "8.4.13", | ||
"version": "8.4.14-canary.0", | ||
"description": "Generic HTTP request library for node, browsers and workers", | ||
@@ -145,5 +145,4 @@ "keywords": [ | ||
"publishConfig": { | ||
"access": "public", | ||
"provenance": true | ||
"access": "public" | ||
} | ||
} |
@@ -14,5 +14,2 @@ import type {MiddlewareHooks, RequestOptions} from '../types' | ||
// Allow parsing relative URLs by setting the origin to `http://localhost` | ||
const {searchParams} = new URL(options.url, 'http://localhost') | ||
// Normalize timeouts | ||
@@ -23,2 +20,4 @@ options.timeout = normalizeTimeout(options.timeout) | ||
if (options.query) { | ||
const {url, searchParams} = splitUrl(options.url) | ||
for (const [key, value] of Object.entries(options.query)) { | ||
@@ -34,10 +33,10 @@ if (value !== undefined) { | ||
} | ||
// Merge back params into url | ||
const search = searchParams.toString() | ||
if (search) { | ||
options.url = `${url}?${search}` | ||
} | ||
} | ||
} | ||
// Merge back params into url | ||
const [url] = options.url.split('?') | ||
const search = searchParams.toString() | ||
if (search) { | ||
options.url = `${url}?${search}` | ||
} | ||
@@ -51,2 +50,50 @@ // Implicit POST if we have not specified a method but have a body | ||
/** | ||
* Given a string URL, extracts the query string and URL from each other, and returns them. | ||
* Note that we cannot use the `URL` constructor because of old React Native versions which are | ||
* majorly broken and returns incorrect results: | ||
* | ||
* (`new URL('http://foo/?a=b').toString()` == 'http://foo/?a=b/') | ||
*/ | ||
function splitUrl(url: string): {url: string; searchParams: URLSearchParams} { | ||
const qIndex = url.indexOf('?') | ||
if (qIndex === -1) { | ||
return {url, searchParams: new URLSearchParams()} | ||
} | ||
const base = url.slice(0, qIndex) | ||
const qs = url.slice(qIndex + 1) | ||
const searchParams = new URLSearchParams(qs) | ||
// Buggy React Native versions do not implement `.set()`, so if we have one, | ||
// we should be able to use a functioning `URLSearchParams` implementation | ||
if (typeof searchParams.set === 'function') { | ||
return {url: base, searchParams} | ||
} | ||
// Sanity-check; we do not know of any environment where this is the case, | ||
// but if it is, we should not proceed without giving a descriptive error | ||
if (typeof decodeURIComponent !== 'function') { | ||
throw new Error( | ||
'Broken `URLSearchParams` implementation, and `decodeURIComponent` is not defined', | ||
) | ||
} | ||
// Another brokenness in React Native: `URLSearchParams` does not accept a string argument, | ||
// so we'll have do attempt to destructure the query string ourselves :( | ||
const params = new URLSearchParams() | ||
for (const pair of qs.split('&')) { | ||
const [key, value] = pair.split('=') | ||
if (key) { | ||
params.append(decodeQueryParam(key), decodeQueryParam(value || '')) | ||
} | ||
} | ||
return {url: base, searchParams: params} | ||
} | ||
function decodeQueryParam(value: string): string { | ||
return decodeURIComponent(value.replace(/\+/g, ' ')) | ||
} | ||
function normalizeTimeout(time: RequestOptions['timeout']) { | ||
@@ -53,0 +100,0 @@ if (time === false || time === 0) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
501670
5560
1