diff --git a/packages/url/URL.rei b/packages/url/URL.rei index a71463cc9..2d282b3e0 100644 --- a/packages/url/URL.rei +++ b/packages/url/URL.rei @@ -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 setSearchAsString: (t, string) => t; +let setSearch: (t, SearchParams.t) => t; let searchParams: t => SearchParams.t; let username: t => option(string); let setUsername: (t, string) => t; diff --git a/packages/url/js/URL.re b/packages/url/js/URL.re index 444257633..eae1cdd12 100644 --- a/packages/url/js/URL.re +++ b/packages/url/js/URL.re @@ -192,11 +192,15 @@ let search = url => { }; }; [@mel.set] external setSearchInPlace: (t, string) => unit = "search"; -let setSearch = (url, newSearch) => { +let setSearchAsString = (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); + setSearchAsString(url, queryString); +}; [@mel.get] external getUsername: t => string = "username"; let username = url => { diff --git a/packages/url/native/URL.re b/packages/url/native/URL.re index 83ebc38a5..4a7ad8419 100644 --- a/packages/url/native/URL.re +++ b/packages/url/native/URL.re @@ -214,9 +214,10 @@ let search = url => { | _ => Some("?" ++ Uri.encoded_of_query(~scheme, query)) }; }; -let setSearch = (t, string) => { - Uri.with_query(t, Uri.query_of_encoded(string)); +let setSearchAsString = (t, searchString) => { + Uri.with_query(t, Uri.query_of_encoded(searchString)); }; +let setSearch = Uri.with_query; let searchParams = _url => assert(false); let username = url => { switch (Uri.user(url)) { diff --git a/packages/url/test/test_native.re b/packages/url/test/test_native.re index 8901383d4..2328b6c17 100644 --- a/packages/url/test/test_native.re +++ b/packages/url/test/test_native.re @@ -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®ion=cat"); assert_option_string(URL.search(url), Some("?lang=en®ion=cat")); - let url = URL.setSearch(url, "x=1&y=2"); + let url = URL.setSearchAsString(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");