Skip to content

Commit d3438c9

Browse files
authored
fix: Pass custom headers on each network request of every client (auth, realtime, REST, storage) (#311)
1 parent 5f7f3d5 commit d3438c9

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/SupabaseClient.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ export default class SupabaseClient {
3333
protected storageUrl: string
3434
protected realtime: RealtimeClient
3535
protected fetch?: Fetch
36+
protected headers: {
37+
[key: string]: string
38+
}
3639

3740
/**
3841
* Create a new client for use in the browser.
@@ -63,9 +66,10 @@ export default class SupabaseClient {
6366
this.storageUrl = `${supabaseUrl}/storage/v1`
6467
this.schema = settings.schema
6568
this.fetch = settings.fetch
69+
this.headers = { ...DEFAULT_HEADERS, ...options?.headers }
6670

6771
this.auth = this._initSupabaseAuthClient(settings)
68-
this.realtime = this._initRealtimeClient(settings.realtime)
72+
this.realtime = this._initRealtimeClient({ headers: this.headers, ...settings.realtime })
6973

7074
// In the future we might allow the user to pass in a logger to receive these events.
7175
// this.realtime.onOpen(() => console.log('OPEN'))
@@ -191,7 +195,7 @@ export default class SupabaseClient {
191195
}
192196

193197
private _getAuthHeaders(): { [key: string]: string } {
194-
const headers: { [key: string]: string } = DEFAULT_HEADERS
198+
const headers: { [key: string]: string } = this.headers
195199
const authBearer = this.auth.session()?.access_token ?? this.supabaseKey
196200
headers['apikey'] = this.supabaseKey
197201
headers['Authorization'] = `Bearer ${authBearer}`

test/client.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createClient, SupabaseClient } from '../src/index'
2+
import { DEFAULT_HEADERS } from '../src/lib/constants'
23

34
const URL = 'http://localhost:3000'
45
const KEY = 'some.fake.key'
@@ -15,6 +16,19 @@ test('it should throw an error if no valid params are provided', async () => {
1516
expect(() => createClient(URL, '')).toThrowError('supabaseKey is required.')
1617
})
1718

19+
describe('Custom Headers', () => {
20+
const customHeader = { 'X-Test-Header': 'value' }
21+
22+
test('should have custom header set', () => {
23+
const checkHeadersSpy = jest.spyOn(SupabaseClient.prototype as any, '_getAuthHeaders')
24+
createClient(URL, KEY, { headers: customHeader }).rpc('') // Calling public method `rpc` calls private method _getAuthHeaders which result we want to test
25+
const getHeaders = checkHeadersSpy.mock.results[0].value
26+
27+
expect(checkHeadersSpy).toBeCalled()
28+
expect(getHeaders).toHaveProperty('X-Test-Header', 'value')
29+
})
30+
})
31+
1832
// Socket should close when there are no open connections
1933
// https://github.com/supabase/supabase-js/issues/44
2034

0 commit comments

Comments
 (0)