Skip to content

Commit 4a8722b

Browse files
committedNov 18, 2018
Minor code style tweaks
1 parent c8e358f commit 4a8722b

11 files changed

+129
-102
lines changed
 

‎source/errors.js

+24-24
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const PCancelable = require('p-cancelable');
55
const is = require('@sindresorhus/is');
66

77
class GotError extends Error {
8-
constructor(message, error, opts) {
8+
constructor(message, error, options) {
99
super(message);
1010
Error.captureStackTrace(this, this.constructor);
1111
this.name = 'GotError';
@@ -15,51 +15,51 @@ class GotError extends Error {
1515
}
1616

1717
Object.assign(this, {
18-
host: opts.host,
19-
hostname: opts.hostname,
20-
method: opts.method,
21-
path: opts.path,
22-
socketPath: opts.socketPath,
23-
protocol: opts.protocol,
24-
url: opts.href
18+
host: options.host,
19+
hostname: options.hostname,
20+
method: options.method,
21+
path: options.path,
22+
socketPath: options.socketPath,
23+
protocol: options.protocol,
24+
url: options.href
2525
});
2626
}
2727
}
2828

2929
module.exports.GotError = GotError;
3030

3131
module.exports.CacheError = class extends GotError {
32-
constructor(error, opts) {
33-
super(error.message, error, opts);
32+
constructor(error, options) {
33+
super(error.message, error, options);
3434
this.name = 'CacheError';
3535
}
3636
};
3737

3838
module.exports.RequestError = class extends GotError {
39-
constructor(error, opts) {
40-
super(error.message, error, opts);
39+
constructor(error, options) {
40+
super(error.message, error, options);
4141
this.name = 'RequestError';
4242
}
4343
};
4444

4545
module.exports.ReadError = class extends GotError {
46-
constructor(error, opts) {
47-
super(error.message, error, opts);
46+
constructor(error, options) {
47+
super(error.message, error, options);
4848
this.name = 'ReadError';
4949
}
5050
};
5151

5252
module.exports.ParseError = class extends GotError {
53-
constructor(error, statusCode, opts, data) {
54-
super(`${error.message} in "${urlLib.format(opts)}": \n${data.slice(0, 77)}...`, error, opts);
53+
constructor(error, statusCode, options, data) {
54+
super(`${error.message} in "${urlLib.format(options)}": \n${data.slice(0, 77)}...`, error, options);
5555
this.name = 'ParseError';
5656
this.statusCode = statusCode;
5757
this.statusMessage = http.STATUS_CODES[this.statusCode];
5858
}
5959
};
6060

6161
module.exports.HTTPError = class extends GotError {
62-
constructor(response, opts) {
62+
constructor(response, options) {
6363
const {statusCode} = response;
6464
let {statusMessage} = response;
6565

@@ -68,7 +68,7 @@ module.exports.HTTPError = class extends GotError {
6868
} else {
6969
statusMessage = http.STATUS_CODES[statusCode];
7070
}
71-
super(`Response code ${statusCode} (${statusMessage})`, {}, opts);
71+
super(`Response code ${statusCode} (${statusMessage})`, {}, options);
7272
this.name = 'HTTPError';
7373
this.statusCode = statusCode;
7474
this.statusMessage = statusMessage;
@@ -78,8 +78,8 @@ module.exports.HTTPError = class extends GotError {
7878
};
7979

8080
module.exports.MaxRedirectsError = class extends GotError {
81-
constructor(statusCode, redirectUrls, opts) {
82-
super('Redirected 10 times. Aborting.', {}, opts);
81+
constructor(statusCode, redirectUrls, options) {
82+
super('Redirected 10 times. Aborting.', {}, options);
8383
this.name = 'MaxRedirectsError';
8484
this.statusCode = statusCode;
8585
this.statusMessage = http.STATUS_CODES[this.statusCode];
@@ -88,15 +88,15 @@ module.exports.MaxRedirectsError = class extends GotError {
8888
};
8989

9090
module.exports.UnsupportedProtocolError = class extends GotError {
91-
constructor(opts) {
92-
super(`Unsupported protocol "${opts.protocol}"`, {}, opts);
91+
constructor(options) {
92+
super(`Unsupported protocol "${options.protocol}"`, {}, options);
9393
this.name = 'UnsupportedProtocolError';
9494
}
9595
};
9696

9797
module.exports.TimeoutError = class extends GotError {
98-
constructor(error, opts) {
99-
super(error.message, {code: 'ETIMEDOUT'}, opts);
98+
constructor(error, options) {
99+
super(error.message, {code: 'ETIMEDOUT'}, options);
100100
this.name = 'TimeoutError';
101101
this.event = error.event;
102102
}

‎source/request-as-event-emitter.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -119,19 +119,19 @@ module.exports = (options, input) => {
119119

120120
redirects.push(redirectString);
121121

122-
const redirectOpts = {
122+
const redirectOptions = {
123123
...options,
124124
...urlToOptions(redirectURL)
125125
};
126126

127127
for (const hook of options.hooks.beforeRedirect) {
128128
// eslint-disable-next-line no-await-in-loop
129-
await hook(redirectOpts);
129+
await hook(redirectOptions);
130130
}
131131

132-
emitter.emit('redirect', response, redirectOpts);
132+
emitter.emit('redirect', response, redirectOptions);
133133

134-
await get(redirectOpts);
134+
await get(redirectOptions);
135135
return;
136136
}
137137
}
@@ -201,17 +201,17 @@ module.exports = (options, input) => {
201201

202202
if (options.cache) {
203203
const cacheableRequest = new CacheableRequest(fn.request, options.cache);
204-
const cacheReq = cacheableRequest(options, handleResponse);
204+
const cacheRequest = cacheableRequest(options, handleResponse);
205205

206-
cacheReq.once('error', error => {
206+
cacheRequest.once('error', error => {
207207
if (error instanceof CacheableRequest.RequestError) {
208208
emitter.emit('error', new RequestError(error, options));
209209
} else {
210210
emitter.emit('error', new CacheError(error, options));
211211
}
212212
});
213213

214-
cacheReq.once('request', handleRequest);
214+
cacheRequest.once('request', handleRequest);
215215
} else {
216216
// Catches errors thrown by calling fn.request(...)
217217
try {

‎test/arguments.js

+39-21
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,27 @@ test.after('cleanup', async () => {
3636
});
3737

3838
test('url is required', async t => {
39-
const error = await t.throwsAsync(got());
40-
t.is(error.message, 'Parameter `url` must be a string or object, not undefined');
39+
await t.throwsAsync(
40+
got(),
41+
{
42+
message: 'Parameter `url` must be a string or object, not undefined'
43+
}
44+
);
4145
});
4246

4347
test('url should be utf-8 encoded', async t => {
44-
const error = await t.throwsAsync(got(`${s.url}/%D2%E0%EB%EB%E8%ED`));
45-
t.is(error.message, 'URI malformed');
48+
await t.throwsAsync(
49+
got(`${s.url}/%D2%E0%EB%EB%E8%ED`),
50+
{
51+
message: 'URI malformed'
52+
}
53+
);
4654
});
4755

4856
test('string url with query is preserved', async t => {
4957
const path = '/?test=http://example.com?foo=bar';
50-
const response = await got(`${s.url}${path}`);
51-
t.is(response.body, path);
58+
const {body} = await got(`${s.url}${path}`);
59+
t.is(body, path);
5260
});
5361

5462
test('options are optional', async t => {
@@ -81,11 +89,13 @@ test('requestUrl with url.parse object as first argument', async t => {
8189
t.is((await got(parse(`${s.url}/test`))).requestUrl, `${s.url}/test`);
8290
});
8391

84-
test('overrides querystring from opts', async t => {
85-
const response = await got(
92+
test('overrides querystring from options', async t => {
93+
const {body} = await got(
8694
`${s.url}/?drop=this`,
8795
{
88-
query: {test: 'wow'},
96+
query: {
97+
test: 'wow'
98+
},
8999
cache: {
90100
get(key) {
91101
t.is(key, `cacheable-request:GET:${s.url}/?test=wow`);
@@ -96,16 +106,18 @@ test('overrides querystring from opts', async t => {
96106
}
97107
}
98108
);
99-
t.is(response.body, '/?test=wow');
109+
110+
t.is(body, '/?test=wow');
100111
});
101112

102113
test('escapes query parameter values', async t => {
103-
const response = await got(`${s.url}`, {
114+
const {body} = await got(`${s.url}`, {
104115
query: {
105116
test: 'it’s ok'
106117
}
107118
});
108-
t.is(response.body, '/?test=it%E2%80%99s+ok');
119+
120+
t.is(body, '/?test=it%E2%80%99s+ok');
109121
});
110122

111123
test('the `query` option can be a URLSearchParams', async t => {
@@ -119,18 +131,24 @@ test('should ignore empty query object', async t => {
119131
});
120132

121133
test('should throw with auth in url string', async t => {
122-
const error = await t.throwsAsync(got('https://test:45d3ps453@account.myservice.com/api/token'));
123-
t.is(error.message, 'Basic authentication must be done with the `auth` option');
134+
await t.throwsAsync(
135+
got('https://test:45d3ps453@account.myservice.com/api/token'),
136+
{
137+
message: 'Basic authentication must be done with the `auth` option'
138+
}
139+
);
124140
});
125141

126142
test('does not throw with auth in url object', async t => {
127-
await t.notThrowsAsync(got({
128-
auth: 'foo:bar',
129-
hostname: s.host,
130-
port: s.port,
131-
protocol: 'http:',
132-
path: '/test'
133-
}));
143+
await t.notThrowsAsync(
144+
got({
145+
auth: 'foo:bar',
146+
hostname: s.host,
147+
port: s.port,
148+
protocol: 'http:',
149+
path: '/test'
150+
})
151+
);
134152
});
135153

136154
test('should throw when body is set to object', async t => {

‎test/cache.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ test('doesn\'t cache response when received HTTP error', async t => {
122122
const endpoint = '/first-error';
123123
const cache = new Map();
124124

125-
const response = await got(s.url + endpoint, {cache, throwHttpErrors: false});
126-
t.is(response.statusCode, 200);
127-
t.deepEqual(response.body, 'ok');
125+
const {statusCode, body} = await got(s.url + endpoint, {cache, throwHttpErrors: false});
126+
t.is(statusCode, 200);
127+
t.deepEqual(body, 'ok');
128128
});

‎test/cookies.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,16 @@ test('no unhandled errors', async t => {
108108
connection.end('blah');
109109
}).listen(0);
110110

111-
const error = 'snap!';
111+
const message = 'snap!';
112112

113-
const opts = {
113+
const options = {
114114
cookieJar: {
115115
setCookie: () => {},
116-
getCookieString: (_, __, cb) => cb(new Error(error))
116+
getCookieString: (_, __, cb) => cb(new Error(message))
117117
}
118118
};
119-
await t.throwsAsync(got(`http://127.0.0.1:${server.address().port}`, opts), error);
119+
120+
await t.throwsAsync(got(`http://127.0.0.1:${server.address().port}`, options), {message});
120121
await delay(500);
121122
t.pass();
122123

‎test/error.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -123,24 +123,30 @@ test('http.request error', async t => {
123123
request: () => {
124124
throw new TypeError('The header content contains invalid characters');
125125
}
126-
}), {instanceOf: got.RequestError, message: 'The header content contains invalid characters'});
126+
}), {
127+
instanceOf: got.RequestError,
128+
message: 'The header content contains invalid characters'
129+
});
127130
});
128131

129132
test('http.request pipe error', async t => {
130-
const error = 'snap!';
133+
const message = 'snap!';
131134

132135
await t.throwsAsync(got(s.url, {
133-
request: (...opts) => {
134-
const modified = http.request(...opts);
136+
request: (...options) => {
137+
const modified = http.request(...options);
135138
modified.end = () => {
136139
modified.abort();
137-
throw new Error(error);
140+
throw new Error(message);
138141
};
139142

140143
return modified;
141144
},
142145
throwHttpErrors: false
143-
}), {instanceOf: got.RequestError, message: error});
146+
}), {
147+
instanceOf: got.RequestError,
148+
message
149+
});
144150
});
145151

146152
test('http.request error through CacheableRequest', async t => {
@@ -149,7 +155,10 @@ test('http.request error through CacheableRequest', async t => {
149155
throw new TypeError('The header content contains invalid characters');
150156
},
151157
cache: new Map()
152-
}), {instanceOf: got.RequestError, message: 'The header content contains invalid characters'});
158+
}), {
159+
instanceOf: got.RequestError,
160+
message: 'The header content contains invalid characters'
161+
});
153162
});
154163

155164
test('catch error in mimicResponse', async t => {

‎test/gzip.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ test('handles gzip error - stream', async t => {
7878
});
7979

8080
test('decompress option opts out of decompressing', async t => {
81-
const response = await got(s.url, {decompress: false});
82-
t.true(Buffer.compare(response.body, gzipData) === 0);
81+
const {body} = await got(s.url, {decompress: false});
82+
t.is(Buffer.compare(body, gzipData), 0);
8383
});
8484

8585
test('decompress option doesn\'t alter encoding of uncompressed responses', async t => {
86-
const response = await got(`${s.url}/uncompressed`, {decompress: false});
87-
t.is(response.body, testContentUncompressed);
86+
const {body} = await got(`${s.url}/uncompressed`, {decompress: false});
87+
t.is(body, testContentUncompressed);
8888
});
8989

9090
test('preserve headers property', async t => {

‎test/hooks.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ test.after('cleanup', async () => {
6161
});
6262

6363
test('async hooks', async t => {
64-
const response = await got(s.url, {
64+
const {body} = await got(s.url, {
6565
json: true,
6666
hooks: {
6767
beforeRequest: [
@@ -72,7 +72,7 @@ test('async hooks', async t => {
7272
]
7373
}
7474
});
75-
t.is(response.body.foo, 'bar');
75+
t.is(body.foo, 'bar');
7676
});
7777

7878
test('catches thrown errors', async t => {
@@ -144,7 +144,7 @@ test('beforeRequest', async t => {
144144
});
145145

146146
test('beforeRequest allows modifications', async t => {
147-
const response = await got(s.url, {
147+
const {body} = await got(s.url, {
148148
json: true,
149149
hooks: {
150150
beforeRequest: [
@@ -154,7 +154,7 @@ test('beforeRequest allows modifications', async t => {
154154
]
155155
}
156156
});
157-
t.is(response.body.foo, 'bar');
157+
t.is(body.foo, 'bar');
158158
});
159159

160160
test('beforeRedirect', async t => {
@@ -172,7 +172,7 @@ test('beforeRedirect', async t => {
172172
});
173173

174174
test('beforeRedirect allows modifications', async t => {
175-
const response = await got(`${s.url}/redirect`, {
175+
const {body} = await got(`${s.url}/redirect`, {
176176
json: true,
177177
hooks: {
178178
beforeRedirect: [
@@ -182,7 +182,7 @@ test('beforeRedirect allows modifications', async t => {
182182
]
183183
}
184184
});
185-
t.is(response.body.foo, 'bar');
185+
t.is(body.foo, 'bar');
186186
});
187187

188188
test('beforeRetry', async t => {
@@ -201,7 +201,7 @@ test('beforeRetry', async t => {
201201
});
202202

203203
test('beforeRetry allows modifications', async t => {
204-
const response = await got(`${s.url}/retry`, {
204+
const {body} = await got(`${s.url}/retry`, {
205205
json: true,
206206
hooks: {
207207
beforeRetry: [
@@ -211,7 +211,7 @@ test('beforeRetry allows modifications', async t => {
211211
]
212212
}
213213
});
214-
t.is(response.body.foo, 'bar');
214+
t.is(body.foo, 'bar');
215215
});
216216

217217
test('afterResponse', async t => {
@@ -230,7 +230,7 @@ test('afterResponse', async t => {
230230
});
231231

232232
test('afterResponse allows modifications', async t => {
233-
const response = await got(`${s.url}`, {
233+
const {body} = await got(`${s.url}`, {
234234
json: true,
235235
hooks: {
236236
afterResponse: [
@@ -242,11 +242,11 @@ test('afterResponse allows modifications', async t => {
242242
]
243243
}
244244
});
245-
t.is(response.body.hello, 'world');
245+
t.is(body.hello, 'world');
246246
});
247247

248248
test('afterResponse allows to retry', async t => {
249-
const response = await got(`${s.url}/401`, {
249+
const {statusCode} = await got(`${s.url}/401`, {
250250
hooks: {
251251
afterResponse: [
252252
(response, retryWithMergedOptions) => {
@@ -263,7 +263,7 @@ test('afterResponse allows to retry', async t => {
263263
]
264264
}
265265
});
266-
t.is(response.statusCode, 200);
266+
t.is(statusCode, 200);
267267
});
268268

269269
test('no infinity loop when retrying on afterResponse', async t => {
@@ -309,7 +309,7 @@ test.serial('throws on afterResponse retry failure', async t => {
309309
test.serial('doesn\'t throw on afterResponse retry HTTP failure if throwHttpErrors is false', async t => {
310310
visited401then500 = false;
311311

312-
const response = await got(`${s.url}/401then500`, {
312+
const {statusCode} = await got(`${s.url}/401then500`, {
313313
throwHttpErrors: false,
314314
retry: 1,
315315
hooks: {
@@ -328,5 +328,5 @@ test.serial('doesn\'t throw on afterResponse retry HTTP failure if throwHttpErro
328328
]
329329
}
330330
});
331-
t.is(response.statusCode, 500);
331+
t.is(statusCode, 500);
332332
});

‎test/http.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ test('error with code', async t => {
5757
});
5858

5959
test('status code 304 doesn\'t throw', async t => {
60-
const p = got(`${s.url}/304`);
61-
await t.notThrowsAsync(p);
62-
const response = await p;
63-
t.is(response.statusCode, 304);
64-
t.is(response.body, '');
60+
const promise = got(`${s.url}/304`);
61+
await t.notThrowsAsync(promise);
62+
const {statusCode, body} = await promise;
63+
t.is(statusCode, 304);
64+
t.is(body, '');
6565
});
6666

6767
test('doesn\'t throw on throwHttpErrors === false', async t => {

‎test/progress.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ test.after('cleanup', async () => {
7171
test('download progress', async t => {
7272
const events = [];
7373

74-
const response = await got(`${s.url}/download`, {encoding: null})
74+
const {body} = await got(`${s.url}/download`, {encoding: null})
7575
.on('downloadProgress', event => events.push(event));
7676

77-
checkEvents(t, events, response.body.length);
77+
checkEvents(t, events, body.length);
7878
});
7979

8080
test('download progress - missing total size', async t => {

‎test/stream.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ test('have request event', async t => {
7272
});
7373

7474
test('have redirect event', async t => {
75-
const response = await pEvent(got.stream(`${s.url}/redirect`), 'redirect');
76-
t.is(response.headers.location, s.url);
75+
const {headers} = await pEvent(got.stream(`${s.url}/redirect`), 'redirect');
76+
t.is(headers.location, s.url);
7777
});
7878

7979
test('have response event', async t => {
80-
const response = await pEvent(got.stream(s.url), 'response');
81-
t.is(response.statusCode, 200);
80+
const {statusCode} = await pEvent(got.stream(s.url), 'response');
81+
t.is(statusCode, 200);
8282
});
8383

8484
test('have error event', async t => {
@@ -92,8 +92,8 @@ test('have error event #2', async t => {
9292
});
9393

9494
test('have response event on throwHttpErrors === false', async t => {
95-
const response = await pEvent(got.stream(`${s.url}/error`, {throwHttpErrors: false}), 'response');
96-
t.is(response.statusCode, 404);
95+
const {statusCode} = await pEvent(got.stream(`${s.url}/error`, {throwHttpErrors: false}), 'response');
96+
t.is(statusCode, 404);
9797
});
9898

9999
test('accepts option.body as Stream', async t => {
@@ -103,8 +103,8 @@ test('accepts option.body as Stream', async t => {
103103
});
104104

105105
test('redirect response contains old url', async t => {
106-
const response = await pEvent(got.stream(`${s.url}/redirect`), 'response');
107-
t.is(response.requestUrl, `${s.url}/redirect`);
106+
const {requestUrl} = await pEvent(got.stream(`${s.url}/redirect`), 'response');
107+
t.is(requestUrl, `${s.url}/redirect`);
108108
});
109109

110110
test('check for pipe method', t => {
@@ -184,10 +184,9 @@ test('proxies content-encoding header when options.decompress is false', async t
184184

185185
test('destroying got.stream() cancels the request', async t => {
186186
const stream = got.stream(s.url);
187-
188-
const req = await pEvent(stream, 'request');
187+
const request = await pEvent(stream, 'request');
189188
stream.destroy();
190-
t.truthy(req.aborted);
189+
t.truthy(request.aborted);
191190
});
192191

193192
test('piping to got.stream.put()', async t => {

0 commit comments

Comments
 (0)
Please sign in to comment.