Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: .rpc() with GET #529

Merged
merged 1 commit into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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