Feat: Replace node querystring with URLSearchParams (#1828)
Fix #1570 Replace node querystring with URLSearchParams it mimics the way that querystring works ## Checklist - [x] I have ensured my pull request is not behind the main or master branch of the original repository. - [x] I have rebased all commits where necessary so that reviewing this pull request can be done without having to merge it first. - [x] I have written a commit message that passes commitlint linting. - [x] I have ensured that my code changes pass linting tests. - [x] I have ensured that my code changes pass unit tests. - [x] I have described my pull request and the reasons for code changes along with context if necessary.
This commit is contained in:
40
__tests__/lib/search-params.js
Normal file
40
__tests__/lib/search-params.js
Normal file
@@ -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: '' })
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -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')
|
||||
@@ -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))
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -182,7 +183,7 @@ module.exports = {
|
||||
*/
|
||||
|
||||
set query (obj) {
|
||||
this.querystring = qs.stringify(obj)
|
||||
this.querystring = sp.stringify(obj)
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -210,7 +211,6 @@ module.exports = {
|
||||
|
||||
url.search = str
|
||||
url.path = null
|
||||
|
||||
this.url = stringify(url)
|
||||
},
|
||||
|
||||
|
||||
33
lib/search-params.js
Normal file
33
lib/search-params.js
Normal file
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user