From a26df0ca12721b2bad6fad40d0318aecfb2c888e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Admir=20=C5=A0aheta?= Date: Tue, 12 Mar 2024 17:37:45 +0100 Subject: [PATCH] fix: type-asserations --- src/PostgrestBuilder.ts | 28 +++++++++++++++++++--------- src/PostgrestError.ts | 6 +++--- src/PostgrestQueryBuilder.ts | 6 +++--- src/types.ts | 6 +++--- 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/PostgrestBuilder.ts b/src/PostgrestBuilder.ts index 96a8bc55..6c62fbac 100644 --- a/src/PostgrestBuilder.ts +++ b/src/PostgrestBuilder.ts @@ -1,7 +1,7 @@ // @ts-ignore import nodeFetch from '@supabase/node-fetch' -import type { Fetch, PostgrestSingleResponse } from './types' +import type { Fetch, PostgrestSingleResponse, PostgrestResponseSuccess } from './types' import PostgrestError from './PostgrestError' export default abstract class PostgrestBuilder @@ -49,7 +49,7 @@ export default abstract class PostgrestBuilder then, TResult2 = never>( onfulfilled?: - | ((value: PostgrestSingleResponse) => TResult1 | PromiseLike) + | ((value: PostgrestResponseSuccess) => TResult1 | PromiseLike) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike) | undefined | null @@ -75,8 +75,8 @@ export default abstract class PostgrestBuilder body: JSON.stringify(this.body), signal: this.signal, }).then(async (res) => { - let error = null - let data = null + let error: PostgrestError | null = null + let data: string | any[] | null = null let count: number | null = null let status = res.status let statusText = res.statusText @@ -110,9 +110,10 @@ export default abstract class PostgrestBuilder if (data.length > 1) { error = { // https://github.com/PostgREST/postgrest/blob/a867d79c42419af16c18c3fb019eba8df992626f/src/PostgREST/Error.hs#L553 + name: 'PostgrestSingleRowError', code: 'PGRST116', details: `Results contain ${data.length} rows, application/vnd.pgrst.object+json requires 1 row`, - hint: null, + hint: 'Use application/vnd.pgrst.row+json or application/json instead', message: 'JSON object requested, multiple (or no) rows returned', } data = null @@ -146,6 +147,10 @@ export default abstract class PostgrestBuilder } else { error = { message: body, + details: '', + hint: '', + code: 'UNKNOWN', + name: 'UnknownError', } } } @@ -161,9 +166,13 @@ export default abstract class PostgrestBuilder } } - const postgrestResponse = { + if (error !== null) { + throw new PostgrestError(error) + } + + const postgrestResponse: PostgrestResponseSuccess = { error, - data, + data: data as Result, count, status, statusText, @@ -174,16 +183,17 @@ export default abstract class PostgrestBuilder if (!this.shouldThrowOnError) { res = res.catch((fetchError) => ({ error: { + name: fetchError?.name ?? 'FetchError', message: `${fetchError?.name ?? 'FetchError'}: ${fetchError?.message}`, details: `${fetchError?.stack ?? ''}`, hint: '', code: `${fetchError?.code ?? ''}`, }, - data: null, + data: null as Result | null, count: null, status: 0, statusText: '', - })) + })) as Promise> } return res.then(onfulfilled, onrejected) diff --git a/src/PostgrestError.ts b/src/PostgrestError.ts index 8253ae96..60d34a64 100644 --- a/src/PostgrestError.ts +++ b/src/PostgrestError.ts @@ -1,9 +1,9 @@ import type { PostgrestError as IPostgrestError } from './types' export default class PostgrestError extends Error implements IPostgrestError { - details: string - hint: string - code: string + details?: string + hint?: string + code?: string constructor(context: IPostgrestError) { super(context.message) diff --git a/src/PostgrestQueryBuilder.ts b/src/PostgrestQueryBuilder.ts index 8f6db3f5..6e163199 100644 --- a/src/PostgrestQueryBuilder.ts +++ b/src/PostgrestQueryBuilder.ts @@ -149,7 +149,7 @@ export default class PostgrestQueryBuilder< ): PostgrestFilterBuilder { const method = 'POST' - const prefersHeaders = [] + const prefersHeaders: string[] = [] if (this.headers['Prefer']) { prefersHeaders.push(this.headers['Prefer']) } @@ -315,7 +315,7 @@ export default class PostgrestQueryBuilder< } = {} ): PostgrestFilterBuilder { const method = 'PATCH' - const prefersHeaders = [] + const prefersHeaders: string[] = [] if (this.headers['Prefer']) { prefersHeaders.push(this.headers['Prefer']) } @@ -360,7 +360,7 @@ export default class PostgrestQueryBuilder< count?: 'exact' | 'planned' | 'estimated' } = {}): PostgrestFilterBuilder { const method = 'DELETE' - const prefersHeaders = [] + const prefersHeaders: string[] = [] if (count) { prefersHeaders.push(`count=${count}`) } diff --git a/src/types.ts b/src/types.ts index 5379b271..3d279238 100644 --- a/src/types.ts +++ b/src/types.ts @@ -7,9 +7,9 @@ export type Fetch = typeof fetch */ export type PostgrestError = { message: string - details: string - hint: string - code: string + details?: string + hint?: string + code?: string } /**