Skip to content

Commit 18f855f

Browse files
committed
fix(types): computed field over rpc call
Fixes: supabase/supabase-js#1364
1 parent cdca4a2 commit 18f855f

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

src/types.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import PostgrestError from './PostgrestError'
2-
import { ContainsNull } from './select-query-parser/types'
2+
import { ContainsNull, TablesAndViews } from './select-query-parser/types'
33
import { FindMatchingFunctionByArgs, IsAny, SelectQueryError } from './select-query-parser/utils'
44
import { LastOf } from './select-query-parser/types'
55

@@ -43,7 +43,11 @@ export type GetRpcFunctionFilterBuilderByArgs<
4343
: // Otherwise, we use the arguments based function definition narrowing to get the rigt value
4444
Fn extends GenericFunction
4545
? {
46-
Row: Fn['Returns'] extends any[]
46+
Row: Fn['SetofOptions'] extends GenericSetofOption
47+
? Fn['SetofOptions']['isSetofReturn'] extends true
48+
? TablesAndViews<Schema>[Fn['SetofOptions']['to']]['Row']
49+
: TablesAndViews<Schema>[Fn['SetofOptions']['to']]['Row']
50+
: Fn['Returns'] extends any[]
4751
? Fn['Returns'][number] extends Record<string, unknown>
4852
? Fn['Returns'][number]
4953
: never

test/advanced_rpc.test.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,6 @@ describe('advanced rpc', () => {
529529
user_row: { username: 'supabot' },
530530
})
531531
.select('id, username, users(username, catchphrase)')
532-
//@ts-expect-error Property 'eq' does not exist on type
533532
.eq('username', 'nope')
534533

535534
expect(res).toMatchInlineSnapshot(`
@@ -1310,3 +1309,47 @@ describe('advanced rpc', () => {
13101309
UserProfileSchema.parse(res.data)
13111310
})
13121311
})
1312+
1313+
test('RPC call with subselect and computed field', async () => {
1314+
const res = await postgrest
1315+
.rpc('get_messages_by_username', { search_username: 'supabot' })
1316+
// should be able to select computed field
1317+
.select('message, blurb_message')
1318+
// .limit(1)
1319+
expect(res).toMatchInlineSnapshot(`
1320+
Object {
1321+
"count": null,
1322+
"data": Array [
1323+
Object {
1324+
"blurb_message": "Hel",
1325+
"message": "Hello World 👋",
1326+
},
1327+
Object {
1328+
"blurb_message": "Per",
1329+
"message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.",
1330+
},
1331+
Object {
1332+
"blurb_message": "Som",
1333+
"message": "Some message on channel wihtout details",
1334+
},
1335+
Object {
1336+
"blurb_message": "Som",
1337+
"message": "Some message on channel wihtout details",
1338+
},
1339+
],
1340+
"error": null,
1341+
"status": 200,
1342+
"statusText": "OK",
1343+
}
1344+
`)
1345+
let result: Exclude<typeof res.data, null>
1346+
const ExpectedSchema = z.array(
1347+
z.object({
1348+
message: z.string().nullable(),
1349+
blurb_message: z.string().nullable(),
1350+
})
1351+
)
1352+
let expected: z.infer<typeof ExpectedSchema>
1353+
expectType<TypeEqual<typeof result, typeof expected>>(true)
1354+
ExpectedSchema.parse(res.data)
1355+
})

0 commit comments

Comments
 (0)