Skip to content

Commit 7d33349

Browse files
authored
Fix slash added to extra parameters (#346)
1 parent 9ad4bf8 commit 7d33349

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/__tests__/useFetch.test.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ describe('useFetch - BROWSER - basic functionality', (): void => {
133133
})
134134

135135
describe('useFetch - handling host/path/route parsing properly', (): void => {
136-
it ('should have addSlash run properly', (): void => {
136+
it('should have addSlash run properly', (): void => {
137137
expect(addSlash('', '')).toBe('')
138138
expect(addSlash('')).toBe('')
139139
expect(addSlash('?foo=bar', 'a.com')).toBe('?foo=bar')
@@ -145,6 +145,9 @@ describe('useFetch - handling host/path/route parsing properly', (): void => {
145145
expect(addSlash('foo', 'a.com/')).toBe('foo')
146146
expect(addSlash('foo')).toBe('/foo')
147147
expect(addSlash('/foo')).toBe('/foo')
148+
expect(addSlash('bar', 'a.com?foo=')).toBe('bar')
149+
expect(addSlash('&foo=bar', 'a.com?b=k')).toBe('&foo=bar')
150+
expect(addSlash('&foo=bar')).toBe('&foo=bar')
148151
})
149152
})
150153

@@ -159,7 +162,7 @@ describe('useFetch - responseType', (): void => {
159162
fetch.resetMocks()
160163
fetch.mockResponseOnce('Alex Cory')
161164
const { result, waitForNextUpdate } = renderHook(
162-
() => useFetch('a-fake-url', { data: '', responseType: 'json' }, []), // onMount === true
165+
() => useFetch('a-fake-url', { data: '', responseType: 'json' }, []) // onMount === true
163166
)
164167
expect(result.current.data).toEqual('')
165168
expect(result.current.loading).toBe(true)
@@ -174,7 +177,7 @@ describe('useFetch - responseType', (): void => {
174177
const expectedString = 'Alex Cory'
175178
fetch.mockResponseOnce(JSON.stringify(expectedString))
176179
const { result, waitForNextUpdate } = renderHook(
177-
() => useFetch('a-fake-url', { data: '' }, []), // onMount === true
180+
() => useFetch('a-fake-url', { data: '' }, []) // onMount === true
178181
)
179182
expect(result.current.data).toEqual('')
180183
expect(result.current.loading).toBe(true)
@@ -203,7 +206,7 @@ describe('useFetch - BROWSER - with <Provider />', (): void => {
203206
fetch.mockResponseOnce(JSON.stringify(expected))
204207
})
205208

206-
it(`should work correctly: useFetch({ data: [] }, [])`, async (): Promise<void> => {
209+
it('should work correctly: useFetch({ data: [] }, [])', async (): Promise<void> => {
207210
const { result, waitForNextUpdate } = renderHook(
208211
() => useFetch({ data: {} }, []), // onMount === true
209212
{ wrapper }
@@ -294,7 +297,7 @@ describe('useFetch - BROWSER - with <Provider />', (): void => {
294297
expect(fetch.mock.calls.length).toBe(2)
295298
})
296299

297-
it(`should execute GET using Provider url: useFetch('/people', [])`, async (): Promise<
300+
it('should execute GET using Provider url: useFetch(\'/people\', [])', async (): Promise<
298301
void
299302
> => {
300303
const { result, waitForNextUpdate } = renderHook(
@@ -711,7 +714,7 @@ describe('useFetch - BROWSER - interceptors', (): void => {
711714
return response
712715
}
713716
}
714-
}, []),
717+
}, [])
715718
)
716719
await waitForNextUpdate()
717720
await act(result.current.get)
@@ -1138,7 +1141,7 @@ describe('useFetch - BROWSER - errors', (): void => {
11381141
})
11391142

11401143
it('should set the `error` with custom errors', async (): Promise<void> => {
1141-
const customError = { id: 'Custom Error'}
1144+
const customError = { id: 'Custom Error' }
11421145
fetch.resetMocks()
11431146
fetch.mockResponse(() => Promise.reject(customError))
11441147
const { result } = renderHook(

src/utils.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,21 +255,24 @@ export const makeError = (name: string | number, message: string) => {
255255
* (path = '', url = '' || null | undefined) => ''
256256
* (path = '?foo=bar', url = 'a.com') => '?foo=bar'
257257
* (path = '?foo=bar', url = 'a.com/') => '?foo=bar'
258+
* (path = 'bar', url = 'a.com/?foo=') => 'bar'
258259
* (path = 'foo', url = 'a.com') => '/foo'
259260
* (path = 'foo', url = 'a.com/') => 'foo'
260261
* (path = '/foo', url = 'a.com') => '/foo'
261262
* (path = '/foo', url = 'a.com/') => 'foo'
262263
* (path = '?foo=bar') => '?foo=bar'
263264
* (path = 'foo') => '/foo'
264265
* (path = '/foo') => '/foo'
266+
* (path = '&foo=bar', url = 'a.com?b=k') => '&foo=bar'
267+
* (path = '&foo=bar') => '&foo=bar'
265268
*/
266269
export const addSlash = (input?: string, url?: string) => {
267270
if (!input) return ''
268271
if (!url) {
269-
if (input.startsWith('?') || input.startsWith('/')) return input
272+
if (input.startsWith('?') || input.startsWith('&') || input.startsWith('/')) return input
270273
return `/${input}`
271274
}
272275
if (url.endsWith('/') && input.startsWith('/')) return input.substr(1)
273-
if (!url.endsWith('/') && !input.startsWith('/') && !input.startsWith('?')) return `/${input}`
276+
if (!url.endsWith('/') && !input.startsWith('/') && !input.startsWith('?') && !input.startsWith('&') && !url.includes('?') ) return `/${input}`
274277
return input
275278
}

0 commit comments

Comments
 (0)