Repository: sindresorhus/got
Pull request #541 (2018-08-02 09:11:42)

Ignore JSON option when using `got.stream()`


Patch:
diff --git a/advanced-creation.md b/advanced-creation.md
index 0bef1fc..4335c85 100644
--- a/advanced-creation.md
+++ b/advanced-creation.md
@@ -38,7 +38,7 @@ To use the default handler, just omit specifying this.
 
 ###### next()
 
-Normalizes arguments and returns a `Promise` or a `Stream` depending on [`options.stream`](readme.md#stream).
+Returns a `Promise` or a `Stream` depending on [`options.stream`](readme.md#stream).
 
 ```js
 const settings = {
diff --git a/readme.md b/readme.md
index 2aa93df..1a7df57 100644
--- a/readme.md
+++ b/readme.md
@@ -137,7 +137,7 @@ Returns a `Stream` instead of a `Promise`. This is equivalent to calling `got.st
 
 Type: `string` `Buffer` `stream.Readable` [`form-data` instance](https://github.com/form-data/form-data)
 
-*This is mutually exclusive with stream mode.*
+*If you provide this option, `got.stream()` will be read-only.*
 
 Body that will be sent with a `POST` request.
 
@@ -157,7 +157,7 @@ Default: `'utf8'`
 Type: `boolean`<br>
 Default: `false`
 
-*This is mutually exclusive with stream mode.*
+*If you provide this option, `got.stream()` will be read-only.*
 
 If set to `true` and `Content-Type` header is not set, it will be set to `application/x-www-form-urlencoded`.
 
@@ -168,7 +168,7 @@ If set to `true` and `Content-Type` header is not set, it will be set to `applic
 Type: `boolean`<br>
 Default: `false`
 
-*This is mutually exclusive with stream mode.*
+*If you use `got.stream()`, this option will be ignored.*
 
 If set to `true` and `Content-Type` header is not set, it will be set to `application/json`.
 
@@ -749,7 +749,7 @@ const createTestServer = require('create-test-server');
 
 ### User Agent
 
-It's a good idea to set the `'user-agent'` header so the provider can more easily see how their resource is used. By default, it's the URL to this repo. You can omit this header by setting it to `null` or `undefined`.
+It's a good idea to set the `'user-agent'` header so the provider can more easily see how their resource is used. By default, it's the URL to this repo. You can omit this header by setting it to `null`.
 
 ```js
 const got = require('got');
diff --git a/source/as-stream.js b/source/as-stream.js
index e878b41..eb44047 100644
--- a/source/as-stream.js
+++ b/source/as-stream.js
@@ -14,10 +14,6 @@ module.exports = options => {
 
 	options.gotRetry.retries = () => 0;
 
-	if (options.json) {
-		throw new Error('Got can not be used as a stream when the `json` option is used');
-	}
-
 	if (options.body) {
 		proxy.write = () => {
 			throw new Error('Got\'s stream is not writable when the `body` option is used');
diff --git a/source/normalize-arguments.js b/source/normalize-arguments.js
index d224366..36d4e50 100644
--- a/source/normalize-arguments.js
+++ b/source/normalize-arguments.js
@@ -49,6 +49,10 @@ module.exports = (url, options, defaults) => {
 		...options
 	};
 
+	if (options.stream && options.json) {
+		options.json = false;
+	}
+
 	if (options.decompress && is.undefined(options.headers['accept-encoding'])) {
 		options.headers['accept-encoding'] = 'gzip, deflate';
 	}


Test patch:
diff --git a/test/arguments.js b/test/arguments.js
index 21a4b93..77d2623 100644
--- a/test/arguments.js
+++ b/test/arguments.js
@@ -109,9 +109,9 @@ test('should return streams when using stream option', async t => {
 	t.is(data.toString(), 'ok');
 });
 
-test('should not allow stream and JSON option at the same time', async t => {
-	const error = await t.throws(got(`${s.url}/stream`, {stream: true, json: true}));
-	t.is(error.message, 'Got can not be used as a stream when the `json` option is used');
+test('should ignore JSON option when using stream option', async t => {
+	const data = await pEvent(got(`${s.url}/stream`, {stream: true, json: true}), 'data');
+	t.is(data.toString(), 'ok');
 });
 
 test('throws TypeError when `url` is passed as an option', async t => {
diff --git a/test/stream.js b/test/stream.js
index 5e1defa..12f7341 100644
--- a/test/stream.js
+++ b/test/stream.js
@@ -41,10 +41,8 @@ test.after('cleanup', async () => {
 	await s.close();
 });
 
-test('option.json can not be used', t => {
-	t.throws(() => {
-		got.stream(s.url, {json: true});
-	}, 'Got can not be used as a stream when the `json` option is used');
+test('options.json is ignored', t => {
+	t.notThrows(() => got.stream(s.url, {json: true}));
 });
 
 test('returns readable stream', async t => {