Skip to content

Commit 29b5149

Browse files
committed
Merge branch 'master' into fix/gotrue_auth_events
2 parents 5034d6f + 594ed87 commit 29b5149

File tree

3 files changed

+44
-14
lines changed

3 files changed

+44
-14
lines changed

README.md

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
# `supabase-js`
22

3-
An isomorphic Javascript client for Supabase.
3+
An isomorphic JavaScript client for Supabase.
44

5-
- Documentation: https://supabase.io/docs/client/supabase-client
5+
**Documentation:** https://supabase.io/docs/client/supabase-client
66

77
## Usage
88

9+
First of all, you need to install the library:
10+
911
```sh
1012
npm install @supabase/supabase-js
1113
```
1214

15+
Then you're able to import the library and establish the connection with the database:
16+
1317
```js
1418
import { createClient } from '@supabase/supabase-js'
1519

@@ -19,19 +23,27 @@ const supabase = createClient('https://xyzcompany.supabase.co', 'public-anon-key
1923

2024
### UMD
2125

22-
You can now use plain `<script>`s to import supabase-js from CDNs, like
26+
You can now use plain `<script>`s to import supabase-js from CDNs, like:
27+
28+
```html
29+
<script src="https://cdn.jsdelivr.net/npm/@supabase/supabase-js"></script>
30+
```
2331

24-
`<script src="https://cdn.jsdelivr.net/npm/@supabase/supabase-js"></script>`
25-
or
26-
`<script src="https://unpkg.com/@supabase/supabase-js"></script>`.
32+
or even:
33+
34+
```html
35+
<script src="https://unpkg.com/@supabase/supabase-js"></script>
36+
```
2737

2838
Then you can use it from a global `supabase` variable:
2939

3040
```html
3141
<script>
32-
const { createClient } = supabase
33-
supabase = createClient('https://xyzcompany.supabase.co', 'public-anon-key')
34-
...
42+
const { createClient } = supabase
43+
const _supabase = createClient('https://xyzcompany.supabase.co', 'public-anon-key')
44+
45+
console.log('Supabase Instance: ', _supabase)
46+
// ...
3547
</script>
3648
```
3749

src/SupabaseClient.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ export default class SupabaseClient {
3737
protected multiTab: boolean
3838
protected fetch?: Fetch
3939
protected changedAccessToken: string | undefined
40+
protected headers: {
41+
[key: string]: string
42+
}
4043

4144
/**
4245
* Create a new client for use in the browser.
@@ -68,11 +71,11 @@ export default class SupabaseClient {
6871
this.storageUrl = `${supabaseUrl}/storage/v1`
6972
this.schema = settings.schema
7073
this.multiTab = settings.multiTab
74+
this.fetch = settings.fetch
75+
this.headers = { ...DEFAULT_HEADERS, ...options?.headers }
7176

7277
this.auth = this._initSupabaseAuthClient(settings)
73-
this.realtime = this._initRealtimeClient(settings.realtime)
74-
75-
this.fetch = settings.fetch
78+
this.realtime = this._initRealtimeClient({ headers: this.headers, ...settings.realtime })
7679

7780
this._listenForAuthEvents()
7881
this._listenForMultiTabEvents()
@@ -177,6 +180,7 @@ export default class SupabaseClient {
177180
detectSessionInUrl,
178181
localStorage,
179182
headers,
183+
fetch,
180184
}: SupabaseClientOptions) {
181185
const authHeaders = {
182186
Authorization: `Bearer ${this.supabaseKey}`,
@@ -189,7 +193,7 @@ export default class SupabaseClient {
189193
persistSession,
190194
detectSessionInUrl,
191195
localStorage,
192-
fetch: this.fetch,
196+
fetch,
193197
})
194198
}
195199

@@ -209,7 +213,7 @@ export default class SupabaseClient {
209213
}
210214

211215
private _getAuthHeaders(): { [key: string]: string } {
212-
const headers: { [key: string]: string } = DEFAULT_HEADERS
216+
const headers: { [key: string]: string } = this.headers
213217
const authBearer = this.auth.session()?.access_token ?? this.supabaseKey
214218
headers['apikey'] = this.supabaseKey
215219
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)