@supabase/realtime-js 2.7.3 → 2.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,6 +14,8 @@ import Serializer from './lib/serializer'
14
14
  import RealtimeChannel from './RealtimeChannel'
15
15
  import type { RealtimeChannelOptions } from './RealtimeChannel'
16
16
 
17
+ type Fetch = typeof fetch
18
+
17
19
  export type RealtimeClientOptions = {
18
20
  transport?: WebSocket
19
21
  timeout?: number
@@ -25,6 +27,7 @@ export type RealtimeClientOptions = {
25
27
  headers?: { [key: string]: string }
26
28
  params?: { [key: string]: any }
27
29
  log_level?: 'info' | 'debug' | 'warn' | 'error'
30
+ fetch?: Fetch
28
31
  }
29
32
 
30
33
  export type RealtimeMessage = {
@@ -72,6 +75,7 @@ export default class RealtimeClient {
72
75
  }
73
76
  eventsPerSecondLimitMs: number = 100
74
77
  inThrottle: boolean = false
78
+ fetch: Fetch
75
79
 
76
80
  /**
77
81
  * Initializes the Socket.
@@ -102,6 +106,9 @@ export default class RealtimeClient {
102
106
  if (eventsPerSecond)
103
107
  this.eventsPerSecondLimitMs = Math.floor(1000 / eventsPerSecond)
104
108
 
109
+ const accessToken = options?.params?.apikey
110
+ if (accessToken) this.accessToken = accessToken
111
+
105
112
  this.reconnectAfterMs = options?.reconnectAfterMs
106
113
  ? options.reconnectAfterMs
107
114
  : (tries: number) => {
@@ -119,6 +126,8 @@ export default class RealtimeClient {
119
126
  this.disconnect()
120
127
  this.connect()
121
128
  }, this.reconnectAfterMs)
129
+
130
+ this.fetch = this._resolveFetch(options?.fetch)
122
131
  }
123
132
 
124
133
  /**
@@ -229,10 +238,6 @@ export default class RealtimeClient {
229
238
  topic: string,
230
239
  params: RealtimeChannelOptions = { config: {} }
231
240
  ): RealtimeChannel {
232
- if (!this.isConnected()) {
233
- this.connect()
234
- }
235
-
236
241
  const chan = new RealtimeChannel(`realtime:${topic}`, params, this)
237
242
  this.channels.push(chan)
238
243
  return chan
@@ -282,6 +287,26 @@ export default class RealtimeClient {
282
287
  })
283
288
  }
284
289
 
290
+ /**
291
+ * Use either custom fetch, if provided, or default fetch to make HTTP requests
292
+ *
293
+ * @internal
294
+ */
295
+ _resolveFetch = (customFetch?: Fetch): Fetch => {
296
+ let _fetch: Fetch
297
+ if (customFetch) {
298
+ _fetch = customFetch
299
+ } else if (typeof fetch === 'undefined') {
300
+ _fetch = (...args) =>
301
+ import('@supabase/node-fetch' as any).then(({ default: fetch }) =>
302
+ fetch(...args)
303
+ )
304
+ } else {
305
+ _fetch = fetch
306
+ }
307
+ return (...args) => _fetch(...args)
308
+ }
309
+
285
310
  /**
286
311
  * Return the next message ref, accounting for overflows
287
312
  *
@@ -1 +1 @@
1
- export const version = '2.7.3'
1
+ export const version = '2.8.0'