Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"@supabase/auth-js": "2.71.1",
"@supabase/functions-js": "2.4.5",
"@supabase/node-fetch": "2.6.15",
"@supabase/postgrest-js": "1.19.4",
"@supabase/postgrest-js": "file:../postgrest-js",
"@supabase/realtime-js": "2.15.1",
"@supabase/storage-js": "^2.10.4"
},
Expand Down
43 changes: 33 additions & 10 deletions src/SupabaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
PostgrestClient,
PostgrestFilterBuilder,
PostgrestQueryBuilder,
} from '@supabase/postgrest-js'

Check failure on line 7 in src/SupabaseClient.ts

View workflow job for this annotation

GitHub Actions / Unit + Type Check / Node.js 20 / OS macos-latest

Cannot find module '@supabase/postgrest-js' or its corresponding type declarations.

Check failure on line 7 in src/SupabaseClient.ts

View workflow job for this annotation

GitHub Actions / Unit + Type Check / Node.js 20 / OS ubuntu-latest

Cannot find module '@supabase/postgrest-js' or its corresponding type declarations.

Check failure on line 7 in src/SupabaseClient.ts

View workflow job for this annotation

GitHub Actions / Build supabase-js package

Cannot find module '@supabase/postgrest-js' or its corresponding type declarations.

Check failure on line 7 in src/SupabaseClient.ts

View workflow job for this annotation

GitHub Actions / Unit + Type Check / Node.js 20 / OS windows-latest

