Skip to content

Commit 4718b1b

Browse files
committed
fix(query): stop including undefined keys
1 parent 6ac238f commit 4718b1b

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

Diff for: index.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,20 @@ function regFetch (uri, opts) {
5656
if (typeof q === 'string') {
5757
q = qs.parse(q)
5858
}
59-
const parsed = url.parse(uri)
60-
parsed.search = '?' + qs.stringify(
61-
parsed.query
62-
? Object.assign(qs.parse(parsed.query), q)
63-
: q
64-
)
65-
uri = url.format(parsed)
59+
Object.keys(q).forEach(key => {
60+
if (q[key] === undefined) {
61+
delete q[key]
62+
}
63+
})
64+
if (Object.keys(q).length) {
65+
const parsed = url.parse(uri)
66+
parsed.search = '?' + qs.stringify(
67+
parsed.query
68+
? Object.assign(qs.parse(parsed.query), q)
69+
: q
70+
)
71+
uri = url.format(parsed)
72+
}
6673
}
6774
return opts.Promise.resolve(body).then(body => fetch(uri, {
6875
agent: opts.agent,

Diff for: test/index.js

+10
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,16 @@ test('query strings', t => {
220220
.then(json => t.equal(json.hello, 'world', 'query-string merged'))
221221
})
222222

223+
test('query strings - undefined values', t => {
224+
tnock(t, OPTS.registry)
225+
.get('/hello?who=wor%20ld')
226+
.reply(200, {ok: true})
227+
return fetch.json('/hello', Object.assign({
228+
query: {hi: undefined, who: 'wor ld'}
229+
}, OPTS))
230+
.then(json => t.ok(json.ok, 'undefined keys not included in query string'))
231+
})
232+
223233
test('json()', t => {
224234
tnock(t, OPTS.registry)
225235
.get('/hello')

0 commit comments

Comments
 (0)