Skip to content

Commit 4aa679f

Browse files
committed
fix(postgrest): address comments
1 parent 21a0b5b commit 4aa679f

6 files changed

Lines changed: 317 additions & 155 deletions

File tree

‎packages/core/postgrest-js/src/PostgrestBuilder.ts‎

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -227,29 +227,15 @@ export default abstract class PostgrestBuilder<
227227
requestHeaders.set('X-Retry-Count', String(attemptCount))
228228
}
229229

230+
// Only wrap the fetch call itself — processResponse errors must never trigger retries
231+
let res: Response
230232
try {
231-
const res = await _fetch(this.url.toString(), {
233+
res = await _fetch(this.url.toString(), {
232234
method: this.method,
233235
headers: requestHeaders,
234236
body: JSON.stringify(this.body),
235237
signal: this.signal,
236238
})
237-
238-
// Check if we should retry this response
239-
if (shouldRetry(this.method, res.status, attemptCount, this.retryEnabled)) {
240-
const retryAfterHeader = res.headers.get('Retry-After')
241-
const delay =
242-
retryAfterHeader !== null
243-
? Math.max(0, parseInt(retryAfterHeader, 10) || 0) * 1000
244-
: getRetryDelay(attemptCount)
245-
await res.text()
246-
attemptCount++
247-
await sleep(delay)
248-
continue
249-
}
250-
251-
// Process successful or final response
252-
return await this.processResponse(res)
253239
} catch (fetchError: any) {
254240
// Never retry aborted requests
255241
if (fetchError?.name === 'AbortError' || fetchError?.code === 'ABORT_ERR') {
@@ -272,6 +258,21 @@ export default abstract class PostgrestBuilder<
272258
// Exhausted retries or retries disabled, throw the last error
273259
throw fetchError
274260
}
261+
262+
// Check if we should retry this HTTP response
263+
if (shouldRetry(this.method, res.status, attemptCount, this.retryEnabled)) {
264+
const retryAfterHeader = res.headers.get('Retry-After')
265+
const delay =
266+
retryAfterHeader !== null
267+
? Math.max(0, parseInt(retryAfterHeader, 10) || 0) * 1000
268+
: getRetryDelay(attemptCount)
269+
await res.text()
270+
attemptCount++
271+
await sleep(delay)
272+
continue
273+
}
274+
275+
return await this.processResponse(res)
275276
}
276277
}
277278

‎packages/core/postgrest-js/src/index.ts‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@ export type {
2929
PostgrestMaybeSingleResponse,
3030
} from './types/types'
3131
export type { ClientServerOptions as PostgrestClientOptions } from './types/common/common'
32-
export {
33-
DEFAULT_MAX_RETRIES,
34-
getRetryDelay,
35-
RETRYABLE_STATUS_CODES,
36-
RETRYABLE_METHODS,
37-
} from './types/common/common'
3832
// https://github.com/supabase/postgrest-js/issues/551
3933
// To be replaced with a helper type that only uses public types
4034
export type { GetResult as UnstableGetResult } from './select-query-parser/result'

‎packages/core/postgrest-js/test/advanced_rpc.test.ts‎

Lines changed: 21 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,6 @@ describe('advanced rpc', () => {
120120
"message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.",
121121
"username": "supabot",
122122
},
123-
{
124-
"channel_id": 3,
125-
"data": null,
126-
"id": 3,
127-
"message": "Some message on channel without details",
128-
"username": "supabot",
129-
},
130123
{
131124
"channel_id": 3,
132125
"data": null,
@@ -199,13 +192,6 @@ describe('advanced rpc', () => {
199192
"message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.",
200193
"username": "supabot",
201194
},
202-
{
203-
"channel_id": 3,
204-
"data": null,
205-
"id": 3,
206-
"message": "Some message on channel without details",
207-
"username": "supabot",
208-
},
209195
{
210196
"channel_id": 3,
211197
"data": null,
@@ -254,13 +240,6 @@ describe('advanced rpc', () => {
254240
"message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.",
255241
"username": "supabot",
256242
},
257-
{
258-
"channel_id": 3,
259-
"data": null,
260-
"id": 3,
261-
"message": "Some message on channel without details",
262-
"username": "supabot",
263-
},
264243
{
265244
"channel_id": 3,
266245
"data": null,
@@ -309,13 +288,6 @@ describe('advanced rpc', () => {
309288
"message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.",
310289
"username": "supabot",
311290
},
312-
{
313-
"channel_id": 3,
314-
"data": null,
315-
"id": 3,
316-
"message": "Some message on channel without details",
317-
"username": "supabot",
318-
},
319291
{
320292
"channel_id": 3,
321293
"data": null,
@@ -356,13 +328,6 @@ describe('advanced rpc', () => {
356328
"message": "Some message on channel without details",
357329
"username": "supabot",
358330
},
359-
{
360-
"channel_id": 3,
361-
"data": null,
362-
"id": 3,
363-
"message": "Some message on channel without details",
364-
"username": "supabot",
365-
},
366331
{
367332
"channel_id": 2,
368333
"data": null,
@@ -404,13 +369,6 @@ describe('advanced rpc', () => {
404369
"message": "Some message on channel without details",
405370
"username": "supabot",
406371
},
407-
{
408-
"channel_id": 3,
409-
"data": null,
410-
"id": 3,
411-
"message": "Some message on channel without details",
412-
"username": "supabot",
413-
},
414372
{
415373
"channel_id": 2,
416374
"data": null,
@@ -447,14 +405,6 @@ describe('advanced rpc', () => {
447405
{
448406
"count": null,
449407
"data": [
450-
{
451-
"channel_id": 3,
452-
"message": "Some message on channel without details",
453-
"users": {
454-
"catchphrase": "'cat' 'fat'",
455-
"username": "supabot",
456-
},
457-
},
458408
{
459409
"channel_id": 3,
460410
"message": "Some message on channel without details",
@@ -723,13 +673,6 @@ describe('advanced rpc', () => {
723673
"message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.",
724674
"username": "supabot",
725675
},
726-
{
727-
"channel_id": 3,
728-
"data": null,
729-
"id": 3,
730-
"message": "Some message on channel without details",
731-
"username": "supabot",
732-
},
733676
{
734677
"channel_id": 3,
735678
"data": null,
@@ -1158,13 +1101,6 @@ describe('advanced rpc', () => {
11581101
"status": "ONLINE",
11591102
"username": "awailas",
11601103
},
1161-
{
1162-
"age_range": "[20,30)",
1163-
"catchphrase": "'fat' 'rat'",
1164-
"data": null,
1165-
"status": "ONLINE",
1166-
"username": "dragarcia",
1167-
},
11681104
{
11691105
"age_range": "[20,30)",
11701106
"catchphrase": "'json' 'test'",
@@ -1179,6 +1115,27 @@ describe('advanced rpc', () => {
11791115
"status": "ONLINE",
11801116
"username": "jsonuser",
11811117
},
1118+
{
1119+
"age_range": "[20,30)",
1120+
"catchphrase": "'fat' 'rat'",
1121+
"data": null,
1122+
"status": "ONLINE",
1123+
"username": "dragarcia",
1124+
},
1125+
{
1126+
"age_range": null,
1127+
"catchphrase": null,
1128+
"data": null,
1129+
"status": "ONLINE",
1130+
"username": "a",
1131+
},
1132+
{
1133+
"age_range": null,
1134+
"catchphrase": null,
1135+
"data": null,
1136+
"status": "ONLINE",
1137+
"username": "b",
1138+
},
11821139
],
11831140
"error": null,
11841141
"status": 200,
@@ -1332,10 +1289,6 @@ test('RPC call with subselect and computed field', async () => {
13321289
"blurb_message": "Som",
13331290
"message": "Some message on channel without details",
13341291
},
1335-
{
1336-
"blurb_message": "Som",
1337-
"message": "Some message on channel without details",
1338-
},
13391292
],
13401293
"error": null,
13411294
"status": 200,

0 commit comments

Comments
 (0)