Skip to content

Commit

Permalink
feat: change setSearch to handle SearchParams.t
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobslisboa committed Oct 29, 2024
1 parent c16925b commit 7929444
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
3 changes: 2 additions & 1 deletion packages/url/URL.rei
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ let setPort: (t, string) => t;
let protocol: t => option(string);
let setProtocol: (t, string) => t;
let search: t => option(string);
let setSearch: (t, string) => t;
let setSearchString: (t, string) => t;
let setSearch: (t, SearchParams.t) => t;
let searchParams: t => SearchParams.t;
let username: t => option(string);
let setUsername: (t, string) => t;
Expand Down
8 changes: 6 additions & 2 deletions packages/url/js/URL.re
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,15 @@ let search = url => {
};
};
[@mel.set] external setSearchInPlace: (t, string) => unit = "search";
let setSearch = (url, newSearch) => {
let setSearchString = (url, searchString) => {
let newUrl = makeExn(toString(url));
let () = setSearchInPlace(newUrl, newSearch);
let () = setSearchInPlace(newUrl, searchString);
newUrl;
};
let setSearch = (url, searchParams) => {
let queryString = SearchParams.toString(searchParams);
setSearchString(url, queryString);
};

[@mel.get] external getUsername: t => string = "username";
let username = url => {
Expand Down
8 changes: 6 additions & 2 deletions packages/url/native/URL.re
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,12 @@ let search = url => {
| _ => Some("?" ++ Uri.encoded_of_query(~scheme, query))
};
};
let setSearch = (t, string) => {
Uri.with_query(t, Uri.query_of_encoded(string));
let setSearchString = (t, searchString) => {
Uri.with_query(t, Uri.query_of_encoded(searchString));
};
let setSearch = (t, searchParams) => {
let queryString = SearchParams.toString(searchParams);
setSearchString(t, queryString);
};
let searchParams = _url => assert(false);
let username = url => {
Expand Down
12 changes: 11 additions & 1 deletion packages/url/test/test_native.re
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,18 @@ let url_tests = (
assert_option_string(URL.search(url), Some("?lang=en"));
let url = URL.makeExn("https://www.google.es?lang=en&region=cat");
assert_option_string(URL.search(url), Some("?lang=en&region=cat"));
let url = URL.setSearch(url, "x=1&y=2");
let url = URL.setSearchString(url, "x=1&y=2");
assert_string(URL.toString(url), "https://www.google.es?x=1&y=2");
let search_params =
URL.SearchParams.makeWithArray([|
("name", "John"),
("last_name", "Doe"),
|]);
let url = URL.setSearch(url, search_params);
assert_string(
URL.toString(url),
"https://www.google.es?name=John&last_name=Doe",
);
}),
case("protocol", () => {
let url = URL.makeExn("//cdn.example.com/somewhere/something.js");
Expand Down

0 comments on commit 7929444

Please sign in to comment.