Skip to content

Commit

Permalink
review sqlfilter json as url param encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
dbauszus-glx committed Oct 30, 2023
1 parent 301862d commit 31a9960
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 20 deletions.
7 changes: 0 additions & 7 deletions api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,6 @@ module.exports = async (req, res) => {

req.params.template = req.params._template || req.params.template

// Decode string params.
Object.entries(req.params)
.filter(entry => typeof entry[1] === 'string')
.forEach(entry => {
req.params[entry[0]] = decodeURIComponent(entry[1])
})

// Short circuit login view or post request.
if (req.params.login || req.body && req.body.login) return login(req, res)

Expand Down
12 changes: 5 additions & 7 deletions lib/utils/paramString.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// Create param string for XHR request.
export default params => Object.entries(params)

// Value should be 0 or truthy
.filter(entry => entry[1] === 0 || !!entry[1])

// Value must not be empty functional brackets.
.filter(entry => entry[1] !== '{}')

// Filter out zero length array and objects with empty object values.
Expand All @@ -10,16 +14,10 @@ export default params => Object.entries(params)

.map(entry => {

// if (Array.isArray(entry[1])) {

// return entry[1].map(val => `${entry[0]}=${val}`).join('&')
// }

// Stringify non array objects.
if (typeof entry[1] === 'object' && !Array.isArray(entry[1])) {

entry[1] = JSON.stringify(entry[1])

return `${entry[0]}=${encodeURIComponent(JSON.stringify(entry[1]))}`
}

return encodeURI(`${entry[0]}=${entry[1]}`)
Expand Down
9 changes: 3 additions & 6 deletions mod/utils/sqlFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,17 @@ const filterTypes = {
`(${val
.split(',')
.filter((val) => val.length > 0)
.map((val) => `"${col}" ILIKE \$${addValues(`${val}%`, true)}`)
.map((val) => `"${col}" ILIKE \$${addValues(`${val}%`)}`)
.join(' OR ')})`,

match: (col, val) => `"${col}"::text ILIKE \$${addValues(val)}`
}

let SQLparams

function addValues(val, skip) {
function addValues(val) {

SQLparams.push(Array.isArray(val)
&& val[0].map(v=>decodeURIComponent(v))
|| skip && val
|| decodeURIComponent(val))
SQLparams.push(val)

return SQLparams.length
}
Expand Down

0 comments on commit 31a9960

Please sign in to comment.