Comparing version
53
index.js
@@ -13,2 +13,3 @@ 'use strict'; | ||
const urlParseLax = require('url-parse-lax'); | ||
const urlToOptions = require('url-to-options'); | ||
const lowercaseKeys = require('lowercase-keys'); | ||
@@ -89,5 +90,10 @@ const decompressResponse = require('decompress-response'); | ||
setImmediate(() => { | ||
const response = typeof decompressResponse === 'function' && | ||
const response = opts.decompress === true && | ||
typeof decompressResponse === 'function' && | ||
req.method !== 'HEAD' ? decompressResponse(res) : res; | ||
if (!opts.decompress && ['gzip', 'deflate'].indexOf(res.headers['content-encoding']) !== -1) { | ||
opts.encoding = null; | ||
} | ||
response.redirectUrls = redirects; | ||
@@ -262,19 +268,11 @@ | ||
throw new TypeError(`Parameter \`url\` must be a string or object, not ${typeof url}`); | ||
} | ||
if (typeof url === 'string') { | ||
} else if (typeof url === 'string') { | ||
url = url.replace(/^unix:/, 'http://$&'); | ||
url = urlParseLax(url); | ||
if (url.auth) { | ||
throw new Error('Basic authentication must be done with auth option'); | ||
} | ||
} else if (isURL.lenient(url)) { | ||
url = urlToOptions(url); | ||
} | ||
if (isURL.lenient(url)) { | ||
url = urlParseLax(url.href); | ||
if (url.auth) { | ||
throw new Error('Basic authentication must be done with auth option'); | ||
} | ||
if (url.auth) { | ||
throw new Error('Basic authentication must be done with auth option'); | ||
} | ||
@@ -286,2 +284,3 @@ | ||
retries: 2, | ||
decompress: true, | ||
useElectronNet: true | ||
@@ -323,4 +322,5 @@ }, | ||
if ((opts.form || opts.json) && !isPlainObj(body)) { | ||
throw new TypeError('options.body must be a plain Object when options.form or options.json is used'); | ||
const canBodyBeStringified = isPlainObj(body) || Array.isArray(body); | ||
if ((opts.form || opts.json) && !canBodyBeStringified) { | ||
throw new TypeError('options.body must be a plain Object or Array when options.form or options.json is used'); | ||
} | ||
@@ -331,6 +331,6 @@ | ||
headers['content-type'] = headers['content-type'] || `multipart/form-data; boundary=${body.getBoundary()}`; | ||
} else if (opts.form && isPlainObj(body)) { | ||
} else if (opts.form && canBodyBeStringified) { | ||
headers['content-type'] = headers['content-type'] || 'application/x-www-form-urlencoded'; | ||
opts.body = querystring.stringify(body); | ||
} else if (opts.json && isPlainObj(body)) { | ||
} else if (opts.json && canBodyBeStringified) { | ||
headers['content-type'] = headers['content-type'] || 'application/json'; | ||
@@ -351,3 +351,3 @@ opts.body = JSON.stringify(body); | ||
if (opts.hostname === 'unix') { | ||
const matches = /(.+):(.+)/.exec(opts.path); | ||
const matches = /(.+?):(.+)/.exec(opts.path); | ||
@@ -399,3 +399,5 @@ if (matches) { | ||
const helpers = [ | ||
got.stream = (url, opts) => asStream(normalizeArguments(url, opts)); | ||
const methods = [ | ||
'get', | ||
@@ -409,10 +411,5 @@ 'post', | ||
helpers.forEach(el => { | ||
got[el] = (url, opts) => got(url, Object.assign({}, opts, {method: el})); | ||
}); | ||
got.stream = (url, opts) => asStream(normalizeArguments(url, opts)); | ||
for (const el of helpers) { | ||
got.stream[el] = (url, opts) => got.stream(url, Object.assign({}, opts, {method: el})); | ||
for (const method of methods) { | ||
got[method] = (url, opts) => got(url, Object.assign({}, opts, {method})); | ||
got.stream[method] = (url, opts) => got.stream(url, Object.assign({}, opts, {method})); | ||
} | ||
@@ -419,0 +416,0 @@ |
{ | ||
"name": "got", | ||
"version": "7.0.0", | ||
"version": "7.1.0", | ||
"description": "Simplified HTTP requests", | ||
@@ -61,10 +61,11 @@ "license": "MIT", | ||
"lowercase-keys": "^1.0.0", | ||
"p-cancelable": "^0.2.0", | ||
"p-cancelable": "^0.3.0", | ||
"p-timeout": "^1.1.1", | ||
"safe-buffer": "^5.0.1", | ||
"timed-out": "^4.0.0", | ||
"url-parse-lax": "^1.0.0" | ||
"url-parse-lax": "^1.0.0", | ||
"url-to-options": "^1.0.1" | ||
}, | ||
"devDependencies": { | ||
"ava": "^0.19.1", | ||
"ava": "^0.20.0", | ||
"coveralls": "^2.11.4", | ||
@@ -74,6 +75,6 @@ "form-data": "^2.1.1", | ||
"into-stream": "^3.0.0", | ||
"nyc": "^10.0.0", | ||
"nyc": "^11.0.2", | ||
"pem": "^1.4.4", | ||
"pify": "^2.3.0", | ||
"tempfile": "^1.1.1", | ||
"pify": "^3.0.0", | ||
"tempfile": "^2.0.0", | ||
"tempy": "^0.1.0", | ||
@@ -80,0 +81,0 @@ "universal-url": "^1.0.0-alpha", |
@@ -15,13 +15,21 @@ <h1 align="center"> | ||
It supports following redirects, promises, streams, retries, automagically handling gzip/deflate, canceling of requests, and some convenience options. | ||
Created because [`request`](https://github.com/request/request) is bloated *(several megabytes!)*. | ||
When used with Electron, it takes advantage of [`electron.net`](https://electron.atom.io/docs/api/net/). | ||
## Highlights | ||
- [Promise & stream API](#api) | ||
- [Request cancelation](#aborting-the-request) | ||
- [Follows redirects](#followredirect) | ||
- [Retries on network failure](#retries) | ||
- [Handles gzip/deflate](#decompress) | ||
- [Timeout handling](#timeout) | ||
- [Errors with metadata](#errors) | ||
- [JSON mode](#json) | ||
- [WHATWG URL support](#url) | ||
- [Electron support](#useelectronnet) | ||
## Install | ||
**WARNING: Node.js 4 or higher is required for got@6 and above.** For older Node.js versions use [got@5](https://github.com/sindresorhus/got/tree/v5.x). | ||
``` | ||
@@ -66,3 +74,3 @@ $ npm install --save got | ||
Type: `string`, `object` | ||
Type: `string` `Object` | ||
@@ -75,3 +83,3 @@ The URL to request as simple string, a [`http.request` options](https://nodejs.org/api/http.html#http_http_request_options_callback), or a [WHATWG `URL`](https://nodejs.org/api/url.html#url_class_url). | ||
Type: `object` | ||
Type: `Object` | ||
@@ -82,3 +90,3 @@ Any of the [`http.request`](http://nodejs.org/api/http.html#http_http_request_options_callback) options. | ||
Type: `string`, `buffer`, `readableStream` | ||
Type: `string` `Buffer` `stream.Readable` | ||
@@ -95,6 +103,6 @@ *This is mutually exclusive with stream mode.* | ||
Type: `string`, `null`<br> | ||
Type: `string` `null`<br> | ||
Default: `'utf8'` | ||
Encoding to be used on `setEncoding` of the response data. If `null`, the body is returned as a Buffer. | ||
[Encoding](https://nodejs.org/api/buffer.html#buffer_buffers_and_character_encodings) to be used on `setEncoding` of the response data. If `null`, the body is returned as a Buffer. | ||
@@ -110,3 +118,3 @@ ###### form | ||
`body` must be a plain object and will be stringified. | ||
`body` must be a plain object or array and will be stringified. | ||
@@ -124,7 +132,7 @@ ###### json | ||
`body` must be a plain object and will be stringified. | ||
`body` must be a plain object or array and will be stringified. | ||
###### query | ||
Type: `string`, `object`<br> | ||
Type: `string` `Object`<br> | ||
@@ -135,3 +143,3 @@ Query string object that will be added to the request URL. This will override the query string in `url`. | ||
Type: `number`, `object` | ||
Type: `number` `Object` | ||
@@ -144,3 +152,3 @@ Milliseconds to wait for the server to end the response before aborting request with `ETIMEDOUT` error. | ||
Type: `number`, `function`<br> | ||
Type: `number` `Function`<br> | ||
Default: `2` | ||
@@ -164,2 +172,11 @@ | ||
###### decompress | ||
Type: `boolean`<br> | ||
Default: `true` | ||
Decompress the response automatically. | ||
If this is disabled, a compressed response is returned as a `Buffer`. This may be useful if you want to handle decompression yourself or stream the raw compressed data. | ||
###### useElectronNet | ||
@@ -166,0 +183,0 @@ |
28140
1.98%432
4.1%14
7.69%+ Added
+ Added
+ Added
- Removed
Updated