Skip to content

Commit

Permalink
feat: .rpc() with GET
Browse files Browse the repository at this point in the history
  • Loading branch information
soedirgo committed Mar 28, 2024
1 parent e26266a commit 243bf57
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/PostgrestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ export default class PostgrestClient<
* @param options - Named parameters
* @param options.head - When set to `true`, `data` will not be returned.
* Useful if you only need the count.
* @param options.get - When set to `true`, the function will be called with
* read-only access mode.
* @param options.count - Count algorithm to use to count rows returned by the
* function. Only applicable for [set-returning
* functions](https://www.postgresql.org/docs/current/functions-srf.html).
Expand All @@ -128,9 +130,11 @@ export default class PostgrestClient<
args: Function_['Args'] = {},
{
head = false,
get = false,
count,
}: {
head?: boolean
get?: boolean
count?: 'exact' | 'planned' | 'estimated'
} = {}
): PostgrestFilterBuilder<
Expand All @@ -142,14 +146,19 @@ export default class PostgrestClient<
: never,
Function_['Returns']
> {
let method: 'HEAD' | 'POST'
let method: 'HEAD' | 'GET' | 'POST'
const url = new URL(`${this.url}/rpc/${fn}`)
let body: unknown | undefined
if (head) {
method = 'HEAD'
Object.entries(args).forEach(([name, value]) => {
url.searchParams.append(name, `${value}`)
})
} else if (get) {
method = 'GET'
Object.entries(args).forEach(([name, value]) => {
url.searchParams.append(name, `${value}`)
})
} else {
method = 'POST'
body = args
Expand Down
17 changes: 17 additions & 0 deletions test/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,23 @@ test('rpc with head:true, count:exact', async () => {
`)
})

test('rpc with get:true, count:exact', async () => {
const res = await postgrest.rpc(
'get_status',
{ name_param: 'supabot' },
{ get: true, count: 'exact' }
)
expect(res).toMatchInlineSnapshot(`
Object {
"count": 1,
"data": "ONLINE",
"error": null,
"status": 200,
"statusText": "OK",
}
`)
})

test('rpc with dynamic schema', async () => {
const res = await postgrest.schema('personal').rpc('get_status', { name_param: 'kiwicopple' })
expect(res).toMatchInlineSnapshot(`
Expand Down
12 changes: 9 additions & 3 deletions test/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,16 +270,22 @@ test('abort signal', async () => {
ac.abort()
const res = await postgrest.from('users').select().abortSignal(ac.signal)
expect(res).toMatchInlineSnapshot(
{ error: { details: expect.any(String) } },
{
error: {
code: expect.any(String),
details: expect.any(String),
message: expect.stringMatching(/^AbortError:/),
},
},
`
Object {
"count": null,
"data": null,
"error": Object {
"code": "",
"code": Any<String>,
"details": Any<String>,
"hint": "",
"message": "AbortError: The user aborted a request.",
"message": StringMatching /\\^AbortError:/,
},
"status": 0,
"statusText": "",
Expand Down

0 comments on commit 243bf57

Please sign in to comment.