@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.
- package/README.md +14 -0
- package/dist/main/SupabaseClient.d.ts +17 -6
- package/dist/main/SupabaseClient.d.ts.map +1 -1
- package/dist/main/SupabaseClient.js +42 -43
- package/dist/main/SupabaseClient.js.map +1 -1
- package/dist/main/index.d.ts.map +1 -1
- package/dist/main/index.js.map +1 -1
- package/dist/main/lib/SupabaseQueryBuilder.d.ts +2 -4
- package/dist/main/lib/SupabaseQueryBuilder.d.ts.map +1 -1
- package/dist/main/lib/SupabaseRealtimeClient.d.ts +2 -4
- package/dist/main/lib/SupabaseRealtimeClient.d.ts.map +1 -1
- package/dist/main/lib/SupabaseRealtimeClient.js.map +1 -1
- package/dist/main/lib/helpers.d.ts.map +1 -1
- package/dist/main/lib/helpers.js +1 -2
- package/dist/main/lib/helpers.js.map +1 -1
- package/dist/main/lib/types.d.ts +5 -4
- package/dist/main/lib/types.d.ts.map +1 -1
- package/dist/main/lib/version.d.ts +1 -1
- package/dist/main/lib/version.js +1 -1
- package/dist/module/SupabaseClient.d.ts +17 -6
- package/dist/module/SupabaseClient.d.ts.map +1 -1
- package/dist/module/SupabaseClient.js +41 -42
- package/dist/module/SupabaseClient.js.map +1 -1
- package/dist/module/index.d.ts.map +1 -1
- package/dist/module/index.js.map +1 -1
- package/dist/module/lib/SupabaseQueryBuilder.d.ts +2 -4
- package/dist/module/lib/SupabaseQueryBuilder.d.ts.map +1 -1
- package/dist/module/lib/SupabaseRealtimeClient.d.ts +2 -4
- package/dist/module/lib/SupabaseRealtimeClient.d.ts.map +1 -1
- package/dist/module/lib/SupabaseRealtimeClient.js.map +1 -1
- package/dist/module/lib/helpers.d.ts.map +1 -1
- package/dist/module/lib/types.d.ts +5 -4
- package/dist/module/lib/types.d.ts.map +1 -1
- package/dist/module/lib/version.d.ts +1 -1
- package/dist/module/lib/version.js +1 -1
- package/dist/umd/supabase.js +1 -1
- package/package.json +4 -4
- package/src/SupabaseClient.ts +55 -46
- package/src/index.ts +1 -1
- package/src/lib/SupabaseQueryBuilder.ts +3 -3
- package/src/lib/SupabaseRealtimeClient.ts +3 -8
- package/src/lib/helpers.ts +1 -1
- package/src/lib/types.ts +5 -3
- package/src/lib/version.ts +1 -1
package/src/SupabaseClient.ts
CHANGED
|
@@ -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
|
-
|
|
66
|
-
|
|
65
|
+
const _supabaseUrl = stripTrailingSlash(supabaseUrl)
|
|
67
66
|
const settings = { ...DEFAULT_OPTIONS, ...options }
|
|
68
|
-
|
|
69
|
-
this.
|
|
70
|
-
this.
|
|
71
|
-
this.
|
|
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
|
-
*
|
|
134
|
+
* Closes and removes all subscriptions and returns a list of removed
|
|
135
|
+
* subscriptions and their errors.
|
|
135
136
|
*/
|
|
136
|
-
async removeAllSubscriptions()
|
|
137
|
-
|
|
138
|
-
|
|
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
|
-
*
|
|
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(
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
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
|
-
|
|
152
|
-
const openSubscriptionsCount = allSubscriptions.filter((chan) => chan.isJoined()).length
|
|
164
|
+
if (allSubs.length === 0) await this.realtime.disconnect()
|
|
153
165
|
|
|
154
|
-
|
|
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(
|
|
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.
|
|
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
|
-
|
|
172
|
-
|
|
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():
|
|
222
|
-
const 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.
|
|
301
|
+
this.realtime.setAuth(this.supabaseKey)
|
|
293
302
|
if (source == 'STORAGE') this.auth.signOut()
|
|
294
303
|
}
|
|
295
304
|
}
|
package/src/index.ts
CHANGED
|
@@ -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:
|
|
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?:
|
|
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
|
-
|
|
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
|
|
package/src/lib/helpers.ts
CHANGED
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?:
|
|
20
|
+
headers?: GenericObject
|
|
19
21
|
/**
|
|
20
22
|
* Automatically refreshes the token for logged in users.
|
|
21
23
|
*/
|
package/src/lib/version.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// generated by genversion
|
|
2
|
-
export const version = '1.
|
|
2
|
+
export const version = '1.30.1'
|