Skip to content

Commit 59a5899

Browse files
committed
refactor(postgrest): integrate postgrest-js changes into new monorepo
1 parent 24865c5 commit 59a5899

File tree

5 files changed

+60
-24
lines changed

5 files changed

+60
-24
lines changed

packages/core/postgrest-js/src/PostgrestClient.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import PostgrestQueryBuilder from './PostgrestQueryBuilder'
22
import PostgrestFilterBuilder from './PostgrestFilterBuilder'
33
import { Fetch, GenericSchema, ClientServerOptions } from './types/common/common'
44
import { GetRpcFunctionFilterBuilderByArgs } from './types/common/rpc'
5+
import { PostgrestQueryBuilderOptions, PostgrestQueryBuilderOptionsWithSchema } from './types/types'
6+
import { mergeHeaders } from './utils'
57

68
/**
79
* PostgREST client.
@@ -18,21 +20,21 @@ export default class PostgrestClient<
1820
ClientOptions extends ClientServerOptions = Database extends {
1921
__InternalSupabase: infer I extends ClientServerOptions
2022
}
21-
? I
22-
: {},
23+
? I
24+
: {},
2325
SchemaName extends string &
24-
keyof Omit<Database, '__InternalSupabase'> = 'public' extends keyof Omit<
26+
keyof Omit<Database, '__InternalSupabase'> = 'public' extends keyof Omit<
2527
Database,
2628
'__InternalSupabase'
2729
>
28-
? 'public'
29-
: string & keyof Omit<Database, '__InternalSupabase'>,
30+
? 'public'
31+
: string & keyof Omit<Database, '__InternalSupabase'>,
3032
Schema extends GenericSchema = Omit<
3133
Database,
3234
'__InternalSupabase'
3335
>[SchemaName] extends GenericSchema
34-
? Omit<Database, '__InternalSupabase'>[SchemaName]
35-
: any,
36+
? Omit<Database, '__InternalSupabase'>[SchemaName]
37+
: any,
3638
> {
3739
url: string
3840
headers: Headers
@@ -55,11 +57,7 @@ export default class PostgrestClient<
5557
headers = {},
5658
schema,
5759
fetch,
58-
}: {
59-
headers?: HeadersInit
60-
schema?: SchemaName
61-
fetch?: Fetch
62-
} = {}
60+
}: PostgrestQueryBuilderOptionsWithSchema<SchemaName> = {}
6361
) {
6462
this.url = url
6563
this.headers = new Headers(headers)
@@ -69,21 +67,28 @@ export default class PostgrestClient<
6967
from<
7068
TableName extends string & keyof Schema['Tables'],
7169
Table extends Schema['Tables'][TableName],
72-
>(relation: TableName): PostgrestQueryBuilder<ClientOptions, Schema, Table, TableName>
70+
>(
71+
relation: TableName,
72+
options?: PostgrestQueryBuilderOptions
73+
): PostgrestQueryBuilder<ClientOptions, Schema, Table, TableName>;
7374
from<ViewName extends string & keyof Schema['Views'], View extends Schema['Views'][ViewName]>(
74-
relation: ViewName
75-
): PostgrestQueryBuilder<ClientOptions, Schema, View, ViewName>
75+
relation: ViewName,
76+
options?: PostgrestQueryBuilderOptions
77+
): PostgrestQueryBuilder<ClientOptions, Schema, View, ViewName>;
7678
/**
7779
* Perform a query on a table or a view.
7880
*
7981
* @param relation - The table or view name to query
8082
*/
81-
from(relation: string): PostgrestQueryBuilder<ClientOptions, Schema, any, any> {
83+
from(
84+
relation: string,
85+
options?: PostgrestQueryBuilderOptions
86+
): PostgrestQueryBuilder<ClientOptions, Schema, any, any> {
8287
const url = new URL(`${this.url}/${relation}`)
8388
return new PostgrestQueryBuilder(url, {
84-
headers: new Headers(this.headers),
89+
headers: mergeHeaders(this.headers, options?.headers),
8590
schema: this.schemaName,
86-
fetch: this.fetch,
91+
fetch: options?.fetch ?? this.fetch,
8792
})
8893
}
8994

packages/core/postgrest-js/src/PostgrestQueryBuilder.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
GenericTable,
88
GenericView,
99
} from './types/common/common'
10+
import { PostgrestQueryBuilderOptionsWithSchema } from './types/types'
1011

1112
export default class PostgrestQueryBuilder<
1213
ClientOptions extends ClientServerOptions,
@@ -27,11 +28,7 @@ export default class PostgrestQueryBuilder<
2728
headers = {},
2829
schema,
2930
fetch,
30-
}: {
31-
headers?: HeadersInit
32-
schema?: string
33-
fetch?: Fetch
34-
}
31+
}: PostgrestQueryBuilderOptionsWithSchema<string>
3532
) {
3633
this.url = url
3734
this.headers = new Headers(headers)

packages/core/postgrest-js/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ export type {
2828
PostgrestResponseSuccess,
2929
PostgrestSingleResponse,
3030
PostgrestMaybeSingleResponse,
31+
PostgrestQueryBuilderOptions,
32+
PostgrestQueryBuilderOptionsWithSchema
3133
} from './types/types'
3234
export type { ClientServerOptions as PostgrestClientOptions } from './types/common/common'
3335
// https://github.com/supabase/postgrest-js/issues/551

packages/core/postgrest-js/src/types/types.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import PostgrestError from '../PostgrestError'
22
import { ContainsNull } from '../select-query-parser/types'
33
import { SelectQueryError } from '../select-query-parser/utils'
4-
import { ClientServerOptions } from './common/common'
4+
import { ClientServerOptions, Fetch } from './common/common'
55

66
/**
77
* Response format
@@ -30,6 +30,15 @@ export type PostgrestSingleResponse<T> = PostgrestResponseSuccess<T> | Postgrest
3030
export type PostgrestMaybeSingleResponse<T> = PostgrestSingleResponse<T | null>
3131
export type PostgrestResponse<T> = PostgrestSingleResponse<T[]>
3232

33+
export type PostgrestQueryBuilderOptions = {
34+
headers?: HeadersInit
35+
fetch?: Fetch
36+
}
37+
38+
export type PostgrestQueryBuilderOptionsWithSchema<TSchema extends string> = PostgrestQueryBuilderOptions & {
39+
schema?: TSchema
40+
}
41+
3342
export type DatabaseWithOptions<Database, Options extends ClientServerOptions> = {
3443
db: Database
3544
options: Options
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Non-destructively merges an optional {@link HeadersInit} into a {@link Headers} object, where {@link right} takes precedence over {@link left} if it exists.
3+
* @param {Headers} left Base {@link Headers} object
4+
* @param {HeadersInit?} right Optional {@link HeadersInit} to merge into {@link left}
5+
* @returns The resulting merged {@link HeadersInit} object.
6+
*/
7+
8+
export function mergeHeaders(left: Headers, right?: HeadersInit): HeadersInit {
9+
if (!right) return new Headers(left)
10+
11+
const merged = new Headers(left)
12+
const rightEntries =
13+
right instanceof Headers
14+
? right.entries()
15+
: Array.isArray(right)
16+
? right
17+
: Object.entries(right)
18+
for (const [key, value] of rightEntries) {
19+
merged.set(key, value)
20+
}
21+
22+
return merged
23+
}

0 commit comments

Comments
 (0)