Skip to content

Commit 2895b77

Browse files
committed
fix: ignore undefined args when using rpc with head/get
1 parent 7c3dd21 commit 2895b77

File tree

4 files changed

+120
-10
lines changed

4 files changed

+120
-10
lines changed

src/PostgrestClient.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,18 @@ export default class PostgrestClient<
150150
let body: unknown | undefined
151151
if (head) {
152152
method = 'HEAD'
153-
Object.entries(args).forEach(([name, value]) => {
154-
url.searchParams.append(name, `${value}`)
155-
})
153+
Object.entries(args)
154+
.filter(([_, value]) => value !== undefined)
155+
.forEach(([name, value]) => {
156+
url.searchParams.append(name, `${value}`)
157+
})
156158
} else if (get) {
157159
method = 'GET'
158-
Object.entries(args).forEach(([name, value]) => {
159-
url.searchParams.append(name, `${value}`)
160-
})
160+
Object.entries(args)
161+
.filter(([_, value]) => value !== undefined)
162+
.forEach(([name, value]) => {
163+
url.searchParams.append(name, `${value}`)
164+
})
161165
} else {
162166
method = 'POST'
163167
body = args

test/basic.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,23 @@ test('rpc with get:true, count:exact', async () => {
933933
`)
934934
})
935935

936+
test('rpc with get:true, optional param', async () => {
937+
const res = await postgrest.rpc(
938+
'function_with_optional_param',
939+
{ param: undefined },
940+
{ get: true }
941+
)
942+
expect(res).toMatchInlineSnapshot(`
943+
Object {
944+
"count": null,
945+
"data": "",
946+
"error": null,
947+
"status": 200,
948+
"statusText": "OK",
949+
}
950+
`)
951+
})
952+
936953
test('rpc with dynamic schema', async () => {
937954
const res = await postgrest.schema('personal').rpc('get_status', { name_param: 'kiwicopple' })
938955
expect(res).toMatchInlineSnapshot(`

test/db/00-schema.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,8 @@ CREATE FUNCTION personal.get_status(name_param text)
9191
RETURNS user_status AS $$
9292
SELECT status from users WHERE username=name_param;
9393
$$ LANGUAGE SQL IMMUTABLE;
94+
95+
create function public.function_with_optional_param(param text default '')
96+
returns text as $$
97+
select param;
98+
$$ language sql immutable;

test/types.ts

Lines changed: 88 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export type Json = string | number | boolean | null | { [key: string]: Json | undefined } | Json[]
22

3-
export interface Database {
3+
export type Database = {
44
personal: {
55
Tables: {
66
users: {
@@ -112,25 +112,29 @@ export interface Database {
112112
{
113113
foreignKeyName: 'messages_channel_id_fkey'
114114
columns: ['channel_id']
115+
isOneToOne: false
115116
referencedRelation: 'channels'
116117
referencedColumns: ['id']
117118
},
118119
{
119120
foreignKeyName: 'messages_username_fkey'
120121
columns: ['username']
121-
referencedRelation: 'users'
122+
isOneToOne: false
123+
referencedRelation: 'non_updatable_view'
122124
referencedColumns: ['username']
123125
},
124126
{
125127
foreignKeyName: 'messages_username_fkey'
126128
columns: ['username']
127-
referencedRelation: 'non_updatable_view'
129+
isOneToOne: false
130+
referencedRelation: 'updatable_view'
128131
referencedColumns: ['username']
129132
},
130133
{
131134
foreignKeyName: 'messages_username_fkey'
132135
columns: ['username']
133-
referencedRelation: 'updatable_view'
136+
isOneToOne: false
137+
referencedRelation: 'users'
134138
referencedColumns: ['username']
135139
}
136140
]
@@ -202,6 +206,12 @@ export interface Database {
202206
}
203207
}
204208
Functions: {
209+
function_with_optional_param: {
210+
Args: {
211+
param?: string
212+
}
213+
Returns: string
214+
}
205215
get_status: {
206216
Args: {
207217
name_param: string
@@ -236,3 +246,77 @@ export interface Database {
236246
}
237247
}
238248
}
249+
250+
type PublicSchema = Database[Extract<keyof Database, 'public'>]
251+
252+
export type Tables<
253+
PublicTableNameOrOptions extends
254+
| keyof (PublicSchema['Tables'] & PublicSchema['Views'])
255+
| { schema: keyof Database },
256+
TableName extends PublicTableNameOrOptions extends { schema: keyof Database }
257+
? keyof (Database[PublicTableNameOrOptions['schema']]['Tables'] &
258+
Database[PublicTableNameOrOptions['schema']]['Views'])
259+
: never = never
260+
> = PublicTableNameOrOptions extends { schema: keyof Database }
261+
? (Database[PublicTableNameOrOptions['schema']]['Tables'] &
262+
Database[PublicTableNameOrOptions['schema']]['Views'])[TableName] extends {
263+
Row: infer R
264+
}
265+
? R
266+
: never
267+
: PublicTableNameOrOptions extends keyof (PublicSchema['Tables'] & PublicSchema['Views'])
268+
? (PublicSchema['Tables'] & PublicSchema['Views'])[PublicTableNameOrOptions] extends {
269+
Row: infer R
270+
}
271+
? R
272+
: never
273+
: never
274+
275+
export type TablesInsert<
276+
PublicTableNameOrOptions extends keyof PublicSchema['Tables'] | { schema: keyof Database },
277+
TableName extends PublicTableNameOrOptions extends { schema: keyof Database }
278+
? keyof Database[PublicTableNameOrOptions['schema']]['Tables']
279+
: never = never
280+
> = PublicTableNameOrOptions extends { schema: keyof Database }
281+
? Database[PublicTableNameOrOptions['schema']]['Tables'][TableName] extends {
282+
Insert: infer I
283+
}
284+
? I
285+
: never
286+
: PublicTableNameOrOptions extends keyof PublicSchema['Tables']
287+
? PublicSchema['Tables'][PublicTableNameOrOptions] extends {
288+
Insert: infer I
289+
}
290+
? I
291+
: never
292+
: never
293+
294+
export type TablesUpdate<
295+
PublicTableNameOrOptions extends keyof PublicSchema['Tables'] | { schema: keyof Database },
296+
TableName extends PublicTableNameOrOptions extends { schema: keyof Database }
297+
? keyof Database[PublicTableNameOrOptions['schema']]['Tables']
298+
: never = never
299+
> = PublicTableNameOrOptions extends { schema: keyof Database }
300+
? Database[PublicTableNameOrOptions['schema']]['Tables'][TableName] extends {
301+
Update: infer U
302+
}
303+
? U
304+
: never
305+
: PublicTableNameOrOptions extends keyof PublicSchema['Tables']
306+
? PublicSchema['Tables'][PublicTableNameOrOptions] extends {
307+
Update: infer U
308+
}
309+
? U
310+
: never
311+
: never
312+
313+
export type Enums<
314+
PublicEnumNameOrOptions extends keyof PublicSchema['Enums'] | { schema: keyof Database },
315+
EnumName extends PublicEnumNameOrOptions extends { schema: keyof Database }
316+
? keyof Database[PublicEnumNameOrOptions['schema']]['Enums']
317+
: never = never
318+
> = PublicEnumNameOrOptions extends { schema: keyof Database }
319+
? Database[PublicEnumNameOrOptions['schema']]['Enums'][EnumName]
320+
: PublicEnumNameOrOptions extends keyof PublicSchema['Enums']
321+
? PublicSchema['Enums'][PublicEnumNameOrOptions]
322+
: never

0 commit comments

Comments
 (0)