Maybe, List encoders etc. for Url.Builder

I think it’s very common that query parameters are optional, so I think it would be nice Url.Builder had a built-in way to handle them. This would produce much neater code than if the user first has to make a List Maybe QueryParameter and then run it though List.filterMap identity.

I’ve already made this package which adds encoders for Maybe, List, Float, Bool etc:
https://package.elm-lang.org/packages/Herteby/url-builder-plus/latest/

I can see some of them could be slightly problematic for a core package, as I don’t think there’s a definitive standard for bools and lists in urls. But at least a Maybe encoder would be pretty handy to have in the core package I think.

This is an example from our app, quite a monster query, and the code used to be pretty messy, but now with encoders for List, Maybe etc. it looks pretty clean I think :slightly_smiling_face:

[ UB.list UB.string "orgNrList" (List.map IDString.toString filters.orgNrList)
, UB.string "period" (Date.toIsoString filters.period.monitorPeriod)
, UB.int "limit" limit
, UB.int "offset" offset
, UB.int "frappStatus" (frappStatus.toInt filters.period.frappStatus)
, UB.nonEmptyString "freeTextFilter" filters.search
, UB.maybe UB.string "eventFilter" (Maybe.map Event.name.toString filters.eventType)
, UB.maybe UB.string "eventTriggerFilter" (Maybe.map EventTrigger.toString filters.eventTrigger)
, UB.maybe UB.bool "verified" (VerifiedFilter.toMaybeBool filters.verified)
]
1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.