Cannot find module '@supabase/postgrest-js' or its corresponding type declarations.
import {
RealtimeChannel,
RealtimeChannelOptions,
Expand All @@ -30,12 +30,32 @@
*/
export default class SupabaseClient<
Database = any,
SchemaName extends string & keyof Database = 'public' extends keyof Database
// The second type parameter is also used for specifying db_schema, so we
// support both cases.
// TODO: Allow setting db_schema from ClientOptions.
SchemaNameOrClientOptions extends
| (string & keyof Database)
| { PostgrestVersion: string } = 'public' extends keyof Database
? 'public'
: string & keyof Database,
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
? Database[SchemaName]
: any
SchemaName extends string &
keyof Omit<Database, '__InternalSupabase'> = SchemaNameOrClientOptions extends string &
keyof Omit<Database, '__InternalSupabase'>
? SchemaNameOrClientOptions
: 'public' extends keyof Omit<Database, '__InternalSupabase'>
? 'public'
: string & keyof Omit<Omit<Database, '__InternalSupabase'>, '__InternalSupabase'>,
Schema extends Omit<Database, '__InternalSupabase'>[SchemaName] extends GenericSchema
? Omit<Database, '__InternalSupabase'>[SchemaName]
: never = Omit<Database, '__InternalSupabase'>[SchemaName] extends GenericSchema
? Omit<Database, '__InternalSupabase'>[SchemaName]
: never,
ClientOptions extends { PostgrestVersion: string } = SchemaNameOrClientOptions extends string &
keyof Omit<Database, '__InternalSupabase'>
? { PostgrestVersion: '12' }
: SchemaNameOrClientOptions extends { PostgrestVersion: string }
? SchemaNameOrClientOptions
: never
> {
/**
* Supabase Auth allows you to create and manage user sessions for access to data that is secured by access policies.
Expand All @@ -51,7 +71,7 @@
protected authUrl: URL
protected storageUrl: URL
protected functionsUrl: URL
protected rest: PostgrestClient<Database, SchemaName, Schema>
protected rest: PostgrestClient<Database, ClientOptions, SchemaName>
protected storageKey: string
protected fetch?: Fetch
protected changedAccessToken?: string
Expand Down Expand Up @@ -161,16 +181,16 @@
from<
TableName extends string & keyof Schema['Tables'],
Table extends Schema['Tables'][TableName]
>(relation: TableName): PostgrestQueryBuilder<Schema, Table, TableName>
>(relation: TableName): PostgrestQueryBuilder<ClientOptions, Schema, Table, TableName>
from<ViewName extends string & keyof Schema['Views'], View extends Schema['Views'][ViewName]>(
relation: ViewName
): PostgrestQueryBuilder<Schema, View, ViewName>
): PostgrestQueryBuilder<ClientOptions, Schema, View, ViewName>
/**
* Perform a query on a table or a view.
*
* @param relation - The table or view name to query
*/
from(relation: string): PostgrestQueryBuilder<Schema, any, any> {
from(relation: string): PostgrestQueryBuilder<ClientOptions, Schema, any> {
return this.rest.from(relation)
}

Expand All @@ -182,10 +202,11 @@
*
* @param schema - The schema to query
*/
schema<DynamicSchema extends string & keyof Database>(
schema<DynamicSchema extends string & keyof Omit<Database, '__InternalSupabase'>>(
schema: DynamicSchema
): PostgrestClient<
Database,
ClientOptions,
DynamicSchema,
Database[DynamicSchema] extends GenericSchema ? Database[DynamicSchema] : any
> {
Expand Down Expand Up @@ -225,6 +246,7 @@
count?: 'exact' | 'planned' | 'estimated'
} = {}
): PostgrestFilterBuilder<
ClientOptions,
Schema,
Fn['Returns'] extends any[]
? Fn['Returns'][number] extends Record<string, unknown>
Expand All @@ -233,7 +255,8 @@
: never,
Fn['Returns'],
FnName,
null
null,
'RPC'
> {
return this.rest.rpc(fn, args, options)
}
Expand Down
24 changes: 17 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import SupabaseClient from './SupabaseClient'
import type { GenericSchema, SupabaseClientOptions } from './lib/types'
import type { SupabaseClientOptions } from './lib/types'

export * from '@supabase/auth-js'
export type { User as AuthUser, Session as AuthSession } from '@supabase/auth-js'
Expand All @@ -8,7 +8,7 @@
type PostgrestSingleResponse,
type PostgrestMaybeSingleResponse,
PostgrestError,
} from '@supabase/postgrest-js'

Check failure on line 11 in src/index.ts

View workflow job for this annotation

GitHub Actions / Unit + Type Check / Node.js 20 / OS macos-latest

Cannot find module '@supabase/postgrest-js' or its corresponding type declarations.

Check failure on line 11 in src/index.ts

View workflow job for this annotation

GitHub Actions / Unit + Type Check / Node.js 20 / OS ubuntu-latest

Cannot find module '@supabase/postgrest-js' or its corresponding type declarations.

Check failure on line 11 in src/index.ts

View workflow job for this annotation

GitHub Actions / Build supabase-js package

Cannot find module '@supabase/postgrest-js' or its corresponding type declarations.

Check failure on line 11 in src/index.ts

View workflow job for this annotation

GitHub Actions / Unit + Type Check / Node.js 20 / OS windows-latest

Cannot find module '@supabase/postgrest-js' or its corresponding type declarations.
export {
FunctionsHttpError,
FunctionsFetchError,
Expand All @@ -26,18 +26,28 @@
*/
export const createClient = <
Database = any,
SchemaName extends string & keyof Database = 'public' extends keyof Database
SchemaNameOrClientOptions extends
| (string & keyof Database)
| { PostgrestVersion: string } = 'public' extends keyof Database
? 'public'
: string & keyof Database,
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
? Database[SchemaName]
: any
SchemaName extends string &
keyof Omit<Database, '__InternalSupabase'> = SchemaNameOrClientOptions extends string &
keyof Omit<Database, '__InternalSupabase'>
? SchemaNameOrClientOptions
: 'public' extends keyof Omit<Database, '__InternalSupabase'>
? 'public'
: string & keyof Omit<Omit<Database, '__InternalSupabase'>, '__InternalSupabase'>
>(
supabaseUrl: string,
supabaseKey: string,
options?: SupabaseClientOptions<SchemaName>
): SupabaseClient<Database, SchemaName, Schema> => {
return new SupabaseClient<Database, SchemaName, Schema>(supabaseUrl, supabaseKey, options)
): SupabaseClient<Database, SchemaNameOrClientOptions, SchemaName> => {
return new SupabaseClient<Database, SchemaNameOrClientOptions, SchemaName>(
supabaseUrl,
supabaseKey,
options
)
}

// Check for Node.js <= 18 deprecation
Expand Down
8 changes: 7 additions & 1 deletion test/types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { expectError, expectType } from 'tsd'
import { PostgrestSingleResponse, createClient } from '../../src/index'
import { PostgrestSingleResponse, createClient, SupabaseClient } from '../../src/index'
import { Database, Json } from '../types'

const URL = 'http://localhost:3000'
const KEY = 'some.fake.key'
const supabase = createClient<Database>(URL, KEY)

// createClient with custom schema
{
createClient<Database, 'personal'>(URL, KEY, { db: { schema: 'personal' } })
new SupabaseClient<Database, 'personal'>(URL, KEY, { db: { schema: 'personal' } })
}

// table invalid type
{
expectError(supabase.from(42))
Expand Down
Loading