Skip to content

Commit 4d92eb6

Browse files
szmarczaksindresorhus
authored andcommitted
Ignore JSON option when using got.stream() (#541)
1 parent 6ba9e68 commit 4d92eb6

File tree

5 files changed

+12
-14
lines changed

5 files changed

+12
-14
lines changed

readme.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ Returns a `Stream` instead of a `Promise`. This is equivalent to calling `got.st
137137

138138
Type: `string` `Buffer` `stream.Readable` [`form-data` instance](https://github.com/form-data/form-data)
139139

140-
*This is mutually exclusive with stream mode.*
140+
*If you provide this option, `got.stream()` will be read-only.*
141141

142142
Body that will be sent with a `POST` request.
143143

@@ -157,7 +157,7 @@ Default: `'utf8'`
157157
Type: `boolean`<br>
158158
Default: `false`
159159

160-
*This is mutually exclusive with stream mode.*
160+
*If you provide this option, `got.stream()` will be read-only.*
161161

162162
If set to `true` and `Content-Type` header is not set, it will be set to `application/x-www-form-urlencoded`.
163163

@@ -168,7 +168,7 @@ If set to `true` and `Content-Type` header is not set, it will be set to `applic
168168
Type: `boolean`<br>
169169
Default: `false`
170170

171-
*This is mutually exclusive with stream mode.*
171+
*If you use `got.stream()`, this option will be ignored.*
172172

173173
If set to `true` and `Content-Type` header is not set, it will be set to `application/json`.
174174

source/as-stream.js

-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ module.exports = options => {
1414

1515
options.gotRetry.retries = () => 0;
1616

17-
if (options.json) {
18-
throw new Error('Got can not be used as a stream when the `json` option is used');
19-
}
20-
2117
if (options.body) {
2218
proxy.write = () => {
2319
throw new Error('Got\'s stream is not writable when the `body` option is used');

source/normalize-arguments.js

+4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ module.exports = (url, options, defaults) => {
4949
...options
5050
};
5151

52+
if (options.stream && options.json) {
53+
options.json = false;
54+
}
55+
5256
if (options.decompress && is.undefined(options.headers['accept-encoding'])) {
5357
options.headers['accept-encoding'] = 'gzip, deflate';
5458
}

test/arguments.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ test('should return streams when using stream option', async t => {
109109
t.is(data.toString(), 'ok');
110110
});
111111

112-
test('should not allow stream and JSON option at the same time', async t => {
113-
const error = await t.throws(got(`${s.url}/stream`, {stream: true, json: true}));
114-
t.is(error.message, 'Got can not be used as a stream when the `json` option is used');
112+
test('should ignore JSON option when using stream option', async t => {
113+
const data = await pEvent(got(`${s.url}/stream`, {stream: true, json: true}), 'data');
114+
t.is(data.toString(), 'ok');
115115
});
116116

117117
test('throws TypeError when `url` is passed as an option', async t => {

test/stream.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,8 @@ test.after('cleanup', async () => {
4141
await s.close();
4242
});
4343

44-
test('option.json can not be used', t => {
45-
t.throws(() => {
46-
got.stream(s.url, {json: true});
47-
}, 'Got can not be used as a stream when the `json` option is used');
44+
test('options.json is ignored', t => {
45+
t.notThrows(() => got.stream(s.url, {json: true}));
4846
});
4947

5048
test('returns readable stream', async t => {

0 commit comments

Comments
 (0)