-
Notifications
You must be signed in to change notification settings - Fork 1
/
uri_ext.gleam
55 lines (45 loc) · 1.08 KB
/
uri_ext.gleam
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import gleam/list
import gleam/option.{None, Some}
import gleam/string
import gleam/uri.{type Uri, Uri}
import gleam/http.{type Scheme}
const empty = Uri(
scheme: None,
userinfo: None,
host: None,
port: None,
path: "",
query: None,
fragment: None,
)
pub fn new() -> Uri {
empty
}
pub fn path_from_segments(segments: List(String)) -> Result(Uri, Nil) {
segments
|> list.map(string.replace(in: _, each: "/", with: ""))
|> string.join(with: "/")
|> uri.parse
}
pub fn host_from_string(host_str: String) -> Result(Uri, Nil) {
uri.parse(host_str)
}
pub fn set_host(uri: Uri, host: Uri) -> Uri {
Uri(..uri, host: host.host)
}
pub fn set_scheme(uri: Uri, scheme: Scheme) -> Uri {
Uri(..uri, scheme: Some(http.scheme_to_string(scheme)))
}
pub fn set_path(uri: Uri, path: Uri) -> Uri {
Uri(..uri, path: path.path)
}
pub fn set_port(uri: Uri, port: Int) -> Uri {
Uri(..uri, port: Some(port))
}
pub fn set_query(uri: Uri, query_params: List(#(String, String))) -> Uri {
let query =
query_params
|> uri.query_to_string
|> Some
Uri(..uri, query: query)
}