Skip to content

Commit 55fb77c

Browse files
refactor: pass down constructor
1 parent a6120fc commit 55fb77c

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

src/SupabaseClient.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ import { SupabaseRealtimeChannel } from './lib/SupabaseRealtimeChannel'
1515
import { Fetch, GenericSchema, SupabaseClientOptions, SupabaseAuthClientOptions } from './lib/types'
1616

1717
const DEFAULT_OPTIONS = {
18-
schema: 'public',
19-
autoRefreshToken: true,
20-
persistSession: true,
21-
detectSessionInUrl: true,
22-
headers: DEFAULT_HEADERS,
18+
auth: {
19+
autoRefreshToken: true,
20+
persistSession: true,
21+
detectSessionInUrl: true,
22+
},
23+
general: {
24+
schema: 'public',
25+
},
2326
}
2427

2528
/**
@@ -50,7 +53,6 @@ export default class SupabaseClient<
5053
protected storageKey: string
5154
protected fetch?: Fetch
5255
protected changedAccessToken: string | undefined
53-
protected shouldThrowOnError: boolean
5456

5557
protected headers: {
5658
[key: string]: string
@@ -93,20 +95,23 @@ export default class SupabaseClient<
9395
const defaultStorageKey = `sb-${new URL(this.authUrl).hostname.split('.')[0]}-auth-token`
9496
this.storageKey = options?.auth?.storageKey ?? defaultStorageKey
9597

96-
const settings = { ...DEFAULT_OPTIONS, ...options, storageKey: this.storageKey }
98+
const settings = { ...DEFAULT_OPTIONS?.general, ...options }
9799

98100
this.headers = { ...DEFAULT_HEADERS, ...options?.headers }
99-
this.shouldThrowOnError = settings.shouldThrowOnError || false
100101

101-
this.auth = this._initSupabaseAuthClient(settings.auth || {}, this.headers, settings.fetch)
102+
this.auth = this._initSupabaseAuthClient(
103+
settings.auth || DEFAULT_OPTIONS.auth,
104+
this.headers,
105+
settings.fetch
106+
)
102107
this.fetch = fetchWithAuth(supabaseKey, this._getAccessToken.bind(this), settings.fetch)
103108

104109
this.realtime = this._initRealtimeClient({ headers: this.headers, ...settings.realtime })
105110
this.rest = new PostgrestClient(`${_supabaseUrl}/rest/v1`, {
106111
headers: this.headers,
107112
schema: options?.db?.schema,
108113
fetch: this.fetch,
109-
throwOnError: this.shouldThrowOnError,
114+
throwOnError: options?.db?.shouldThrowOnError || false,
110115
})
111116

112117
this._listenForAuthEvents()

src/lib/types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export type SupabaseClientOptions<SchemaName> = {
1313
*/
1414
db?: {
1515
schema?: SchemaName
16+
/**
17+
* Throw errors, instead of returning them.
18+
*/
19+
shouldThrowOnError?: boolean
1620
}
1721

1822
auth?: {
@@ -53,10 +57,6 @@ export type SupabaseClientOptions<SchemaName> = {
5357
* Optional headers for initializing the client.
5458
*/
5559
headers?: Record<string, string>
56-
/**
57-
* Throw errors, instead of returning them.
58-
*/
59-
shouldThrowOnError?: boolean
6060
}
6161

6262
export type SupabaseRealtimePayload<T> = {

0 commit comments

Comments
 (0)