@@ -9,19 +9,27 @@ import { RealtimeChannel, RealtimeClient, RealtimeClientOptions } from '@supabas
9
9
import { StorageClient as SupabaseStorageClient } from '@supabase/storage-js'
10
10
import { DEFAULT_HEADERS } from './lib/constants'
11
11
import { fetchWithAuth } from './lib/fetch'
12
- import { stripTrailingSlash } from './lib/helpers'
12
+ import { stripTrailingSlash , applySettingDefaults } from './lib/helpers'
13
13
import { SupabaseAuthClient } from './lib/SupabaseAuthClient'
14
14
import { SupabaseRealtimeChannel } from './lib/SupabaseRealtimeChannel'
15
15
import { Fetch , GenericSchema , SupabaseClientOptions , SupabaseAuthClientOptions } from './lib/types'
16
16
17
- const DEFAULT_OPTIONS = {
17
+ const DEFAULT_GLOBAL_OPTIONS = {
18
+ headers : DEFAULT_HEADERS ,
19
+ }
20
+
21
+ const DEFAULT_DB_OPTIONS = {
18
22
schema : 'public' ,
23
+ }
24
+
25
+ const DEFAULT_AUTH_OPTIONS : SupabaseAuthClientOptions = {
19
26
autoRefreshToken : true ,
20
27
persistSession : true ,
21
28
detectSessionInUrl : true ,
22
- headers : DEFAULT_HEADERS ,
23
29
}
24
30
31
+ const DEFAULT_REALTIME_OPTIONS : RealtimeClientOptions = { }
32
+
25
33
/**
26
34
* Supabase Client.
27
35
*
@@ -59,13 +67,13 @@ export default class SupabaseClient<
59
67
* Create a new client for use in the browser.
60
68
* @param supabaseUrl The unique Supabase URL which is supplied when you create a new project in your project dashboard.
61
69
* @param supabaseKey The unique Supabase Key which is supplied when you create a new project in your project dashboard.
62
- * @param options.schema You can switch in between schemas. The schema needs to be on the list of exposed schemas inside Supabase.
63
- * @param options.autoRefreshToken Set to "true" if you want to automatically refresh the token before expiring.
64
- * @param options.persistSession Set to "true" if you want to automatically save the user session into local storage.
65
- * @param options.detectSessionInUrl Set to "true" if you want to automatically detects OAuth grants in the URL and signs in the user.
66
- * @param options.headers Any additional headers to send with each network request.
70
+ * @param options.db.schema You can switch in between schemas. The schema needs to be on the list of exposed schemas inside Supabase.
71
+ * @param options.auth.autoRefreshToken Set to "true" if you want to automatically refresh the token before expiring.
72
+ * @param options.auth.persistSession Set to "true" if you want to automatically save the user session into local storage.
73
+ * @param options.auth.detectSessionInUrl Set to "true" if you want to automatically detects OAuth grants in the URL and signs in the user.
67
74
* @param options.realtime Options passed along to realtime-js constructor.
68
- * @param options.fetch A custom fetch implementation.
75
+ * @param options.global.fetch A custom fetch implementation.
76
+ * @param options.global.headers Any additional headers to send with each network request.
69
77
*/
70
78
constructor (
71
79
protected supabaseUrl : string ,
@@ -90,19 +98,29 @@ export default class SupabaseClient<
90
98
}
91
99
// default storage key uses the supabase project ref as a namespace
92
100
const defaultStorageKey = `sb-${ new URL ( this . authUrl ) . hostname . split ( '.' ) [ 0 ] } -auth-token`
93
- this . storageKey = options ?. auth ?. storageKey ?? defaultStorageKey
101
+ const DEFAULTS = {
102
+ db : DEFAULT_DB_OPTIONS ,
103
+ realtime : DEFAULT_REALTIME_OPTIONS ,
104
+ auth : { ...DEFAULT_AUTH_OPTIONS , storageKey : defaultStorageKey } ,
105
+ global : DEFAULT_GLOBAL_OPTIONS ,
106
+ }
94
107
95
- const settings = { ... DEFAULT_OPTIONS , ... options , storageKey : this . storageKey }
108
+ const settings = applySettingDefaults ( options ?? { } , DEFAULTS )
96
109
97
- this . headers = { ...DEFAULT_HEADERS , ...options ?. headers }
110
+ this . storageKey = settings . auth ?. storageKey ?? ''
111
+ this . headers = settings . global ?. headers ?? { }
98
112
99
- this . auth = this . _initSupabaseAuthClient ( settings . auth || { } , this . headers , settings . fetch )
100
- this . fetch = fetchWithAuth ( supabaseKey , this . _getAccessToken . bind ( this ) , settings . fetch )
113
+ this . auth = this . _initSupabaseAuthClient (
114
+ settings . auth ?? { } ,
115
+ this . headers ,
116
+ settings . global ?. fetch
117
+ )
118
+ this . fetch = fetchWithAuth ( supabaseKey , this . _getAccessToken . bind ( this ) , settings . global ?. fetch )
101
119
102
120
this . realtime = this . _initRealtimeClient ( { headers : this . headers , ...settings . realtime } )
103
121
this . rest = new PostgrestClient ( `${ _supabaseUrl } /rest/v1` , {
104
122
headers : this . headers ,
105
- schema : options ? .db ?. schema ,
123
+ schema : settings . db ?. schema ,
106
124
fetch : this . fetch ,
107
125
} )
108
126
0 commit comments