Url.Builder

HTML & web

Build absolute, relative, and cross-origin URLs with query parameters.

Path segments are joined as-is; query keys and values are percent-encoded via Url.percentEncode.

Types

Root

type Root
= Absolute
| Relative
| CrossOrigin String

Whether a custom URL is absolute, relative, or cross-origin.

Examples

Absolute
Relative
CrossOrigin "https://cdn.example.com"

QueryParameter

type QueryParameter = QueryParameter String String

A percent-encoded query parameter.

Examples

string "q" "a b"

Values

absolute

absolute : List String -> List QueryParameter -> String

Create an absolute URL (always starts with /).

Examples

absolute [] []
    == "/"

absolute [ "products" ] [ string "search" "hat", int "page" 2 ]
    == "/products?search=hat&page=2"

relative

relative : List String -> List QueryParameter -> String

Create a relative URL (no leading slash).

Examples

relative [] []
    == ""

relative [ "products" ] [ string "search" "hat" ]
    == "products?search=hat"

crossOrigin

crossOrigin : String -> List String -> List QueryParameter -> String

Create a cross-origin URL from a pre-path (scheme + host + optional port).

Examples

crossOrigin "https://example.com" [ "products" ] []
    == "https://example.com/products"

custom

custom : Root -> List String -> List QueryParameter -> Maybe String -> String

Build a URL that may include a fragment.

Examples

custom Absolute [ "docs", "stdlib" ] [] (Just "Time")
    == "/docs/stdlib#Time"

string

string : String -> String -> QueryParameter

Create a percent-encoded string query parameter.

Examples

string "search" "a b"

int

int : String -> Int -> QueryParameter

Create a query parameter from an integer key name and value.

Examples

int "page" 2

toQuery

toQuery : List QueryParameter -> String

Convert query parameters to a ?... suffix, or "" if empty.

Examples

toQuery [] == ""
toQuery [ string "q" "hi" ] == "?q=hi"