Skip to content

Commit 2e24d16

Browse files
refactor: adopt spread syntax
1 parent 0a1e4ea commit 2e24d16

File tree

2 files changed

+58
-24
lines changed

2 files changed

+58
-24
lines changed

src/SupabaseClient.ts

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,24 @@ import { SupabaseAuthClient } from './lib/SupabaseAuthClient'
1414
import { SupabaseRealtimeChannel } from './lib/SupabaseRealtimeChannel'
1515
import { Fetch, GenericSchema, SupabaseClientOptions, SupabaseAuthClientOptions } from './lib/types'
1616

17-
const DEFAULT_OPTIONS = {
18-
auth: {
19-
autoRefreshToken: true,
20-
persistSession: true,
21-
detectSessionInUrl: true,
22-
},
23-
global: {
24-
headers: DEFAULT_HEADERS,
25-
},
17+
const DEFAULT_GLOBAL_OPTIONS = {
18+
headers: DEFAULT_HEADERS,
19+
}
20+
21+
const DEFAULT_DB_OPTIONS = {
2622
db: {
2723
schema: 'public',
28-
shouldThrowOnError: false,
2924
},
3025
}
3126

27+
const DEFAULT_AUTH_OPTIONS: SupabaseAuthClientOptions = {
28+
autoRefreshToken: true,
29+
persistSession: true,
30+
detectSessionInUrl: true,
31+
}
32+
33+
const DEFAULT_REALTIME_OPTIONS: RealtimeClientOptions = {}
34+
3235
/**
3336
* Supabase Client.
3437
*
@@ -99,22 +102,43 @@ export default class SupabaseClient<
99102
const defaultStorageKey = `sb-${new URL(this.authUrl).hostname.split('.')[0]}-auth-token`
100103
this.storageKey = options?.auth?.storageKey ?? defaultStorageKey
101104

102-
const settings = Object.assign(DEFAULT_OPTIONS, options)
103-
104-
const authSettings = settings.auth
105-
const dbSettings = settings.db
105+
const {
106+
db: dbOptions,
107+
auth: authOptions,
108+
realtime: realtimeOptions,
109+
global: globalOptions,
110+
} = options || {}
111+
112+
const settings = {
113+
db: {
114+
...DEFAULT_DB_OPTIONS,
115+
...dbOptions,
116+
},
117+
auth: {
118+
...DEFAULT_AUTH_OPTIONS,
119+
storageKey: defaultStorageKey,
120+
...authOptions,
121+
},
122+
realtime: {
123+
...DEFAULT_REALTIME_OPTIONS,
124+
...realtimeOptions,
125+
},
126+
global: {
127+
...DEFAULT_GLOBAL_OPTIONS,
128+
...globalOptions,
129+
},
130+
}
106131

107-
this.headers = settings.global.headers
132+
this.headers = { ...DEFAULT_HEADERS, ...globalOptions?.headers }
108133

109-
this.auth = this._initSupabaseAuthClient(authSettings, this.headers, settings.global.fetch)
134+
this.auth = this._initSupabaseAuthClient(settings.auth, this.headers, settings.global.fetch)
110135
this.fetch = fetchWithAuth(supabaseKey, this._getAccessToken.bind(this), settings.global.fetch)
111136

112137
this.realtime = this._initRealtimeClient({ headers: this.headers, ...settings.realtime })
113138
this.rest = new PostgrestClient(`${_supabaseUrl}/rest/v1`, {
114139
headers: this.headers,
115-
schema: dbSettings.schema,
140+
schema: settings?.db?.schema,
116141
fetch: this.fetch,
117-
throwOnError: dbSettings.shouldThrowOnError,
118142
})
119143

120144
this._listenForAuthEvents()

test/SupabaseAuthClient.test.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
import { SupabaseAuthClient } from '../src/lib/SupabaseAuthClient'
2+
import { SupabaseClientOptions } from '../src/lib/types'
3+
import { DEFAULT_HEADERS } from '../src/lib/constants'
24

35
const DEFAULT_OPTIONS = {
4-
schema: 'public',
5-
autoRefreshToken: true,
6-
persistSession: true,
7-
detectSessionInUrl: true,
8-
headers: {},
6+
auth: {
7+
autoRefreshToken: true,
8+
persistSession: true,
9+
detectSessionInUrl: true,
10+
},
11+
global: {
12+
headers: DEFAULT_HEADERS,
13+
},
14+
db: {
15+
schema: 'public',
16+
},
917
}
1018
const settings = { ...DEFAULT_OPTIONS }
1119

20+
const authSettings = { ...settings.global, ...settings.auth }
21+
1222
test('it should create a new instance of the class', () => {
13-
const authClient = new SupabaseAuthClient(settings)
23+
const authClient = new SupabaseAuthClient(authSettings)
1424
expect(authClient).toBeInstanceOf(SupabaseAuthClient)
1525
})

0 commit comments

Comments
 (0)