1
1
import { FunctionsClient } from '@supabase/functions-js'
2
2
import { AuthChangeEvent } from '@supabase/gotrue-js'
3
- import { PostgrestClient } from '@supabase/postgrest-js'
4
- import {
5
- RealtimeChannel ,
6
- RealtimeClient ,
7
- RealtimeClientOptions ,
8
- RealtimeSubscription ,
9
- } from '@supabase/realtime-js'
3
+ import { PostgrestClient , PostgrestQueryBuilder } from '@supabase/postgrest-js'
4
+ import { RealtimeChannel , RealtimeClient , RealtimeClientOptions } from '@supabase/realtime-js'
10
5
import { SupabaseStorageClient } from '@supabase/storage-js'
11
6
import { DEFAULT_HEADERS , STORAGE_KEY } from './lib/constants'
12
7
import { fetchWithAuth } from './lib/fetch'
13
8
import { isBrowser , stripTrailingSlash } from './lib/helpers'
14
9
import { SupabaseAuthClient } from './lib/SupabaseAuthClient'
15
- import { SupabaseQueryBuilder } from './lib/SupabaseQueryBuilder '
10
+ import { SupabaseRealtimeClient } from './lib/SupabaseRealtimeClient '
16
11
import { Fetch , SupabaseClientOptions } from './lib/types'
17
12
18
13
const DEFAULT_OPTIONS = {
@@ -129,13 +124,11 @@ export default class SupabaseClient {
129
124
*
130
125
* @param table The table name to operate on.
131
126
*/
132
- from < T = any > ( table : string ) : SupabaseQueryBuilder < T > {
127
+ from < T = any > ( table : string ) : PostgrestQueryBuilder < T > {
133
128
const url = `${ this . restUrl } /${ table } `
134
- return new SupabaseQueryBuilder < T > ( url , {
129
+ return new PostgrestQueryBuilder < T > ( url , {
135
130
headers : this . headers ,
136
131
schema : this . schema ,
137
- realtime : this . realtime ,
138
- table,
139
132
fetch : this . fetch ,
140
133
shouldThrowOnError : this . shouldThrowOnError ,
141
134
} )
@@ -164,32 +157,31 @@ export default class SupabaseClient {
164
157
165
158
/**
166
159
* Creates a channel with Broadcast and Presence.
167
- * Activated when vsndate query param is present in the WebSocket URL.
168
160
*/
169
- channel ( name : string , opts : { selfBroadcast : boolean ; [ key : string ] : any } ) : RealtimeChannel {
170
- const userToken = this . auth . session ( ) ?. access_token ?? this . supabaseKey
161
+ channel ( name : string , opts ? : { [ key : string ] : any } ) : SupabaseRealtimeClient {
162
+ const token = this . realtime . accessToken ?? this . supabaseKey
171
163
172
164
if ( ! this . realtime . isConnected ( ) ) {
173
165
this . realtime . connect ( )
174
166
}
175
167
176
- return this . realtime . channel ( name , { ... opts , user_token : userToken } ) as RealtimeChannel
168
+ return new SupabaseRealtimeClient ( this . realtime , name , token , opts )
177
169
}
178
170
179
171
/**
180
- * Closes and removes all subscriptions and returns a list of removed
181
- * subscriptions and their errors.
172
+ * Closes and removes all channels and returns a list of removed
173
+ * channels and their errors.
182
174
*/
183
- async removeAllSubscriptions ( ) : Promise <
184
- { data : { subscription : RealtimeSubscription } ; error : Error | null } [ ]
175
+ async removeAllChannels ( ) : Promise <
176
+ { data : { channels : RealtimeChannel } ; error : Error | null } [ ]
185
177
> {
186
- const allSubs : RealtimeSubscription [ ] = this . getSubscriptions ( ) . slice ( )
187
- const allSubPromises = allSubs . map ( ( sub ) => this . removeSubscription ( sub ) )
188
- const allRemovedSubs = await Promise . all ( allSubPromises )
178
+ const allChans : RealtimeChannel [ ] = this . getChannels ( ) . slice ( )
179
+ const allChanPromises = allChans . map ( ( chan ) => this . removeChannel ( chan ) )
180
+ const allRemovedChans = await Promise . all ( allChanPromises )
189
181
190
- return allRemovedSubs . map ( ( { error } , i ) => {
182
+ return allRemovedChans . map ( ( { error } , i ) => {
191
183
return {
192
- data : { subscription : allSubs [ i ] } ,
184
+ data : { channels : allChans [ i ] } ,
193
185
error,
194
186
}
195
187
} )
@@ -203,58 +195,37 @@ export default class SupabaseClient {
203
195
async removeChannel (
204
196
channel : RealtimeChannel
205
197
) : Promise < { data : { openChannels : number } ; error : Error | null } > {
206
- const { error } = await this . _closeSubscription ( channel )
207
- const allChans : RealtimeSubscription [ ] = this . getSubscriptions ( )
198
+ const { error } = await this . _closeChannel ( channel )
199
+ const allChans : RealtimeChannel [ ] = this . getChannels ( )
208
200
const openChanCount = allChans . filter ( ( chan ) => chan . isJoined ( ) ) . length
209
201
210
202
if ( allChans . length === 0 ) await this . realtime . disconnect ( )
211
203
212
204
return { data : { openChannels : openChanCount } , error }
213
205
}
214
206
215
- /**
216
- * Closes and removes a subscription and returns the number of open subscriptions.
217
- *
218
- * @param subscription The subscription you want to close and remove.
219
- */
220
- async removeSubscription (
221
- subscription : RealtimeSubscription
222
- ) : Promise < { data : { openSubscriptions : number } ; error : Error | null } > {
223
- const { error } = await this . _closeSubscription ( subscription )
224
- const allSubs : RealtimeSubscription [ ] = this . getSubscriptions ( )
225
- const openSubCount = allSubs . filter ( ( chan ) => chan . isJoined ( ) ) . length
226
-
227
- if ( allSubs . length === 0 ) await this . realtime . disconnect ( )
228
-
229
- return { data : { openSubscriptions : openSubCount } , error }
230
- }
231
-
232
207
private async _getAccessToken ( ) {
233
208
const { session } = await this . auth . getSession ( )
234
209
235
210
return session ?. access_token ?? null
236
211
}
237
212
238
- private async _closeSubscription (
239
- subscription : RealtimeSubscription | RealtimeChannel
240
- ) : Promise < { error : Error | null } > {
213
+ private async _closeChannel ( channel : RealtimeChannel ) : Promise < { error : Error | null } > {
241
214
let error = null
242
215
243
- if ( ! subscription . isClosed ( ) ) {
244
- const { error : unsubError } = await this . _unsubscribeSubscription ( subscription )
216
+ if ( ! channel . isClosed ( ) ) {
217
+ const { error : unsubError } = await this . _unsubscribeChannel ( channel )
245
218
error = unsubError
246
219
}
247
220
248
- this . realtime . remove ( subscription )
221
+ this . realtime . remove ( channel )
249
222
250
223
return { error }
251
224
}
252
225
253
- private _unsubscribeSubscription (
254
- subscription : RealtimeSubscription | RealtimeChannel
255
- ) : Promise < { error : Error | null } > {
226
+ private _unsubscribeChannel ( channel : RealtimeChannel ) : Promise < { error : Error | null } > {
256
227
return new Promise ( ( resolve ) => {
257
- subscription
228
+ channel
258
229
. unsubscribe ( )
259
230
. receive ( 'ok' , ( ) => resolve ( { error : null } ) )
260
231
. receive ( 'error' , ( error : Error ) => resolve ( { error } ) )
@@ -263,10 +234,10 @@ export default class SupabaseClient {
263
234
}
264
235
265
236
/**
266
- * Returns an array of all your subscriptions .
237
+ * Returns an array of all your channels .
267
238
*/
268
- getSubscriptions ( ) : RealtimeSubscription [ ] {
269
- return this . realtime . channels as RealtimeSubscription [ ]
239
+ getChannels ( ) : RealtimeChannel [ ] {
240
+ return this . realtime . channels as RealtimeChannel [ ]
270
241
}
271
242
272
243
private _initSupabaseAuthClient ( {
@@ -299,7 +270,7 @@ export default class SupabaseClient {
299
270
private _initRealtimeClient ( options ?: RealtimeClientOptions ) {
300
271
return new RealtimeClient ( this . realtimeUrl , {
301
272
...options ,
302
- params : { ...options ?. params , apikey : this . supabaseKey } ,
273
+ params : { ...{ apikey : this . supabaseKey , vsndate : '2022' } , ... options ?. params } ,
303
274
} )
304
275
}
305
276
0 commit comments