Skip to content

Commit dba07cf

Browse files
fix: clone URL when adding query (#65)
1 parent 7ee0a21 commit dba07cf

File tree

4 files changed

+49
-46
lines changed

4 files changed

+49
-46
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ This little library has plenty of other useful functions that you can use to bui
496496
## addQueryToURL
497497
It receives a URL instance or URL string and an object-like query and returns a new URL with the query appended to it.
498498

499-
It will preserve the original query if it exists and will also preserve the type of the given URL.
499+
It will preserve the original query if it exists and will also preserve the type of the given URL. When a `URL` object is provided, the original instance is left untouched and a new one is returned.
500500

501501
```ts
502502
import { addQueryToURL } from 'make-service'
@@ -510,17 +510,17 @@ addQueryToURL(
510510
)
511511
// https://example.com/api/users?role=admin&page=2
512512

513-
addQueryToURL(
514-
new URL("https://example.com/api/users"),
515-
{ page: "2" },
516-
)
513+
const url = new URL("https://example.com/api/users")
514+
addQueryToURL(url, { page: "2" })
517515
// https://example.com/api/users?page=2
516+
url.toString()
517+
// 'https://example.com/api/users'
518518

519-
addQueryToURL(
520-
new URL("https://example.com/api/users?role=admin"),
521-
{ page: "2" },
522-
)
519+
const urlWithRole = new URL("https://example.com/api/users?role=admin")
520+
addQueryToURL(urlWithRole, { page: "2" })
523521
// https://example.com/api/users?role=admin&page=2
522+
urlWithRole.toString()
523+
// 'https://example.com/api/users?role=admin'
524524
```
525525

526526
## ensureStringBody

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@
2626
"vitest": "latest",
2727
"zod": "4.0.0-beta.20250420T053007"
2828
},
29-
"files": [
30-
"README.md",
31-
"./dist/*"
32-
],
29+
"files": ["README.md", "./dist/*"],
3330
"repository": {
3431
"type": "git",
3532
"url": "git+https://github.com/gustavoguichard/make-service.git"

src/primitives.test.ts

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,19 @@ describe('addQueryToURL', () => {
1515
})
1616

1717
it('should add the query object to a URL input', () => {
18-
expect(
19-
subject
20-
.addQueryToURL(new URL('https://example.com/api'), {
21-
id: '1',
22-
})
23-
.toString()
24-
).toEqual(new URL('https://example.com/api?id=1').toString())
25-
expect(
26-
subject
27-
.addQueryToURL(new URL('https://example.com/api'), 'page=2')
28-
.toString()
29-
).toEqual(new URL('https://example.com/api?page=2').toString())
18+
const base = new URL('https://example.com/api')
19+
const result = subject.addQueryToURL(base, { id: '1' })
20+
expect(result.toString()).toEqual(
21+
new URL('https://example.com/api?id=1').toString()
22+
)
23+
expect(base.toString()).toEqual('https://example.com/api')
24+
25+
const base2 = new URL('https://example.com/api')
26+
const result2 = subject.addQueryToURL(base2, 'page=2')
27+
expect(result2.toString()).toEqual(
28+
new URL('https://example.com/api?page=2').toString()
29+
)
30+
expect(base2.toString()).toEqual('https://example.com/api')
3031
})
3132

3233
it('should append the query to a URL string that already has QS', () => {
@@ -45,26 +46,29 @@ describe('addQueryToURL', () => {
4546
})
4647

4748
it('should append the query to a URL instance that already has QS', () => {
48-
expect(
49-
subject
50-
.addQueryToURL(new URL('https://example.com/api?id=1'), {
51-
page: '2',
52-
})
53-
.toString()
54-
).toEqual(new URL('https://example.com/api?id=1&page=2').toString())
55-
expect(
56-
subject
57-
.addQueryToURL(new URL('https://example.com/api?id=1'), 'page=2')
58-
.toString()
59-
).toEqual(new URL('https://example.com/api?id=1&page=2').toString())
60-
expect(
61-
subject
62-
.addQueryToURL(
63-
new URL('https://example.com/api?id=1'),
64-
new URLSearchParams({ page: '2' })
65-
)
66-
.toString()
67-
).toEqual(new URL('https://example.com/api?id=1&page=2').toString())
49+
const base = new URL('https://example.com/api?id=1')
50+
const result = subject.addQueryToURL(base, { page: '2' })
51+
expect(result.toString()).toEqual(
52+
new URL('https://example.com/api?id=1&page=2').toString()
53+
)
54+
expect(base.toString()).toEqual('https://example.com/api?id=1')
55+
56+
const base2 = new URL('https://example.com/api?id=1')
57+
const result2 = subject.addQueryToURL(base2, 'page=2')
58+
expect(result2.toString()).toEqual(
59+
new URL('https://example.com/api?id=1&page=2').toString()
60+
)
61+
expect(base2.toString()).toEqual('https://example.com/api?id=1')
62+
63+
const base3 = new URL('https://example.com/api?id=1')
64+
const result3 = subject.addQueryToURL(
65+
base3,
66+
new URLSearchParams({ page: '2' })
67+
)
68+
expect(result3.toString()).toEqual(
69+
new URL('https://example.com/api?id=1&page=2').toString()
70+
)
71+
expect(base3.toString()).toEqual('https://example.com/api?id=1')
6872
})
6973

7074
it("should return the input in case there's no query", () => {

src/primitives.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ function addQueryToURL<T extends string | URL>(
1717
return `${url}${separator}${new URLSearchParams(searchParams)}` as T
1818
}
1919
if (searchParams && url instanceof URL) {
20+
const result = new URL(url.toString())
2021
for (const [key, value] of new URLSearchParams(searchParams).entries()) {
21-
url.searchParams.set(key, value)
22+
result.searchParams.set(key, value)
2223
}
24+
return result as T
2325
}
2426
return url
2527
}

0 commit comments

Comments
 (0)