diff --git a/src/builder.ts b/src/builder.ts index af7678c7..451f33d8 100644 --- a/src/builder.ts +++ b/src/builder.ts @@ -9,10 +9,14 @@ interface PostgrestError { code: string } -// type PostgrestResponse = T | T[] | PostgrestError - -interface PostgrestResponse extends Omit { - body: T | T[] | PostgrestError +// Response format: https://github.com/supabase/supabase-js/issues/32 +interface PostgrestResponse { + error: PostgrestError | null + data: T | T[] | null + status: number + statusText: string + // For backward compatibility: body === data + body: T | T[] | null } /** @@ -30,7 +34,7 @@ export abstract class PostgrestBuilder implements PromiseLike { Object.assign(this, builder) } - then(onfulfilled?: any, onrejected?: any): Promise { + then(onfulfilled?: (value: any) => any, onrejected?: (value: any) => any): Promise { // https://postgrest.org/en/stable/api.html#switching-schemas if (typeof this.schema === 'undefined') { // skip @@ -49,9 +53,21 @@ export abstract class PostgrestBuilder implements PromiseLike { body: JSON.stringify(this.body), }) .then(async (res) => { - // HACK: Coerce the type to PostgrestResponse - ;(res as any).body = await res.json() - return (res as unknown) as PostgrestResponse + let error, data + if (res.ok) { + error = null + data = await res.json() + } else { + error = await res.json() + data = null + } + return { + error, + data, + status: res.status, + statusText: res.statusText, + body: data, + } as PostgrestResponse }) .then(onfulfilled, onrejected) }