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

Commit b33c5ed

Browse files
committed
chore: Types improvements.
1 parent 38d604d commit b33c5ed

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

src/PostgrestClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default class PostgrestClient {
3939
*/
4040
from<T = any>(table: string): PostgrestQueryBuilder<T> {
4141
const url = `${this.url}/${table}`
42-
return new PostgrestQueryBuilder(url, { headers: this.headers, schema: this.schema })
42+
return new PostgrestQueryBuilder<T>(url, { headers: this.headers, schema: this.schema })
4343
}
4444

4545
/**

src/lib/PostgrestTransformBuilder.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PostgrestBuilder } from './types'
1+
import { PostgrestBuilder, PostgrestSingleResponse } from './types'
22

33
/**
44
* Post-filters (transforms)
@@ -68,8 +68,8 @@ export default class PostgrestTransformBuilder<T> extends PostgrestBuilder<T> {
6868
* Retrieves only one row from the result. Result must be one row (e.g. using
6969
* `limit`), otherwise this will result in an error.
7070
*/
71-
single(): PostgrestTransformBuilder<T> {
71+
single(): PromiseLike<PostgrestSingleResponse<T>> {
7272
this.headers['Accept'] = 'application/vnd.pgrst.object+json'
73-
return this
73+
return this as PromiseLike<PostgrestSingleResponse<T>>
7474
}
7575
}

src/lib/types.ts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,40 @@ interface PostgrestError {
1919
*/
2020
interface PostgrestResponse<T> {
2121
error: PostgrestError | null
22-
data: T | T[] | null
22+
data: T[] | null
2323
status: number
2424
statusText: string
2525
// For backward compatibility: body === data
26-
body: T | T[] | null
26+
body: T[] | null
2727
}
2828

29-
export abstract class PostgrestBuilder<T> implements PromiseLike<any> {
30-
method!: 'GET' | 'HEAD' | 'POST' | 'PATCH' | 'DELETE'
31-
url!: URL
32-
headers!: { [key: string]: string }
33-
schema?: string
34-
body?: Partial<T> | Partial<T>[]
29+
export interface PostgrestSingleResponse<T> {
30+
error: PostgrestError | null
31+
data: T | null
32+
status: number
33+
statusText: string
34+
// For backward compatibility: body === data
35+
body: T | null
36+
}
37+
38+
export abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestResponse<T>> {
39+
protected method!: 'GET' | 'HEAD' | 'POST' | 'PATCH' | 'DELETE'
40+
protected url!: URL
41+
protected headers!: { [key: string]: string }
42+
protected schema?: string
43+
protected body?: Partial<T> | Partial<T>[]
3544

3645
constructor(builder: PostgrestBuilder<T>) {
3746
Object.assign(this, builder)
3847
}
3948

40-
then(onfulfilled?: (value: any) => any, onrejected?: (value: any) => any): Promise<any> {
49+
then<TResult1 = PostgrestResponse<T>, TResult2 = never>(
50+
onfulfilled?:
51+
| ((value: PostgrestResponse<T>) => TResult1 | PromiseLike<TResult1>)
52+
| undefined
53+
| null,
54+
onrejected?: (value: any) => any
55+
): Promise<any> {
4156
// https://postgrest.org/en/stable/api.html#switching-schemas
4257
if (typeof this.schema === 'undefined') {
4358
// skip
@@ -64,13 +79,14 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<any> {
6479
error = await res.json()
6580
data = null
6681
}
67-
return {
82+
const postgrestResponse: PostgrestResponse<T> = {
6883
error,
6984
data,
7085
status: res.status,
7186
statusText: res.statusText,
7287
body: data,
73-
} as PostgrestResponse<T>
88+
}
89+
return postgrestResponse
7490
})
7591
.then(onfulfilled, onrejected)
7692
}

0 commit comments

Comments
 (0)