Skip to content

Commit 7c3dd21

Browse files
committed
feat: .rpc() with GET
1 parent 9dd41d9 commit 7c3dd21

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

src/PostgrestClient.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ export default class PostgrestClient<
106106
* @param options - Named parameters
107107
* @param options.head - When set to `true`, `data` will not be returned.
108108
* Useful if you only need the count.
109+
* @param options.get - When set to `true`, the function will be called with
110+
* read-only access mode.
109111
* @param options.count - Count algorithm to use to count rows returned by the
110112
* function. Only applicable for [set-returning
111113
* functions](https://www.postgresql.org/docs/current/functions-srf.html).
@@ -127,9 +129,11 @@ export default class PostgrestClient<
127129
args: Function_['Args'] = {},
128130
{
129131
head = false,
132+
get = false,
130133
count,
131134
}: {
132135
head?: boolean
136+
get?: boolean
133137
count?: 'exact' | 'planned' | 'estimated'
134138
} = {}
135139
): PostgrestFilterBuilder<
@@ -141,14 +145,19 @@ export default class PostgrestClient<
141145
: never,
142146
Function_['Returns']
143147
> {
144-
let method: 'HEAD' | 'POST'
148+
let method: 'HEAD' | 'GET' | 'POST'
145149
const url = new URL(`${this.url}/rpc/${fn}`)
146150
let body: unknown | undefined
147151
if (head) {
148152
method = 'HEAD'
149153
Object.entries(args).forEach(([name, value]) => {
150154
url.searchParams.append(name, `${value}`)
151155
})
156+
} else if (get) {
157+
method = 'GET'
158+
Object.entries(args).forEach(([name, value]) => {
159+
url.searchParams.append(name, `${value}`)
160+
})
152161
} else {
153162
method = 'POST'
154163
body = args

test/basic.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,23 @@ test('rpc with head:true, count:exact', async () => {
916916
`)
917917
})
918918

919+
test('rpc with get:true, count:exact', async () => {
920+
const res = await postgrest.rpc(
921+
'get_status',
922+
{ name_param: 'supabot' },
923+
{ get: true, count: 'exact' }
924+
)
925+
expect(res).toMatchInlineSnapshot(`
926+
Object {
927+
"count": 1,
928+
"data": "ONLINE",
929+
"error": null,
930+
"status": 200,
931+
"statusText": "OK",
932+
}
933+
`)
934+
})
935+
919936
test('rpc with dynamic schema', async () => {
920937
const res = await postgrest.schema('personal').rpc('get_status', { name_param: 'kiwicopple' })
921938
expect(res).toMatchInlineSnapshot(`

test/transforms.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,16 +270,22 @@ test('abort signal', async () => {
270270
ac.abort()
271271
const res = await postgrest.from('users').select().abortSignal(ac.signal)
272272
expect(res).toMatchInlineSnapshot(
273-
{ error: { details: expect.any(String) } },
273+
{
274+
error: {
275+
code: expect.any(String),
276+
details: expect.any(String),
277+
message: expect.stringMatching(/^AbortError:/),
278+
},
279+
},
274280
`
275281
Object {
276282
"count": null,
277283
"data": null,
278284
"error": Object {
279-
"code": "",
285+
"code": Any<String>,
280286
"details": Any<String>,
281287
"hint": "",
282-
"message": "AbortError: The user aborted a request.",
288+
"message": StringMatching /\\^AbortError:/,
283289
},
284290
"status": 0,
285291
"statusText": "",

0 commit comments

Comments
 (0)