Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Replace node querystring with URLSearchParams #1828

Merged
merged 5 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions __tests__/lib/search-params.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const sp = require('../../lib/search-params')
const assert = require('assert')

describe('search-params', () => {
describe('stringify', () => {
it('Should stringify a simple object', () => {
assert.deepStrictEqual(sp.stringify({ a: 1, b: 'b' }), 'a=1&b=b')
})

it('Should stringify an object with an array', () => {
assert.deepStrictEqual(sp.stringify({ a: [1, 2] }), 'a=1&a=2')
})

it('Should stringify an object with an array with a single value', () => {
assert.deepStrictEqual(sp.stringify({ a: [1] }), 'a=1')
})

it('Stringify an object with an array with a single empty value', () => {
assert.deepStrictEqual(sp.stringify({ a: [''] }), 'a=')
})

it('Should not stringify an object with a nested object', () => {
assert.deepStrictEqual(sp.stringify({ a: { b: 1 } }), 'a=')
})
})

describe('parse', () => {
it('Should parse a simple query string', () => {
assert.deepStrictEqual(sp.parse('a=1&b=2'), { a: '1', b: '2' })
})

it('Should parse a query string with same key and multiple values', () => {
assert.deepEqual(sp.parse('a=1&a=2'), { a: ['1', '2'] })
})

it('Should parse a query string with an array with a single empty value', () => {
assert.deepStrictEqual(sp.parse('a='), { a: '' })
})
})
})
8 changes: 4 additions & 4 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const accepts = require('accepts')
const contentType = require('content-type')
const stringify = require('url').format
const parse = require('parseurl')
const qs = require('querystring')
const sp = require('./search-params.js')

const typeis = require('type-is')
const fresh = require('fresh')
const only = require('./only.js')
Expand Down Expand Up @@ -171,7 +172,7 @@ module.exports = {
get query () {
const str = this.querystring
const c = this._querycache = this._querycache || {}
return c[str] || (c[str] = qs.parse(str))
return c[str] || (c[str] = sp.parse(str))
},

/**
Expand All @@ -182,7 +183,7 @@ module.exports = {
*/

set query (obj) {
this.querystring = qs.stringify(obj)
this.querystring = sp.stringify(obj)
},

/**
Expand Down Expand Up @@ -210,7 +211,6 @@ module.exports = {

url.search = str
url.path = null

this.url = stringify(url)
},

Expand Down
33 changes: 33 additions & 0 deletions lib/search-params.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const URLSearchParams = require('url').URLSearchParams

module.exports = {
stringify: (obj) => {
const searchParams = new URLSearchParams()
const addKey = (k, v, params) => {
const val = typeof v === 'string' || typeof v === 'number' ? v : ''
params.append(k, val)
}

for (const [key, value] of Object.entries(obj)) {
if (Array.isArray(value)) {
const lgth = value.length
for (let i = 0; i < lgth; i++) {
addKey(key, value[i], searchParams)
}
} else {
addKey(key, value, searchParams)
}
}
return searchParams.toString()
},

parse: (str) => {
const searchParams = new URLSearchParams(str)
const obj = {}
for (const key of searchParams.keys()) {
const values = searchParams.getAll(key)
obj[key] = values.length <= 1 ? values[0] : values
}
return obj
}
}