Uri Extensions
Kotlin extensions on org.http4k.core.Uri for the URI manipulation patterns common in
server-rendered pages: filter chips, pagination links, sort headers, etc. The same operations are
exposed on InvirtRequest for use inside Pebble
templates.
hasQueryParam
- Declaration
fun Uri.hasQueryParam(name: String): Boolean
hasQueryValue
Checks whether the URI has a query parameter with the given name and value.
- Example
- Declaration
// true
Uri.of("/test?q=kotlin").hasQueryValue("q", "kotlin")
// false
Uri.of("/test?size=large").hasQueryValue("size", "small")
fun Uri.hasQueryValue(name: String, value: String): Boolean
queryValue
Returns the first value of the named query parameter, case-insensitive on the parameter name.
- Declaration
fun Uri.queryValue(name: String): String?
removeQueryValue
Returns a new Uri without the specified name/value pair. All other query params are left unchanged.
- Example
- Declaration
// "/test"
Uri.of("/test?q=John").removeQueryValue("q", "John")
// "/test?q=kotlin"
Uri.of("/test?q=kotlin&q=java").removeQueryValue("q", "java")
fun Uri.removeQueryValue(name: String, value: Any): Uri
toggleQueryValue
Returns a new Uri with the specified name/value pair added when not present, or removed when
present. All other query params are left unchanged.
- Example
- Declaration
// "/test?q=John"
Uri.of("/test").toggleQueryValue("q", "John")
// "/test"
Uri.of("/test?q=John").toggleQueryValue("q", "John")
fun Uri.toggleQueryValue(name: String, value: Any): Uri
removeQueries
Removes all query parameters with the given names (regardless of value).
- Example
- Declaration
// "/test"
Uri.of("/test?q=john&filter=name").removeQueries(listOf("q", "filter"))
fun Uri.removeQueries(names: Collection<String>): Uri
replaceQuery
Replaces the given query parameters with new values. All other query params are left unchanged.
- Example
- Declaration
// "/test?q=John&size=10"
Uri.of("/test?q=nothing&size=5").replaceQuery("q" to "John", "size" to "10")
fun Uri.replaceQuery(vararg queryValues: Pair<String, Any>): Uri
fun Uri.replaceQuery(queries: Map<String, Any>): Uri
replacePage
Returns a new Uri with the from/size pagination parameters replaced to match the given Page.
- Example
- Declaration
// "/test?from=10&size=5"
Uri.of("/test?from=0&size=10").replacePage(Page(10, 5))
fun Uri.replacePage(page: Page): Uri
replaceSort
Returns a new Uri with the sort query parameter replaced to match the given Sort. By default
pagination (from/size) is reset; pass resetPagination = false to keep it.
- Example
- Declaration
// "/test?sort=createdAt:desc"
Uri.of("/test?sort=name:asc").replaceSort(Sort.desc("createdAt"))
fun Uri.replaceSort(sort: Sort, resetPagination: Boolean = true): Uri
CSV-encoded multi-value parameters
For situations where multiple values are encoded into a single comma-separated query parameter
(?tags=red,green,blue), Invirt provides helpers to read and mutate the list:
- Example
- Declaration
// ["red", "green"]
Uri.of("/?tags=red,green").csvQuery("tags")
// "/?tags=red,green,blue"
Uri.of("/?tags=red,green").csvAppend("tags", "blue")
// "/?tags=red"
Uri.of("/?tags=red,green").csvRemove("tags", "green")
// Toggles: add if missing, remove if present
Uri.of("/?tags=red,green").csvToggle("tags", "blue")
fun Uri.csvQuery(name: String): List<String>
fun Uri.csvAppend(name: String, value: Any): Uri
fun Uri.csvRemove(name: String, value: Any): Uri
fun Uri.csvToggle(name: String, value: Any): Uri