Skip to content

Commit

Permalink
Get rid of some dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jul 5, 2018
1 parent 2ee8124 commit 013e668
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
24 changes: 9 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {PassThrough, Transform} = require('stream');
const urlLib = require('url');
const fs = require('fs');
const querystring = require('querystring');
const URLSearchParamsGlobal = typeof URLSearchParams === 'undefined' ? require('url').URLSearchParams : URLSearchParams; // TODO: Use the `URL` global when targeting Node.js 10
const extend = require('extend');
const CacheableRequest = require('cacheable-request');
const duplexer3 = require('duplexer3');
Expand All @@ -16,11 +17,9 @@ const getStream = require('get-stream');
const timedOut = require('timed-out');
const urlParseLax = require('url-parse-lax');
const urlToOptions = require('url-to-options');
const lowercaseKeys = require('lowercase-keys');
const decompressResponse = require('decompress-response');
const mimicResponse = require('mimic-response');
const isRetryAllowed = require('is-retry-allowed');
const isURL = require('isurl');
const PCancelable = require('p-cancelable');
const pTimeout = require('p-timeout');
const pkg = require('./package.json');
Expand Down Expand Up @@ -526,15 +525,15 @@ function normalizeArguments(url, opts) {

try {
decodeURI(url);
} catch (err) {
} catch (_) {
throw new Error('Parameter `url` must contain valid UTF-8 character sequences');
}

url = urlParseLax(url);
if (url.auth) {
throw new Error('Basic authentication must be done with the `auth` option');
}
} else if (isURL.lenient(url)) {
} else if (is(url) === 'URL') {
url = urlToOptions(url);
}

Expand All @@ -545,28 +544,23 @@ function normalizeArguments(url, opts) {
...opts
};

const headers = lowercaseKeys(opts.headers);
if (!Object.keys(headers).includes('user-agent')) {
headers['user-agent'] = `${pkg.name}/${pkg.version} (https://github.com/sindresorhus/got)`;
}

for (const [key, value] of Object.entries(headers)) {
for (const [key, value] of Object.entries(opts.headers)) {
if (is.nullOrUndefined(value)) {
delete headers[key];
delete opts.headers[key];
continue;
}
}

opts.headers = headers;
opts.headers[key.toLowerCase()] = value;
}

if (opts.decompress && is.undefined(opts.headers['accept-encoding'])) {
opts.headers['accept-encoding'] = 'gzip, deflate';
}

const {query} = opts;

if (query) {
if (!is.string(query)) {
opts.query = querystring.stringify(query);
opts.query = (new URLSearchParamsGlobal(query)).toString();
}

opts.path = `${opts.path.split('?')[0]}?${opts.query}`;
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@
"extend": "^3.0.1",
"get-stream": "^3.0.0",
"is-retry-allowed": "^1.1.0",
"isurl": "^1.0.0-alpha5",
"lowercase-keys": "^1.0.0",
"mimic-response": "^1.0.0",
"p-cancelable": "^0.5.0",
"p-timeout": "^2.0.1",
Expand Down

0 comments on commit 013e668

Please sign in to comment.