@@ -54,7 +54,7 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestRespon
54
54
protected headers ! : { [ key : string ] : string }
55
55
protected schema ?: string
56
56
protected body ?: Partial < T > | Partial < T > [ ]
57
- protected shouldThrowOnError ?: boolean
57
+ protected shouldThrowOnError = false
58
58
59
59
constructor ( builder : PostgrestBuilder < T > ) {
60
60
Object . assign ( this , builder )
@@ -90,53 +90,68 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestRespon
90
90
this . headers [ 'Content-Type' ] = 'application/json'
91
91
}
92
92
93
- return fetch ( this . url . toString ( ) , {
93
+ let res = fetch ( this . url . toString ( ) , {
94
94
method : this . method ,
95
95
headers : this . headers ,
96
96
body : JSON . stringify ( this . body ) ,
97
- } )
98
- . then ( async ( res ) => {
99
- let error = null
100
- let data = null
101
- let count = null
102
-
103
- if ( res . ok ) {
104
- const isReturnMinimal = this . headers [ 'Prefer' ] ?. split ( ',' ) . includes ( 'return=minimal' )
105
- if ( this . method !== 'HEAD' && ! isReturnMinimal ) {
106
- const text = await res . text ( )
107
- if ( ! text ) {
108
- // discard `text`
109
- } else if ( this . headers [ 'Accept' ] === 'text/csv' ) {
110
- data = text
111
- } else {
112
- data = JSON . parse ( text )
113
- }
114
- }
97
+ } ) . then ( async ( res ) => {
98
+ let error = null
99
+ let data = null
100
+ let count = null
115
101
116
- const countHeader = this . headers [ 'Prefer' ] ?. match ( / c o u n t = ( e x a c t | p l a n n e d | e s t i m a t e d ) / )
117
- const contentRange = res . headers . get ( 'content-range' ) ?. split ( '/' )
118
- if ( countHeader && contentRange && contentRange . length > 1 ) {
119
- count = parseInt ( contentRange [ 1 ] )
102
+ if ( res . ok ) {
103
+ const isReturnMinimal = this . headers [ 'Prefer' ] ?. split ( ',' ) . includes ( 'return=minimal' )
104
+ if ( this . method !== 'HEAD' && ! isReturnMinimal ) {
105
+ const text = await res . text ( )
106
+ if ( ! text ) {
107
+ // discard `text`
108
+ } else if ( this . headers [ 'Accept' ] === 'text/csv' ) {
109
+ data = text
110
+ } else {
111
+ data = JSON . parse ( text )
120
112
}
121
- } else {
122
- error = await res . json ( )
113
+ }
123
114
124
- if ( error && this . shouldThrowOnError ) {
125
- throw error
126
- }
115
+ const countHeader = this . headers [ 'Prefer' ] ?. match ( / c o u n t = ( e x a c t | p l a n n e d | e s t i m a t e d ) / )
116
+ const contentRange = res . headers . get ( 'content-range' ) ?. split ( '/' )
117
+ if ( countHeader && contentRange && contentRange . length > 1 ) {
118
+ count = parseInt ( contentRange [ 1 ] )
127
119
}
120
+ } else {
121
+ error = await res . json ( )
128
122
129
- const postgrestResponse : PostgrestResponse < T > = {
130
- error,
131
- data,
132
- count,
133
- status : res . status ,
134
- statusText : res . statusText ,
135
- body : data ,
123
+ if ( error && this . shouldThrowOnError ) {
124
+ throw error
136
125
}
126
+ }
127
+
128
+ const postgrestResponse = {
129
+ error,
130
+ data,
131
+ count,
132
+ status : res . status ,
133
+ statusText : res . statusText ,
134
+ body : data ,
135
+ }
136
+
137
+ return postgrestResponse
138
+ } )
139
+ if ( ! this . shouldThrowOnError ) {
140
+ res = res . catch ( ( fetchError ) => ( {
141
+ error : {
142
+ message : `FetchError: ${ fetchError . message } ` ,
143
+ details : '' ,
144
+ hint : '' ,
145
+ code : fetchError . code || '' ,
146
+ } ,
147
+ data : null ,
148
+ body : null ,
149
+ count : null ,
150
+ status : 400 ,
151
+ statusText : 'Bad Request' ,
152
+ } ) )
153
+ }
137
154
138
- return postgrestResponse
139
- } )
140
- . then ( onfulfilled , onrejected )
155
+ return res . then ( onfulfilled , onrejected )
141
156
}
142
157
}
0 commit comments