Skip to content

Commit 1508524

Browse files
authored
Merge pull request #619 from supabase/avallete/psql-372-postgrest-add-maxaffected-in-client-libraries
feat: postgrest 13 add maxaffected in client libraries
2 parents fd36306 + 2698df8 commit 1508524

15 files changed

+365
-119
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818

1919
- uses: actions/setup-node@v2
2020
with:
21-
node-version: '16'
21+
node-version: '20'
2222

2323
- run: |
2424
npm clean-install

src/PostgrestBuilder.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,33 @@ export default abstract class PostgrestBuilder<
2424
{
2525
protected method: 'GET' | 'HEAD' | 'POST' | 'PATCH' | 'DELETE'
2626
protected url: URL
27-
protected headers: Record<string, string>
27+
protected headers: Headers
2828
protected schema?: string
2929
protected body?: unknown
3030
protected shouldThrowOnError = false
3131
protected signal?: AbortSignal
3232
protected fetch: Fetch
3333
protected isMaybeSingle: boolean
3434

35-
constructor(builder: PostgrestBuilder<ClientOptions, Result>) {
35+
constructor(builder: {
36+
method: 'GET' | 'HEAD' | 'POST' | 'PATCH' | 'DELETE'
37+
url: URL
38+
headers: HeadersInit
39+
schema?: string
40+
body?: unknown
41+
shouldThrowOnError?: boolean
42+
signal?: AbortSignal
43+
fetch?: Fetch
44+
isMaybeSingle?: boolean
45+
}) {
3646
this.method = builder.method
3747
this.url = builder.url
38-
this.headers = builder.headers
48+
this.headers = new Headers(builder.headers)
3949
this.schema = builder.schema
4050
this.body = builder.body
41-
this.shouldThrowOnError = builder.shouldThrowOnError
51+
this.shouldThrowOnError = builder.shouldThrowOnError ?? false
4252
this.signal = builder.signal
43-
this.isMaybeSingle = builder.isMaybeSingle
53+
this.isMaybeSingle = builder.isMaybeSingle ?? false
4454

4555
if (builder.fetch) {
4656
this.fetch = builder.fetch
@@ -66,8 +76,8 @@ export default abstract class PostgrestBuilder<
6676
* Set an HTTP header for the request.
6777
*/
6878
setHeader(name: string, value: string): this {
69-
this.headers = { ...this.headers }
70-
this.headers[name] = value
79+
this.headers = new Headers(this.headers)
80+
this.headers.set(name, value)
7181
return this
7282
}
7383

@@ -91,12 +101,12 @@ export default abstract class PostgrestBuilder<
91101
if (this.schema === undefined) {
92102
// skip
93103
} else if (['GET', 'HEAD'].includes(this.method)) {
94-
this.headers['Accept-Profile'] = this.schema
104+
this.headers.set('Accept-Profile', this.schema)
95105
} else {
96-
this.headers['Content-Profile'] = this.schema
106+
this.headers.set('Content-Profile', this.schema)
97107
}
98108
if (this.method !== 'GET' && this.method !== 'HEAD') {
99-
this.headers['Content-Type'] = 'application/json'
109+
this.headers.set('Content-Type', 'application/json')
100110
}
101111

102112
// NOTE: Invoke w/o `this` to avoid illegal invocation error.
@@ -119,19 +129,19 @@ export default abstract class PostgrestBuilder<
119129
const body = await res.text()
120130
if (body === '') {
121131
// Prefer: return=minimal
122-
} else if (this.headers['Accept'] === 'text/csv') {
132+
} else if (this.headers.get('Accept') === 'text/csv') {
123133
data = body
124134
} else if (
125-
this.headers['Accept'] &&
126-
this.headers['Accept'].includes('application/vnd.pgrst.plan+text')
135+
this.headers.get('Accept') &&
136+
this.headers.get('Accept')?.includes('application/vnd.pgrst.plan+text')
127137
) {
128138
data = body
129139
} else {
130140
data = JSON.parse(body)
131141
}
132142
}
133143

134-
const countHeader = this.headers['Prefer']?.match(/count=(exact|planned|estimated)/)
144+
const countHeader = this.headers.get('Prefer')?.match(/count=(exact|planned|estimated)/)
135145
const contentRange = res.headers.get('content-range')?.split('/')
136146
if (countHeader && contentRange && contentRange.length > 1) {
137147
count = parseInt(contentRange[1])

src/PostgrestClient.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import PostgrestQueryBuilder from './PostgrestQueryBuilder'
22
import PostgrestFilterBuilder from './PostgrestFilterBuilder'
3-
import PostgrestBuilder from './PostgrestBuilder'
4-
import { DEFAULT_HEADERS } from './constants'
53
import { Fetch, GenericSchema, ClientServerOptions, GetGenericDatabaseWithOptions } from './types'
64

75
/**
@@ -29,7 +27,7 @@ export default class PostgrestClient<
2927
: any
3028
> {
3129
url: string
32-
headers: Record<string, string>
30+
headers: Headers
3331
schemaName?: SchemaName
3432
fetch?: Fetch
3533

@@ -50,17 +48,16 @@ export default class PostgrestClient<
5048
schema,
5149
fetch,
5250
}: {
53-
headers?: Record<string, string>
51+
headers?: HeadersInit
5452
schema?: SchemaName
5553
fetch?: Fetch
5654
} = {}
5755
) {
5856
this.url = url
59-
this.headers = { ...DEFAULT_HEADERS, ...headers }
57+
this.headers = new Headers(headers)
6058
this.schemaName = schema
6159
this.fetch = fetch
6260
}
63-
6461
from<
6562
TableName extends string & keyof Schema['Tables'],
6663
Table extends Schema['Tables'][TableName]
@@ -76,7 +73,7 @@ export default class PostgrestClient<
7673
from(relation: string): PostgrestQueryBuilder<ClientOptions, Schema, any, any> {
7774
const url = new URL(`${this.url}/${relation}`)
7875
return new PostgrestQueryBuilder(url, {
79-
headers: { ...this.headers },
76+
headers: new Headers(this.headers),
8077
schema: this.schemaName,
8178
fetch: this.fetch,
8279
})
@@ -149,7 +146,8 @@ export default class PostgrestClient<
149146
: never,
150147
Fn['Returns'],
151148
FnName,
152-
null
149+
null,
150+
'RPC'
153151
> {
154152
let method: 'HEAD' | 'GET' | 'POST'
155153
const url = new URL(`${this.url}/rpc/${fn}`)
@@ -170,9 +168,9 @@ export default class PostgrestClient<
170168
body = args
171169
}
172170

173-
const headers = { ...this.headers }
171+
const headers = new Headers(this.headers)
174172
if (count) {
175-
headers['Prefer'] = `count=${count}`
173+
headers.set('Prefer', `count=${count}`)
176174
}
177175

178176
return new PostgrestFilterBuilder({
@@ -181,8 +179,7 @@ export default class PostgrestClient<
181179
headers,
182180
schema: this.schemaName,
183181
body,
184-
fetch: this.fetch,
185-
allowEmpty: false,
186-
} as unknown as PostgrestBuilder<ClientOptions, Fn['Returns']>)
182+
fetch: this.fetch ?? fetch,
183+
})
187184
}
188185
}

src/PostgrestFilterBuilder.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,16 @@ export default class PostgrestFilterBuilder<
7777
Row extends Record<string, unknown>,
7878
Result,
7979
RelationName = unknown,
80-
Relationships = unknown
80+
Relationships = unknown,
81+
Method = unknown
8182
> extends PostgrestTransformBuilder<
8283
ClientOptions,
8384
Schema,
8485
Row,
8586
Result,
8687
RelationName,
87-
Relationships
88+
Relationships,
89+
Method
8890
> {
8991
/**
9092
* Match only rows where `column` is equal to `value`.

0 commit comments

Comments
 (0)