A tiny library for building parameterized URLs. It is intended to be used with records to give the parameters names and therefore reducing errors.
userUrl : Url { id : Int, show : Bool }
userUrl =
root |> hash |> s "users" |> int .id |> boolQuery "show" .show
userUrl |> toString { id = 42, show = True }
--> "/#/users/42?show=true"Append an unparameterized (static) segment.
root |> s "users" |> toString () --> "/users"Append an unparameterized (static) segment with a Maybe value; it is omitted when the value is Nothing.
root
|> maybeS (Just "users")
|> maybeS Nothing
|> maybeS (Just "1")
|> toString ()
--> "/users/1"Append a hash segment.
root
|> s "base"
|> hash
|> s "page"
|> toString {}
--> "/base/#/page"NOTE: Only the first hash is unencoded:
root |> hash |> hash |> toString {} --> "/#/%23"A custom root URL.
customRoot "http://example.com:8080/"
|> toString () --> "http://example.com:8080/"If the string has no trailing slash, one is added:
customRoot "http://example.com:8080"
|> toString () --> "http://example.com:8080/"Append an integer segment.
root
|> s "users"
|> int .id
|> toString { id = 42 }
--> "/users/42"Append a string segment.
root
|> s "say"
|> string .word
|> toString { word = "Hello" }
--> "/say/Hello"Append a boolean segment.
root
|> bool .show
|> toString { show = True }
--> "/true"Append a custom segment.
root
|> custom (.ids >> List.map String.fromInt >> String.join ";")
|> toString { ids = [1, 2, 3] }
--> "/1%3B2%3B3"Append an integer segment with a Maybe value; it is omitted when the value is Nothing.
url : Url { id : Maybe Int }
url =
root
|> s "users"
|> maybeInt .id
|> s "images"
toString { id = Just 42 } url --> "/users/42/images"
toString { id = Nothing } url --> "/users/images"Append a string segment with a Maybe value; it is omitted when the value is Nothing.
url : Url { word : Maybe String }
url =
root
|> s "say"
|> maybeString .word
|> s "world"
toString { word = Just "Hello" } url --> "/say/Hello/world"
toString { word = Nothing } url --> "/say/world"Append a boolean segment with a Maybe value; it is omitted when the value is Nothing.
url : Url { show : Maybe Bool }
url =
root
|> maybeBool .show
toString { show = Just True } url --> "/true"
toString { show = Nothing } url --> "/"Append a custom segment with a Maybe value; it is omitted when the value is Nothing.
url : Url { ids : Maybe (List Int) }
url =
root
|> maybeCustom (.ids >> Maybe.map (List.map String.fromInt >> String.join ";"))
url |> toString { ids = Just [ 1, 2, 3 ] } --> "/1%3B2%3B3"
url |> toString { ids = Nothing } --> "/"Append an integer query value.
root
|> s "users"
|> intQuery "id" .id
|> toString { id = 42 }
--> "/users?id=42"Append a string query value.
root
|> s "say"
|> stringQuery "word" .word
|> toString { word = "Hello" }
--> "/say?word=Hello"Append a boolean query value.
root
|> boolQuery "show" .show
|> toString { show = True }
--> "/?show=true"Append a custom query value.
root
|> customQuery "ids" (.ids >> List.map String.fromInt >> String.join ";")
|> toString { ids = [1, 2, 3] }
--> "/?ids=1%3B2%3B3"Append an integer query value with a Maybe value; it is omitted when the value is Nothing.
url : Url { id : Maybe Int }
url =
root
|> s "users"
|> s "images"
|> maybeIntQuery "id" .id
toString { id = Just 42 } url --> "/users/images?id=42"
toString { id = Nothing } url --> "/users/images"Append a string query value with a Maybe value; it is omitted when the value is Nothing.
url : Url { word : Maybe String }
url =
root
|> s "say"
|> maybeStringQuery "word" .word
toString { word = Just "Hello" } url --> "/say?word=Hello"
toString { word = Nothing } url --> "/say"Append a boolean query value with a Maybe value; it is omitted when the value is Nothing.
url : Url { show : Maybe Bool }
url =
root
|> maybeBoolQuery "show" .show
toString { show = Just True } url --> "/?show=true"
toString { show = Nothing } url --> "/"Append a custom query value with a Maybe value; it is omitted when the value is Nothing.
url : Url { ids : Maybe (List Int) }
url =
root
|> maybeCustomQuery "ids" (.ids >> Maybe.map (List.map String.fromInt >> String.join ";"))
url |> toString { ids = Just [ 1, 2, 3 ] } --> "/?ids=1%3B2%3B3"
url |> toString { ids = Nothing } --> "/"
A URL parameterized over the type
a, which is typically a record containing a field for each variable segment and query parameter.