Skip to content

Commit ca15642

Browse files
refactor: abstract into auth settings
1 parent 55fb77c commit ca15642

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/SupabaseClient.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ const DEFAULT_OPTIONS = {
2020
persistSession: true,
2121
detectSessionInUrl: true,
2222
},
23-
general: {
23+
global: {
24+
timeoutMs: 500,
25+
headers: DEFAULT_HEADERS,
26+
},
27+
db: {
2428
schema: 'public',
29+
shouldThrowOnError: false,
2530
},
2631
}
2732

@@ -95,23 +100,21 @@ export default class SupabaseClient<
95100
const defaultStorageKey = `sb-${new URL(this.authUrl).hostname.split('.')[0]}-auth-token`
96101
this.storageKey = options?.auth?.storageKey ?? defaultStorageKey
97102

98-
const settings = { ...DEFAULT_OPTIONS?.general, ...options }
103+
const settings = { ...DEFAULT_OPTIONS?.global, ...options }
104+
const authSettings = Object.assign(DEFAULT_OPTIONS.global, DEFAULT_OPTIONS.auth, settings.auth)
105+
const dbSettings = Object.assign(DEFAULT_OPTIONS.global, DEFAULT_OPTIONS.db, settings.db)
99106

100-
this.headers = { ...DEFAULT_HEADERS, ...options?.headers }
107+
this.headers = { ...DEFAULT_OPTIONS?.global?.headers, ...options?.headers }
101108

102-
this.auth = this._initSupabaseAuthClient(
103-
settings.auth || DEFAULT_OPTIONS.auth,
104-
this.headers,
105-
settings.fetch
106-
)
107-
this.fetch = fetchWithAuth(supabaseKey, this._getAccessToken.bind(this), settings.fetch)
109+
this.auth = this._initSupabaseAuthClient(authSettings, this.headers, this.fetch)
110+
this.fetch = fetchWithAuth(supabaseKey, this._getAccessToken.bind(this), this.fetch)
108111

109112
this.realtime = this._initRealtimeClient({ headers: this.headers, ...settings.realtime })
110113
this.rest = new PostgrestClient(`${_supabaseUrl}/rest/v1`, {
111114
headers: this.headers,
112-
schema: options?.db?.schema,
115+
schema: dbSettings.schema,
113116
fetch: this.fetch,
114-
throwOnError: options?.db?.shouldThrowOnError || false,
117+
throwOnError: dbSettings.shouldThrowOnError,
115118
})
116119

117120
this._listenForAuthEvents()

0 commit comments

Comments
 (0)