Skip to content

Commit 890c020

Browse files
committed
feat: revert to null in response
To make it consistent with other client libs. Will decide on `null` vs. `undefined` in response for supabase-js v3.
1 parent 3bfa221 commit 890c020

File tree

6 files changed

+192
-193
lines changed

6 files changed

+192
-193
lines changed

src/PostgrestBuilder.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export default abstract class PostgrestBuilder<Result>
5353
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null
5454
): PromiseLike<TResult1 | TResult2> {
5555
// https://postgrest.org/en/stable/api.html#switching-schemas
56-
if (typeof this.schema === 'undefined') {
56+
if (this.schema === undefined) {
5757
// skip
5858
} else if (['GET', 'HEAD'].includes(this.method)) {
5959
this.headers['Accept-Profile'] = this.schema
@@ -73,27 +73,26 @@ export default abstract class PostgrestBuilder<Result>
7373
body: JSON.stringify(this.body),
7474
signal: this.signal,
7575
}).then(async (res) => {
76-
let error = undefined
77-
let data = undefined
78-
let count = undefined
76+
let error = null
77+
let data = null
78+
let count: number | null = null
7979
let status = res.status
8080
let statusText = res.statusText
8181

8282
if (res.ok) {
83-
const isReturnMinimal = this.headers['Prefer']?.split(',').includes('return=minimal')
84-
if (this.method !== 'HEAD' && !isReturnMinimal) {
85-
const text = await res.text()
86-
if (!text) {
87-
// discard `text`
83+
if (this.method !== 'HEAD') {
84+
const body = await res.text()
85+
if (body === "") {
86+
// Prefer: return=minimal
8887
} else if (this.headers['Accept'] === 'text/csv') {
89-
data = text
88+
data = body
9089
} else if (
9190
this.headers['Accept'] &&
92-
this.headers['Accept'].indexOf('application/vnd.pgrst.plan+text') !== -1
91+
this.headers['Accept'].includes('application/vnd.pgrst.plan+text')
9392
) {
94-
data = text
93+
data = body
9594
} else {
96-
data = JSON.parse(text)
95+
data = JSON.parse(body)
9796
}
9897
}
9998

@@ -114,7 +113,7 @@ export default abstract class PostgrestBuilder<Result>
114113
}
115114

116115
if (error && this.allowEmpty && error?.details?.includes('Results contain 0 rows')) {
117-
error = undefined
116+
error = null
118117
status = 200
119118
statusText = 'OK'
120119
}
@@ -142,10 +141,10 @@ export default abstract class PostgrestBuilder<Result>
142141
hint: '',
143142
code: fetchError.code || '',
144143
},
145-
data: undefined,
146-
count: undefined,
147-
status: 400,
148-
statusText: 'Bad Request',
144+
data: null,
145+
count: null,
146+
status: 0,
147+
statusText: '',
149148
}))
150149
}
151150

src/types.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,26 @@ interface PostgrestResponseBase {
2323
}
2424

2525
interface PostgrestResponseSuccess<T> extends PostgrestResponseBase {
26-
error: undefined
26+
error: null
2727
data: T[]
28-
count: number | undefined
28+
count: number | null
2929
}
3030
interface PostgrestResponseFailure extends PostgrestResponseBase {
3131
error: PostgrestError
32-
data: undefined
33-
count: undefined
32+
data: null
33+
count: null
3434
}
3535
export type PostgrestResponse<T> = PostgrestResponseSuccess<T> | PostgrestResponseFailure
3636

3737
interface PostgrestSingleResponseSuccess<T> extends PostgrestResponseBase {
38-
error: undefined
38+
error: null
3939
data: T
40-
count: number | undefined
40+
count: number | null
4141
}
4242
export type PostgrestSingleResponse<T> =
4343
| PostgrestSingleResponseSuccess<T>
4444
| PostgrestResponseFailure
45-
export type PostgrestMaybeSingleResponse<T> = PostgrestSingleResponse<T | undefined>
45+
export type PostgrestMaybeSingleResponse<T> = PostgrestSingleResponse<T | null>
4646

4747
export type GenericTable = {
4848
Row: Record<string, unknown>

0 commit comments

Comments
 (0)