From 76ccaa1d0cbfb2cce2c0832decd9ec87cf5c7f42 Mon Sep 17 00:00:00 2001 From: steinroe Date: Tue, 31 May 2022 15:43:38 +0200 Subject: [PATCH 1/2] fix: make throwOnError work with maybeSingle --- .gitignore | 2 ++ src/lib/PostgrestTransformBuilder.ts | 19 ++----------------- src/lib/types.ts | 14 ++++++++++++-- test/basic.ts | 14 ++++++++++++++ 4 files changed, 30 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 5044e41f..1b1d4286 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ docs # nyc coverage .nyc_output + +.idea \ No newline at end of file diff --git a/src/lib/PostgrestTransformBuilder.ts b/src/lib/PostgrestTransformBuilder.ts index f2e48e0c..fa8d54de 100644 --- a/src/lib/PostgrestTransformBuilder.ts +++ b/src/lib/PostgrestTransformBuilder.ts @@ -109,23 +109,8 @@ export default class PostgrestTransformBuilder extends PostgrestBuilder { */ maybeSingle(): PromiseLike> { this.headers['Accept'] = 'application/vnd.pgrst.object+json' - const _this = new PostgrestTransformBuilder(this) - _this.then = ((onfulfilled: any, onrejected: any) => - this.then((res: any): any => { - if (res.error?.details?.includes('Results contain 0 rows')) { - return onfulfilled({ - error: null, - data: null, - count: res.count, - status: 200, - statusText: 'OK', - body: null, - }) - } - - return onfulfilled(res) - }, onrejected)) as any - return _this as PromiseLike> + this.allowEmpty = true + return this as PromiseLike> } /** diff --git a/src/lib/types.ts b/src/lib/types.ts index 40a2548f..3f0726f5 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -59,6 +59,7 @@ export abstract class PostgrestBuilder implements PromiseLike) { Object.assign(this, builder) @@ -72,6 +73,7 @@ export abstract class PostgrestBuilder implements PromiseLike _fetch(...args) this.shouldThrowOnError = builder.shouldThrowOnError || false + this.allowEmpty = false } /** @@ -116,6 +118,8 @@ export abstract class PostgrestBuilder implements PromiseLike implements PromiseLike implements PromiseLike { expect(isErrorCaught).toBe(true) }) +test('maybeSingle w/ throwOnError', async () => { + let passes = true + await postgrest + .from('messages') + .select() + .eq('message', 'i do not exist') + .throwOnError(true) + .maybeSingle() + .then(undefined, () => { + passes = false + }) + expect(passes).toEqual(true) +}) + test('custom type', async () => { interface User { username: string From 7850781727b4aea28fa22e17ca38d96e405edd0a Mon Sep 17 00:00:00 2001 From: steinroe Date: Tue, 31 May 2022 15:52:25 +0200 Subject: [PATCH 2/2] fix: pass allowEmpty in constructor --- src/lib/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/types.ts b/src/lib/types.ts index 3f0726f5..2670a4ca 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -73,7 +73,7 @@ export abstract class PostgrestBuilder implements PromiseLike _fetch(...args) this.shouldThrowOnError = builder.shouldThrowOnError || false - this.allowEmpty = false + this.allowEmpty = builder.allowEmpty || false } /**