1
- import { DEFAULT_HEADERS , STORAGE_KEY } from './lib/constants'
2
- import { stripTrailingSlash , isBrowser } from './lib/helpers'
3
- import { Fetch , GenericObject , SupabaseClientOptions } from './lib/types'
4
- import { SupabaseAuthClient } from './lib/SupabaseAuthClient'
5
- import { SupabaseQueryBuilder } from './lib/SupabaseQueryBuilder'
6
- import { SupabaseStorageClient } from '@supabase/storage-js'
7
1
import { FunctionsClient } from '@supabase/functions-js'
8
- import { PostgrestClient } from '@supabase/postgrest-js'
9
2
import { AuthChangeEvent } from '@supabase/gotrue-js'
3
+ import { PostgrestClient } from '@supabase/postgrest-js'
10
4
import {
5
+ RealtimeChannel ,
11
6
RealtimeClient ,
12
- RealtimeSubscription ,
13
7
RealtimeClientOptions ,
14
- RealtimeChannel ,
8
+ RealtimeSubscription ,
15
9
} from '@supabase/realtime-js'
10
+ import { SupabaseStorageClient } from '@supabase/storage-js'
11
+ import { DEFAULT_HEADERS , STORAGE_KEY } from './lib/constants'
12
+ import { fetchWithAuth } from './lib/fetch'
13
+ import { isBrowser , stripTrailingSlash } from './lib/helpers'
14
+ import { SupabaseAuthClient } from './lib/SupabaseAuthClient'
15
+ import { SupabaseQueryBuilder } from './lib/SupabaseQueryBuilder'
16
+ import { Fetch , SupabaseClientOptions } from './lib/types'
16
17
17
18
const DEFAULT_OPTIONS = {
18
19
schema : 'public' ,
@@ -89,13 +90,14 @@ export default class SupabaseClient {
89
90
90
91
this . schema = settings . schema
91
92
this . multiTab = settings . multiTab
92
- this . fetch = settings . fetch
93
93
this . headers = { ...DEFAULT_HEADERS , ...options ?. headers }
94
94
this . shouldThrowOnError = settings . shouldThrowOnError || false
95
95
96
96
this . auth = this . _initSupabaseAuthClient ( settings )
97
97
this . realtime = this . _initRealtimeClient ( { headers : this . headers , ...settings . realtime } )
98
98
99
+ this . fetch = fetchWithAuth ( this . _getAccessToken . bind ( this ) , settings . fetch )
100
+
99
101
this . _listenForAuthEvents ( )
100
102
this . _listenForMultiTabEvents ( )
101
103
@@ -110,7 +112,7 @@ export default class SupabaseClient {
110
112
*/
111
113
get functions ( ) {
112
114
return new FunctionsClient ( this . functionsUrl , {
113
- headers : this . _getAuthHeaders ( ) ,
115
+ headers : this . headers ,
114
116
customFetch : this . fetch ,
115
117
} )
116
118
}
@@ -119,7 +121,7 @@ export default class SupabaseClient {
119
121
* Supabase Storage allows you to manage user-generated content, such as photos or videos.
120
122
*/
121
123
get storage ( ) {
122
- return new SupabaseStorageClient ( this . storageUrl , this . _getAuthHeaders ( ) , this . fetch )
124
+ return new SupabaseStorageClient ( this . storageUrl , this . headers , this . fetch )
123
125
}
124
126
125
127
/**
@@ -130,7 +132,7 @@ export default class SupabaseClient {
130
132
from < T = any > ( table : string ) : SupabaseQueryBuilder < T > {
131
133
const url = `${ this . restUrl } /${ table } `
132
134
return new SupabaseQueryBuilder < T > ( url , {
133
- headers : this . _getAuthHeaders ( ) ,
135
+ headers : this . headers ,
134
136
schema : this . schema ,
135
137
realtime : this . realtime ,
136
138
table,
@@ -227,6 +229,12 @@ export default class SupabaseClient {
227
229
return { data : { openSubscriptions : openSubCount } , error }
228
230
}
229
231
232
+ private async _getAccessToken ( ) {
233
+ const { session } = await this . auth . getSession ( )
234
+
235
+ return session ?. access_token ?? null
236
+ }
237
+
230
238
private async _closeSubscription (
231
239
subscription : RealtimeSubscription | RealtimeChannel
232
240
) : Promise < { error : Error | null } > {
@@ -297,21 +305,13 @@ export default class SupabaseClient {
297
305
298
306
private _initPostgRESTClient ( ) {
299
307
return new PostgrestClient ( this . restUrl , {
300
- headers : this . _getAuthHeaders ( ) ,
308
+ headers : this . headers ,
301
309
schema : this . schema ,
302
310
fetch : this . fetch ,
303
311
throwOnError : this . shouldThrowOnError ,
304
312
} )
305
313
}
306
314
307
- private _getAuthHeaders ( ) : GenericObject {
308
- const headers : GenericObject = { ...this . headers }
309
- const authBearer = this . auth . session ( ) ?. access_token ?? this . supabaseKey
310
- headers [ 'apikey' ] = this . supabaseKey
311
- headers [ 'Authorization' ] = headers [ 'Authorization' ] || `Bearer ${ authBearer } `
312
- return headers
313
- }
314
-
315
315
private _listenForMultiTabEvents ( ) {
316
316
if ( ! this . multiTab || ! isBrowser ( ) || ! window ?. addEventListener ) {
317
317
return null
0 commit comments