Add in support for types decoded from arrays and objects
Closed this issue · 3 comments
Currently type array always decodes to an array of strings. It would be nice to indicate that a data type is an array of numbers, or an array of dates and have the decode work accordingly. Simplest is probably providing top-level types like numberArray
, booleanArray
, dateArray
. But I think a more general approach involving maybe shape
could be done that would also have it work with objects.
For instance of you have type array
and shape number
you get an array of numbers back. If you have type object
and shape { key1: number, key2: string }
you get back the properly decoded object with key1 as a number and key2 as a string.
A simple update would just be to make these smarter by default. On each entry:
- try decodeBoolean
- if undefined, try decodeNumber
- if undefined, treat as a string
Could try decoding as date as well?
Alternatively define a shape as part of the url query config that specifies properties and their type, essentially nested object property configuration. That way you can support any types that currently exist in UrlQueryParamTypes
.
Supporting complex objects like this would be awesome:
{
name: string,
children: string[],
yearsMarried: Date[],
friends: {
count: number,
names: string[]
}
}
Not going to do this for now, but in the future it could happen in https://github.com/pbeshai/serialize-query-params