From 2e5f26c622a802a5c89e3097872e660c59cc788a Mon Sep 17 00:00:00 2001 From: avallete Date: Mon, 19 May 2025 10:09:07 +0200 Subject: [PATCH 1/3] chore(wip): upgrade postgrest-js introduce services version options --- src/SupabaseClient.ts | 15 +++++++++++---- src/index.ts | 10 ++++++++-- test/client.test.ts | 9 +++++++++ 3 files changed, 28 insertions(+), 6 deletions(-) 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..f13efefc 100644 --- a/test/client.test.ts +++ b/test/client.test.ts @@ -79,6 +79,15 @@ describe('Dynamic schema', () => { }) }) +describe('Postgrest 13 client', () => { + test('should be able to declare specific postgrestVersion ', async () => { + 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 From 609dc51ff1d01d7d582fcf99e8a2cc654f889e72 Mon Sep 17 00:00:00 2001 From: avallete Date: Tue, 20 May 2025 09:30:25 +0200 Subject: [PATCH 2/3] chore: add comment for autocomplete --- test/client.test.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/client.test.ts b/test/client.test.ts index f13efefc..5d7e0614 100644 --- a/test/client.test.ts +++ b/test/client.test.ts @@ -81,6 +81,8 @@ 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 From 942ab4f7ba01c66537ef9fe50de4ca6494b168b5 Mon Sep 17 00:00:00 2001 From: avallete Date: Fri, 4 Jul 2025 16:49:32 +0200 Subject: [PATCH 3/3] chore: sync types --- src/SupabaseClient.ts | 17 ++++++++++------- src/index.ts | 12 +++++++----- test/client.test.ts | 16 ++++++++-------- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/SupabaseClient.ts b/src/SupabaseClient.ts index 0e8a8dfc..e1c9caac 100644 --- a/src/SupabaseClient.ts +++ b/src/SupabaseClient.ts @@ -5,6 +5,7 @@ import { PostgrestFilterBuilder, PostgrestQueryBuilder, ClientServerOptions as PostgrestClientServerOption, + GetGenericDatabaseWithOptions, } from '@supabase/postgrest-js' import { RealtimeChannel, @@ -34,12 +35,13 @@ export type ServicesOptions = PostgrestClientServerOption & {} export default class SupabaseClient< Database = any, - ClientOptions extends ServicesOptions = { postgrestVersion: 12 }, - SchemaName extends string & keyof Database = 'public' extends keyof Database + ClientOptions extends ServicesOptions = { PostgrestVersion: '12' }, + SchemaName extends string & + keyof GetGenericDatabaseWithOptions['db'] = 'public' extends keyof GetGenericDatabaseWithOptions['db'] ? 'public' - : string & keyof Database, - Schema extends GenericSchema = Database[SchemaName] extends GenericSchema - ? Database[SchemaName] + : string & keyof GetGenericDatabaseWithOptions['db'], + Schema extends GenericSchema = GetGenericDatabaseWithOptions['db'][SchemaName] extends GenericSchema + ? GetGenericDatabaseWithOptions['db'][SchemaName] : any > { /** @@ -180,7 +182,7 @@ export default class SupabaseClient< * * @param schema - The schema to query */ - schema( + schema['db']>( schema: DynamicSchema ): PostgrestClient< Database, @@ -233,7 +235,8 @@ export default class SupabaseClient< : never, Fn['Returns'], FnName, - null + null, + 'RPC' > { return this.rest.rpc(fn, args, options) } diff --git a/src/index.ts b/src/index.ts index c08d34cf..5876e68d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ import SupabaseClient from './SupabaseClient' import type { GenericSchema, SupabaseClientOptions } from './lib/types' import type { ServicesOptions } from './SupabaseClient' +import type { GetGenericDatabaseWithOptions } from '@supabase/postgrest-js' export * from '@supabase/auth-js' export type { User as AuthUser, Session as AuthSession } from '@supabase/auth-js' @@ -27,12 +28,13 @@ 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 + ClientOptions extends ServicesOptions = { PostgrestVersion: '12' }, + SchemaName extends string & + keyof GetGenericDatabaseWithOptions['db'] = 'public' extends keyof GetGenericDatabaseWithOptions['db'] ? 'public' - : string & keyof Database, - Schema extends GenericSchema = Database[SchemaName] extends GenericSchema - ? Database[SchemaName] + : string & keyof GetGenericDatabaseWithOptions['db'], + Schema extends GenericSchema = GetGenericDatabaseWithOptions['db'][SchemaName] extends GenericSchema + ? GetGenericDatabaseWithOptions['db'][SchemaName] : any >( supabaseUrl: string, diff --git a/test/client.test.ts b/test/client.test.ts index 5d7e0614..c4f83f86 100644 --- a/test/client.test.ts +++ b/test/client.test.ts @@ -35,10 +35,10 @@ describe('Custom Headers', () => { const request = createClient(URL, KEY, { global: { headers: customHeader } }).rpc('') - // @ts-ignore - const getHeaders = request.headers + //@ts-expect-error headers is protected attribute + const requestHeader = request.headers.get('X-Test-Header') - expect(getHeaders).toHaveProperty('X-Test-Header', 'value') + expect(requestHeader).toBe(customHeader['X-Test-Header']) }) }) @@ -80,13 +80,13 @@ 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 + 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) + 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) + createClient('HTTP://localhost:3000', KEY) }) })