@supabase/supabase-js 1.29.2 → 1.30.1

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.
Files changed (44) hide show
  1. package/README.md +14 -0
  2. package/dist/main/SupabaseClient.d.ts +17 -6
  3. package/dist/main/SupabaseClient.d.ts.map +1 -1
  4. package/dist/main/SupabaseClient.js +42 -43
  5. package/dist/main/SupabaseClient.js.map +1 -1
  6. package/dist/main/index.d.ts.map +1 -1
  7. package/dist/main/index.js.map +1 -1
  8. package/dist/main/lib/SupabaseQueryBuilder.d.ts +2 -4
  9. package/dist/main/lib/SupabaseQueryBuilder.d.ts.map +1 -1
  10. package/dist/main/lib/SupabaseRealtimeClient.d.ts +2 -4
  11. package/dist/main/lib/SupabaseRealtimeClient.d.ts.map +1 -1
  12. package/dist/main/lib/SupabaseRealtimeClient.js.map +1 -1
  13. package/dist/main/lib/helpers.d.ts.map +1 -1
  14. package/dist/main/lib/helpers.js +1 -2
  15. package/dist/main/lib/helpers.js.map +1 -1
  16. package/dist/main/lib/types.d.ts +5 -4
  17. package/dist/main/lib/types.d.ts.map +1 -1
  18. package/dist/main/lib/version.d.ts +1 -1
  19. package/dist/main/lib/version.js +1 -1
  20. package/dist/module/SupabaseClient.d.ts +17 -6
  21. package/dist/module/SupabaseClient.d.ts.map +1 -1
  22. package/dist/module/SupabaseClient.js +41 -42
  23. package/dist/module/SupabaseClient.js.map +1 -1
  24. package/dist/module/index.d.ts.map +1 -1
  25. package/dist/module/index.js.map +1 -1
  26. package/dist/module/lib/SupabaseQueryBuilder.d.ts +2 -4
  27. package/dist/module/lib/SupabaseQueryBuilder.d.ts.map +1 -1
  28. package/dist/module/lib/SupabaseRealtimeClient.d.ts +2 -4
  29. package/dist/module/lib/SupabaseRealtimeClient.d.ts.map +1 -1
  30. package/dist/module/lib/SupabaseRealtimeClient.js.map +1 -1
  31. package/dist/module/lib/helpers.d.ts.map +1 -1
  32. package/dist/module/lib/types.d.ts +5 -4
  33. package/dist/module/lib/types.d.ts.map +1 -1
  34. package/dist/module/lib/version.d.ts +1 -1
  35. package/dist/module/lib/version.js +1 -1
  36. package/dist/umd/supabase.js +1 -1
  37. package/package.json +4 -4
  38. package/src/SupabaseClient.ts +55 -46
  39. package/src/index.ts +1 -1
  40. package/src/lib/SupabaseQueryBuilder.ts +3 -3
  41. package/src/lib/SupabaseRealtimeClient.ts +3 -8
  42. package/src/lib/helpers.ts +1 -1
  43. package/src/lib/types.ts +5 -3
  44. package/src/lib/version.ts +1 -1
@@ -1,6 +1,6 @@
1
1
  import { DEFAULT_HEADERS, STORAGE_KEY } from './lib/constants'
2
2
  import { stripTrailingSlash, isBrowser } from './lib/helpers'
3
- import { Fetch, SupabaseClientOptions } from './lib/types'
3
+ import { Fetch, GenericObject, SupabaseClientOptions } from './lib/types'
4
4
  import { SupabaseAuthClient } from './lib/SupabaseAuthClient'
5
5
  import { SupabaseQueryBuilder } from './lib/SupabaseQueryBuilder'
6
6
  import { SupabaseStorageClient } from '@supabase/storage-js'
