Skip to content

Commit e2f3a2a

Browse files
authored
fix: remove trailing white space from url (#504)
1 parent e621de4 commit e2f3a2a

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

src/SupabaseClient.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { RealtimeChannel, RealtimeClient, RealtimeClientOptions } from '@supabas
99
import { StorageClient as SupabaseStorageClient } from '@supabase/storage-js'
1010
import { DEFAULT_HEADERS } from './lib/constants'
1111
import { fetchWithAuth } from './lib/fetch'
12-
import { stripTrailingSlash, applySettingDefaults } from './lib/helpers'
12+
import { cleanUrl, applySettingDefaults } from './lib/helpers'
1313
import { SupabaseAuthClient } from './lib/SupabaseAuthClient'
1414
import { Fetch, GenericSchema, SupabaseClientOptions, SupabaseAuthClientOptions } from './lib/types'
1515

@@ -82,7 +82,7 @@ export default class SupabaseClient<
8282
if (!supabaseUrl) throw new Error('supabaseUrl is required.')
8383
if (!supabaseKey) throw new Error('supabaseKey is required.')
8484

85-
const _supabaseUrl = stripTrailingSlash(supabaseUrl)
85+
const _supabaseUrl = cleanUrl(supabaseUrl)
8686

8787
this.realtimeUrl = `${_supabaseUrl}/realtime/v1`.replace('http', 'ws')
8888
this.authUrl = `${_supabaseUrl}/auth/v1`

src/lib/helpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ export function uuid() {
99
})
1010
}
1111

12-
export function stripTrailingSlash(url: string): string {
13-
return url.replace(/\/$/, '')
12+
export function cleanUrl(url: string): string {
13+
return url.replace(/[\/\s]*$/, '')
1414
}
1515

1616
export const isBrowser = () => typeof window !== 'undefined'

test/helper.test.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1-
import { stripTrailingSlash } from '../src/lib/helpers'
1+
import { cleanUrl } from '../src/lib/helpers'
22

33
test('Strip trailing slash from URL', () => {
44
const URL = 'http://localhost:3000/'
55
const expectedURL = URL.slice(0, -1)
6-
expect(stripTrailingSlash(URL)).toBe(expectedURL)
6+
expect(cleanUrl(URL)).toBe(expectedURL)
77
})
88

9-
test('Return the original URL if there is no slash at the end', () => {
9+
test('Return the original URL if there is no slash or white space at the end', () => {
1010
const URL = 'http://localhost:3000'
1111
const expectedURL = URL
12-
expect(stripTrailingSlash(URL)).toBe(expectedURL)
12+
expect(cleanUrl(URL)).toBe(expectedURL)
13+
})
14+
15+
test('Strip trailing white space from URL', () => {
16+
const URL = 'http://localhost:3000 '
17+
const expectedURL = URL.slice(0, -1)
18+
expect(cleanUrl(URL)).toBe(expectedURL)
19+
})
20+
21+
test('Strip trailing slash followed by white space from URL', () => {
22+
const URL = 'http://localhost:3000/ '
23+
const expectedURL = URL.slice(0, -2)
24+
expect(cleanUrl(URL)).toBe(expectedURL)
1325
})

0 commit comments

Comments
 (0)