Skip to content

Commit

Permalink
fix(query): stop including undefined keys
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Aug 21, 2018
1 parent 6ac238f commit 4718b1b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
21 changes: 14 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,20 @@ function regFetch (uri, opts) {
if (typeof q === 'string') {
q = qs.parse(q)
}
const parsed = url.parse(uri)
parsed.search = '?' + qs.stringify(
parsed.query
? Object.assign(qs.parse(parsed.query), q)
: q
)
uri = url.format(parsed)
Object.keys(q).forEach(key => {
if (q[key] === undefined) {
delete q[key]
}
})
if (Object.keys(q).length) {
const parsed = url.parse(uri)
parsed.search = '?' + qs.stringify(
parsed.query
? Object.assign(qs.parse(parsed.query), q)
: q
)
uri = url.format(parsed)
}
}
return opts.Promise.resolve(body).then(body => fetch(uri, {
agent: opts.agent,
Expand Down
10 changes: 10 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,16 @@ test('query strings', t => {
.then(json => t.equal(json.hello, 'world', 'query-string merged'))
})

test('query strings - undefined values', t => {
tnock(t, OPTS.registry)
.get('/hello?who=wor%20ld')
.reply(200, {ok: true})
return fetch.json('/hello', Object.assign({
query: {hi: undefined, who: 'wor ld'}
}, OPTS))
.then(json => t.ok(json.ok, 'undefined keys not included in query string'))
})

test('json()', t => {
tnock(t, OPTS.registry)
.get('/hello')
Expand Down

0 comments on commit 4718b1b

Please sign in to comment.