Skip to content

Commit 2649270

Browse files
szmarczaksindresorhus
authored andcommittedJul 25, 2018
Pass normalized options to the handler (#532)
1 parent 07a91cc commit 2649270

File tree

4 files changed

+40
-45
lines changed

4 files changed

+40
-45
lines changed
 

‎advanced-creation.md

+6-8
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,26 @@ Function making additional changes to the request.
3131
To inherit from parent, set it as `got.defaults.handler`.<br>
3232
To use the default handler, just omit specifying this.
3333

34-
###### [url](readme.md#url)
35-
3634
###### [options](readme.md#options)
3735

36+
**Note:** These options are [normalized](source/normalize-arguments.js).
37+
3838
###### next()
3939

4040
Normalizes arguments and returns a `Promise` or a `Stream` depending on [`options.stream`](readme.md#stream).
4141

4242
```js
4343
const settings = {
44-
handler: (url, options, next) => {
44+
handler: (options, next) => {
4545
if (options.stream) {
4646
// It's a Stream
4747
// We can perform stream-specific actions on it
48-
return next(url, options)
48+
return next(options)
4949
.on('request', request => setTimeout(() => request.abort(), 50));
5050
}
5151

5252
// It's a Promise
53-
return next(url, options);
53+
return next(options);
5454
},
5555
methods: got.defaults.methods,
5656
options: {
@@ -64,9 +64,7 @@ const jsonGot = got.create(settings);
6464

6565
```js
6666
const defaults = {
67-
handler: (url, options, next) => {
68-
return next(url, options);
69-
},
67+
handler: (options, next) => next(options),
7068
methods: [
7169
'get',
7270
'post',

‎source/create.js

+4-14
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
11
'use strict';
2-
const URLGlobal = typeof URL === 'undefined' ? require('url').URL : URL; // TODO: Use the `URL` global when targeting Node.js 10
32
const errors = require('./errors');
43
const assignOptions = require('./assign-options');
54
const asStream = require('./as-stream');
65
const asPromise = require('./as-promise');
76
const normalizeArguments = require('./normalize-arguments');
87
const deepFreeze = require('./deep-freeze');
98

10-
const makeNext = defaults => (path, options) => {
11-
let url = path;
12-
13-
if (options.baseUrl) {
14-
url = new URLGlobal(path, options.baseUrl);
15-
}
16-
17-
options = normalizeArguments(url, options, defaults);
18-
9+
const next = options => {
1910
if (options.stream) {
2011
return asStream(options);
2112
}
@@ -24,15 +15,14 @@ const makeNext = defaults => (path, options) => {
2415
};
2516

2617
const create = defaults => {
27-
const next = makeNext(defaults);
2818
if (!defaults.handler) {
29-
defaults.handler = next;
19+
defaults.handler = (options, next) => next(options);
3020
}
3121

3222
function got(url, options) {
3323
try {
3424
options = assignOptions(defaults.options, options);
35-
return defaults.handler(url, options, next);
25+
return defaults.handler(normalizeArguments(url, options, defaults), next);
3626
} catch (error) {
3727
return Promise.reject(error);
3828
}
@@ -48,7 +38,7 @@ const create = defaults => {
4838
got.stream = (url, options) => {
4939
options = assignOptions(defaults.options, options);
5040
options.stream = true;
51-
return defaults.handler(url, options, next);
41+
return defaults.handler(normalizeArguments(url, options, defaults), next);
5242
};
5343

5444
for (const method of defaults.methods) {

‎source/normalize-arguments.js

+19-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
2-
const URLSearchParamsGlobal = typeof URLSearchParams === 'undefined' ? require('url').URLSearchParams : URLSearchParams; // TODO: Use the `URL` global when targeting Node.js 10
2+
const URLGlobal = typeof URL === 'undefined' ? require('url').URL : URL; // TODO: Use the `URL` global when targeting Node.js 10
3+
const URLSearchParamsGlobal = typeof URLSearchParams === 'undefined' ? require('url').URLSearchParams : URLSearchParams;
34
const is = require('@sindresorhus/is');
45
const toReadableStream = require('to-readable-stream');
56
const urlParseLax = require('url-parse-lax');
@@ -17,25 +18,32 @@ module.exports = (url, options, defaults) => {
1718

1819
if (!is.string(url) && !is.object(url)) {
1920
throw new TypeError(`Parameter \`url\` must be a string or object, not ${is(url)}`);
20-
} else if (is.string(url)) {
21-
url = url.replace(/^unix:/, 'http://$&');
21+
}
2222

23-
try {
24-
decodeURI(url);
25-
} catch (_) {
26-
throw new Error('Parameter `url` must contain valid UTF-8 character sequences');
27-
}
23+
if (is.string(url)) {
24+
if (options.baseUrl) {
25+
url = urlToOptions(new URLGlobal(url, options.baseUrl));
26+
} else {
27+
url = url.replace(/^unix:/, 'http://$&');
2828

29-
url = urlParseLax(url);
30-
if (url.auth) {
31-
throw new Error('Basic authentication must be done with the `auth` option');
29+
try {
30+
decodeURI(url);
31+
} catch (_) {
32+
throw new Error('Parameter `url` must contain valid UTF-8 character sequences');
33+
}
34+
35+
url = urlParseLax(url);
36+
if (url.auth) {
37+
throw new Error('Basic authentication must be done with the `auth` option');
38+
}
3239
}
3340
} else if (is(url) === 'URL') {
3441
url = urlToOptions(url);
3542
}
3643

3744
options = {
3845
path: '',
46+
headers: {},
3947
...url,
4048
protocol: url.protocol || 'http:', // Override both null/undefined with default protocol
4149
...options

‎test/create.js

+11-12
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,16 @@ test('custom headers (extend)', async t => {
7676
t.is(headers.unicorn, 'rainbow');
7777
});
7878

79-
test('custom endpoint with custom headers (create)', async t => {
80-
const options = {headers: {unicorn: 'rainbow'}};
81-
const handler = (url, options, next) => {
82-
url = `${s.url}` + url;
83-
84-
return next(url, options);
85-
};
86-
const methods = ['get'];
87-
88-
const instance = got.create({options, methods, handler});
89-
const headers = (await instance('/', {
79+
test('create', async t => {
80+
const instance = got.create({
81+
options: {},
82+
methods: ['get'],
83+
handler: (options, next) => {
84+
options.headers.unicorn = 'rainbow';
85+
return next(options);
86+
}
87+
});
88+
const headers = (await instance(s.url, {
9089
json: true
9190
})).body;
9291
t.is(headers.unicorn, 'rainbow');
@@ -99,7 +98,7 @@ test('custom endpoint with custom headers (extend)', async t => {
9998
json: true
10099
})).body;
101100
t.is(headers.unicorn, 'rainbow');
102-
t.is(headers['user-agent'] === undefined, false);
101+
t.not(headers['user-agent'], undefined);
103102
});
104103

105104
test('no tampering with defaults', t => {

0 commit comments

Comments
 (0)
Please sign in to comment.