Skip to content

Commit 03a811d

Browse files
committed
fix: rpc w/ HEAD/GET w/ array param
1 parent ec0762c commit 03a811d

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

src/PostgrestClient.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,19 +145,16 @@ export default class PostgrestClient<
145145
let method: 'HEAD' | 'GET' | 'POST'
146146
const url = new URL(`${this.url}/rpc/${fn}`)
147147
let body: unknown | undefined
148-
if (head) {
149-
method = 'HEAD'
148+
if (head || get) {
149+
method = head ? 'HEAD' : 'GET'
150150
Object.entries(args)
151+
// params with undefined value needs to be filtered out, otherwise it'll
152+
// show up as `?param=undefined`
151153
.filter(([_, value]) => value !== undefined)
154+
// array values need special syntax
155+
.map(([name, value]) => [name, Array.isArray(value) ? `{${value.join(',')}}` : `${value}`])
152156
.forEach(([name, value]) => {
153-
url.searchParams.append(name, `${value}`)
154-
})
155-
} else if (get) {
156-
method = 'GET'
157-
Object.entries(args)
158-
.filter(([_, value]) => value !== undefined)
159-
.forEach(([name, value]) => {
160-
url.searchParams.append(name, `${value}`)
157+
url.searchParams.append(name, value)
161158
})
162159
} else {
163160
method = 'POST'

test/basic.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,23 @@ test('rpc with get:true, optional param', async () => {
950950
`)
951951
})
952952

953+
test('rpc with get:true, array param', async () => {
954+
const res = await postgrest.rpc(
955+
'function_with_array_param',
956+
{ param: ['00000000-0000-0000-0000-000000000000'] },
957+
{ get: true }
958+
)
959+
expect(res).toMatchInlineSnapshot(`
960+
Object {
961+
"count": null,
962+
"data": null,
963+
"error": null,
964+
"status": 204,
965+
"statusText": "No Content",
966+
}
967+
`)
968+
})
969+
953970
test('rpc with dynamic schema', async () => {
954971
const res = await postgrest.schema('personal').rpc('get_status', { name_param: 'kiwicopple' })
955972
expect(res).toMatchInlineSnapshot(`

test/db/00-schema.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,6 @@ create function public.function_with_optional_param(param text default '')
9696
returns text as $$
9797
select param;
9898
$$ language sql immutable;
99+
100+
create function public.function_with_array_param(param uuid[])
101+
returns void as '' language sql immutable;

test/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,12 @@ export type Database = {
206206
}
207207
}
208208
Functions: {
209+
function_with_array_param: {
210+
Args: {
211+
param: string[]
212+
}
213+
Returns: undefined
214+
}
209215
function_with_optional_param: {
210216
Args: {
211217
param?: string

0 commit comments

Comments
 (0)