Skip to content

Commit

Permalink
fix: rpc w/ HEAD/GET w/ array param
Browse files Browse the repository at this point in the history
  • Loading branch information
soedirgo committed Apr 11, 2024
1 parent ec0762c commit 03a811d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/PostgrestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,16 @@ export default class PostgrestClient<
let method: 'HEAD' | 'GET' | 'POST'
const url = new URL(`${this.url}/rpc/${fn}`)
let body: unknown | undefined
if (head) {
method = 'HEAD'
if (head || get) {
method = head ? 'HEAD' : 'GET'
Object.entries(args)
// params with undefined value needs to be filtered out, otherwise it'll
// show up as `?param=undefined`
.filter(([_, value]) => value !== undefined)
// array values need special syntax
.map(([name, value]) => [name, Array.isArray(value) ? `{${value.join(',')}}` : `${value}`])
.forEach(([name, value]) => {
url.searchParams.append(name, `${value}`)
})
} else if (get) {
method = 'GET'
Object.entries(args)
.filter(([_, value]) => value !== undefined)
.forEach(([name, value]) => {
url.searchParams.append(name, `${value}`)
url.searchParams.append(name, value)
})
} else {
method = 'POST'
Expand Down
17 changes: 17 additions & 0 deletions test/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,23 @@ test('rpc with get:true, optional param', async () => {
`)
})

test('rpc with get:true, array param', async () => {
const res = await postgrest.rpc(
'function_with_array_param',
{ param: ['00000000-0000-0000-0000-000000000000'] },
{ get: true }
)
expect(res).toMatchInlineSnapshot(`
Object {
"count": null,
"data": null,
"error": null,
"status": 204,
"statusText": "No Content",
}
`)
})

test('rpc with dynamic schema', async () => {
const res = await postgrest.schema('personal').rpc('get_status', { name_param: 'kiwicopple' })
expect(res).toMatchInlineSnapshot(`
Expand Down
3 changes: 3 additions & 0 deletions test/db/00-schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,6 @@ create function public.function_with_optional_param(param text default '')
returns text as $$
select param;
$$ language sql immutable;

create function public.function_with_array_param(param uuid[])
returns void as '' language sql immutable;
6 changes: 6 additions & 0 deletions test/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ export type Database = {
}
}
Functions: {
function_with_array_param: {
Args: {
param: string[]
}
Returns: undefined
}
function_with_optional_param: {
Args: {
param?: string
Expand Down

0 comments on commit 03a811d

Please sign in to comment.