Skip to content

Commit 033ef86

Browse files
authored
Merge pull request #458 from supabase/j0_revamp_constructor
Revamp Supabase Constructor for V2
2 parents e9849e1 + 39b2642 commit 033ef86

File tree

2 files changed

+49
-46
lines changed

2 files changed

+49
-46
lines changed

src/SupabaseClient.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { fetchWithAuth } from './lib/fetch'
1212
import { isBrowser, stripTrailingSlash } from './lib/helpers'
1313
import { SupabaseAuthClient } from './lib/SupabaseAuthClient'
1414
import { SupabaseRealtimeClient } from './lib/SupabaseRealtimeClient'
15-
import { Fetch, GenericSchema, SupabaseClientOptions } from './lib/types'
15+
import { Fetch, GenericSchema, SupabaseClientOptions, SupabaseAuthClientOptions } from './lib/types'
1616

1717
const DEFAULT_OPTIONS = {
1818
schema: 'public',
@@ -93,17 +93,17 @@ export default class SupabaseClient<
9393
this.functionsUrl = `${_supabaseUrl}/functions/v1`
9494
}
9595

96-
this.multiTab = settings.multiTab
96+
this.multiTab = settings.auth?.multiTab ?? false
9797
this.headers = { ...DEFAULT_HEADERS, ...options?.headers }
9898
this.shouldThrowOnError = settings.shouldThrowOnError || false
9999

100100
this.fetch = fetchWithAuth(supabaseKey, this._getAccessToken.bind(this), settings.fetch)
101101

102-
this.auth = this._initSupabaseAuthClient(settings)
102+
this.auth = this._initSupabaseAuthClient(settings.auth || {}, this.headers, this.fetch)
103103
this.realtime = this._initRealtimeClient({ headers: this.headers, ...settings.realtime })
104104
this.rest = new PostgrestClient(`${_supabaseUrl}/rest/v1`, {
105105
headers: this.headers,
106-
schema: options?.schema,
106+
schema: options?.db?.schema,
107107
fetch: this.fetch,
108108
throwOnError: this.shouldThrowOnError,
109109
})
@@ -261,16 +261,18 @@ export default class SupabaseClient<
261261
return this.realtime.channels as RealtimeChannel[]
262262
}
263263

264-
private _initSupabaseAuthClient({
265-
autoRefreshToken,
266-
persistSession,
267-
detectSessionInUrl,
268-
localStorage,
269-
headers,
270-
fetch,
271-
cookieOptions,
272-
multiTab,
273-
}: SupabaseClientOptions<string>) {
264+
private _initSupabaseAuthClient(
265+
{
266+
autoRefreshToken,
267+
persistSession,
268+
detectSessionInUrl,
269+
localStorage,
270+
cookieOptions,
271+
multiTab,
272+
}: SupabaseAuthClientOptions,
273+
headers?: Record<string, string>,
274+
fetch?: Fetch
275+
) {
274276
const authHeaders = {
275277
Authorization: `Bearer ${this.supabaseKey}`,
276278
apikey: `${this.supabaseKey}`,

src/lib/types.ts

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,51 +11,52 @@ export type SupabaseClientOptions<SchemaName> = {
1111
/**
1212
* The Postgres schema which your tables belong to. Must be on the list of exposed schemas in Supabase. Defaults to 'public'.
1313
*/
14-
schema?: SchemaName
15-
/**
16-
* Optional headers for initializing the client.
17-
*/
18-
headers?: Record<string, string>
19-
/**
20-
* Automatically refreshes the token for logged in users.
21-
*/
22-
autoRefreshToken?: boolean
23-
/**
24-
* Allows to enable/disable multi-tab/window events
25-
*/
26-
multiTab?: boolean
27-
/**
28-
* Whether to persist a logged in session to storage.
29-
*/
30-
persistSession?: boolean
31-
/**
32-
* Detect a session from the URL. Used for OAuth login callbacks.
33-
*/
34-
detectSessionInUrl?: boolean
35-
/**
36-
* A storage provider. Used to store the logged in session.
37-
*/
38-
localStorage?: SupabaseAuthClientOptions['localStorage']
14+
db?: {
15+
schema?: SchemaName
16+
}
3917

18+
auth?: {
19+
/**
20+
* Automatically refreshes the token for logged in users.
21+
*/
22+
autoRefreshToken?: boolean
23+
/**
24+
* Whether to persist a logged in session to storage.
25+
*/
26+
persistSession?: boolean
27+
/**
28+
* Detect a session from the URL. Used for OAuth login callbacks.
29+
*/
30+
detectSessionInUrl?: boolean
31+
/**
32+
* A storage provider. Used to store the logged in session.
33+
*/
34+
localStorage?: SupabaseAuthClientOptions['localStorage']
35+
/**
36+
* Options passed to the gotrue-js instance
37+
*/
38+
cookieOptions?: SupabaseAuthClientOptions['cookieOptions']
39+
/**
40+
* Allows to enable/disable multi-tab/window events
41+
*/
42+
multiTab?: boolean
43+
}
4044
/**
4145
* Options passed to the realtime-js instance
4246
*/
4347
realtime?: RealtimeClientOptions
44-
4548
/**
4649
* A custom `fetch` implementation.
4750
*/
4851
fetch?: Fetch
49-
5052
/**
51-
* Throw errors, instead of returning them.
53+
* Optional headers for initializing the client.
5254
*/
53-
shouldThrowOnError?: boolean
54-
55+
headers?: Record<string, string>
5556
/**
56-
* Options passed to the gotrue-js instance
57+
* Throw errors, instead of returning them.
5758
*/
58-
cookieOptions?: SupabaseAuthClientOptions['cookieOptions']
59+
shouldThrowOnError?: boolean
5960
}
6061

6162
export type SupabaseRealtimePayload<T> = {

0 commit comments

Comments
 (0)