Skip to content

Commit f0da868

Browse files
refactor: extract applySettingsDefault to helper
1 parent f7c2130 commit f0da868

File tree

3 files changed

+73
-40
lines changed

3 files changed

+73
-40
lines changed

src/SupabaseClient.ts

Lines changed: 2 additions & 40 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 } from './lib/helpers'
12+
import { stripTrailingSlash, applySettingDefaults } from './lib/helpers'
1313
import { SupabaseAuthClient } from './lib/SupabaseAuthClient'
1414
import { SupabaseRealtimeChannel } from './lib/SupabaseRealtimeChannel'
1515
import { Fetch, GenericSchema, SupabaseClientOptions, SupabaseAuthClientOptions } from './lib/types'
@@ -105,7 +105,7 @@ export default class SupabaseClient<
105105
global: DEFAULT_GLOBAL_OPTIONS,
106106
}
107107

108-
const settings = this._applySettingDefaults(options || {}, DEFAULTS)
108+
const settings = applySettingDefaults(options || {}, DEFAULTS)
109109

110110
this.storageKey = settings.auth?.storageKey ?? ''
111111
this.headers = settings.global?.headers ?? {}
@@ -336,42 +336,4 @@ export default class SupabaseClient<
336336
if (source == 'STORAGE') this.auth.signOut()
337337
}
338338
}
339-
340-
// TODO(Joel): Figure out how to properly type this
341-
private _applySettingDefaults(
342-
options: SupabaseClientOptions<SchemaName>,
343-
defaults: any
344-
): SupabaseClientOptions<SchemaName> {
345-
const {
346-
db: dbOptions,
347-
auth: authOptions,
348-
realtime: realtimeOptions,
349-
global: globalOptions,
350-
} = options
351-
const {
352-
db: DEFAULT_DB_OPTIONS,
353-
auth: DEFAULT_AUTH_OPTIONS,
354-
realtime: DEFAULT_REALTIME_OPTIONS,
355-
global: DEFAULT_GLOBAL_OPTIONS,
356-
} = defaults
357-
358-
return {
359-
db: {
360-
...DEFAULT_DB_OPTIONS,
361-
...dbOptions,
362-
},
363-
auth: {
364-
...DEFAULT_AUTH_OPTIONS,
365-
...authOptions,
366-
},
367-
realtime: {
368-
...DEFAULT_REALTIME_OPTIONS,
369-
...realtimeOptions,
370-
},
371-
global: {
372-
...DEFAULT_GLOBAL_OPTIONS,
373-
...globalOptions,
374-
},
375-
}
376-
}
377339
}

src/lib/helpers.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,40 @@ export function stripTrailingSlash(url: string): string {
1313
}
1414

1515
export const isBrowser = () => typeof window !== 'undefined'
16+
17+
export function applySettingDefaults(
18+
options: SupabaseClientOptions<SchemaName>,
19+
defaults: SupabaseClientOptions<any>
20+
): SupabaseClientOptions<SchemaName> {
21+
const {
22+
db: dbOptions,
23+
auth: authOptions,
24+
realtime: realtimeOptions,
25+
global: globalOptions,
26+
} = options
27+
const {
28+
db: DEFAULT_DB_OPTIONS,
29+
auth: DEFAULT_AUTH_OPTIONS,
30+
realtime: DEFAULT_REALTIME_OPTIONS,
31+
global: DEFAULT_GLOBAL_OPTIONS,
32+
} = defaults
33+
34+
return {
35+
db: {
36+
...DEFAULT_DB_OPTIONS,
37+
...dbOptions,
38+
},
39+
auth: {
40+
...DEFAULT_AUTH_OPTIONS,
41+
...authOptions,
42+
},
43+
realtime: {
44+
...DEFAULT_REALTIME_OPTIONS,
45+
...realtimeOptions,
46+
},
47+
global: {
48+
...DEFAULT_GLOBAL_OPTIONS,
49+
...globalOptions,
50+
},
51+
}
52+
}

test/helpers.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,39 @@
11
import * as helpers from '../src/lib/helpers'
2+
import { DEFAULT_HEADERS } from '../src/lib/constants'
23

34
test('uuid', async () => {
45
expect(helpers.uuid()).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/)
56
})
7+
8+
test('override setting defaults', async () => {
9+
const DEFAULT_GLOBAL_OPTIONS = {
10+
headers: DEFAULT_HEADERS,
11+
}
12+
13+
const DEFAULT_DB_OPTIONS = {
14+
schema: 'public',
15+
}
16+
17+
const DEFAULT_AUTH_OPTIONS = {
18+
autoRefreshToken: true,
19+
persistSession: true,
20+
detectSessionInUrl: true,
21+
}
22+
23+
let defaults = {
24+
db: DEFAULT_DB_OPTIONS,
25+
auth: DEFAULT_AUTH_OPTIONS,
26+
global: DEFAULT_GLOBAL_OPTIONS,
27+
}
28+
29+
let autoRefreshOption = false
30+
let options = {
31+
auth: {
32+
autoRefreshToken: autoRefreshOption,
33+
},
34+
}
35+
let settings = helpers.applySettingDefaults(options, defaults)
36+
expect(settings.auth.autoRefreshToken).toBe(autoRefreshOption)
37+
expect(settings.auth.persistSession).not.toBeNull()
38+
expect(settings.db.schema).toBe(defaults.db.schema)
39+
})

0 commit comments

Comments
 (0)