Skip to content

Commit 6b19b5e

Browse files
authored
fix(client): handle query parameters in removeIndexString (#4352)
* fix(client): handle query parameters in `removeIndexString` * fix the typo
1 parent 193d5dc commit 6b19b5e

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

src/client/client.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,14 @@ describe('Basic - $url()', () => {
356356
it('Should return a correct url via $url().href', async () => {
357357
const client = hc<typeof app>('http://fake')
358358
expect(client.index.$url().href).toBe('http://fake/')
359+
expect(
360+
client.index.$url({
361+
query: {
362+
page: '123',
363+
limit: '20',
364+
},
365+
}).href
366+
).toBe('http://fake/?page=123&limit=20')
359367
expect(client.api.$url().href).toBe('http://fake/api')
360368
expect(
361369
client.api.posts[':id'].$url({

src/client/utils.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,24 @@ describe('removeIndexString', () => {
122122
newUrl = removeIndexString(url)
123123
expect(newUrl).toBe('/subindex')
124124
})
125+
126+
it('Should remove `/index` with query parameters', () => {
127+
let url = 'http://localhost/index?page=123&limit=20'
128+
let newUrl = removeIndexString(url)
129+
expect(newUrl).toBe('http://localhost/?page=123&limit=20')
130+
131+
url = 'https://example.com/index?q=search'
132+
newUrl = removeIndexString(url)
133+
expect(newUrl).toBe('https://example.com/?q=search')
134+
135+
url = '/api/index?filter=test'
136+
newUrl = removeIndexString(url)
137+
expect(newUrl).toBe('/api?filter=test')
138+
139+
url = '/index?a=1&b=2&c=3'
140+
newUrl = removeIndexString(url)
141+
expect(newUrl).toBe('?a=1&b=2&c=3')
142+
})
125143
})
126144

127145
describe('deepMerge', () => {

src/client/utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ export const replaceUrlProtocol = (urlString: string, protocol: 'ws' | 'http') =
5252
}
5353
}
5454

55-
export const removeIndexString = (urlSting: string) => {
56-
if (/^https?:\/\/[^\/]+?\/index$/.test(urlSting)) {
57-
return urlSting.replace(/\/index$/, '/')
55+
export const removeIndexString = (urlString: string) => {
56+
if (/^https?:\/\/[^\/]+?\/index(?=\?|$)/.test(urlString)) {
57+
return urlString.replace(/\/index(?=\?|$)/, '/')
5858
}
59-
return urlSting.replace(/\/index$/, '')
59+
return urlString.replace(/\/index(?=\?|$)/, '')
6060
}
6161

6262
function isObject(item: unknown): item is ObjectType {

0 commit comments

Comments
 (0)