Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion package-lock.json

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

41 changes: 23 additions & 18 deletions packages/core/postgrest-js/src/PostgrestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import PostgrestQueryBuilder from './PostgrestQueryBuilder'
import PostgrestFilterBuilder from './PostgrestFilterBuilder'
import { Fetch, GenericSchema, ClientServerOptions } from './types/common/common'
import { GetRpcFunctionFilterBuilderByArgs } from './types/common/rpc'
import { PostgrestQueryBuilderOptions, PostgrestQueryBuilderOptionsWithSchema } from './types/types'
import { mergeHeaders } from './utils'

/**
* PostgREST client.
Expand All @@ -18,21 +20,21 @@ export default class PostgrestClient<
ClientOptions extends ClientServerOptions = Database extends {
__InternalSupabase: infer I extends ClientServerOptions
}
? I
: {},
? I
: {},
SchemaName extends string &
keyof Omit<Database, '__InternalSupabase'> = 'public' extends keyof Omit<
keyof Omit<Database, '__InternalSupabase'> = 'public' extends keyof Omit<
Database,
'__InternalSupabase'
>
? 'public'
: string & keyof Omit<Database, '__InternalSupabase'>,
? 'public'
: string & keyof Omit<Database, '__InternalSupabase'>,
Schema extends GenericSchema = Omit<
Database,
'__InternalSupabase'
>[SchemaName] extends GenericSchema
? Omit<Database, '__InternalSupabase'>[SchemaName]
: any,
? Omit<Database, '__InternalSupabase'>[SchemaName]
: any,
> {
url: string
headers: Headers
Expand All @@ -55,11 +57,7 @@ export default class PostgrestClient<
headers = {},
schema,
fetch,
}: {
headers?: HeadersInit
schema?: SchemaName
fetch?: Fetch
} = {}
}: PostgrestQueryBuilderOptionsWithSchema<SchemaName> = {}
) {
this.url = url
this.headers = new Headers(headers)
Expand All @@ -69,21 +67,28 @@ export default class PostgrestClient<
from<
TableName extends string & keyof Schema['Tables'],
Table extends Schema['Tables'][TableName],
>(relation: TableName): PostgrestQueryBuilder<ClientOptions, Schema, Table, TableName>
>(
relation: TableName,
options?: PostgrestQueryBuilderOptions
): PostgrestQueryBuilder<ClientOptions, Schema, Table, TableName>;
from<ViewName extends string & keyof Schema['Views'], View extends Schema['Views'][ViewName]>(
relation: ViewName
): PostgrestQueryBuilder<ClientOptions, Schema, View, ViewName>
relation: ViewName,
options?: PostgrestQueryBuilderOptions
): 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<ClientOptions, Schema, any, any> {
from(
relation: string,
options?: PostgrestQueryBuilderOptions
): PostgrestQueryBuilder<ClientOptions, Schema, any, any> {
const url = new URL(`${this.url}/${relation}`)
return new PostgrestQueryBuilder(url, {
headers: new Headers(this.headers),
headers: mergeHeaders(this.headers, options?.headers),
schema: this.schemaName,
fetch: this.fetch,
fetch: options?.fetch ?? this.fetch,
})
}

Expand Down
7 changes: 2 additions & 5 deletions packages/core/postgrest-js/src/PostgrestQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
GenericTable,
GenericView,
} from './types/common/common'
import { PostgrestQueryBuilderOptionsWithSchema } from './types/types'

export default class PostgrestQueryBuilder<
ClientOptions extends ClientServerOptions,
Expand All @@ -27,11 +28,7 @@ export default class PostgrestQueryBuilder<
headers = {},
schema,
fetch,
}: {
headers?: HeadersInit
schema?: string
fetch?: Fetch
}
}: PostgrestQueryBuilderOptionsWithSchema<string>
) {
this.url = url
this.headers = new Headers(headers)
Expand Down
2 changes: 2 additions & 0 deletions packages/core/postgrest-js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export type {
PostgrestResponseSuccess,
PostgrestSingleResponse,
PostgrestMaybeSingleResponse,
PostgrestQueryBuilderOptions,
PostgrestQueryBuilderOptionsWithSchema
} from './types/types'
export type { ClientServerOptions as PostgrestClientOptions } from './types/common/common'
// https://github.com/supabase/postgrest-js/issues/551
Expand Down
11 changes: 10 additions & 1 deletion packages/core/postgrest-js/src/types/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import PostgrestError from '../PostgrestError'
import { ContainsNull } from '../select-query-parser/types'
import { SelectQueryError } from '../select-query-parser/utils'
import { ClientServerOptions } from './common/common'
import { ClientServerOptions, Fetch } from './common/common'

/**
* Response format
Expand Down Expand Up @@ -30,6 +30,15 @@ export type PostgrestSingleResponse<T> = PostgrestResponseSuccess<T> | Postgrest
export type PostgrestMaybeSingleResponse<T> = PostgrestSingleResponse<T | null>
export type PostgrestResponse<T> = PostgrestSingleResponse<T[]>

export type PostgrestQueryBuilderOptions = {
headers?: HeadersInit
fetch?: Fetch
}

export type PostgrestQueryBuilderOptionsWithSchema<TSchema extends string> = PostgrestQueryBuilderOptions & {
schema?: TSchema
}

export type DatabaseWithOptions<Database, Options extends ClientServerOptions> = {
db: Database
options: Options
Expand Down
23 changes: 23 additions & 0 deletions packages/core/postgrest-js/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Non-destructively merges an optional {@link HeadersInit} into a {@link Headers} object, where {@link right} takes precedence over {@link left} if it exists.
* @param {Headers} left Base {@link Headers} object
* @param {HeadersInit?} right Optional {@link HeadersInit} to merge into {@link left}
* @returns The resulting merged {@link HeadersInit} object.
*/

