Skip to content

Commit c4f1d65

Browse files
committed
non-nullable throwOnError
1 parent 18f580c commit c4f1d65

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

src/PostgrestBuilder.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
// @ts-ignore
22
import nodeFetch from '@supabase/node-fetch'
33

4-
import type { Fetch, PostgrestSingleResponse } from './types'
4+
import type { Fetch, PostgrestResponseSuccess, PostgrestSingleResponse } from './types'
55

6-
export default abstract class PostgrestBuilder<Result>
6+
export default abstract class PostgrestBuilder<Result, ThrowOnError extends boolean = false>
77
implements PromiseLike<PostgrestSingleResponse<Result>>
88
{
99
protected method: 'GET' | 'HEAD' | 'POST' | 'PATCH' | 'DELETE'
1010
protected url: URL
1111
protected headers: Record<string, string>
1212
protected schema?: string
1313
protected body?: unknown
14-
protected shouldThrowOnError = false
14+
protected shouldThrowOnError: boolean
1515
protected signal?: AbortSignal
1616
protected fetch: Fetch
1717
protected isMaybeSingle: boolean
1818

19-
constructor(builder: PostgrestBuilder<Result>) {
19+
constructor(builder: PostgrestBuilder<Result, ThrowOnError>) {
2020
this.method = builder.method
2121
this.url = builder.url
2222
this.headers = builder.headers
@@ -35,20 +35,18 @@ export default abstract class PostgrestBuilder<Result>
3535
}
3636
}
3737

38-
/**
39-
* If there's an error with the query, throwOnError will reject the promise by
40-
* throwing the error instead of returning it as part of a successful response.
41-
*
42-
* {@link https://github.com/supabase/supabase-js/issues/92}
43-
*/
44-
throwOnError(): this {
38+
throwOnError(): PostgrestBuilder<Result, true> {
4539
this.shouldThrowOnError = true
46-
return this
40+
return this as PostgrestBuilder<Result, true>
4741
}
4842

4943
then<TResult1 = PostgrestSingleResponse<Result>, TResult2 = never>(
5044
onfulfilled?:
51-
| ((value: PostgrestSingleResponse<Result>) => TResult1 | PromiseLike<TResult1>)
45+
| ((
46+
value: ThrowOnError extends true
47+
? PostgrestResponseSuccess<Result>
48+
: PostgrestSingleResponse<Result>
49+
) => TResult1 | PromiseLike<TResult1>)
5250
| undefined
5351
| null,
5452
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null

test/index.test-d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,14 @@ const postgrest = new PostgrestClient<Database>(REST_URL)
9797
const res = await postgrest.from('users').select('username, dat')
9898
expectType<PostgrestSingleResponse<SelectQueryError<`Referencing missing column \`dat\``>[]>>(res)
9999
}
100+
101+
// throw on error
102+
{
103+
const { data } = await postgrest
104+
.from('users')
105+
.select('*')
106+
.returns<{ a: string; b: number }[]>()
107+
.throwOnError()
108+
109+
expectType<{ a: string; b: number }>(data[0])
110+
}

0 commit comments

Comments
 (0)