Skip to content

Commit b303b52

Browse files
authored
fix: make throwOnError work with maybeSingle (#272)
* fix: make throwOnError work with maybeSingle * fix: pass allowEmpty in constructor
1 parent 2b216cf commit b303b52

File tree

4 files changed

+30
-19
lines changed

4 files changed

+30
-19
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ docs
66
# nyc
77
coverage
88
.nyc_output
9+
10+
.idea

src/lib/PostgrestTransformBuilder.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -109,23 +109,8 @@ export default class PostgrestTransformBuilder<T> extends PostgrestBuilder<T> {
109109
*/
110110
maybeSingle(): PromiseLike<PostgrestMaybeSingleResponse<T>> {
111111
this.headers['Accept'] = 'application/vnd.pgrst.object+json'
112-
const _this = new PostgrestTransformBuilder(this)
113-
_this.then = ((onfulfilled: any, onrejected: any) =>
114-
this.then((res: any): any => {
115-
if (res.error?.details?.includes('Results contain 0 rows')) {
116-
return onfulfilled({
117-
error: null,
118-
data: null,
119-
count: res.count,
120-
status: 200,
121-
statusText: 'OK',
122-
body: null,
123-
})
124-
}
125-
126-
return onfulfilled(res)
127-
}, onrejected)) as any
128-
return _this as PromiseLike<PostgrestMaybeSingleResponse<T>>
112+
this.allowEmpty = true
113+
return this as PromiseLike<PostgrestMaybeSingleResponse<T>>
129114
}
130115

131116
/**

src/lib/types.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestRespon
5959
protected shouldThrowOnError: boolean
6060
protected signal?: AbortSignal
6161
protected fetch: Fetch
62+
protected allowEmpty: boolean
6263

6364
constructor(builder: PostgrestBuilder<T>) {
6465
Object.assign(this, builder)
@@ -72,6 +73,7 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestRespon
7273
}
7374
this.fetch = (...args) => _fetch(...args)
7475
this.shouldThrowOnError = builder.shouldThrowOnError || false
76+
this.allowEmpty = builder.allowEmpty || false
7577
}
7678

7779
/**
@@ -116,6 +118,8 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestRespon
116118
let error = null
117119
let data = null
118120
let count = null
121+
let status = res.status
122+
let statusText = res.statusText
119123

120124
if (res.ok) {
121125
const isReturnMinimal = this.headers['Prefer']?.split(',').includes('return=minimal')
@@ -146,6 +150,12 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestRespon
146150
}
147151
}
148152

153+
if (error && this.allowEmpty && error?.details?.includes('Results contain 0 rows')) {
154+
error = null
155+
status = 200
156+
statusText = 'OK'
157+
}
158+
149159
if (error && this.shouldThrowOnError) {
150160
throw error
151161
}
@@ -155,8 +165,8 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestRespon
155165
error,
156166
data,
157167
count,
158-
status: res.status,
159-
statusText: res.statusText,
168+
status,
169+
statusText,
160170
body: data,
161171
}
162172

test/basic.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,20 @@ test('connection error w/ throwOnError', async () => {
204204
expect(isErrorCaught).toBe(true)
205205
})
206206

207+
test('maybeSingle w/ throwOnError', async () => {
208+
let passes = true
209+
await postgrest
210+
.from('messages')
211+
.select()
212+
.eq('message', 'i do not exist')
213+
.throwOnError(true)
214+
.maybeSingle()
215+
.then(undefined, () => {
216+
passes = false
217+
})
218+
expect(passes).toEqual(true)
219+
})
220+
207221
test('custom type', async () => {
208222
interface User {
209223
username: string

0 commit comments

Comments
 (0)