@@ -62,13 +62,13 @@ export default class SupabaseClient {
62
62
  if (!supabaseUrl) throw new Error('supabaseUrl is required.')
63
63
  if (!supabaseKey) throw new Error('supabaseKey is required.')
64
64
 
65
- supabaseUrl = stripTrailingSlash(supabaseUrl)
66
-
65
+ const _supabaseUrl = stripTrailingSlash(supabaseUrl)
67
66
  const settings = { ...DEFAULT_OPTIONS, ...options }
68
- this.restUrl = `${supabaseUrl}/rest/v1`
69
- this.realtimeUrl = `${supabaseUrl}/realtime/v1`.replace('http', 'ws')
70
- this.authUrl = `${supabaseUrl}/auth/v1`
71
- this.storageUrl = `${supabaseUrl}/storage/v1`
67
+
68
+ this.restUrl = `${_supabaseUrl}/rest/v1`
69
+ this.realtimeUrl = `${_supabaseUrl}/realtime/v1`.replace('http', 'ws')
70
+ this.authUrl = `${_supabaseUrl}/auth/v1`
71
+ this.storageUrl = `${_supabaseUrl}/storage/v1`
72
72
  this.schema = settings.schema
73
73
  this.multiTab = settings.multiTab
74
74
  this.fetch = settings.fetch
@@ -131,45 +131,65 @@ export default class SupabaseClient {
131
131
  }
132
132
 
133
133
  /**
134
- * Remove all subscriptions.
134
+ * Closes and removes all subscriptions and returns a list of removed
135
+ * subscriptions and their errors.
135
136
  */
136
- async removeAllSubscriptions() {
137
- const subscriptions = this.realtime.channels.slice()
138
- return await Promise.allSettled(subscriptions.map((sub) => this.removeSubscription(sub)))
137
+ async removeAllSubscriptions(): Promise<
138
+ { data: { subscription: RealtimeSubscription }; error: Error | null }[]
139
+ > {
140
+ const allSubs: RealtimeSubscription[] = this.getSubscriptions().slice()
141
+ const allSubPromises = allSubs.map((sub) => this.removeSubscription(sub))
142
+ const allRemovedSubs = await Promise.all(allSubPromises)
143
+
144
+ return allRemovedSubs.map(({ error }, i) => {
145
+ return {
146
+ data: { subscription: allSubs[i] },
147
+ error,
148
+ }
149
+ })
139
150
  }
140
151
 
141
152
  /**
142
- * Removes an active subscription and returns the number of open connections.
153
+ * Closes and removes a subscription and returns the number of open subscriptions.
143
154
  *
144
- * @param subscription The subscription you want to remove.
155
+ * @param subscription The subscription you want to close and remove.
145
156
  */
146
- removeSubscription(subscription: RealtimeSubscription) {
147
- return new Promise(async (resolve) => {
148
- try {
149
- await this._closeSubscription(subscription)
157
+ async removeSubscription(
158
+ subscription: RealtimeSubscription
159
+ ): Promise<{ data: { openSubscriptions: number }; error: Error | null }> {
160
+ const { error } = await this._closeSubscription(subscription)
161
+ const allSubs: RealtimeSubscription[] = this.getSubscriptions()
162
+ const openSubCount = allSubs.filter((chan) => chan.isJoined()).length
150
163
 
151
- const allSubscriptions = this.getSubscriptions()
152
- const openSubscriptionsCount = allSubscriptions.filter((chan) => chan.isJoined()).length
164
+ if (allSubs.length === 0) await this.realtime.disconnect()
153
165
 
154
- if (!allSubscriptions.length) {
155
- const { error } = await this.realtime.disconnect()
156
- if (error) return resolve({ error })
157
- }
158
- return resolve({ error: null, data: { openSubscriptions: openSubscriptionsCount } })
159
- } catch (error) {
160
- return resolve({ error })
161
- }
162
- })
166
+ return { data: { openSubscriptions: openSubCount }, error }
163
167
  }
164
168
 
165
- private async _closeSubscription(subscription: RealtimeSubscription) {
169
+ private async _closeSubscription(
170
+ subscription: RealtimeSubscription
171
+ ): Promise<{ error: Error | null }> {
172
+ let error = null
173
+
166
174
  if (!subscription.isClosed()) {
167
- await this._closeChannel(subscription)
175
+ const { error: unsubError } = await this._unsubscribeSubscription(subscription)
176
+ error = unsubError
168
177
  }
169
178
 
179
+ this.realtime.remove(subscription)
180
+
181
+ return { error }
182
+ }
183
+
184
+ private _unsubscribeSubscription(
185
+ subscription: RealtimeSubscription
186
+ ): Promise<{ error: Error | null }> {
170
187
  return new Promise((resolve) => {
171
- this.realtime.remove(subscription)
172
- return resolve(true)
188
+ subscription
189
+ .unsubscribe()
190
+ .receive('ok', () => resolve({ error: null }))
191
+ .receive('error', (error: Error) => resolve({ error }))
192
+ .receive('timeout', () => resolve({ error: new Error('timed out') }))
173
193
  })
174
194
  }
175
195
 
@@ -218,25 +238,14 @@ export default class SupabaseClient {
218
238
  })
219
239
  }
220
240
 
221
- private _getAuthHeaders(): { [key: string]: string } {
222
- const headers: { [key: string]: string } = this.headers
241
+ private _getAuthHeaders(): GenericObject {
242
+ const headers: GenericObject = this.headers
223
243
  const authBearer = this.auth.session()?.access_token ?? this.supabaseKey
224
244
  headers['apikey'] = this.supabaseKey
225
245
  headers['Authorization'] = `Bearer ${authBearer}`
226
246
  return headers
227
247
  }
228
248
 
229
- private _closeChannel(subscription: RealtimeSubscription) {
230
- return new Promise((resolve, reject) => {
231
- subscription
232
- .unsubscribe()
233
- .receive('ok', () => {
234
- return resolve(true)
235
- })
236
- .receive('error', (e: Error) => reject(e))
237
- })
238
- }
239
-
240
249
  private _listenForMultiTabEvents() {
241
250
  if (!this.multiTab || !isBrowser() || !window?.addEventListener) {
242
251
  return null
@@ -289,7 +298,7 @@ export default class SupabaseClient {
289
298
  this.changedAccessToken = token
290
299
  } else if (event === 'SIGNED_OUT' || event === 'USER_DELETED') {
291
300
  // Token is removed
292
- this.removeAllSubscriptions()
301
+ this.realtime.setAuth(this.supabaseKey)
293
302
  if (source == 'STORAGE') this.auth.signOut()
294
303
  }
295
304
  }
package/src/index.ts CHANGED
@@ -17,7 +17,7 @@ const createClient = (
17
17
  supabaseUrl: string,
18
18
  supabaseKey: string,
19
19
  options?: SupabaseClientOptions
20
- ) => {
20
+ ): SupabaseClient => {
21
21
  return new SupabaseClient(supabaseUrl, supabaseKey, options)
22
22
  }
23
23
 
@@ -1,12 +1,12 @@
1
1
  import { PostgrestQueryBuilder } from '@supabase/postgrest-js'
2
2
  import { SupabaseRealtimeClient } from './SupabaseRealtimeClient'
3
3
  import { RealtimeClient } from '@supabase/realtime-js'
4
- import { Fetch, SupabaseEventTypes, SupabaseRealtimePayload } from './types'
4
+ import { Fetch, GenericObject, SupabaseEventTypes, SupabaseRealtimePayload } from './types'
5
5
 
6
6
  export class SupabaseQueryBuilder<T> extends PostgrestQueryBuilder<T> {
7
7
  private _subscription: SupabaseRealtimeClient | null = null
8
8
  private _realtime: RealtimeClient
9
- private _headers: { [key: string]: string }
9
+ private _headers: GenericObject
10
10
  private _schema: string
11
11
  private _table: string
12
12
 
@@ -19,7 +19,7 @@ export class SupabaseQueryBuilder<T> extends PostgrestQueryBuilder<T> {
19
19
  table,
20
20
  fetch,
21
21
  }: {
22
- headers?: { [key: string]: string }
22
+ headers?: GenericObject
23
23
  schema: string
24
24
  realtime: RealtimeClient
25
25
  table: string
@@ -1,16 +1,11 @@
1
1
  import { RealtimeSubscription, RealtimeClient, Transformers } from '@supabase/realtime-js'
2
- import { SupabaseEventTypes, SupabaseRealtimePayload } from './types'
2
+ import { GenericObject, SupabaseEventTypes, SupabaseRealtimePayload } from './types'
3
3
 
4
4
  export class SupabaseRealtimeClient {
5
5
  subscription: RealtimeSubscription
6
6
 
7
- constructor(
8
- socket: RealtimeClient,
9
- headers: { [key: string]: string },
10
- schema: string,
11
- tableName: string
12
- ) {
13
- const chanParams: { [key: string]: string } = {}
7
+ constructor(socket: RealtimeClient, headers: GenericObject, schema: string, tableName: string) {
8
+ const chanParams: GenericObject = {}
14
9
  const topic = tableName === '*' ? `realtime:${schema}` : `realtime:${schema}:${tableName}`
15
10
  const userToken = headers['Authorization'].split(' ')[1]
16
11
 
@@ -8,7 +8,7 @@ export function uuid() {
8
8
  })
9
9
  }
10
10
 
11
- export function stripTrailingSlash(url: string) {
11
+ export function stripTrailingSlash(url: string): string {
12
12
  return url.replace(/\/$/, '')
13
13
  }
14
14
 
package/src/lib/types.ts CHANGED
@@ -1,12 +1,14 @@
1
1
  import { GoTrueClient } from '@supabase/gotrue-js'
2
2
  import { RealtimeClientOptions } from '@supabase/realtime-js'
3
3
 
4
- export type Fetch = typeof fetch
5
-
6
4
  type GoTrueClientOptions = ConstructorParameters<typeof GoTrueClient>[0]
7
5
 
8
6
  export interface SupabaseAuthClientOptions extends GoTrueClientOptions {}
9
7
 
8
+ export type GenericObject = { [key: string]: string }
9
+
10
+ export type Fetch = typeof fetch
11
+
10
12
  export type SupabaseClientOptions = {
11
13
  /**
12
14
  * The Postgres schema which your tables belong to. Must be on the list of exposed schemas in Supabase. Defaults to 'public'.
@@ -15,7 +17,7 @@ export type SupabaseClientOptions = {
15
17
  /**
16
18
  * Optional headers for initializing the client.
17
19
  */
18
- headers?: { [key: string]: string }
20
+ headers?: GenericObject
19
21
  /**
20
22
  * Automatically refreshes the token for logged in users.
21
23
  */
@@ -1,2 +1,2 @@
1
1
  // generated by genversion
2
- export const version = '1.29.2'
2
+ export const version = '1.30.1'