Skip to content

Commit

Permalink
refactor: shape response format
Browse files Browse the repository at this point in the history
  • Loading branch information
soedirgo committed Sep 14, 2020
1 parent 6d9b081 commit 731a3b5
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/builder.ts
Expand Up @@ -9,10 +9,14 @@ interface PostgrestError {
code: string
}

// type PostgrestResponse<T> = T | T[] | PostgrestError

interface PostgrestResponse<T> extends Omit<Response, 'body'> {
body: T | T[] | PostgrestError
// Response format: https://github.com/supabase/supabase-js/issues/32
interface PostgrestResponse<T> {
error: PostgrestError | null
data: T | T[] | null
status: number
statusText: string
// For backward compatibility: body === data
body: T | T[] | null
}

/**
Expand All @@ -30,7 +34,7 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<any> {
Object.assign(this, builder)
}

then(onfulfilled?: any, onrejected?: any): Promise<any> {
then(onfulfilled?: (value: any) => any, onrejected?: (value: any) => any): Promise<any> {
// https://postgrest.org/en/stable/api.html#switching-schemas
if (typeof this.schema === 'undefined') {
// skip
Expand All @@ -49,9 +53,21 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<any> {
body: JSON.stringify(this.body),
})
.then(async (res) => {
// HACK: Coerce the type to PostgrestResponse<T>
;(res as any).body = await res.json()
return (res as unknown) as PostgrestResponse<T>
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<T>
})
.then(onfulfilled, onrejected)
}
Expand Down

0 comments on commit 731a3b5

Please sign in to comment.