Skip to content
This repository was archived by the owner on Oct 9, 2025. It is now read-only.

Commit a53f81b

Browse files
authored
feat: support HEAD for RPC (#215)
* Allow customizing RPC request method * Update test snapshots * Add comments * Remove `GET` HTTP method
1 parent 5e332bf commit a53f81b

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed

src/PostgrestClient.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,24 @@ export default class PostgrestClient {
4949
*
5050
* @param fn The function name to call.
5151
* @param params The parameters to pass to the function call.
52+
* @param head When set to true, no data will be returned.
5253
* @param count Count algorithm to use to count rows in a table.
5354
*/
5455
rpc<T = any>(
5556
fn: string,
5657
params?: object,
5758
{
59+
head = false,
5860
count = null,
5961
}: {
62+
head?: boolean
6063
count?: null | 'exact' | 'planned' | 'estimated'
6164
} = {}
6265
): PostgrestFilterBuilder<T> {
6366
const url = `${this.url}/rpc/${fn}`
6467
return new PostgrestRpcBuilder<T>(url, {
6568
headers: this.headers,
6669
schema: this.schema,
67-
}).rpc(params, { count })
70+
}).rpc(params, { head, count })
6871
}
6972
}

src/lib/PostgrestRpcBuilder.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,25 @@ export default class PostgrestRpcBuilder<T> extends PostgrestBuilder<T> {
1818
rpc(
1919
params?: object,
2020
{
21+
head = false,
2122
count = null,
2223
}: {
24+
head?: boolean
2325
count?: null | 'exact' | 'planned' | 'estimated'
2426
} = {}
2527
): PostgrestFilterBuilder<T> {
26-
this.method = 'POST'
27-
this.body = params
28+
if (head) {
29+
this.method = 'HEAD'
30+
31+
if (params) {
32+
Object.entries(params).forEach(([name, value]) => {
33+
this.url.searchParams.append(name, value)
34+
})
35+
}
36+
} else {
37+
this.method = 'POST'
38+
this.body = params
39+
}
2840

2941
if (count) {
3042
if (this.headers['Prefer'] !== undefined) this.headers['Prefer'] += `,count=${count}`

test/__snapshots__/index.test.ts.snap

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2004,6 +2004,17 @@ Object {
20042004
}
20052005
`;
20062006

2007+
exports[`rpc with head:true, count:exact 1`] = `
2008+
Object {
2009+
"body": null,
2010+
"count": 1,
2011+
"data": null,
2012+
"error": null,
2013+
"status": 200,
2014+
"statusText": "OK",
2015+
}
2016+
`;
2017+
20072018
exports[`select on insert 1`] = `
20082019
Object {
20092020
"body": Array [

test/basic.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,15 @@ test("rpc with count: 'exact'", async () => {
213213
expect(res).toMatchSnapshot()
214214
})
215215

216+
test('rpc with head:true, count:exact', async () => {
217+
const res = await postgrest.rpc(
218+
'get_status',
219+
{ name_param: 'supabot' },
220+
{ head: true, count: 'exact' }
221+
)
222+
expect(res).toMatchSnapshot()
223+
})
224+
216225
describe("insert, update, delete with count: 'exact'", () => {
217226
test("insert with count: 'exact'", async () => {
218227
let res = await postgrest

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"rootDir": "src",
99
"sourceMap": true,
1010
"target": "ES2015",
11+
"lib": ["dom", "ES2017"],
1112

1213
"strict": true,
1314

0 commit comments

Comments
 (0)