Skip to content

Commit

Permalink
Provide a single param to request (issue node-fetch#1620)
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaume-fr committed Dec 22, 2022
1 parent 55a4870 commit 1bf62ab
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Expand Up @@ -91,7 +91,7 @@ export default async function fetch(url, options_) {
};

// Send request
const request_ = send(parsedURL.toString(), options);
const request_ = send(options);

if (signal) {
signal.addEventListener('abort', abortAndFinalize);
Expand Down
17 changes: 15 additions & 2 deletions src/request.js
Expand Up @@ -297,18 +297,31 @@ export const getNodeRequestOptions = request => {

const search = getSearch(parsedURL);

// Pass the full URL directly to request(), but overwrite the following
// options:
// Provide full options from URL (issue #1620)
const options = {
protocol: parsedURL.protocol,
hostname: parsedURL.hostname.startsWith("[") && parsedURL.hostname.endsWith("]") ? parsedURL.hostname.slice(1, -1) : parsedURL.hostname,
pathname: parsedURL.pathname,
// Overwrite search to retain trailing ? (issue #776)
path: parsedURL.pathname + search,
search: parsedURL.search,
hash: parsedURL.hash,
href: parsedURL.href,
// The following options are not expressed in the URL
method: request.method,
headers: headers[Symbol.for('nodejs.util.inspect.custom')](),
insecureHTTPParser: request.insecureHTTPParser,
agent
};

if (parsedURL.username || parsedURL.password) {
options.auth = `${decodeURIComponent(parsedURL.username)}:${decodeURIComponent(parsedURL.password)}`;
}

if (parsedURL.port !== '') {
options.port = Number(parsedURL.port);
}

return {
/** @type {URL} */
parsedURL,
Expand Down

0 comments on commit 1bf62ab

Please sign in to comment.