@supabase/supabase-js 1.29.3 → 1.30.2

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 +11 -17
  3. package/dist/main/SupabaseClient.d.ts.map +1 -1
  4. package/dist/main/SupabaseClient.js +41 -52
  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 +11 -17
  21. package/dist/module/SupabaseClient.d.ts.map +1 -1
  22. package/dist/module/SupabaseClient.js +40 -51
  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 +51 -77
  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,81 +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
137
  async removeAllSubscriptions(): Promise<
137
- (
138
- | {
139
- status: 'fulfilled'
140
- value: {
141
- error: null
142
- }
143
- }
144
- | { status: 'rejected'; reason: { error: Error } }
145
- )[]
138
+ { data: { subscription: RealtimeSubscription }; error: Error | null }[]
146
139
  > {
147
- const subs: RealtimeSubscription[] = this.realtime.channels.slice()
148
- const removeSubPromises = subs.map((sub: RealtimeSubscription) =>
149
- this.removeSubscription(sub)
150
- .then((): { status: 'fulfilled'; value: { error: null } } => ({
151
- status: 'fulfilled',
152
- value: { error: null },
153
- }))
154
- .catch((reason: { error: Error }): { status: 'rejected'; reason: { error: Error } } => ({
155
- status: 'rejected',
156
- reason,
157
- }))
158
- )
159
- return Promise.all(removeSubPromises)
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
+ })
160
150
  }
161
151
 
162
152
  /**
163
- * Removes an active subscription and returns the number of open connections.
153
+ * Closes and removes a subscription and returns the number of open subscriptions.
164
154
  *
165
- * @param subscription The subscription you want to remove.
155
+ * @param subscription The subscription you want to close and remove.
166
156
  */
167
- removeSubscription(subscription: RealtimeSubscription): Promise<
168
- | {
169
- data: { openSubscriptions: number }
170
- error: null
171
- }
172
- | { error: Error }
173
- > {
174
- return new Promise(async (resolve, reject) => {
175
- const { error } = await this._closeSubscription(subscription)
176
-
177
- if (error) {
178
- return reject({ error })
179
- }
180
-
181
- const allSubscriptions = this.getSubscriptions()
182
- const openSubscriptionsCount = allSubscriptions.filter((chan) => chan.isJoined()).length
183
-
184
- if (allSubscriptions.length === 0) {
185
- const { error } = await this.realtime.disconnect()
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
186
163
 
187
- if (error) {
188
- return reject({ error })
189
- }
190
- }
164
+ if (allSubs.length === 0) await this.realtime.disconnect()
191
165
 
192
- return resolve({
193
- data: { openSubscriptions: openSubscriptionsCount },
194
- error: null,
195
- })
196
- })
166
+ return { data: { openSubscriptions: openSubCount }, error }
197
167
  }
198
168
 
199
169
  private async _closeSubscription(
200
170
  subscription: RealtimeSubscription
201
- ): Promise<{ error: null | Error }> {
171
+ ): Promise<{ error: Error | null }> {
172
+ let error = null
173
+
202
174
  if (!subscription.isClosed()) {
203
- return await this._closeChannel(subscription)
175
+ const { error: unsubError } = await this._unsubscribeSubscription(subscription)
176
+ error = unsubError
204
177
  }
205
178
 
179
+ this.realtime.remove(subscription)
180
+
181
+ return { error }
182
+ }
183
+
184
+ private _unsubscribeSubscription(
185
+ subscription: RealtimeSubscription
186
+ ): Promise<{ error: Error | null }> {
206
187
  return new Promise((resolve) => {
207
- this.realtime.remove(subscription)
208
- return resolve({ error: null })
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') }))
209
193
  })
210
194
  }
211
195
 
@@ -254,24 +238,14 @@ export default class SupabaseClient {
254
238
  })
255
239
  }
256
240
 
257
- private _getAuthHeaders(): { [key: string]: string } {
258
- const headers: { [key: string]: string } = this.headers
241
+ private _getAuthHeaders(): GenericObject {
242
+ const headers: GenericObject = this.headers
259
243
  const authBearer = this.auth.session()?.access_token ?? this.supabaseKey
260
244
  headers['apikey'] = this.supabaseKey
261
245
  headers['Authorization'] = `Bearer ${authBearer}`
262
246
  return headers
263
247
  }
264
248
 
265
- private _closeChannel(subscription: RealtimeSubscription): Promise<{ error: null | Error }> {
266
- return new Promise((resolve, reject) => {
267
- subscription
268
- .unsubscribe()
269
- .receive('ok', () => resolve({ error: null }))
270
- .receive('error', (error: Error) => reject({ error }))
271
- .receive('timeout', () => reject({ error: 'timed out' }))
272
- })
273
- }
274
-
275
249
  private _listenForMultiTabEvents() {
276
250
  if (!this.multiTab || !isBrowser() || !window?.addEventListener) {
277
251
  return null
@@ -324,7 +298,7 @@ export default class SupabaseClient {
324
298
  this.changedAccessToken = token
325
299
  } else if (event === 'SIGNED_OUT' || event === 'USER_DELETED') {
326
300
  // Token is removed
327
- this.removeAllSubscriptions()
301
+ this.realtime.setAuth(this.supabaseKey)
328
302
  if (source == 'STORAGE') this.auth.signOut()
329
303
  }
330
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.3'
2
+ export const version = '1.30.2'