Skip to content

Commit

Permalink
allow copy of searchParams too
Browse files Browse the repository at this point in the history
  • Loading branch information
BBB committed Nov 1, 2023
1 parent da5eda0 commit c44f46a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/http-client/src/ImmutableURL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ export class ImmutableURL implements Readonly<URL> {
this.#searchParams = new ImmutableURLSearchParams(this.#url.searchParams);
}

copy(update?: Partial<Pick<ImmutableURL, MutableURLFields>>) {
copy(
update?: Partial<Pick<ImmutableURL, MutableURLFields | "searchParams">>,
) {
const next = new URL(this.#url.toString());
if (update) {
Object.entries(update).forEach(([key, value]) => {
if (isMutableUrlField(key)) {
if (key === "searchParams") {
next.search = value.toString();
} else if (isMutableUrlField(key) && typeof value === "string") {
next[key] = value;
}
});
Expand Down
11 changes: 11 additions & 0 deletions packages/http-client/test/ImmutableURL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,14 @@ it("can copy the fields", () => {
expect(underTest.password).not.toEqual(changed.password);
expect(changed.toString()).toEqual("https://user:pass@example.com/api");
});

it("can copy using search params", () => {
const underTest = new ImmutableURL("https://example.com/api");
const changed = underTest.copy({
searchParams: underTest.searchParams.set("updated", "true"),
});
expect(underTest.searchParams).not.toEqual(changed.searchParams);
expect(underTest.search).not.toEqual(changed.search);
expect(changed.toString()).toEqual("https://example.com/api?updated=true");
expect(changed.search).toEqual("?updated=true");
});

0 comments on commit c44f46a

Please sign in to comment.