Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
Signed-off-by: schogges <[email protected]>
  • Loading branch information
schogges committed Jan 27, 2025
1 parent 3dd7314 commit 7b74034
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,20 +206,11 @@ watch(() => {
...route.params,
}
const propsParams = Object.entries(props.params).reduce((acc, [key, value]) => {
let param = params[key]
if(value === Number || typeof value === 'number') {
param = params[key] ? Number(params[key]) : get(stored, `params.${key}`, params[key])
}
acc[key] = param ?? value
return acc
}, {} as PrimitiveParams)
// normalize/validate/default all params using the RouteView :params
// 1. Ignore any `?param[]=0` type params, we just take the first one
// 2. Using normalizeUrlParam and the type information from RouteView :params convert things to the correct type i.e. null > false
// 3. Use RouteView :params to set any params that are empty, i.e. use RouteView :params as defaults.
Object.entries(propsParams).reduce((prev, [prop, def]) => {
Object.entries(props.params).reduce((prev, [prop, def]) => {
const param = urlParam(typeof params[prop] === 'undefined' ? '' : params[prop])
prev[prop] = normalizeUrlParam(param, def)
return prev
Expand Down
23 changes: 14 additions & 9 deletions packages/kuma-gui/src/app/application/utilities/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import jsYaml from 'js-yaml'

type URLParamDefault = string | number | boolean
type URLParamDefault = string | number | boolean | NumberConstructor | StringConstructor
type URLParamValues = string | number | boolean
type URLParamValue = string | null

export const runInDebug = (func: () => void) => {
Expand Down Expand Up @@ -90,20 +91,24 @@ export const urlParam = function <T extends URLParamValue> (param: T | T[]): T {
}

//
export const normalizeUrlParam = (param: URLParamValue, def: URLParamDefault): URLParamDefault => {
export const normalizeUrlParam = (param: URLParamValue, definition: URLParamDefault): URLParamValues => {
switch (true) {
case typeof def === 'boolean':
return param === null ? true : def
case typeof def === 'number': {
const value = param === null || param.length === 0 ? def : Number(decodeURIComponent(param))
case definition === Number:
return Number(param)
case definition === String:
return String(param)
case typeof definition === 'boolean':
return param === null ? true : definition
case typeof definition === 'number': {
const value = param === null || param.length === 0 ? definition : Number(decodeURIComponent(param))
if (isNaN(value)) {
return Number(def)
return Number(definition)
} else {
return value
}
}
case typeof def === 'string': {
return param === null || param.length === 0 ? def : decodeURIComponent(param)
case typeof definition === 'string': {
return param === null || param.length === 0 ? definition : decodeURIComponent(param)
}
}
throw new TypeError('URL parameters can only be string | number | boolean')
Expand Down

0 comments on commit 7b74034

Please sign in to comment.