diff --git a/packages/kuma-gui/src/app/application/components/route-view/RouteView.vue b/packages/kuma-gui/src/app/application/components/route-view/RouteView.vue index 670b75c3ea..5e418605bb 100644 --- a/packages/kuma-gui/src/app/application/components/route-view/RouteView.vue +++ b/packages/kuma-gui/src/app/application/components/route-view/RouteView.vue @@ -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 diff --git a/packages/kuma-gui/src/app/application/utilities/index.ts b/packages/kuma-gui/src/app/application/utilities/index.ts index 33ce396c2b..ae80cc7482 100644 --- a/packages/kuma-gui/src/app/application/utilities/index.ts +++ b/packages/kuma-gui/src/app/application/utilities/index.ts @@ -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) => { @@ -90,20 +91,24 @@ export const urlParam = function (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')