diff --git a/src/__tests__/decodeQueryParams-test.ts b/src/__tests__/decodeQueryParams-test.ts index 0e5a275..1225e89 100644 --- a/src/__tests__/decodeQueryParams-test.ts +++ b/src/__tests__/decodeQueryParams-test.ts @@ -1,10 +1,12 @@ import { decodeQueryParams } from '../index'; + import { NumberParam, ArrayParam, StringParam, DelimitedArrayParam, } from '../params'; +import withDefault from '../withDefault'; describe('decodeQueryParams', () => { it('works', () => { @@ -14,6 +16,7 @@ describe('decodeQueryParams', () => { bar: NumberParam, baz: ArrayParam, box: DelimitedArrayParam, + not: withDefault(NumberParam, 94), }, { foo: '123', @@ -27,6 +30,7 @@ describe('decodeQueryParams', () => { bar: 555, baz: ['a', 'b', 'c'], box: ['a', 'b', 'c'], + not: 94, }); }); diff --git a/src/decodeQueryParams.ts b/src/decodeQueryParams.ts index b0fd54f..7bf2b6d 100644 --- a/src/decodeQueryParams.ts +++ b/src/decodeQueryParams.ts @@ -13,7 +13,16 @@ export function decodeQueryParams( ): Partial> { const decodedQuery: Partial> = {}; - const paramNames = Object.keys(encodedQuery); + // iterate over all keys in the config (#30) + const paramNames = Object.keys(paramConfigMap); + + // ensure any non configured keys that are in the URL are also included + for (const encodedKey of Object.keys(encodedQuery)) { + if (paramConfigMap[encodedKey] == null) { + paramNames.push(encodedKey); + } + } + for (const paramName of paramNames) { const encodedValue = encodedQuery[paramName];