Skip to content

Commit f7c2130

Browse files
chore: create utility function
1 parent 2b3284c commit f7c2130

File tree

1 file changed

+54
-33
lines changed

1 file changed

+54
-33
lines changed

src/SupabaseClient.ts

Lines changed: 54 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ const DEFAULT_GLOBAL_OPTIONS = {
1919
}
2020

2121
const DEFAULT_DB_OPTIONS = {
22-
db: {
23-
schema: 'public',
24-
},
22+
schema: 'public',
2523
}
2624

2725
const DEFAULT_AUTH_OPTIONS: SupabaseAuthClientOptions = {
@@ -100,39 +98,24 @@ export default class SupabaseClient<
10098
}
10199
// default storage key uses the supabase project ref as a namespace
102100
const defaultStorageKey = `sb-${new URL(this.authUrl).hostname.split('.')[0]}-auth-token`
103-
104-
const {
105-
db: dbOptions,
106-
auth: authOptions,
107-
realtime: realtimeOptions,
108-
global: globalOptions,
109-
} = options || {}
110-
111-
const settings = {
112-
db: {
113-
...DEFAULT_DB_OPTIONS,
114-
...dbOptions,
115-
},
116-
auth: {
117-
...DEFAULT_AUTH_OPTIONS,
118-
storageKey: defaultStorageKey,
119-
...authOptions,
120-
},
121-
realtime: {
122-
...DEFAULT_REALTIME_OPTIONS,
123-
...realtimeOptions,
124-
},
125-
global: {
126-
...DEFAULT_GLOBAL_OPTIONS,
127-
...globalOptions,
128-
},
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,
129106
}
130107

131-
this.storageKey = settings.auth.storageKey
132-
this.headers = settings.global.headers
108+
const settings = this._applySettingDefaults(options || {}, DEFAULTS)
109+
110+
this.storageKey = settings.auth?.storageKey ?? ''
111+
this.headers = settings.global?.headers ?? {}
133112

134-
this.auth = this._initSupabaseAuthClient(settings.auth, this.headers, settings.global.fetch)
135-
this.fetch = fetchWithAuth(supabaseKey, this._getAccessToken.bind(this), settings.global.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)
136119

137120
this.realtime = this._initRealtimeClient({ headers: this.headers, ...settings.realtime })
138121
this.rest = new PostgrestClient(`${_supabaseUrl}/rest/v1`, {
@@ -353,4 +336,42 @@ export default class SupabaseClient<
353336
if (source == 'STORAGE') this.auth.signOut()
354337
}
355338
}
339+
340+
// TODO(Joel): Figure out how to properly type this
341+
private _applySettingDefaults(
342+
options: SupabaseClientOptions<SchemaName>,
343+
defaults: any
344+
): SupabaseClientOptions<SchemaName> {
345+
const {
346+
db: dbOptions,
347+
auth: authOptions,
348+
realtime: realtimeOptions,
349+
global: globalOptions,
350+
} = options
351+
const {
352+
db: DEFAULT_DB_OPTIONS,
353+
auth: DEFAULT_AUTH_OPTIONS,
354+
realtime: DEFAULT_REALTIME_OPTIONS,
355+
global: DEFAULT_GLOBAL_OPTIONS,
356+
} = defaults
357+
358+
return {
359+
db: {
360+
...DEFAULT_DB_OPTIONS,
361+
...dbOptions,
362+
},
363+
auth: {
364+
...DEFAULT_AUTH_OPTIONS,
365+
...authOptions,
366+
},
367+
realtime: {
368+
...DEFAULT_REALTIME_OPTIONS,
369+
...realtimeOptions,
370+
},
371+
global: {
372+
...DEFAULT_GLOBAL_OPTIONS,
373+
...globalOptions,
374+
},
375+
}
376+
}
356377
}

0 commit comments

Comments
 (0)