@@ -154,10 +154,9 @@ export default abstract class PostgrestBuilder<
154154 return this
155155 }
156156
157- /** *
158- * @category Database
159- */
160157 /**
158+ * @category Database
159+ *
161160 * Configure retry behavior for this request.
162161 *
163162 * By default, retries are enabled for GET/HEAD requests that fail with
@@ -229,29 +228,6 @@ export default abstract class PostgrestBuilder<
229228 requestHeaders . set ( 'X-Retry-Count' , String ( attemptCount ) )
230229 }
231230
232- // Fix for https://github.com/supabase/postgrest-js/issues/361 — applies to all methods.
233- if ( this . isMaybeSingle && Array . isArray ( data ) ) {
234- if ( data . length > 1 ) {
235- error = {
236- // https://github.com/PostgREST/postgrest/blob/a867d79c42419af16c18c3fb019eba8df992626f/src/PostgREST/Error.hs#L553
237- code : 'PGRST116' ,
238- details : `Results contain ${ data . length } rows, application/vnd.pgrst.object+json requires 1 row` ,
239- hint : null ,
240- message : 'JSON object requested, multiple (or no) rows returned' ,
241- }
242- data = null
243- count = null
244- status = 406
245- statusText = 'Not Acceptable'
246- } else if ( data . length === 1 ) {
247- data = data [ 0 ]
248- } else {
249- data = null
250- }
251- }
252- } else {
253- const body = await res . text ( )
254-
255231 try {
256232 const res = await _fetch ( this . url . toString ( ) , {
257233 method : this . method ,
@@ -262,7 +238,12 @@ export default abstract class PostgrestBuilder<
262238
263239 // Check if we should retry this response
264240 if ( shouldRetry ( this . method , res . status , attemptCount , this . retryEnabled ) ) {
265- const delay = getRetryDelay ( attemptCount )
241+ const retryAfterHeader = res . headers . get ( 'Retry-After' )
242+ const delay =
243+ retryAfterHeader !== null
244+ ? Math . max ( 0 , parseInt ( retryAfterHeader , 10 ) || 0 ) * 1000
245+ : getRetryDelay ( attemptCount )
246+ await res . body ?. cancel ( )
266247 attemptCount ++
267248 await sleep ( delay )
268249 continue
@@ -271,13 +252,16 @@ export default abstract class PostgrestBuilder<
271252 // Process successful or final response
272253 return await this . processResponse ( res )
273254 } catch ( fetchError : any ) {
255+ // Never retry aborted requests
256+ if ( fetchError ?. name === 'AbortError' || fetchError ?. code === 'ABORT_ERR' ) {
257+ throw fetchError
258+ }
259+
274260 // Don't retry network errors for non-idempotent methods
275261 if ( ! RETRYABLE_METHODS . includes ( this . method as ( typeof RETRYABLE_METHODS ) [ number ] ) ) {
276262 throw fetchError
277263 }
278264
279- if ( error && this . shouldThrowOnError ) {
280- throw new PostgrestError ( error )
281265 // Check if we should retry network errors
282266 if ( this . retryEnabled && attemptCount < DEFAULT_MAX_RETRIES ) {
283267 const delay = getRetryDelay ( attemptCount )
@@ -404,9 +388,8 @@ export default abstract class PostgrestBuilder<
404388 count = parseInt ( contentRange [ 1 ] )
405389 }
406390
407- // Temporary partial fix for https://github.com/supabase/postgrest-js/issues/361
408- // Issue persists e.g. for `.insert([...]).select().maybeSingle()`
409- if ( this . isMaybeSingle && this . method === 'GET' && Array . isArray ( data ) ) {
391+ // Fix for https://github.com/supabase/postgrest-js/issues/361 — applies to all methods.
392+ if ( this . isMaybeSingle && Array . isArray ( data ) ) {
410393 if ( data . length > 1 ) {
411394 error = {
412395 // https://github.com/PostgREST/postgrest/blob/a867d79c42419af16c18c3fb019eba8df992626f/src/PostgREST/Error.hs#L553
@@ -450,12 +433,6 @@ export default abstract class PostgrestBuilder<
450433 }
451434 }
452435
453- if ( error && this . isMaybeSingle && error ?. details ?. includes ( '0 rows' ) ) {
454- error = null
455- status = 200
456- statusText = 'OK'
457- }
458-
459436 if ( error && this . shouldThrowOnError ) {
460437 throw new PostgrestError ( error )
461438 }
0 commit comments