diff --git a/src/SupabaseClient.ts b/src/SupabaseClient.ts index df7bb15f..0e8a8dfc 100644 --- a/src/SupabaseClient.ts +++ b/src/SupabaseClient.ts @@ -4,6 +4,7 @@ import { PostgrestClient, PostgrestFilterBuilder, PostgrestQueryBuilder, + ClientServerOptions as PostgrestClientServerOption, } from '@supabase/postgrest-js' import { RealtimeChannel, @@ -28,8 +29,12 @@ import { Fetch, GenericSchema, SupabaseClientOptions, SupabaseAuthClientOptions * * An isomorphic Javascript client for interacting with Postgres. */ + +export type ServicesOptions = PostgrestClientServerOption & {} + export default class SupabaseClient< Database = any, + ClientOptions extends ServicesOptions = { postgrestVersion: 12 }, SchemaName extends string & keyof Database = 'public' extends keyof Database ? 'public' : string & keyof Database, @@ -47,7 +52,7 @@ export default class SupabaseClient< protected authUrl: string protected storageUrl: string protected functionsUrl: string - protected rest: PostgrestClient + protected rest: PostgrestClient protected storageKey: string protected fetch?: Fetch protected changedAccessToken?: string @@ -154,16 +159,16 @@ export default class SupabaseClient< from< TableName extends string & keyof Schema['Tables'], Table extends Schema['Tables'][TableName] - >(relation: TableName): PostgrestQueryBuilder + >(relation: TableName): PostgrestQueryBuilder from( relation: ViewName - ): PostgrestQueryBuilder + ): PostgrestQueryBuilder /** * Perform a query on a table or a view. * * @param relation - The table or view name to query */ - from(relation: string): PostgrestQueryBuilder { + from(relation: string): PostgrestQueryBuilder { return this.rest.from(relation) } @@ -179,6 +184,7 @@ export default class SupabaseClient< schema: DynamicSchema ): PostgrestClient< Database, + ClientOptions, DynamicSchema, Database[DynamicSchema] extends GenericSchema ? Database[DynamicSchema] : any > { @@ -218,6 +224,7 @@ export default class SupabaseClient< count?: 'exact' | 'planned' | 'estimated' } = {} ): PostgrestFilterBuilder< + ClientOptions, Schema, Fn['Returns'] extends any[] ? Fn['Returns'][number] extends Record diff --git a/src/index.ts b/src/index.ts index ff601926..c08d34cf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ import SupabaseClient from './SupabaseClient' import type { GenericSchema, SupabaseClientOptions } from './lib/types' +import type { ServicesOptions } from './SupabaseClient' export * from '@supabase/auth-js' export type { User as AuthUser, Session as AuthSession } from '@supabase/auth-js' @@ -26,6 +27,7 @@ export type { SupabaseClientOptions, QueryResult, QueryData, QueryError } from ' */ export const createClient = < Database = any, + ClientOptions extends ServicesOptions = { postgrestVersion: 12 }, SchemaName extends string & keyof Database = 'public' extends keyof Database ? 'public' : string & keyof Database, @@ -36,6 +38,10 @@ export const createClient = < supabaseUrl: string, supabaseKey: string, options?: SupabaseClientOptions -): SupabaseClient => { - return new SupabaseClient(supabaseUrl, supabaseKey, options) +): SupabaseClient => { + return new SupabaseClient( + supabaseUrl, + supabaseKey, + options + ) } diff --git a/test/client.test.ts b/test/client.test.ts index 3bca2aaf..5d7e0614 100644 --- a/test/client.test.ts +++ b/test/client.test.ts @@ -79,6 +79,17 @@ describe('Dynamic schema', () => { }) }) +describe('Postgrest 13 client', () => { + test('should be able to declare specific postgrestVersion ', async () => { + // Note: The template argument properties (postgrestVersion) will not be autocompleted + // due to a Typescript bug tracked here: https://github.com/microsoft/TypeScript/issues/56299 + createClient('HTTP://localhost:3000', KEY) + createClient('HTTP://localhost:3000', KEY) + // @ts-expect-error should raise error if provinding invalid version + createClient('HTTP://localhost:3000', KEY) + }) +}) + // Socket should close when there are no open connections // https://github.com/supabase/supabase-js/issues/44