diff --git a/src/SupabaseClient.ts b/src/SupabaseClient.ts index 3acd10edf..9877bf334 100644 --- a/src/SupabaseClient.ts +++ b/src/SupabaseClient.ts @@ -4,6 +4,7 @@ import { PostgrestClient, PostgrestFilterBuilder, PostgrestQueryBuilder, + PostgrestQueryBuilderOptions, } from '@supabase/postgrest-js' import { RealtimeChannel, @@ -144,7 +145,7 @@ export default class SupabaseClient< }) } - this.fetch = fetchWithAuth(supabaseKey, this._getAccessToken.bind(this), settings.global.fetch) + this.fetch = this._createFetchWithAuth(settings.global.fetch) this.realtime = this._initRealtimeClient({ headers: this.headers, accessToken: this._getAccessToken.bind(this), @@ -182,17 +183,28 @@ export default class SupabaseClient< from< TableName extends string & keyof Schema['Tables'], Table extends Schema['Tables'][TableName] - >(relation: TableName): PostgrestQueryBuilder + >( + relation: TableName, + options?: PostgrestQueryBuilderOptions + ): PostgrestQueryBuilder from( - relation: ViewName + relation: ViewName, + options?: PostgrestQueryBuilderOptions ): PostgrestQueryBuilder /** * Perform a query on a table or a view. * * @param relation - The table or view name to query */ - from(relation: string): PostgrestQueryBuilder { - return this.rest.from(relation) + from( + relation: string, + options?: PostgrestQueryBuilderOptions + ): PostgrestQueryBuilder { + if (options?.fetch) { + options.fetch = this._createFetchWithAuth(options.fetch) + } + + return this.rest.from(relation, options) } // NOTE: signatures must be kept in sync with PostgrestClient.schema @@ -378,4 +390,8 @@ export default class SupabaseClient< this.changedAccessToken = undefined } } + + private _createFetchWithAuth(_fetch?: typeof global.fetch) { + return fetchWithAuth(this.supabaseKey, this._getAccessToken.bind(this), _fetch) + } }