Skip to content

Commit 88c855a

Browse files
authored
Merge pull request #297 from jacobwgillespie/fetch
feat: allow providing custom fetch implementation
2 parents 3096a5e + 667a455 commit 88c855a

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ Then you can use it from a global `supabase` variable:
3535
</script>
3636
```
3737

38+
### Custom `fetch` implementation
39+
40+
`supabase-js` uses the [`cross-fetch`](https://www.npmjs.com/package/cross-fetch) library to make HTTP requests, but an alternative `fetch` implementation can be provided as an option. This is most useful in environments where `cross-fetch` is not compatible, for instance Cloudflare Workers:
41+
42+
```js
43+
import { createClient } from '@supabase/supabase-js'
44+
45+
// Provide a custom `fetch` implementation as an option
46+
const supabase = createClient('https://xyzcompany.supabase.co', 'public-anon-key', { fetch: fetch })
47+
```
48+
3849
## Sponsors
3950

4051
We are building the features of Firebase using enterprise-grade, open source products. We support existing communities wherever possible, and if the products don’t exist we build them and open source them ourselves. Thanks to these sponsors who are making the OSS ecosystem better for everyone.

src/SupabaseClient.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { DEFAULT_HEADERS } from './lib/constants'
22
import { stripTrailingSlash } from './lib/helpers'
3-
import { SupabaseClientOptions } from './lib/types'
3+
import { Fetch, SupabaseClientOptions } from './lib/types'
44
import { SupabaseAuthClient } from './lib/SupabaseAuthClient'
55
import { SupabaseQueryBuilder } from './lib/SupabaseQueryBuilder'
66
import { SupabaseStorageClient } from '@supabase/storage-js'
@@ -32,6 +32,7 @@ export default class SupabaseClient {
3232
protected authUrl: string
3333
protected storageUrl: string
3434
protected realtime: RealtimeClient
35+
protected fetch?: Fetch
3536

3637
/**
3738
* Create a new client for use in the browser.
@@ -43,6 +44,7 @@ export default class SupabaseClient {
4344
* @param options.detectSessionInUrl Set to "true" if you want to automatically detects OAuth grants in the URL and signs in the user.
4445
* @param options.headers Any additional headers to send with each network request.
4546
* @param options.realtime Options passed along to realtime-js constructor.
47+
* @param options.fetch A custom fetch implementation.
4648
*/
4749
constructor(
4850
protected supabaseUrl: string,
@@ -64,6 +66,8 @@ export default class SupabaseClient {
6466
this.auth = this._initSupabaseAuthClient(settings)
6567
this.realtime = this._initRealtimeClient(settings.realtime)
6668

69+
this.fetch = settings.fetch
70+
6771
// In the future we might allow the user to pass in a logger to receive these events.
6872
// this.realtime.onOpen(() => console.log('OPEN'))
6973
// this.realtime.onClose(() => console.log('CLOSED'))
@@ -74,7 +78,7 @@ export default class SupabaseClient {
7478
* Supabase Storage allows you to manage user-generated content, such as photos or videos.
7579
*/
7680
get storage() {
77-
return new SupabaseStorageClient(this.storageUrl, this._getAuthHeaders())
81+
return new SupabaseStorageClient(this.storageUrl, this._getAuthHeaders(), this.fetch)
7882
}
7983

8084
/**
@@ -89,6 +93,7 @@ export default class SupabaseClient {
8993
schema: this.schema,
9094
realtime: this.realtime,
9195
table,
96+
fetch: this.fetch,
9297
})
9398
}
9499

@@ -166,6 +171,7 @@ export default class SupabaseClient {
166171
persistSession,
167172
detectSessionInUrl,
168173
localStorage,
174+
fetch: this.fetch,
169175
})
170176
}
171177

@@ -180,6 +186,7 @@ export default class SupabaseClient {
180186
return new PostgrestClient(this.restUrl, {
181187
headers: this._getAuthHeaders(),
182188
schema: this.schema,
189+
fetch: this.fetch,
183190
})
184191
}
185192

src/lib/SupabaseQueryBuilder.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { PostgrestQueryBuilder } from '@supabase/postgrest-js'
22
import { SupabaseRealtimeClient } from './SupabaseRealtimeClient'
33
import { RealtimeClient } from '@supabase/realtime-js'
4-
import { SupabaseEventTypes, SupabaseRealtimePayload } from './types'
4+
import { Fetch, SupabaseEventTypes, SupabaseRealtimePayload } from './types'
55

66
export class SupabaseQueryBuilder<T> extends PostgrestQueryBuilder<T> {
77
private _subscription: SupabaseRealtimeClient
@@ -14,14 +14,16 @@ export class SupabaseQueryBuilder<T> extends PostgrestQueryBuilder<T> {
1414
schema,
1515
realtime,
1616
table,
17+
fetch,
1718
}: {
1819
headers?: { [key: string]: string }
1920
schema: string
2021
realtime: RealtimeClient
2122
table: string
23+
fetch?: Fetch
2224
}
2325
) {
24-
super(url, { headers, schema })
26+
super(url, { headers, schema, fetch })
2527

2628
this._subscription = new SupabaseRealtimeClient(realtime, headers, schema, table)
2729
this._realtime = realtime

src/lib/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { GoTrueClient } from '@supabase/gotrue-js'
22
import { RealtimeClientOptions } from '@supabase/realtime-js'
33

4+
export type Fetch = typeof fetch
5+
46
type GoTrueClientOptions = ConstructorParameters<typeof GoTrueClient>[0]
57

68
export interface SupabaseAuthClientOptions extends GoTrueClientOptions {}
@@ -35,6 +37,11 @@ export type SupabaseClientOptions = {
3537
* Options passed to the realtime-js instance
3638
*/
3739
realtime?: RealtimeClientOptions
40+
41+
/**
42+
* A custom `fetch` implementation.
43+
*/
44+
fetch?: Fetch
3845
}
3946

4047
export type SupabaseRealtimePayload<T> = {

0 commit comments

Comments
 (0)