Skip to content

Commit cc72764

Browse files
committed
refactor(supabase): reduce codedup between PostgrestClient and SupabaseClient leverage inference
We currently duplicate a lot of the typing logic between postgrest-js and supabase-js This makes it very hard to change anything type-wise in postgrest-js without needing to update supabase-js as well. This is mainly because we re-declare functions type returns instead of relying over return type inference. With this change, as long as the arguments passed to a rest function don't change, the result type will be infered from the this.rest call. This will allows postgrest-js to alter the result types / implementation details without having to duplicate all changes over to supabase-js.
1 parent 3d70605 commit cc72764

File tree

1 file changed

+31
-46
lines changed

1 file changed

+31
-46
lines changed

packages/core/supabase-js/src/SupabaseClient.ts

Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
1+
import type { AuthChangeEvent } from '@supabase/auth-js'
12
import { FunctionsClient } from '@supabase/functions-js'
2-
import { AuthChangeEvent } from '@supabase/auth-js'
3+
import { PostgrestClient } from '@supabase/postgrest-js'
34
import {
4-
PostgrestClient,
5-
PostgrestFilterBuilder,
6-
PostgrestQueryBuilder,
7-
} from '@supabase/postgrest-js'
8-
import {
9-
RealtimeChannel,
10-
RealtimeChannelOptions,
5+
type RealtimeChannel,
6+
type RealtimeChannelOptions,
117
RealtimeClient,
12-
RealtimeClientOptions,
8+
type RealtimeClientOptions,
139
} from '@supabase/realtime-js'
1410
import { StorageClient as SupabaseStorageClient } from '@supabase/storage-js'
1511
import {
16-
DEFAULT_GLOBAL_OPTIONS,
17-
DEFAULT_DB_OPTIONS,
1812
DEFAULT_AUTH_OPTIONS,
13+
DEFAULT_DB_OPTIONS,
14+
DEFAULT_GLOBAL_OPTIONS,
1915
DEFAULT_REALTIME_OPTIONS,
2016
} from './lib/constants'
2117
import { fetchWithAuth } from './lib/fetch'
2218
import { applySettingDefaults, validateSupabaseUrl } from './lib/helpers'
2319
import { SupabaseAuthClient } from './lib/SupabaseAuthClient'
24-
import { Fetch, GenericSchema, SupabaseClientOptions, SupabaseAuthClientOptions } from './lib/types'
20+
import type {
21+
Fetch,
22+
GenericSchema,
23+
SupabaseAuthClientOptions,
24+
SupabaseClientOptions,
25+
} from './lib/types'
2526

2627
/**
2728
* Supabase Client.
@@ -178,20 +179,14 @@ export default class SupabaseClient<
178179
})
179180
}
180181

181-
// NOTE: signatures must be kept in sync with PostgrestClient.from
182-
from<
183-
TableName extends string & keyof Schema['Tables'],
184-
Table extends Schema['Tables'][TableName],
185-
>(relation: TableName): PostgrestQueryBuilder<ClientOptions, Schema, Table, TableName>
186-
from<ViewName extends string & keyof Schema['Views'], View extends Schema['Views'][ViewName]>(
187-
relation: ViewName
188-
): PostgrestQueryBuilder<ClientOptions, Schema, View, ViewName>
189182
/**
190183
* Perform a query on a table or a view.
191184
*
192185
* @param relation - The table or view name to query
193186
*/
194-
from(relation: string): PostgrestQueryBuilder<ClientOptions, Schema, any> {
187+
from<RelationName extends string & (keyof Schema['Tables'] | keyof Schema['Views'])>(
188+
relation: RelationName
189+
) {
195190
return this.rest.from(relation)
196191
}
197192

@@ -205,13 +200,8 @@ export default class SupabaseClient<
205200
*/
206201
schema<DynamicSchema extends string & keyof Omit<Database, '__InternalSupabase'>>(
207202
schema: DynamicSchema
208-
): PostgrestClient<
209-
Database,
210-
ClientOptions,
211-
DynamicSchema,
212-
Database[DynamicSchema] extends GenericSchema ? Database[DynamicSchema] : any
213-
> {
214-
return this.rest.schema<DynamicSchema>(schema)
203+
) {
204+
return this.rest.schema(schema)
215205
}
216206

217207
// NOTE: signatures must be kept in sync with PostgrestClient.rpc
@@ -238,27 +228,22 @@ export default class SupabaseClient<
238228
* `"estimated"`: Uses exact count for low numbers and planned count for high
239229
* numbers.
240230
*/
241-
rpc<FnName extends string & keyof Schema['Functions'], Fn extends Schema['Functions'][FnName]>(
231+
rpc<
232+
FnName extends string & keyof Schema['Functions'],
233+
Args extends Schema['Functions'][FnName]['Args'] = never,
234+
>(
242235
fn: FnName,
243-
args: Fn['Args'] = {},
236+
args: Args = {} as Args,
244237
options: {
245238
head?: boolean
246239
get?: boolean
247240
count?: 'exact' | 'planned' | 'estimated'
248-
} = {}
249-
): PostgrestFilterBuilder<
250-
ClientOptions,
251-
Schema,
252-
Fn['Returns'] extends any[]
253-
? Fn['Returns'][number] extends Record<string, unknown>
254-
? Fn['Returns'][number]
255-
: never
256-
: never,
257-
Fn['Returns'],
258-
FnName,
259-
null,
260-
'RPC'
261-
> {
241+
} = {
242+
head: false,
243+
get: false,
244+
count: undefined,
245+
}
246+
) {
262247
return this.rest.rpc(fn, args, options)
263248
}
264249

@@ -355,7 +340,7 @@ export default class SupabaseClient<
355340
}
356341

357342
private _listenForAuthEvents() {
358-
let data = this.auth.onAuthStateChange((event, session) => {
343+
const data = this.auth.onAuthStateChange((event, session) => {
359344
this._handleTokenChanged(event, 'CLIENT', session?.access_token)
360345
})
361346
return data
@@ -374,7 +359,7 @@ export default class SupabaseClient<
374359
this.realtime.setAuth(token)
375360
} else if (event === 'SIGNED_OUT') {
376361
this.realtime.setAuth()
377-
if (source == 'STORAGE') this.auth.signOut()
362+
if (source === 'STORAGE') this.auth.signOut()
378363
this.changedAccessToken = undefined
379364
}
380365
}

0 commit comments

Comments
 (0)