export function mergeHeaders(left: Headers, right?: HeadersInit): HeadersInit {
if (!right) return new Headers(left)

const merged = new Headers(left)
const rightEntries =
right instanceof Headers
? right.entries()
: Array.isArray(right)
? right
: Object.entries(right)
for (const [key, value] of rightEntries) {
merged.set(key, value)
}

return merged
}
6 changes: 3 additions & 3 deletions packages/core/supabase-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,21 @@
"dependencies": {
"@supabase/auth-js": "*",
"@supabase/functions-js": "*",
"@supabase/node-fetch": "2.6.15",
"@supabase/postgrest-js": "*",
"@supabase/realtime-js": "*",
"@supabase/storage-js": "*",
"@supabase/node-fetch": "2.6.15"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please revert changes to package.json

"@supabase/storage-js": "*"
},
"devDependencies": {
"@sebbo2002/semantic-release-jsr": "^1.0.0",
"eslint-formatter-pretty": "^4.1.0",
"jsr": "^0.13.5",
"pretty-quick": "^3.1.3",
"puppeteer": "^24.9.0",
"serve": "^14.2.1",
"ts-jest": "^29.0.5",
"ts-loader": "^9.5.4",
"tsd": "^0.30.4",
"eslint-formatter-pretty": "^4.1.0",
"webpack": "^5.69.1",
"webpack-cli": "^4.9.2"
},
Expand Down
32 changes: 24 additions & 8 deletions packages/core/supabase-js/src/SupabaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import type { AuthChangeEvent } from '@supabase/auth-js'
import { FunctionsClient } from '@supabase/functions-js'
import {
PostgrestClient,
type PostgrestFilterBuilder,
type PostgrestQueryBuilder,
PostgrestFilterBuilder,
PostgrestQueryBuilder,
PostgrestQueryBuilderOptions,
} from '@supabase/postgrest-js'
import {
type RealtimeChannel,
Expand Down Expand Up @@ -150,7 +151,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),
Expand Down Expand Up @@ -187,18 +188,29 @@ export default class SupabaseClient<
// NOTE: signatures must be kept in sync with PostgrestClient.from
from<
TableName extends string & keyof Schema['Tables'],
Table extends Schema['Tables'][TableName],
>(relation: TableName): PostgrestQueryBuilder<ClientOptions, Schema, Table, TableName>
Table extends Schema['Tables'][TableName]
>(
relation: TableName,
options?: PostgrestQueryBuilderOptions
): PostgrestQueryBuilder<ClientOptions, Schema, Table, TableName>
from<ViewName extends string & keyof Schema['Views'], View extends Schema['Views'][ViewName]>(
relation: ViewName
relation: ViewName,
options?: PostgrestQueryBuilderOptions
): 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<ClientOptions, Schema, any> {
return this.rest.from(relation)
from(
relation: string,
options?: PostgrestQueryBuilderOptions
): PostgrestQueryBuilder<ClientOptions, Schema, any> {
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
Expand Down Expand Up @@ -400,4 +412,8 @@ export default class SupabaseClient<
this.changedAccessToken = undefined
}
}

private _createFetchWithAuth(_fetch?: typeof global.fetch) {
return fetchWithAuth(this.supabaseKey, this._getAccessToken.bind(this), _fetch)
}
}