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:

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" ( IDString.toString filters.orgNrList)
, UB.string "period" (Date.toIsoString filters.period.monitorPeriod)
, "limit" limit
, "offset" offset
, "frappStatus" (frappStatus.toInt filters.period.frappStatus)
, UB.nonEmptyString "freeTextFilter"
, UB.maybe UB.string "eventFilter" ( filters.eventType)
, UB.maybe UB.string "eventTriggerFilter" ( 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.