Skip to content
Open
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
12 changes: 7 additions & 5 deletions src/parse-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ export function parseQueryFromURL(

if (array && array?.[finalKey]) {
if (finalValue.charCodeAt(0) === 91) {
if (object && object?.[finalKey])
try {
finalValue = JSON.parse(finalValue) as any
else finalValue = finalValue.slice(1, -1).split(',') as any
} catch {
finalValue = finalValue.slice(1, -1).split(',') as any
}

if (currentValue === undefined) result[finalKey] = finalValue
else if (Array.isArray(currentValue))
Expand All @@ -103,10 +105,10 @@ export function parseQueryFromURL(
result[finalKey].unshift(currentValue)
}
} else {
// join multi-param values as comma-separated so ArrayQuery
// Decode handles them uniformly (same as comma format)
if (currentValue === undefined) result[finalKey] = finalValue
else if (Array.isArray(currentValue))
currentValue.push(finalValue)
else result[finalKey] = [currentValue, finalValue]
else result[finalKey] = currentValue + ',' + finalValue
}
} else {
result[finalKey] = finalValue
Expand Down
52 changes: 48 additions & 4 deletions src/type-system/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,12 +425,56 @@ export const ElysiaType = {
const schema = t.Array(children, options)
const compiler = compile(schema)

const tryParseItem = (v: string) => {
const c = v.charCodeAt(0)
if (c === 123 || c === 91) {
try { return JSON.parse(v) } catch {}
}
return v
}

// split by commas but respect {} [] and quoted strings
const splitTopLevel = (value: string) => {
const parts: string[] = []
let depth = 0
let inStr = 0 // 0=none, 34=", 39='
let start = 0

for (let i = 0; i < value.length; i++) {
const ch = value.charCodeAt(i)

if (inStr) {
if (ch === inStr && value.charCodeAt(i - 1) !== 92)
inStr = 0
continue
}

if (ch === 34 || ch === 39) { inStr = ch; continue }
if (ch === 123 || ch === 91) { depth++; continue }
if (ch === 125 || ch === 93) { depth--; continue }

if (ch === 44 && depth === 0) {
parts.push(value.slice(start, i))
start = i + 1
}
}

parts.push(value.slice(start))
return parts
}

const decode = (value: string) => {
// has , (as used in nuqs)
if (value.indexOf(',') !== -1)
return compiler.Decode(value.split(','))
if (value.indexOf(',') !== -1) {
try {
return compiler.Decode(JSON.parse(`[${value}]`))
} catch {
return compiler.Decode(
splitTopLevel(value).map(tryParseItem)
)
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

return compiler.Decode([value])
return compiler.Decode([tryParseItem(value)])
}

return t
Expand Down