Skip to content

Commit e4d41d5

Browse files
authored
Merge pull request #258 from ftonato/refactor/create-new-type
refactor: create a new type to avoid repeat ourselves and add the correct return type for createClient method
2 parents b7f693f + ad98708 commit e4d41d5

File tree

4 files changed

+13
-10
lines changed

4 files changed

+13
-10
lines changed

src/SupabaseClient.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { DEFAULT_HEADERS, STORAGE_KEY } from './lib/constants'
22
import { stripTrailingSlash, isBrowser } from './lib/helpers'
3-
import { Fetch, SupabaseClientOptions } from './lib/types'
3+
import { Fetch, GenericObject, SupabaseClientOptions } from './lib/types'
44
import { SupabaseAuthClient } from './lib/SupabaseAuthClient'
55
import { SupabaseQueryBuilder } from './lib/SupabaseQueryBuilder'
66
import { SupabaseStorageClient } from '@supabase/storage-js'
@@ -179,7 +179,6 @@ export default class SupabaseClient {
179179
}
180180

181181
const allSubscriptions = this.getSubscriptions()
182-
const openSubscriptionsCount = allSubscriptions.filter((chan) => chan.isJoined()).length
183182

184183
if (allSubscriptions.length === 0) {
185184
const { error } = await this.realtime.disconnect()
@@ -189,6 +188,8 @@ export default class SupabaseClient {
189188
}
190189
}
191190

191+
const openSubscriptionsCount = allSubscriptions.filter((chan) => chan.isJoined()).length
192+
192193
return resolve({
193194
data: { openSubscriptions: openSubscriptionsCount },
194195
error: null,
@@ -254,8 +255,8 @@ export default class SupabaseClient {
254255
})
255256
}
256257

257-
private _getAuthHeaders(): { [key: string]: string } {
258-
const headers: { [key: string]: string } = this.headers
258+
private _getAuthHeaders(): GenericObject {
259+
const headers: GenericObject = this.headers
259260
const authBearer = this.auth.session()?.access_token ?? this.supabaseKey
260261
headers['apikey'] = this.supabaseKey
261262
headers['Authorization'] = `Bearer ${authBearer}`

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const createClient = (
1717
supabaseUrl: string,
1818
supabaseKey: string,
1919
options?: SupabaseClientOptions
20-
) => {
20+
): SupabaseClient => {
2121
return new SupabaseClient(supabaseUrl, supabaseKey, options)
2222
}
2323

src/lib/SupabaseQueryBuilder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { PostgrestQueryBuilder } from '@supabase/postgrest-js'
22
import { SupabaseRealtimeClient } from './SupabaseRealtimeClient'
33
import { RealtimeClient } from '@supabase/realtime-js'
4-
import { Fetch, SupabaseEventTypes, SupabaseRealtimePayload } from './types'
4+
import { Fetch, GenericObject, SupabaseEventTypes, SupabaseRealtimePayload } from './types'
55

66
export class SupabaseQueryBuilder<T> extends PostgrestQueryBuilder<T> {
77
private _subscription: SupabaseRealtimeClient | null = null
@@ -19,7 +19,7 @@ export class SupabaseQueryBuilder<T> extends PostgrestQueryBuilder<T> {
1919
table,
2020
fetch,
2121
}: {
22-
headers?: { [key: string]: string }
22+
headers?: GenericObject
2323
schema: string
2424
realtime: RealtimeClient
2525
table: string

src/lib/types.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { GoTrueClient } from '@supabase/gotrue-js'
22
import { RealtimeClientOptions } from '@supabase/realtime-js'
33

4-
export type Fetch = typeof fetch
5-
64
type GoTrueClientOptions = ConstructorParameters<typeof GoTrueClient>[0]
75

86
export interface SupabaseAuthClientOptions extends GoTrueClientOptions {}
97

8+
export type GenericObject = { [key: string]: string }
9+
10+
export type Fetch = typeof fetch
11+
1012
export type SupabaseClientOptions = {
1113
/**
1214
* 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 = {
1517
/**
1618
* Optional headers for initializing the client.
1719
*/
18-
headers?: { [key: string]: string }
20+
headers?: GenericObject
1921
/**
2022
* Automatically refreshes the token for logged in users.
2123
*/

0 commit comments

Comments
 (0)