Skip to content
This repository was archived by the owner on Oct 9, 2025. It is now read-only.

Commit ebe94e7

Browse files
committed
test: allow overriding result types
1 parent 4081771 commit ebe94e7

File tree

3 files changed

+44
-28
lines changed

3 files changed

+44
-28
lines changed

test/basic.ts

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,6 @@ describe('basic insert, update, delete', () => {
178178
})
179179
})
180180

181-
test('table invalid type', async () => {
182-
// @ts-expect-error table invalid type
183-
postgrest.from(42)
184-
})
185-
186181
test('throwOnError throws errors instead of returning them', async () => {
187182
let isErrorCaught = false
188183

@@ -275,18 +270,6 @@ test('maybeSingle w/ throwOnError', async () => {
275270
expect(passes).toEqual(true)
276271
})
277272

278-
test('custom type', async () => {
279-
const { data: users, error } = await postgrest.from('users').select().eq('username', 'supabot')
280-
281-
if (error) {
282-
throw new Error(error.message)
283-
}
284-
285-
const user = users[0]
286-
// Autocomplete should show properties of user after '.'
287-
user?.username
288-
})
289-
290273
test("don't mutate PostgrestClient.headers", async () => {
291274
await postgrest.from('users').select().limit(1).single()
292275
const { error } = await postgrest.from('users').select()
@@ -440,13 +423,3 @@ test('select with no match', async () => {
440423
}
441424
`)
442425
})
443-
444-
test('cannot update non-updatable views', () => {
445-
// @ts-expect-error TS2345
446-
postgrest.from('non_updatable_view').update({})
447-
})
448-
449-
test('cannot update non-updatable columns', () => {
450-
// @ts-expect-error TS2322
451-
postgrest.from('updatable_view').update({ non_updatable_column: 0 })
452-
})

test/index.test-d.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { expectError, expectType } from 'tsd'
2+
import { PostgrestClient } from '../src/index'
3+
import { Database } from './types'
4+
5+
const REST_URL = 'http://localhost:3000'
6+
const postgrest = new PostgrestClient<Database>(REST_URL)
7+
8+
// table invalid type
9+
{
10+
expectError(postgrest.from(42))
11+
}
12+
13+
// can override result type
14+
{
15+
const { data, error } = await postgrest
16+
.from('users')
17+
.select('*, messages(*)')
18+
.returns<{ messages: { foo: 'bar' }[] }>()
19+
if (error) {
20+
throw new Error(error.message)
21+
}
22+
expectType<{ foo: 'bar' }[]>(data[0].messages)
23+
}
24+
{
25+
const { data, error } = await postgrest
26+
.from('users')
27+
.insert({ username: 'foo' })
28+
.select('*, messages(*)')
29+
.returns<{ messages: { foo: 'bar' }[] }>()
30+
if (error) {
31+
throw new Error(error.message)
32+
}
33+
expectType<{ foo: 'bar' }[]>(data[0].messages)
34+
}
35+
36+
// cannot update non-updatable views
37+
{
38+
expectError(postgrest.from('updatable_view').update({ non_updatable_column: 0 }))
39+
}
40+
41+
// cannot update non-updatable columns
42+
{
43+
expectError(postgrest.from('updatable_view').update({ non_updatable_column: 0 }))
44+
}

tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"declarationMap": true,
66
"module": "CommonJS",
77
"outDir": "dist/main",
8-
"rootDir": "src",
98
"sourceMap": true,
109
"target": "ES2015",
1110

0 commit comments

Comments
 (0)