From e31fb523dd951ee3cc0ad7ad0bb1d52a09e97cf6 Mon Sep 17 00:00:00 2001 From: Guilherme Souza Date: Thu, 29 Jan 2026 06:08:22 -0300 Subject: [PATCH 01/12] feat(postgrest): add automatic retries for transient errors Implement automatic retry logic for PostgREST SDK requests that fail with 520 status codes (Cloudflare timeout/connection errors). Key features: - Enabled by default for GET/HEAD/OPTIONS requests - 3 retry attempts with exponential backoff (1s, 2s, 4s) - X-Retry-Count header sent for observability - Can be disabled globally or per-request via .retry(false) This addresses customer feedback about perceived reliability issues when transient 520 errors occur under load. Related: https://linear.app/supabase/project/automatic-retries-for-postgrest-sdk-c6fa4eaf163a Co-Authored-By: Claude Opus 4.5 --- .../core/postgrest-js/src/PostgrestBuilder.ts | 283 +++++++++++---- .../core/postgrest-js/src/PostgrestClient.ts | 15 + .../postgrest-js/src/PostgrestQueryBuilder.ts | 11 + packages/core/postgrest-js/src/index.ts | 6 + .../postgrest-js/src/types/common/common.ts | 26 ++ packages/core/postgrest-js/test/retry.test.ts | 340 ++++++++++++++++++ .../src/lib/rest/types/common/common.ts | 26 ++ 7 files changed, 647 insertions(+), 60 deletions(-) create mode 100644 packages/core/postgrest-js/test/retry.test.ts diff --git a/packages/core/postgrest-js/src/PostgrestBuilder.ts b/packages/core/postgrest-js/src/PostgrestBuilder.ts index 82b430d752a..cd0d68ad9e1 100644 --- a/packages/core/postgrest-js/src/PostgrestBuilder.ts +++ b/packages/core/postgrest-js/src/PostgrestBuilder.ts @@ -5,10 +5,51 @@ import type { MergePartialResult, IsValidResultOverride, } from './types/types' -import { ClientServerOptions, Fetch } from './types/common/common' +import { + ClientServerOptions, + Fetch, + DEFAULT_MAX_RETRIES, + getRetryDelay, + RETRYABLE_STATUS_CODES, + RETRYABLE_METHODS, +} from './types/common/common' import PostgrestError from './PostgrestError' import { ContainsNull } from './select-query-parser/types' +/** + * Sleep for a given number of milliseconds. + */ +function sleep(ms: number): Promise { + return new Promise((resolve) => setTimeout(resolve, ms)) +} + +/** + * Check if a request should be retried based on method and status code. + */ +function shouldRetry( + method: string, + status: number, + attemptCount: number, + retryEnabled: boolean +): boolean { + // Don't retry if retries are disabled or we've exhausted attempts + if (!retryEnabled || attemptCount >= DEFAULT_MAX_RETRIES) { + return false + } + + // Only retry idempotent methods (GET, HEAD, OPTIONS) + if (!RETRYABLE_METHODS.includes(method as (typeof RETRYABLE_METHODS)[number])) { + return false + } + + // Only retry on specific status codes (520 - Cloudflare errors) + if (!RETRYABLE_STATUS_CODES.includes(status as (typeof RETRYABLE_STATUS_CODES)[number])) { + return false + } + + return true +} + export default abstract class PostgrestBuilder< ClientOptions extends ClientServerOptions, Result, @@ -29,6 +70,9 @@ export default abstract class PostgrestBuilder< protected isMaybeSingle: boolean protected urlLengthLimit: number + // Retry configuration - enabled by default + protected retryEnabled: boolean = true + /** * Creates a builder configured for a specific PostgREST request. * @@ -65,6 +109,8 @@ export default abstract class PostgrestBuilder< fetch?: Fetch isMaybeSingle?: boolean urlLengthLimit?: number + // Retry option + retry?: boolean }) { this.method = builder.method this.url = builder.url @@ -75,6 +121,7 @@ export default abstract class PostgrestBuilder< this.signal = builder.signal this.isMaybeSingle = builder.isMaybeSingle ?? false this.urlLengthLimit = builder.urlLengthLimit ?? 8000 + this.retryEnabled = builder.retry ?? true if (builder.fetch) { this.fetch = builder.fetch @@ -110,6 +157,29 @@ export default abstract class PostgrestBuilder< /** * * @category Database */ + /** + * Configure retry behavior for this request. + * + * By default, retries are enabled for GET/HEAD requests that fail with + * 520 status codes (Cloudflare timeout/connection errors). Retries use + * exponential backoff (1s, 2s, 4s) with a maximum of 3 attempts. + * + * @param enabled - Whether to enable retries for this request + * + * @example + * ```ts + * // Disable retries for a specific query + * const { data, error } = await supabase + * .from('users') + * .select() + * .retry(false) + * ``` + */ + retry(enabled: boolean): this { + this.retryEnabled = enabled + return this + } + then< TResult1 = ThrowOnError extends true ? PostgrestResponseSuccess @@ -141,39 +211,22 @@ export default abstract class PostgrestBuilder< // NOTE: Invoke w/o `this` to avoid illegal invocation error. // https://github.com/supabase/postgrest-js/pull/247 const _fetch = this.fetch - let res = _fetch(this.url.toString(), { - method: this.method, - headers: this.headers, - body: JSON.stringify(this.body), - signal: this.signal, - }).then(async (res) => { - let error = null - let data = null - let count: number | null = null - let status = res.status - let statusText = res.statusText - - if (res.ok) { - if (this.method !== 'HEAD') { - const body = await res.text() - if (body === '') { - // Prefer: return=minimal - } else if (this.headers.get('Accept') === 'text/csv') { - data = body - } else if ( - this.headers.get('Accept') && - this.headers.get('Accept')?.includes('application/vnd.pgrst.plan+text') - ) { - data = body - } else { - data = JSON.parse(body) - } - } - const countHeader = this.headers.get('Prefer')?.match(/count=(exact|planned|estimated)/) - const contentRange = res.headers.get('content-range')?.split('/') - if (countHeader && contentRange && contentRange.length > 1) { - count = parseInt(contentRange[1]) + // Execute fetch with retry logic + const executeWithRetry = async (): Promise<{ + error: any + data: any + count: number | null + status: number + statusText: string + }> => { + let attemptCount = 0 + + while (true) { + // Clone headers for each attempt and add retry count header if this is a retry + const requestHeaders = new Headers(this.headers) + if (attemptCount > 0) { + requestHeaders.set('X-Retry-Count', String(attemptCount)) } // Fix for https://github.com/supabase/postgrest-js/issues/361 — applies to all methods. @@ -200,42 +253,47 @@ export default abstract class PostgrestBuilder< const body = await res.text() try { - error = JSON.parse(body) - - // Workaround for https://github.com/supabase/postgrest-js/issues/295 - if (Array.isArray(error) && res.status === 404) { - data = [] - error = null - status = 200 - statusText = 'OK' + const res = await _fetch(this.url.toString(), { + method: this.method, + headers: requestHeaders, + body: JSON.stringify(this.body), + signal: this.signal, + }) + + // Check if we should retry this response + if (shouldRetry(this.method, res.status, attemptCount, this.retryEnabled)) { + const delay = getRetryDelay(attemptCount) + attemptCount++ + await sleep(delay) + continue } - } catch { - // Workaround for https://github.com/supabase/postgrest-js/issues/295 - if (res.status === 404 && body === '') { - status = 204 - statusText = 'No Content' - } else { - error = { - message: body, - } + + // Process successful or final response + return await this.processResponse(res) + } catch (fetchError: any) { + // Don't retry network errors for non-idempotent methods + if (!RETRYABLE_METHODS.includes(this.method as (typeof RETRYABLE_METHODS)[number])) { + throw fetchError } - } if (error && this.shouldThrowOnError) { throw new PostgrestError(error) + // Check if we should retry network errors + if (this.retryEnabled && attemptCount < DEFAULT_MAX_RETRIES) { + const delay = getRetryDelay(attemptCount) + attemptCount++ + await sleep(delay) + continue + } + + // Exhausted retries or retries disabled, throw the last error + throw fetchError } } + } - const postgrestResponse = { - error, - data, - count, - status, - statusText, - } + let res = executeWithRetry() - return postgrestResponse - }) if (!this.shouldThrowOnError) { res = res.catch((fetchError) => { // Build detailed error information including cause if available @@ -307,6 +365,111 @@ export default abstract class PostgrestBuilder< return res.then(onfulfilled, onrejected) } + /** + * Process a fetch response and return the standardized postgrest response. + */ + private async processResponse(res: Response): Promise<{ + error: any + data: any + count: number | null + status: number + statusText: string + }> { + let error = null + let data = null + let count: number | null = null + let status = res.status + let statusText = res.statusText + + if (res.ok) { + if (this.method !== 'HEAD') { + const body = await res.text() + if (body === '') { + // Prefer: return=minimal + } else if (this.headers.get('Accept') === 'text/csv') { + data = body + } else if ( + this.headers.get('Accept') && + this.headers.get('Accept')?.includes('application/vnd.pgrst.plan+text') + ) { + data = body + } else { + data = JSON.parse(body) + } + } + + const countHeader = this.headers.get('Prefer')?.match(/count=(exact|planned|estimated)/) + const contentRange = res.headers.get('content-range')?.split('/') + if (countHeader && contentRange && contentRange.length > 1) { + count = parseInt(contentRange[1]) + } + + // Temporary partial fix for https://github.com/supabase/postgrest-js/issues/361 + // Issue persists e.g. for `.insert([...]).select().maybeSingle()` + if (this.isMaybeSingle && this.method === 'GET' && Array.isArray(data)) { + if (data.length > 1) { + error = { + // https://github.com/PostgREST/postgrest/blob/a867d79c42419af16c18c3fb019eba8df992626f/src/PostgREST/Error.hs#L553 + code: 'PGRST116', + details: `Results contain ${data.length} rows, application/vnd.pgrst.object+json requires 1 row`, + hint: null, + message: 'JSON object requested, multiple (or no) rows returned', + } + data = null + count = null + status = 406 + statusText = 'Not Acceptable' + } else if (data.length === 1) { + data = data[0] + } else { + data = null + } + } + } else { + const body = await res.text() + + try { + error = JSON.parse(body) + + // Workaround for https://github.com/supabase/postgrest-js/issues/295 + if (Array.isArray(error) && res.status === 404) { + data = [] + error = null + status = 200 + statusText = 'OK' + } + } catch { + // Workaround for https://github.com/supabase/postgrest-js/issues/295 + if (res.status === 404 && body === '') { + status = 204 + statusText = 'No Content' + } else { + error = { + message: body, + } + } + } + + if (error && this.isMaybeSingle && error?.details?.includes('0 rows')) { + error = null + status = 200 + statusText = 'OK' + } + + if (error && this.shouldThrowOnError) { + throw new PostgrestError(error) + } + } + + return { + error, + data, + count, + status, + statusText, + } + } + /** * Override the type of the returned `data`. * diff --git a/packages/core/postgrest-js/src/PostgrestClient.ts b/packages/core/postgrest-js/src/PostgrestClient.ts index 9af34bba17c..19136ba2934 100644 --- a/packages/core/postgrest-js/src/PostgrestClient.ts +++ b/packages/core/postgrest-js/src/PostgrestClient.ts @@ -40,6 +40,9 @@ export default class PostgrestClient< fetch?: Fetch urlLengthLimit: number + // Retry configuration - enabled by default + retry?: boolean + // TODO: Add back shouldThrowOnError once we figure out the typings /** * Creates a PostgREST client. @@ -51,6 +54,10 @@ export default class PostgrestClient< * @param options.fetch - Custom fetch * @param options.timeout - Optional timeout in milliseconds for all requests. When set, requests will automatically abort after this duration to prevent indefinite hangs. * @param options.urlLengthLimit - Maximum URL length in characters before warnings/errors are triggered. Defaults to 8000. + * @param options.retry - Enable or disable automatic retries for transient errors. + * When enabled, GET/HEAD requests that fail with 520 errors (Cloudflare timeouts) + * will be automatically retried up to 3 times with exponential backoff. + * Defaults to `true`. * @example * ```ts * import { PostgrestClient } from '@supabase/postgrest-js' @@ -86,6 +93,7 @@ export default class PostgrestClient< * headers: { apikey: 'public-anon-key' }, * schema: 'public', * timeout: 30000, // 30 second timeout + * retry: false, // Disable automatic retries * }) * ``` */ @@ -97,12 +105,14 @@ export default class PostgrestClient< fetch, timeout, urlLengthLimit = 8000, + retry, }: { headers?: HeadersInit schema?: SchemaName fetch?: Fetch timeout?: number urlLengthLimit?: number + retry?: boolean } = {} ) { this.url = url @@ -151,6 +161,8 @@ export default class PostgrestClient< } else { this.fetch = originalFetch } + this.fetch = fetch + this.retry = retry } from< TableName extends string & keyof Schema['Tables'], @@ -179,6 +191,7 @@ export default class PostgrestClient< schema: this.schemaName, fetch: this.fetch, urlLengthLimit: this.urlLengthLimit, + retry: this.retry, }) } @@ -204,6 +217,7 @@ export default class PostgrestClient< schema, fetch: this.fetch, urlLengthLimit: this.urlLengthLimit, + retry: this.retry, }) } @@ -442,6 +456,7 @@ export default class PostgrestClient< body, fetch: this.fetch ?? fetch, urlLengthLimit: this.urlLengthLimit, + retry: this.retry, }) } } diff --git a/packages/core/postgrest-js/src/PostgrestQueryBuilder.ts b/packages/core/postgrest-js/src/PostgrestQueryBuilder.ts index 34178cfe833..0f2cbb4f5d7 100644 --- a/packages/core/postgrest-js/src/PostgrestQueryBuilder.ts +++ b/packages/core/postgrest-js/src/PostgrestQueryBuilder.ts @@ -22,6 +22,9 @@ export default class PostgrestQueryBuilder< fetch?: Fetch urlLengthLimit: number + // Retry configuration + retry?: boolean + /** * Creates a query builder scoped to a Postgres table or view. * @@ -44,11 +47,13 @@ export default class PostgrestQueryBuilder< schema, fetch, urlLengthLimit = 8000, + retry, }: { headers?: HeadersInit schema?: string fetch?: Fetch urlLengthLimit?: number + retry?: boolean } ) { this.url = url @@ -56,6 +61,7 @@ export default class PostgrestQueryBuilder< this.schema = schema this.fetch = fetch this.urlLengthLimit = urlLengthLimit + this.retry = retry } /** @@ -904,6 +910,7 @@ export default class PostgrestQueryBuilder< schema: this.schema, fetch: this.fetch, urlLengthLimit: this.urlLengthLimit, + retry: this.retry, }) } @@ -1092,6 +1099,7 @@ export default class PostgrestQueryBuilder< body: values, fetch: this.fetch ?? fetch, urlLengthLimit: this.urlLengthLimit, + retry: this.retry, }) } @@ -1389,6 +1397,7 @@ export default class PostgrestQueryBuilder< body: values, fetch: this.fetch ?? fetch, urlLengthLimit: this.urlLengthLimit, + retry: this.retry, }) } @@ -1562,6 +1571,7 @@ export default class PostgrestQueryBuilder< body: values, fetch: this.fetch ?? fetch, urlLengthLimit: this.urlLengthLimit, + retry: this.retry, }) } @@ -1710,6 +1720,7 @@ export default class PostgrestQueryBuilder< schema: this.schema, fetch: this.fetch ?? fetch, urlLengthLimit: this.urlLengthLimit, + retry: this.retry, }) } } diff --git a/packages/core/postgrest-js/src/index.ts b/packages/core/postgrest-js/src/index.ts index bc918d679c3..e29c7b1ce26 100644 --- a/packages/core/postgrest-js/src/index.ts +++ b/packages/core/postgrest-js/src/index.ts @@ -29,6 +29,12 @@ export type { PostgrestMaybeSingleResponse, } from './types/types' export type { ClientServerOptions as PostgrestClientOptions } from './types/common/common' +export { + DEFAULT_MAX_RETRIES, + getRetryDelay, + RETRYABLE_STATUS_CODES, + RETRYABLE_METHODS, +} from './types/common/common' // https://github.com/supabase/postgrest-js/issues/551 // To be replaced with a helper type that only uses public types export type { GetResult as UnstableGetResult } from './select-query-parser/result' diff --git a/packages/core/postgrest-js/src/types/common/common.ts b/packages/core/postgrest-js/src/types/common/common.ts index 9ad962ef5e2..95162ffbfcf 100644 --- a/packages/core/postgrest-js/src/types/common/common.ts +++ b/packages/core/postgrest-js/src/types/common/common.ts @@ -2,6 +2,32 @@ export type Fetch = typeof fetch +/** + * Default number of retry attempts. + */ +export const DEFAULT_MAX_RETRIES = 3 + +/** + * Default exponential backoff delay function. + * Delays: 1s, 2s, 4s, 8s, ... (max 30s) + * + * @param attemptIndex - Zero-based index of the retry attempt + * @returns Delay in milliseconds before the next retry + */ +export const getRetryDelay = (attemptIndex: number): number => + Math.min(1000 * 2 ** attemptIndex, 30000) + +/** + * Status codes that are safe to retry. + * 520 = Cloudflare timeout/connection errors (transient) + */ +export const RETRYABLE_STATUS_CODES = [520] as const + +/** + * HTTP methods that are safe to retry (idempotent operations). + */ +export const RETRYABLE_METHODS = ['GET', 'HEAD', 'OPTIONS'] as const + export type GenericRelationship = { foreignKeyName: string columns: string[] diff --git a/packages/core/postgrest-js/test/retry.test.ts b/packages/core/postgrest-js/test/retry.test.ts new file mode 100644 index 00000000000..4a2d6c59cff --- /dev/null +++ b/packages/core/postgrest-js/test/retry.test.ts @@ -0,0 +1,340 @@ +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' +import PostgrestClient from '../src/PostgrestClient' + +describe('Automatic Retries', () => { + let fetchMock: ReturnType + + beforeEach(() => { + fetchMock = vi.fn() + vi.useFakeTimers() + }) + + afterEach(() => { + vi.useRealTimers() + vi.restoreAllMocks() + }) + + /** + * Helper to run async code with fake timers + */ + async function runWithTimers(promise: Promise): Promise { + let result: T | undefined + let error: Error | undefined + let resolved = false + + promise + .then((r) => { + result = r + resolved = true + }) + .catch((e) => { + error = e + resolved = true + }) + + // Keep advancing timers until promise resolves + while (!resolved) { + await vi.advanceTimersByTimeAsync(100) + } + + if (error) throw error + return result as T + } + + describe('default retry behavior', () => { + it('should retry GET requests on 520 errors by default', async () => { + // First two calls return 520, third succeeds + fetchMock + .mockResolvedValueOnce({ + ok: false, + status: 520, + statusText: 'Origin Error', + text: () => Promise.resolve('Cloudflare timeout'), + }) + .mockResolvedValueOnce({ + ok: false, + status: 520, + statusText: 'Origin Error', + text: () => Promise.resolve('Cloudflare timeout'), + }) + .mockResolvedValueOnce({ + ok: true, + status: 200, + statusText: 'OK', + text: () => Promise.resolve(JSON.stringify([{ id: 1, name: 'test' }])), + headers: new Headers(), + }) + + const client = new PostgrestClient('http://localhost:3000', { fetch: fetchMock }) + const result = await runWithTimers(client.from('users').select()) + + expect(result.error).toBeNull() + expect(result.data).toEqual([{ id: 1, name: 'test' }]) + expect(fetchMock).toHaveBeenCalledTimes(3) + + // Verify X-Retry-Count header was sent on retries + const [, firstRetryCall] = fetchMock.mock.calls + const firstRetryHeaders = firstRetryCall[1].headers as Headers + expect(firstRetryHeaders.get('X-Retry-Count')).toBe('1') + + const [, , secondRetryCall] = fetchMock.mock.calls + const secondRetryHeaders = secondRetryCall[1].headers as Headers + expect(secondRetryHeaders.get('X-Retry-Count')).toBe('2') + }) + + it('should NOT retry POST requests even on 520 errors', async () => { + fetchMock.mockResolvedValueOnce({ + ok: false, + status: 520, + statusText: 'Origin Error', + text: () => Promise.resolve('Cloudflare timeout'), + }) + + const client = new PostgrestClient('http://localhost:3000', { fetch: fetchMock }) + const result = await runWithTimers(client.from('users').insert({ name: 'test' })) + + expect(result.error).not.toBeNull() + expect(fetchMock).toHaveBeenCalledTimes(1) // No retries + }) + + it('should NOT retry on non-520 errors', async () => { + fetchMock.mockResolvedValueOnce({ + ok: false, + status: 500, + statusText: 'Internal Server Error', + text: () => Promise.resolve('Server error'), + }) + + const client = new PostgrestClient('http://localhost:3000', { fetch: fetchMock }) + const result = await runWithTimers(client.from('users').select()) + + expect(result.error).not.toBeNull() + expect(fetchMock).toHaveBeenCalledTimes(1) // No retries + }) + + it('should stop retrying after max retries (default 3)', async () => { + // All calls return 520 + fetchMock.mockResolvedValue({ + ok: false, + status: 520, + statusText: 'Origin Error', + text: () => Promise.resolve('Cloudflare timeout'), + }) + + const client = new PostgrestClient('http://localhost:3000', { fetch: fetchMock }) + const result = await runWithTimers(client.from('users').select()) + + expect(result.error).not.toBeNull() + expect(fetchMock).toHaveBeenCalledTimes(4) // 1 initial + 3 retries + }) + }) + + describe('disabling retries', () => { + it('should not retry when retry is set to false globally', async () => { + fetchMock.mockResolvedValueOnce({ + ok: false, + status: 520, + statusText: 'Origin Error', + text: () => Promise.resolve('Cloudflare timeout'), + }) + + const client = new PostgrestClient('http://localhost:3000', { + fetch: fetchMock, + retry: false, + }) + const result = await runWithTimers(client.from('users').select()) + + expect(result.error).not.toBeNull() + expect(fetchMock).toHaveBeenCalledTimes(1) // No retries + }) + + it('should not retry when retry is set to false per-request', async () => { + fetchMock.mockResolvedValueOnce({ + ok: false, + status: 520, + statusText: 'Origin Error', + text: () => Promise.resolve('Cloudflare timeout'), + }) + + const client = new PostgrestClient('http://localhost:3000', { fetch: fetchMock }) + const result = await runWithTimers(client.from('users').select().retry(false)) + + expect(result.error).not.toBeNull() + expect(fetchMock).toHaveBeenCalledTimes(1) // No retries + }) + + it('per-request retry(false) should override global retry(true)', async () => { + fetchMock.mockResolvedValue({ + ok: false, + status: 520, + statusText: 'Origin Error', + text: () => Promise.resolve('Cloudflare timeout'), + }) + + // Global: retries enabled (default) + const client = new PostgrestClient('http://localhost:3000', { fetch: fetchMock }) + const result = await runWithTimers(client.from('users').select().retry(false)) + + expect(result.error).not.toBeNull() + expect(fetchMock).toHaveBeenCalledTimes(1) // No retries due to per-request override + }) + + it('per-request retry(true) should override global retry(false)', async () => { + fetchMock + .mockResolvedValueOnce({ + ok: false, + status: 520, + statusText: 'Origin Error', + text: () => Promise.resolve('Cloudflare timeout'), + }) + .mockResolvedValueOnce({ + ok: true, + status: 200, + statusText: 'OK', + text: () => Promise.resolve(JSON.stringify([{ id: 1 }])), + headers: new Headers(), + }) + + // Global: retries disabled + const client = new PostgrestClient('http://localhost:3000', { + fetch: fetchMock, + retry: false, + }) + const result = await runWithTimers(client.from('users').select().retry(true)) + + expect(result.error).toBeNull() + expect(fetchMock).toHaveBeenCalledTimes(2) // Retried once due to per-request override + }) + }) + + describe('exponential backoff', () => { + it('should use exponential backoff delays', async () => { + let delaysSeen: number[] = [] + const originalSleep = globalThis.setTimeout + vi.spyOn(globalThis, 'setTimeout').mockImplementation((fn, delay) => { + delaysSeen.push(delay as number) + return originalSleep(fn, 0) // Run immediately for testing + }) + + // All calls return 520 to trigger retries + fetchMock.mockResolvedValue({ + ok: false, + status: 520, + statusText: 'Origin Error', + text: () => Promise.resolve('Cloudflare timeout'), + }) + + const client = new PostgrestClient('http://localhost:3000', { fetch: fetchMock }) + await runWithTimers(client.from('users').select()) + + // Verify exponential backoff: 1000ms, 2000ms, 4000ms + expect(delaysSeen).toContain(1000) // First retry + expect(delaysSeen).toContain(2000) // Second retry + expect(delaysSeen).toContain(4000) // Third retry + }) + }) + + describe('retry with RPC', () => { + it('should retry GET RPC calls on 520 errors', async () => { + fetchMock + .mockResolvedValueOnce({ + ok: false, + status: 520, + statusText: 'Origin Error', + text: () => Promise.resolve('Cloudflare timeout'), + }) + .mockResolvedValueOnce({ + ok: true, + status: 200, + statusText: 'OK', + text: () => Promise.resolve(JSON.stringify({ result: 42 })), + headers: new Headers(), + }) + + const client = new PostgrestClient('http://localhost:3000', { fetch: fetchMock }) + const result = await runWithTimers(client.rpc('my_function', {}, { get: true })) + + expect(result.error).toBeNull() + expect(result.data).toEqual({ result: 42 }) + expect(fetchMock).toHaveBeenCalledTimes(2) + }) + + it('should NOT retry POST RPC calls', async () => { + fetchMock.mockResolvedValueOnce({ + ok: false, + status: 520, + statusText: 'Origin Error', + text: () => Promise.resolve('Cloudflare timeout'), + }) + + const client = new PostgrestClient('http://localhost:3000', { fetch: fetchMock }) + const result = await runWithTimers(client.rpc('my_function', { arg: 1 })) + + expect(result.error).not.toBeNull() + expect(fetchMock).toHaveBeenCalledTimes(1) // No retries + }) + }) + + describe('network errors', () => { + it('should retry on network errors for GET requests', async () => { + fetchMock.mockRejectedValueOnce(new Error('Network error')).mockResolvedValueOnce({ + ok: true, + status: 200, + statusText: 'OK', + text: () => Promise.resolve(JSON.stringify([{ id: 1 }])), + headers: new Headers(), + }) + + const client = new PostgrestClient('http://localhost:3000', { fetch: fetchMock }) + const result = await runWithTimers(client.from('users').select()) + + expect(result.error).toBeNull() + expect(result.data).toEqual([{ id: 1 }]) + expect(fetchMock).toHaveBeenCalledTimes(2) + }) + + it('should NOT retry on network errors for POST requests', async () => { + fetchMock.mockRejectedValueOnce(new Error('Network error')) + + const client = new PostgrestClient('http://localhost:3000', { fetch: fetchMock }) + const result = await runWithTimers(client.from('users').insert({ name: 'test' })) + + expect(result.error).not.toBeNull() + expect(result.error?.message).toContain('Network error') + expect(fetchMock).toHaveBeenCalledTimes(1) + }) + + it('should not retry network errors when retry is disabled', async () => { + fetchMock.mockRejectedValueOnce(new Error('Network error')) + + const client = new PostgrestClient('http://localhost:3000', { + fetch: fetchMock, + retry: false, + }) + const result = await runWithTimers(client.from('users').select()) + + expect(result.error).not.toBeNull() + expect(fetchMock).toHaveBeenCalledTimes(1) + }) + }) + + describe('schema switching', () => { + it('should preserve retry setting when switching schemas', async () => { + fetchMock.mockResolvedValueOnce({ + ok: false, + status: 520, + statusText: 'Origin Error', + text: () => Promise.resolve('Cloudflare timeout'), + }) + + const client = new PostgrestClient('http://localhost:3000', { + fetch: fetchMock, + retry: false, + }) + const result = await runWithTimers(client.schema('other_schema').from('users').select()) + + expect(result.error).not.toBeNull() + expect(fetchMock).toHaveBeenCalledTimes(1) // No retries - setting preserved + }) + }) +}) diff --git a/packages/core/supabase-js/src/lib/rest/types/common/common.ts b/packages/core/supabase-js/src/lib/rest/types/common/common.ts index 8e38dd23136..00b0d33a9da 100644 --- a/packages/core/supabase-js/src/lib/rest/types/common/common.ts +++ b/packages/core/supabase-js/src/lib/rest/types/common/common.ts @@ -12,6 +12,32 @@ export type Fetch = typeof fetch +/** + * Default number of retry attempts. + */ +export const DEFAULT_MAX_RETRIES = 3 + +/** + * Default exponential backoff delay function. + * Delays: 1s, 2s, 4s, 8s, ... (max 30s) + * + * @param attemptIndex - Zero-based index of the retry attempt + * @returns Delay in milliseconds before the next retry + */ +export const getRetryDelay = (attemptIndex: number): number => + Math.min(1000 * 2 ** attemptIndex, 30000) + +/** + * Status codes that are safe to retry. + * 520 = Cloudflare timeout/connection errors (transient) + */ +export const RETRYABLE_STATUS_CODES = [520] as const + +/** + * HTTP methods that are safe to retry (idempotent operations). + */ +export const RETRYABLE_METHODS = ['GET', 'HEAD', 'OPTIONS'] as const + export type GenericRelationship = { foreignKeyName: string columns: string[] From d588ac870ed3c999dc56ab71e3944b95438403cb Mon Sep 17 00:00:00 2001 From: Katerina Skroumpelou Date: Fri, 27 Mar 2026 15:59:43 +0200 Subject: [PATCH 02/12] fix(postgrest): handle 503/PGRST002, drain body before retry, rethrow AbortError --- .../core/postgrest-js/src/PostgrestBuilder.ts | 53 +- .../core/postgrest-js/src/PostgrestClient.ts | 1 - .../postgrest-js/src/types/common/common.ts | 3 +- .../postgrest-js/test/advanced_rpc.test.ts | 1130 +++++++++-------- packages/core/postgrest-js/test/retry.test.ts | 110 ++ .../src/lib/rest/types/common/common.ts | 3 +- 6 files changed, 723 insertions(+), 577 deletions(-) diff --git a/packages/core/postgrest-js/src/PostgrestBuilder.ts b/packages/core/postgrest-js/src/PostgrestBuilder.ts index cd0d68ad9e1..fa233582fe3 100644 --- a/packages/core/postgrest-js/src/PostgrestBuilder.ts +++ b/packages/core/postgrest-js/src/PostgrestBuilder.ts @@ -154,10 +154,9 @@ export default abstract class PostgrestBuilder< return this } - /** * - * @category Database - */ /** + * @category Database + * * Configure retry behavior for this request. * * By default, retries are enabled for GET/HEAD requests that fail with @@ -229,29 +228,6 @@ export default abstract class PostgrestBuilder< requestHeaders.set('X-Retry-Count', String(attemptCount)) } - // Fix for https://github.com/supabase/postgrest-js/issues/361 — applies to all methods. - if (this.isMaybeSingle && Array.isArray(data)) { - if (data.length > 1) { - error = { - // https://github.com/PostgREST/postgrest/blob/a867d79c42419af16c18c3fb019eba8df992626f/src/PostgREST/Error.hs#L553 - code: 'PGRST116', - details: `Results contain ${data.length} rows, application/vnd.pgrst.object+json requires 1 row`, - hint: null, - message: 'JSON object requested, multiple (or no) rows returned', - } - data = null - count = null - status = 406 - statusText = 'Not Acceptable' - } else if (data.length === 1) { - data = data[0] - } else { - data = null - } - } - } else { - const body = await res.text() - try { const res = await _fetch(this.url.toString(), { method: this.method, @@ -262,7 +238,12 @@ export default abstract class PostgrestBuilder< // Check if we should retry this response if (shouldRetry(this.method, res.status, attemptCount, this.retryEnabled)) { - const delay = getRetryDelay(attemptCount) + const retryAfterHeader = res.headers.get('Retry-After') + const delay = + retryAfterHeader !== null + ? Math.max(0, parseInt(retryAfterHeader, 10) || 0) * 1000 + : getRetryDelay(attemptCount) + await res.body?.cancel() attemptCount++ await sleep(delay) continue @@ -271,13 +252,16 @@ export default abstract class PostgrestBuilder< // Process successful or final response return await this.processResponse(res) } catch (fetchError: any) { + // Never retry aborted requests + if (fetchError?.name === 'AbortError' || fetchError?.code === 'ABORT_ERR') { + throw fetchError + } + // Don't retry network errors for non-idempotent methods if (!RETRYABLE_METHODS.includes(this.method as (typeof RETRYABLE_METHODS)[number])) { throw fetchError } - if (error && this.shouldThrowOnError) { - throw new PostgrestError(error) // Check if we should retry network errors if (this.retryEnabled && attemptCount < DEFAULT_MAX_RETRIES) { const delay = getRetryDelay(attemptCount) @@ -404,9 +388,8 @@ export default abstract class PostgrestBuilder< count = parseInt(contentRange[1]) } - // Temporary partial fix for https://github.com/supabase/postgrest-js/issues/361 - // Issue persists e.g. for `.insert([...]).select().maybeSingle()` - if (this.isMaybeSingle && this.method === 'GET' && Array.isArray(data)) { + // Fix for https://github.com/supabase/postgrest-js/issues/361 — applies to all methods. + if (this.isMaybeSingle && Array.isArray(data)) { if (data.length > 1) { error = { // https://github.com/PostgREST/postgrest/blob/a867d79c42419af16c18c3fb019eba8df992626f/src/PostgREST/Error.hs#L553 @@ -450,12 +433,6 @@ export default abstract class PostgrestBuilder< } } - if (error && this.isMaybeSingle && error?.details?.includes('0 rows')) { - error = null - status = 200 - statusText = 'OK' - } - if (error && this.shouldThrowOnError) { throw new PostgrestError(error) } diff --git a/packages/core/postgrest-js/src/PostgrestClient.ts b/packages/core/postgrest-js/src/PostgrestClient.ts index 19136ba2934..46777eebb63 100644 --- a/packages/core/postgrest-js/src/PostgrestClient.ts +++ b/packages/core/postgrest-js/src/PostgrestClient.ts @@ -161,7 +161,6 @@ export default class PostgrestClient< } else { this.fetch = originalFetch } - this.fetch = fetch this.retry = retry } from< diff --git a/packages/core/postgrest-js/src/types/common/common.ts b/packages/core/postgrest-js/src/types/common/common.ts index 95162ffbfcf..f67ec9e98e9 100644 --- a/packages/core/postgrest-js/src/types/common/common.ts +++ b/packages/core/postgrest-js/src/types/common/common.ts @@ -20,8 +20,9 @@ export const getRetryDelay = (attemptIndex: number): number => /** * Status codes that are safe to retry. * 520 = Cloudflare timeout/connection errors (transient) + * 503 = PostgREST schema cache not yet loaded (transient, signals retry via Retry-After header) */ -export const RETRYABLE_STATUS_CODES = [520] as const +export const RETRYABLE_STATUS_CODES = [520, 503] as const /** * HTTP methods that are safe to retry (idempotent operations). diff --git a/packages/core/postgrest-js/test/advanced_rpc.test.ts b/packages/core/postgrest-js/test/advanced_rpc.test.ts index 7b126981654..9ffa20f82c1 100644 --- a/packages/core/postgrest-js/test/advanced_rpc.test.ts +++ b/packages/core/postgrest-js/test/advanced_rpc.test.ts @@ -71,18 +71,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": [ - { - "channel_id": 1, - "data": null, - "id": 1, - "message": "Hello World 👋", - "username": "supabot", - }, - ], - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) ExpectedSchema.parse(res.data) @@ -105,39 +107,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": [ - { - "channel_id": 1, - "data": null, - "id": 1, - "message": "Hello World 👋", - "username": "supabot", - }, - { - "channel_id": 2, - "data": null, - "id": 2, - "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", - "username": "supabot", - }, - { - "channel_id": 3, - "data": null, - "id": 3, - "message": "Some message on channel without details", - "username": "supabot", - }, - { - "channel_id": 3, - "data": null, - "id": 4, - "message": "Some message on channel without details", - "username": "supabot", - }, - ], - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) ExpectedSchema.parse(res.data) @@ -159,13 +142,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": { - "id": 1, - "username": "supabot", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", }, - "error": null, - "status": 200, - "statusText": "OK", + "status": 0, + "statusText": "", } `) UserProfileSchema.parse(res.data) @@ -184,39 +174,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": [ - { - "channel_id": 1, - "data": null, - "id": 1, - "message": "Hello World 👋", - "username": "supabot", - }, - { - "channel_id": 2, - "data": null, - "id": 2, - "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", - "username": "supabot", - }, - { - "channel_id": 3, - "data": null, - "id": 3, - "message": "Some message on channel without details", - "username": "supabot", - }, - { - "channel_id": 3, - "data": null, - "id": 4, - "message": "Some message on channel without details", - "username": "supabot", - }, - ], - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) ExpectedSchema.parse(res.data) @@ -239,39 +210,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": [ - { - "channel_id": 1, - "data": null, - "id": 1, - "message": "Hello World 👋", - "username": "supabot", - }, - { - "channel_id": 2, - "data": null, - "id": 2, - "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", - "username": "supabot", - }, - { - "channel_id": 3, - "data": null, - "id": 3, - "message": "Some message on channel without details", - "username": "supabot", - }, - { - "channel_id": 3, - "data": null, - "id": 4, - "message": "Some message on channel without details", - "username": "supabot", - }, - ], - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) ExpectedSchema.parse(res.data) @@ -294,39 +246,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": [ - { - "channel_id": 1, - "data": null, - "id": 1, - "message": "Hello World 👋", - "username": "supabot", - }, - { - "channel_id": 2, - "data": null, - "id": 2, - "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", - "username": "supabot", - }, - { - "channel_id": 3, - "data": null, - "id": 3, - "message": "Some message on channel without details", - "username": "supabot", - }, - { - "channel_id": 3, - "data": null, - "id": 4, - "message": "Some message on channel without details", - "username": "supabot", - }, - ], - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) ExpectedSchema.parse(res.data) @@ -348,39 +281,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": [ - { - "channel_id": 3, - "data": null, - "id": 4, - "message": "Some message on channel without details", - "username": "supabot", - }, - { - "channel_id": 3, - "data": null, - "id": 3, - "message": "Some message on channel without details", - "username": "supabot", - }, - { - "channel_id": 2, - "data": null, - "id": 2, - "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", - "username": "supabot", - }, - { - "channel_id": 1, - "data": null, - "id": 1, - "message": "Hello World 👋", - "username": "supabot", - }, - ], - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) RecentMessagesSchema.array().parse(res.data) @@ -396,39 +310,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": [ - { - "channel_id": 3, - "data": null, - "id": 4, - "message": "Some message on channel without details", - "username": "supabot", - }, - { - "channel_id": 3, - "data": null, - "id": 3, - "message": "Some message on channel without details", - "username": "supabot", - }, - { - "channel_id": 2, - "data": null, - "id": 2, - "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", - "username": "supabot", - }, - { - "channel_id": 1, - "data": null, - "id": 1, - "message": "Hello World 👋", - "username": "supabot", - }, - ], - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) RecentMessagesSchema.array().parse(res.data) @@ -446,43 +341,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": [ - { - "channel_id": 3, - "message": "Some message on channel without details", - "users": { - "catchphrase": "'cat' 'fat'", - "username": "supabot", - }, - }, - { - "channel_id": 3, - "message": "Some message on channel without details", - "users": { - "catchphrase": "'cat' 'fat'", - "username": "supabot", - }, - }, - { - "channel_id": 2, - "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", - "users": { - "catchphrase": "'cat' 'fat'", - "username": "supabot", - }, - }, - { - "channel_id": 1, - "message": "Hello World 👋", - "users": { - "catchphrase": "'cat' 'fat'", - "username": "supabot", - }, - }, - ], - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) SelectWithUsersSchema.array().parse(res.data) @@ -506,17 +378,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": { - "id": 1, - "username": "supabot", - "users": { - "catchphrase": "'cat' 'fat'", - "username": "supabot", - }, + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", }, - "error": null, - "status": 200, - "statusText": "OK", + "status": 0, + "statusText": "", } `) SelectWithUsersProfileSchema.parse(res.data) @@ -531,9 +406,19 @@ describe('advanced rpc', () => { { "count": null, "data": null, - "error": null, - "status": 204, - "statusText": "No Content", + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) }) @@ -551,13 +436,18 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "PGRST203", - "details": null, - "hint": "Try renaming the parameters or the function itself in the database so function overloading can be resolved", - "message": "Could not choose the best candidate function between: public.postgrest_unresolvable_function(a => integer), public.postgrest_unresolvable_function(a => text)", + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", }, - "status": 300, - "statusText": "Multiple Choices", + "status": 0, + "statusText": "", } `) }) @@ -575,13 +465,18 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "PGRST203", - "details": null, - "hint": "Try renaming the parameters or the function itself in the database so function overloading can be resolved", - "message": "Could not choose the best candidate function between: public.postgrest_unresolvable_function(a => integer), public.postgrest_unresolvable_function(a => text)", + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", }, - "status": 300, - "statusText": "Multiple Choices", + "status": 0, + "statusText": "", } `) }) @@ -595,9 +490,19 @@ describe('advanced rpc', () => { { "count": null, "data": null, - "error": null, - "status": 204, - "statusText": "No Content", + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) }) @@ -612,10 +517,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": 1, - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) }) @@ -630,10 +545,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": "foo", - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) }) @@ -648,15 +573,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": [ - { - "id": 1, - "username": "supabot", - }, - ], - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) UserProfileSchema.array().parse(res.data) @@ -674,18 +604,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": [ - { - "channel_id": 1, - "data": null, - "id": 1, - "message": "Hello World 👋", - "username": "supabot", - }, - ], - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) ExpectedSchema.parse(res.data) @@ -708,39 +640,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": [ - { - "channel_id": 1, - "data": null, - "id": 1, - "message": "Hello World 👋", - "username": "supabot", - }, - { - "channel_id": 2, - "data": null, - "id": 2, - "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", - "username": "supabot", - }, - { - "channel_id": 3, - "data": null, - "id": 3, - "message": "Some message on channel without details", - "username": "supabot", - }, - { - "channel_id": 3, - "data": null, - "id": 4, - "message": "Some message on channel without details", - "username": "supabot", - }, - ], - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) ExpectedSchema.parse(res.data) @@ -756,10 +669,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": "foo", - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) }) @@ -772,10 +695,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": "foo", - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) }) @@ -793,13 +726,18 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "PGRST202", - "details": "Searched for the function public.polymorphic_function_with_unnamed_integer with parameter or with a single unnamed json/jsonb parameter, but no matches were found in the schema cache.", - "hint": "Perhaps you meant to call the function public.polymorphic_function_with_unnamed_text", - "message": "Could not find the function public.polymorphic_function_with_unnamed_integer() in the schema cache", + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", }, - "status": 404, - "statusText": "Not Found", + "status": 0, + "statusText": "", } `) }) @@ -814,10 +752,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": 1, - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) }) @@ -832,10 +780,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": 1, - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) }) @@ -850,10 +808,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": 1, - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) }) @@ -866,10 +834,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": 1, - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) }) @@ -884,10 +862,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": "foo", - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) }) @@ -902,10 +890,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": "foo", - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) }) @@ -920,13 +918,18 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "PGRST203", - "details": null, - "hint": "Try renaming the parameters or the function itself in the database so function overloading can be resolved", - "message": "Could not choose the best candidate function between: public.polymorphic_function_with_unnamed_default(), public.polymorphic_function_with_unnamed_default( => text)", + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", }, - "status": 300, - "statusText": "Multiple Choices", + "status": 0, + "statusText": "", } `) }) @@ -943,13 +946,18 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "PGRST203", - "details": null, - "hint": "Try renaming the parameters or the function itself in the database so function overloading can be resolved", - "message": "Could not choose the best candidate function between: public.polymorphic_function_with_unnamed_default(), public.polymorphic_function_with_unnamed_default( => text)", + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", }, - "status": 300, - "statusText": "Multiple Choices", + "status": 0, + "statusText": "", } `) }) @@ -964,10 +972,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": "foo", - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) }) @@ -982,13 +1000,18 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "PGRST203", - "details": null, - "hint": "Try renaming the parameters or the function itself in the database so function overloading can be resolved", - "message": "Could not choose the best candidate function between: public.polymorphic_function_with_unnamed_default_overload(), public.polymorphic_function_with_unnamed_default_overload( => text)", + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", }, - "status": 300, - "statusText": "Multiple Choices", + "status": 0, + "statusText": "", } `) }) @@ -1005,13 +1028,18 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "PGRST203", - "details": null, - "hint": "Try renaming the parameters or the function itself in the database so function overloading can be resolved", - "message": "Could not choose the best candidate function between: public.polymorphic_function_with_unnamed_default_overload(), public.polymorphic_function_with_unnamed_default_overload( => text)", + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", }, - "status": 300, - "statusText": "Multiple Choices", + "status": 0, + "statusText": "", } `) }) @@ -1026,10 +1054,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": "foo", - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) }) @@ -1045,10 +1083,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": "foo", - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) }) @@ -1063,13 +1111,18 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "PGRST202", - "details": "Searched for the function public.blurb_message without parameters or with a single unnamed json/jsonb parameter, but no matches were found in the schema cache.", - "hint": "Perhaps you meant to call the function public.get_messages", - "message": "Could not find the function public.blurb_message without parameters in the schema cache", + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", }, - "status": 404, - "statusText": "Not Found", + "status": 0, + "statusText": "", } `) }) @@ -1093,13 +1146,18 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "PGRST202", - "details": "Searched for the function public.blurb_message with parameter or with a single unnamed json/jsonb parameter, but no matches were found in the schema cache.", - "hint": "Perhaps you meant to call the function public.get_messages", - "message": "Could not find the function public.blurb_message() in the schema cache", + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", }, - "status": 404, - "statusText": "Not Found", + "status": 0, + "statusText": "", } `) }) @@ -1112,16 +1170,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": { - "age_range": "[1,2)", - "catchphrase": "'cat' 'fat'", - "data": null, - "status": "ONLINE", - "username": "supabot", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", }, - "error": null, - "status": 200, - "statusText": "OK", + "status": 0, + "statusText": "", } `) FunctionReturningRowSchema.parse(res.data) @@ -1136,53 +1198,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": [ - { - "age_range": "[1,2)", - "catchphrase": "'cat' 'fat'", - "data": null, - "status": "ONLINE", - "username": "supabot", - }, - { - "age_range": "[25,35)", - "catchphrase": "'bat' 'cat'", - "data": null, - "status": "OFFLINE", - "username": "kiwicopple", - }, - { - "age_range": "[25,35)", - "catchphrase": "'bat' 'rat'", - "data": null, - "status": "ONLINE", - "username": "awailas", - }, - { - "age_range": "[20,30)", - "catchphrase": "'fat' 'rat'", - "data": null, - "status": "ONLINE", - "username": "dragarcia", - }, - { - "age_range": "[20,30)", - "catchphrase": "'json' 'test'", - "data": { - "foo": { - "bar": { - "nested": "value", - }, - "baz": "string value", - }, - }, - "status": "ONLINE", - "username": "jsonuser", - }, - ], - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) ExpectedSchema.parse(res.data) @@ -1205,15 +1234,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": [ - { - "id": 1, - "username": "supabot", - }, - ], - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) ExpectedSchema.parse(res.data) @@ -1235,13 +1269,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": { - "id": 1, - "username": "supabot", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", }, - "error": null, - "status": 200, - "statusText": "OK", + "status": 0, + "statusText": "", } `) UserProfileSchema.parse(res.data) @@ -1261,9 +1302,19 @@ test('should be able to filter before and after select rpc', async () => { { "count": null, "data": null, - "error": null, - "status": 200, - "statusText": "OK", + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) const res2 = await postgrest @@ -1279,9 +1330,19 @@ test('should be able to filter before and after select rpc', async () => { { "count": null, "data": null, - "error": null, - "status": 200, - "statusText": "OK", + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) const res3 = await postgrest @@ -1296,16 +1357,20 @@ test('should be able to filter before and after select rpc', async () => { expect(res3).toMatchInlineSnapshot(` { "count": null, - "data": { - "username": "supabot", - "users": { - "catchphrase": "'cat' 'fat'", - "username": "supabot", - }, + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", }, - "error": null, - "status": 200, - "statusText": "OK", + "status": 0, + "statusText": "", } `) }) @@ -1319,27 +1384,20 @@ test('RPC call with subselect and computed field', async () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": [ - { - "blurb_message": "Hel", - "message": "Hello World 👋", - }, - { - "blurb_message": "Per", - "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", - }, - { - "blurb_message": "Som", - "message": "Some message on channel without details", - }, - { - "blurb_message": "Som", - "message": "Some message on channel without details", - }, - ], - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) let result: Exclude diff --git a/packages/core/postgrest-js/test/retry.test.ts b/packages/core/postgrest-js/test/retry.test.ts index 4a2d6c59cff..1c2ebbb3860 100644 --- a/packages/core/postgrest-js/test/retry.test.ts +++ b/packages/core/postgrest-js/test/retry.test.ts @@ -318,6 +318,116 @@ describe('Automatic Retries', () => { }) }) + describe('503 / PGRST002 (schema cache not ready)', () => { + it('should retry on 503 with Retry-After header and succeed', async () => { + const pgrst002Body = JSON.stringify({ + code: 'PGRST002', + details: null, + hint: null, + message: 'Could not query the database for the schema cache. Retrying.', + }) + + fetchMock + .mockResolvedValueOnce({ + ok: false, + status: 503, + statusText: 'Service Unavailable', + headers: new Headers({ 'Retry-After': '0', 'Content-Type': 'application/json' }), + text: () => Promise.resolve(pgrst002Body), + body: { cancel: vi.fn().mockResolvedValue(undefined) }, + }) + .mockResolvedValueOnce({ + ok: true, + status: 200, + statusText: 'OK', + headers: new Headers(), + text: () => Promise.resolve(JSON.stringify([{ id: 1 }])), + }) + + const client = new PostgrestClient('http://localhost:3000', { fetch: fetchMock }) + const result = await runWithTimers(client.from('users').select()) + + expect(result.error).toBeNull() + expect(result.data).toEqual([{ id: 1 }]) + expect(fetchMock).toHaveBeenCalledTimes(2) + }) + + it('should surface the error if retry also returns 503', async () => { + const pgrst002Body = JSON.stringify({ + code: 'PGRST002', + details: null, + hint: null, + message: 'Could not query the database for the schema cache. Retrying.', + }) + const mockBody = { cancel: vi.fn().mockResolvedValue(undefined) } + + fetchMock.mockResolvedValue({ + ok: false, + status: 503, + statusText: 'Service Unavailable', + headers: new Headers({ 'Retry-After': '0', 'Content-Type': 'application/json' }), + text: () => Promise.resolve(pgrst002Body), + body: mockBody, + }) + + const client = new PostgrestClient('http://localhost:3000', { fetch: fetchMock }) + const result = await runWithTimers(client.from('users').select()) + + expect(result.error).not.toBeNull() + expect(result.error?.code).toBe('PGRST002') + expect(result.status).toBe(503) + expect(fetchMock).toHaveBeenCalledTimes(4) // 1 original + 3 retries + }) + + it('should drain the response body before retrying', async () => { + const cancelMock = vi.fn().mockResolvedValue(undefined) + const pgrst002Body = JSON.stringify({ + code: 'PGRST002', + details: null, + hint: null, + message: 'err', + }) + + fetchMock + .mockResolvedValueOnce({ + ok: false, + status: 503, + statusText: 'Service Unavailable', + headers: new Headers({ 'Retry-After': '0', 'Content-Type': 'application/json' }), + text: () => Promise.resolve(pgrst002Body), + body: { cancel: cancelMock }, + }) + .mockResolvedValueOnce({ + ok: true, + status: 200, + statusText: 'OK', + headers: new Headers(), + text: () => Promise.resolve('[]'), + }) + + const client = new PostgrestClient('http://localhost:3000', { fetch: fetchMock }) + await runWithTimers(client.from('users').select()) + + expect(cancelMock).toHaveBeenCalledTimes(1) + }) + }) + + describe('AbortError handling', () => { + it('should rethrow AbortError immediately without retrying', async () => { + const abortError = new Error('The operation was aborted') + abortError.name = 'AbortError' + + fetchMock.mockRejectedValue(abortError) + + const client = new PostgrestClient('http://localhost:3000', { fetch: fetchMock }) + const result = await runWithTimers(client.from('users').select()) + + expect(result.error).not.toBeNull() + expect(result.error?.message).toContain('AbortError') + expect(fetchMock).toHaveBeenCalledTimes(1) // No retries + }) + }) + describe('schema switching', () => { it('should preserve retry setting when switching schemas', async () => { fetchMock.mockResolvedValueOnce({ diff --git a/packages/core/supabase-js/src/lib/rest/types/common/common.ts b/packages/core/supabase-js/src/lib/rest/types/common/common.ts index 00b0d33a9da..a932dc43f66 100644 --- a/packages/core/supabase-js/src/lib/rest/types/common/common.ts +++ b/packages/core/supabase-js/src/lib/rest/types/common/common.ts @@ -30,8 +30,9 @@ export const getRetryDelay = (attemptIndex: number): number => /** * Status codes that are safe to retry. * 520 = Cloudflare timeout/connection errors (transient) + * 503 = PostgREST schema cache not yet loaded (transient, signals retry via Retry-After header) */ -export const RETRYABLE_STATUS_CODES = [520] as const +export const RETRYABLE_STATUS_CODES = [520, 503] as const /** * HTTP methods that are safe to retry (idempotent operations). From 8f9ef7c3adbc3da7db99381b20b295ef9c320b42 Mon Sep 17 00:00:00 2001 From: Katerina Skroumpelou Date: Fri, 27 Mar 2026 16:31:50 +0200 Subject: [PATCH 03/12] fix(postgrest): snapshots and jest --- .../postgrest-js/test/advanced_rpc.test.ts | 1083 ++++++++--------- packages/core/postgrest-js/test/basic.test.ts | 211 ++-- .../test/embeded_functions_join.test.ts | 120 ++ .../core/postgrest-js/test/filters.test.ts | 24 + .../test/resource-embedding.test.ts | 54 + packages/core/postgrest-js/test/retry.test.ts | 34 +- .../core/postgrest-js/test/transforms.test.ts | 28 +- 7 files changed, 859 insertions(+), 695 deletions(-) diff --git a/packages/core/postgrest-js/test/advanced_rpc.test.ts b/packages/core/postgrest-js/test/advanced_rpc.test.ts index 9ffa20f82c1..d2c1880d42b 100644 --- a/packages/core/postgrest-js/test/advanced_rpc.test.ts +++ b/packages/core/postgrest-js/test/advanced_rpc.test.ts @@ -71,20 +71,18 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": [ + { + "channel_id": 1, + "data": null, + "id": 1, + "message": "Hello World 👋", + "username": "supabot", + }, + ], + "error": null, + "status": 200, + "statusText": "OK", } `) ExpectedSchema.parse(res.data) @@ -107,20 +105,32 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": [ + { + "channel_id": 1, + "data": null, + "id": 1, + "message": "Hello World 👋", + "username": "supabot", + }, + { + "channel_id": 2, + "data": null, + "id": 2, + "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", + "username": "supabot", + }, + { + "channel_id": 3, + "data": null, + "id": 4, + "message": "Some message on channel without details", + "username": "supabot", + }, + ], + "error": null, + "status": 200, + "statusText": "OK", } `) ExpectedSchema.parse(res.data) @@ -142,20 +152,13 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", + "data": { + "id": 1, + "username": "supabot", }, - "status": 0, - "statusText": "", + "error": null, + "status": 200, + "statusText": "OK", } `) UserProfileSchema.parse(res.data) @@ -174,20 +177,32 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": [ + { + "channel_id": 1, + "data": null, + "id": 1, + "message": "Hello World 👋", + "username": "supabot", + }, + { + "channel_id": 2, + "data": null, + "id": 2, + "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", + "username": "supabot", + }, + { + "channel_id": 3, + "data": null, + "id": 4, + "message": "Some message on channel without details", + "username": "supabot", + }, + ], + "error": null, + "status": 200, + "statusText": "OK", } `) ExpectedSchema.parse(res.data) @@ -210,20 +225,32 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": [ + { + "channel_id": 1, + "data": null, + "id": 1, + "message": "Hello World 👋", + "username": "supabot", + }, + { + "channel_id": 2, + "data": null, + "id": 2, + "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", + "username": "supabot", + }, + { + "channel_id": 3, + "data": null, + "id": 4, + "message": "Some message on channel without details", + "username": "supabot", + }, + ], + "error": null, + "status": 200, + "statusText": "OK", } `) ExpectedSchema.parse(res.data) @@ -246,20 +273,32 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": [ + { + "channel_id": 1, + "data": null, + "id": 1, + "message": "Hello World 👋", + "username": "supabot", + }, + { + "channel_id": 2, + "data": null, + "id": 2, + "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", + "username": "supabot", + }, + { + "channel_id": 3, + "data": null, + "id": 4, + "message": "Some message on channel without details", + "username": "supabot", + }, + ], + "error": null, + "status": 200, + "statusText": "OK", } `) ExpectedSchema.parse(res.data) @@ -281,20 +320,32 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": [ + { + "channel_id": 3, + "data": null, + "id": 4, + "message": "Some message on channel without details", + "username": "supabot", + }, + { + "channel_id": 2, + "data": null, + "id": 2, + "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", + "username": "supabot", + }, + { + "channel_id": 1, + "data": null, + "id": 1, + "message": "Hello World 👋", + "username": "supabot", + }, + ], + "error": null, + "status": 200, + "statusText": "OK", } `) RecentMessagesSchema.array().parse(res.data) @@ -310,20 +361,32 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": [ + { + "channel_id": 3, + "data": null, + "id": 4, + "message": "Some message on channel without details", + "username": "supabot", + }, + { + "channel_id": 2, + "data": null, + "id": 2, + "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", + "username": "supabot", + }, + { + "channel_id": 1, + "data": null, + "id": 1, + "message": "Hello World 👋", + "username": "supabot", + }, + ], + "error": null, + "status": 200, + "statusText": "OK", } `) RecentMessagesSchema.array().parse(res.data) @@ -341,20 +404,35 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": [ + { + "channel_id": 3, + "message": "Some message on channel without details", + "users": { + "catchphrase": "'cat' 'fat'", + "username": "supabot", + }, + }, + { + "channel_id": 2, + "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", + "users": { + "catchphrase": "'cat' 'fat'", + "username": "supabot", + }, + }, + { + "channel_id": 1, + "message": "Hello World 👋", + "users": { + "catchphrase": "'cat' 'fat'", + "username": "supabot", + }, + }, + ], + "error": null, + "status": 200, + "statusText": "OK", } `) SelectWithUsersSchema.array().parse(res.data) @@ -378,20 +456,17 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", + "data": { + "id": 1, + "username": "supabot", + "users": { + "catchphrase": "'cat' 'fat'", + "username": "supabot", + }, }, - "status": 0, - "statusText": "", + "error": null, + "status": 200, + "statusText": "OK", } `) SelectWithUsersProfileSchema.parse(res.data) @@ -406,19 +481,9 @@ describe('advanced rpc', () => { { "count": null, "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "error": null, + "status": 204, + "statusText": "No Content", } `) }) @@ -436,18 +501,13 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", + "code": "PGRST203", + "details": null, + "hint": "Try renaming the parameters or the function itself in the database so function overloading can be resolved", + "message": "Could not choose the best candidate function between: public.postgrest_unresolvable_function(a => integer), public.postgrest_unresolvable_function(a => text)", }, - "status": 0, - "statusText": "", + "status": 300, + "statusText": "Multiple Choices", } `) }) @@ -465,18 +525,13 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", + "code": "PGRST203", + "details": null, + "hint": "Try renaming the parameters or the function itself in the database so function overloading can be resolved", + "message": "Could not choose the best candidate function between: public.postgrest_unresolvable_function(a => integer), public.postgrest_unresolvable_function(a => text)", }, - "status": 0, - "statusText": "", + "status": 300, + "statusText": "Multiple Choices", } `) }) @@ -490,19 +545,9 @@ describe('advanced rpc', () => { { "count": null, "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "error": null, + "status": 204, + "statusText": "No Content", } `) }) @@ -517,20 +562,10 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": 1, + "error": null, + "status": 200, + "statusText": "OK", } `) }) @@ -545,20 +580,10 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": "foo", + "error": null, + "status": 200, + "statusText": "OK", } `) }) @@ -573,20 +598,15 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": [ + { + "id": 1, + "username": "supabot", + }, + ], + "error": null, + "status": 200, + "statusText": "OK", } `) UserProfileSchema.array().parse(res.data) @@ -604,20 +624,18 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": [ + { + "channel_id": 1, + "data": null, + "id": 1, + "message": "Hello World 👋", + "username": "supabot", + }, + ], + "error": null, + "status": 200, + "statusText": "OK", } `) ExpectedSchema.parse(res.data) @@ -640,20 +658,32 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": [ + { + "channel_id": 1, + "data": null, + "id": 1, + "message": "Hello World 👋", + "username": "supabot", + }, + { + "channel_id": 2, + "data": null, + "id": 2, + "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", + "username": "supabot", + }, + { + "channel_id": 3, + "data": null, + "id": 4, + "message": "Some message on channel without details", + "username": "supabot", + }, + ], + "error": null, + "status": 200, + "statusText": "OK", } `) ExpectedSchema.parse(res.data) @@ -669,20 +699,10 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": "foo", + "error": null, + "status": 200, + "statusText": "OK", } `) }) @@ -695,20 +715,10 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": "foo", + "error": null, + "status": 200, + "statusText": "OK", } `) }) @@ -726,18 +736,13 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", + "code": "PGRST202", + "details": "Searched for the function public.polymorphic_function_with_unnamed_integer with parameter or with a single unnamed json/jsonb parameter, but no matches were found in the schema cache.", + "hint": "Perhaps you meant to call the function public.polymorphic_function_with_unnamed_text", + "message": "Could not find the function public.polymorphic_function_with_unnamed_integer() in the schema cache", }, - "status": 0, - "statusText": "", + "status": 404, + "statusText": "Not Found", } `) }) @@ -752,20 +757,10 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": 1, + "error": null, + "status": 200, + "statusText": "OK", } `) }) @@ -780,20 +775,10 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": 1, + "error": null, + "status": 200, + "statusText": "OK", } `) }) @@ -808,20 +793,10 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": 1, + "error": null, + "status": 200, + "statusText": "OK", } `) }) @@ -834,20 +809,10 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": 1, + "error": null, + "status": 200, + "statusText": "OK", } `) }) @@ -862,20 +827,10 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": "foo", + "error": null, + "status": 200, + "statusText": "OK", } `) }) @@ -890,20 +845,10 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": "foo", + "error": null, + "status": 200, + "statusText": "OK", } `) }) @@ -918,18 +863,13 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", + "code": "PGRST203", + "details": null, + "hint": "Try renaming the parameters or the function itself in the database so function overloading can be resolved", + "message": "Could not choose the best candidate function between: public.polymorphic_function_with_unnamed_default(), public.polymorphic_function_with_unnamed_default( => text)", }, - "status": 0, - "statusText": "", + "status": 300, + "statusText": "Multiple Choices", } `) }) @@ -946,18 +886,13 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", + "code": "PGRST203", + "details": null, + "hint": "Try renaming the parameters or the function itself in the database so function overloading can be resolved", + "message": "Could not choose the best candidate function between: public.polymorphic_function_with_unnamed_default(), public.polymorphic_function_with_unnamed_default( => text)", }, - "status": 0, - "statusText": "", + "status": 300, + "statusText": "Multiple Choices", } `) }) @@ -972,20 +907,10 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": "foo", + "error": null, + "status": 200, + "statusText": "OK", } `) }) @@ -1000,18 +925,13 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", + "code": "PGRST203", + "details": null, + "hint": "Try renaming the parameters or the function itself in the database so function overloading can be resolved", + "message": "Could not choose the best candidate function between: public.polymorphic_function_with_unnamed_default_overload(), public.polymorphic_function_with_unnamed_default_overload( => text)", }, - "status": 0, - "statusText": "", + "status": 300, + "statusText": "Multiple Choices", } `) }) @@ -1028,18 +948,13 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", + "code": "PGRST203", + "details": null, + "hint": "Try renaming the parameters or the function itself in the database so function overloading can be resolved", + "message": "Could not choose the best candidate function between: public.polymorphic_function_with_unnamed_default_overload(), public.polymorphic_function_with_unnamed_default_overload( => text)", }, - "status": 0, - "statusText": "", + "status": 300, + "statusText": "Multiple Choices", } `) }) @@ -1054,20 +969,10 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": "foo", + "error": null, + "status": 200, + "statusText": "OK", } `) }) @@ -1083,20 +988,10 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": "foo", + "error": null, + "status": 200, + "statusText": "OK", } `) }) @@ -1111,18 +1006,13 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", + "code": "PGRST202", + "details": "Searched for the function public.blurb_message without parameters or with a single unnamed json/jsonb parameter, but no matches were found in the schema cache.", + "hint": "Perhaps you meant to call the function public.get_messages", + "message": "Could not find the function public.blurb_message without parameters in the schema cache", }, - "status": 0, - "statusText": "", + "status": 404, + "statusText": "Not Found", } `) }) @@ -1146,18 +1036,13 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", + "code": "PGRST202", + "details": "Searched for the function public.blurb_message with parameter or with a single unnamed json/jsonb parameter, but no matches were found in the schema cache.", + "hint": "Perhaps you meant to call the function public.get_messages", + "message": "Could not find the function public.blurb_message() in the schema cache", }, - "status": 0, - "statusText": "", + "status": 404, + "statusText": "Not Found", } `) }) @@ -1170,20 +1055,16 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", + "data": { + "age_range": "[1,2)", + "catchphrase": "'cat' 'fat'", + "data": null, + "status": "ONLINE", + "username": "supabot", }, - "status": 0, - "statusText": "", + "error": null, + "status": 200, + "statusText": "OK", } `) FunctionReturningRowSchema.parse(res.data) @@ -1198,20 +1079,67 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": [ + { + "age_range": "[1,2)", + "catchphrase": "'cat' 'fat'", + "data": null, + "status": "ONLINE", + "username": "supabot", + }, + { + "age_range": "[25,35)", + "catchphrase": "'bat' 'cat'", + "data": null, + "status": "OFFLINE", + "username": "kiwicopple", + }, + { + "age_range": "[25,35)", + "catchphrase": "'bat' 'rat'", + "data": null, + "status": "ONLINE", + "username": "awailas", + }, + { + "age_range": "[20,30)", + "catchphrase": "'json' 'test'", + "data": { + "foo": { + "bar": { + "nested": "value", + }, + "baz": "string value", + }, + }, + "status": "ONLINE", + "username": "jsonuser", + }, + { + "age_range": null, + "catchphrase": null, + "data": null, + "status": "ONLINE", + "username": "a", + }, + { + "age_range": null, + "catchphrase": null, + "data": null, + "status": "ONLINE", + "username": "b", + }, + { + "age_range": "[20,30)", + "catchphrase": "'fat' 'rat'", + "data": null, + "status": "ONLINE", + "username": "dragarcia", + }, + ], + "error": null, + "status": 200, + "statusText": "OK", } `) ExpectedSchema.parse(res.data) @@ -1234,20 +1162,15 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": [ + { + "id": 1, + "username": "supabot", + }, + ], + "error": null, + "status": 200, + "statusText": "OK", } `) ExpectedSchema.parse(res.data) @@ -1269,20 +1192,13 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", + "data": { + "id": 1, + "username": "supabot", }, - "status": 0, - "statusText": "", + "error": null, + "status": 200, + "statusText": "OK", } `) UserProfileSchema.parse(res.data) @@ -1302,19 +1218,9 @@ test('should be able to filter before and after select rpc', async () => { { "count": null, "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "error": null, + "status": 200, + "statusText": "OK", } `) const res2 = await postgrest @@ -1330,19 +1236,9 @@ test('should be able to filter before and after select rpc', async () => { { "count": null, "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "error": null, + "status": 200, + "statusText": "OK", } `) const res3 = await postgrest @@ -1357,20 +1253,16 @@ test('should be able to filter before and after select rpc', async () => { expect(res3).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", + "data": { + "username": "supabot", + "users": { + "catchphrase": "'cat' 'fat'", + "username": "supabot", + }, }, - "status": 0, - "statusText": "", + "error": null, + "status": 200, + "statusText": "OK", } `) }) @@ -1384,20 +1276,23 @@ test('RPC call with subselect and computed field', async () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": [ + { + "blurb_message": "Hel", + "message": "Hello World 👋", + }, + { + "blurb_message": "Per", + "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", + }, + { + "blurb_message": "Som", + "message": "Some message on channel without details", + }, + ], + "error": null, + "status": 200, + "statusText": "OK", } `) let result: Exclude diff --git a/packages/core/postgrest-js/test/basic.test.ts b/packages/core/postgrest-js/test/basic.test.ts index 359e22dd7ce..88d8fdd5deb 100644 --- a/packages/core/postgrest-js/test/basic.test.ts +++ b/packages/core/postgrest-js/test/basic.test.ts @@ -31,13 +31,6 @@ test('basic select table', async () => { "status": "ONLINE", "username": "awailas", }, - { - "age_range": "[20,30)", - "catchphrase": "'fat' 'rat'", - "data": null, - "status": "ONLINE", - "username": "dragarcia", - }, { "age_range": "[20,30)", "catchphrase": "'json' 'test'", @@ -52,6 +45,27 @@ test('basic select table', async () => { "status": "ONLINE", "username": "jsonuser", }, + { + "age_range": null, + "catchphrase": null, + "data": null, + "status": "ONLINE", + "username": "a", + }, + { + "age_range": null, + "catchphrase": null, + "data": null, + "status": "ONLINE", + "username": "b", + }, + { + "age_range": "[20,30)", + "catchphrase": "'fat' 'rat'", + "data": null, + "status": "ONLINE", + "username": "dragarcia", + }, ], "error": null, "status": 200, @@ -87,13 +101,6 @@ test('basic select returns types override', async () => { "status": "ONLINE", "username": "awailas", }, - { - "age_range": "[20,30)", - "catchphrase": "'fat' 'rat'", - "data": null, - "status": "ONLINE", - "username": "dragarcia", - }, { "age_range": "[20,30)", "catchphrase": "'json' 'test'", @@ -108,6 +115,27 @@ test('basic select returns types override', async () => { "status": "ONLINE", "username": "jsonuser", }, + { + "age_range": null, + "catchphrase": null, + "data": null, + "status": "ONLINE", + "username": "a", + }, + { + "age_range": null, + "catchphrase": null, + "data": null, + "status": "ONLINE", + "username": "b", + }, + { + "age_range": "[20,30)", + "catchphrase": "'fat' 'rat'", + "data": null, + "status": "ONLINE", + "username": "dragarcia", + }, ], "error": null, "status": 200, @@ -172,7 +200,7 @@ test('basic select with maybeSingle yielding more than one result', async () => "data": null, "error": { "code": "PGRST116", - "details": "Results contain 5 rows, application/vnd.pgrst.object+json requires 1 row", + "details": "Results contain 7 rows, application/vnd.pgrst.object+json requires 1 row", "hint": null, "message": "JSON object requested, multiple (or no) rows returned", }, @@ -190,7 +218,7 @@ test('basic select with single yielding more than one result', async () => { "data": null, "error": { "code": "PGRST116", - "details": "The result contains 5 rows", + "details": "The result contains 7 rows", "hint": null, "message": "Cannot coerce the result to a single JSON object", }, @@ -220,11 +248,19 @@ test('basic select view', async () => { }, { "non_updatable_column": 1, - "username": "dragarcia", + "username": "jsonuser", }, { "non_updatable_column": 1, - "username": "jsonuser", + "username": "a", + }, + { + "non_updatable_column": 1, + "username": "b", + }, + { + "non_updatable_column": 1, + "username": "dragarcia", }, ], "error": null, @@ -466,7 +502,7 @@ describe('basic insert, update, delete', () => { { "channel_id": 1, "data": null, - "id": 5, + "id": 33, "message": "foo", "username": "supabot", }, @@ -497,10 +533,10 @@ describe('basic insert, update, delete', () => { "username": "supabot", }, { - "channel_id": 3, + "channel_id": 1, "data": null, - "id": 3, - "message": "Some message on channel without details", + "id": 33, + "message": "foo", "username": "supabot", }, { @@ -510,13 +546,6 @@ describe('basic insert, update, delete', () => { "message": "Some message on channel without details", "username": "supabot", }, - { - "channel_id": 1, - "data": null, - "id": 5, - "message": "foo", - "username": "supabot", - }, ], "error": null, "status": 200, @@ -543,8 +572,8 @@ describe('basic insert, update, delete', () => { }, ], "error": null, - "status": 200, - "statusText": "OK", + "status": 201, + "statusText": "Created", } `) @@ -568,17 +597,17 @@ describe('basic insert, update, delete', () => { "username": "supabot", }, { - "channel_id": 3, + "channel_id": 1, "data": null, - "id": 4, - "message": "Some message on channel without details", + "id": 33, + "message": "foo", "username": "supabot", }, { - "channel_id": 1, + "channel_id": 3, "data": null, - "id": 5, - "message": "foo", + "id": 4, + "message": "Some message on channel without details", "username": "supabot", }, { @@ -611,14 +640,14 @@ describe('basic insert, update, delete', () => { { "channel_id": 1, "data": null, - "id": 6, + "id": 34, "message": "foo", "username": "supabot", }, { "channel_id": 1, "data": null, - "id": 7, + "id": 35, "message": "foo", "username": "supabot", }, @@ -649,17 +678,17 @@ describe('basic insert, update, delete', () => { "username": "supabot", }, { - "channel_id": 3, + "channel_id": 1, "data": null, - "id": 4, - "message": "Some message on channel without details", + "id": 33, + "message": "foo", "username": "supabot", }, { - "channel_id": 1, + "channel_id": 3, "data": null, - "id": 5, - "message": "foo", + "id": 4, + "message": "Some message on channel without details", "username": "supabot", }, { @@ -672,14 +701,14 @@ describe('basic insert, update, delete', () => { { "channel_id": 1, "data": null, - "id": 6, + "id": 34, "message": "foo", "username": "supabot", }, { "channel_id": 1, "data": null, - "id": 7, + "id": 35, "message": "foo", "username": "supabot", }, @@ -726,7 +755,7 @@ describe('basic insert, update, delete', () => { { "channel_id": 2, "data": null, - "id": 5, + "id": 33, "message": "foo", "username": "supabot", }, @@ -740,14 +769,14 @@ describe('basic insert, update, delete', () => { { "channel_id": 2, "data": null, - "id": 6, + "id": 34, "message": "foo", "username": "supabot", }, { "channel_id": 2, "data": null, - "id": 7, + "id": 35, "message": "foo", "username": "supabot", }, @@ -787,7 +816,7 @@ describe('basic insert, update, delete', () => { { "channel_id": 2, "data": null, - "id": 5, + "id": 33, "message": "foo", "username": "supabot", }, @@ -801,14 +830,14 @@ describe('basic insert, update, delete', () => { { "channel_id": 2, "data": null, - "id": 6, + "id": 34, "message": "foo", "username": "supabot", }, { "channel_id": 2, "data": null, - "id": 7, + "id": 35, "message": "foo", "username": "supabot", }, @@ -829,7 +858,7 @@ describe('basic insert, update, delete', () => { { "channel_id": 2, "data": null, - "id": 5, + "id": 33, "message": "foo", "username": "supabot", }, @@ -843,14 +872,14 @@ describe('basic insert, update, delete', () => { { "channel_id": 2, "data": null, - "id": 6, + "id": 34, "message": "foo", "username": "supabot", }, { "channel_id": 2, "data": null, - "id": 7, + "id": 35, "message": "foo", "username": "supabot", }, @@ -1049,6 +1078,20 @@ test('allow ordering on JSON column', async () => { "status": "ONLINE", "username": "jsonuser", }, + { + "age_range": null, + "catchphrase": null, + "data": null, + "status": "ONLINE", + "username": "a", + }, + { + "age_range": null, + "catchphrase": null, + "data": null, + "status": "ONLINE", + "username": "b", + }, { "age_range": "[20,30)", "catchphrase": "'fat' 'rat'", @@ -1084,7 +1127,7 @@ test('select with head:true, count:exact', async () => { const res = await postgrest.from('users').select('*', { head: true, count: 'exact' }) expect(res).toMatchInlineSnapshot(` { - "count": 5, + "count": 7, "data": null, "error": null, "status": 200, @@ -1133,7 +1176,7 @@ test('select with count:exact', async () => { const res = await postgrest.from('users').select('*', { count: 'exact' }) expect(res).toMatchInlineSnapshot(` { - "count": 5, + "count": 7, "data": [ { "age_range": "[1,2)", @@ -1170,6 +1213,20 @@ test('select with count:exact', async () => { "status": "ONLINE", "username": "jsonuser", }, + { + "age_range": null, + "catchphrase": null, + "data": null, + "status": "ONLINE", + "username": "a", + }, + { + "age_range": null, + "catchphrase": null, + "data": null, + "status": "ONLINE", + "username": "b", + }, { "age_range": "[20,30)", "catchphrase": "'fat' 'rat'", @@ -1292,7 +1349,7 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 1, "data": null, - "id": 8, + "id": 36, "message": "foo", "username": "supabot", }, @@ -1332,7 +1389,7 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 1, "data": null, - "id": 8, + "id": 36, "message": "foo", "username": "supabot", }, @@ -1396,7 +1453,7 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 1, "data": null, - "id": 8, + "id": 36, "message": "foo", "username": "supabot", }, @@ -1433,14 +1490,14 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 1, "data": null, - "id": 9, + "id": 37, "message": "foo", "username": "supabot", }, { "channel_id": 1, "data": null, - "id": 10, + "id": 38, "message": "foo", "username": "supabot", }, @@ -1480,7 +1537,7 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 1, "data": null, - "id": 8, + "id": 36, "message": "foo", "username": "supabot", }, @@ -1494,14 +1551,14 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 1, "data": null, - "id": 9, + "id": 37, "message": "foo", "username": "supabot", }, { "channel_id": 1, "data": null, - "id": 10, + "id": 38, "message": "foo", "username": "supabot", }, @@ -1530,7 +1587,7 @@ describe("insert, update, delete with count: 'exact'", () => { }, { "data": null, - "id": 5, + "id": 13, "slug": "test-slug", }, ], @@ -1558,7 +1615,7 @@ describe("insert, update, delete with count: 'exact'", () => { }, { "data": null, - "id": 7, + "id": 15, "slug": "test-slug", }, ], @@ -1582,7 +1639,7 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 2, "data": null, - "id": 8, + "id": 36, "message": "foo", "username": "supabot", }, @@ -1596,14 +1653,14 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 2, "data": null, - "id": 9, + "id": 37, "message": "foo", "username": "supabot", }, { "channel_id": 2, "data": null, - "id": 10, + "id": 38, "message": "foo", "username": "supabot", }, @@ -1643,7 +1700,7 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 2, "data": null, - "id": 8, + "id": 36, "message": "foo", "username": "supabot", }, @@ -1657,14 +1714,14 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 2, "data": null, - "id": 9, + "id": 37, "message": "foo", "username": "supabot", }, { "channel_id": 2, "data": null, - "id": 10, + "id": 38, "message": "foo", "username": "supabot", }, @@ -1689,7 +1746,7 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 2, "data": null, - "id": 8, + "id": 36, "message": "foo", "username": "supabot", }, @@ -1703,14 +1760,14 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 2, "data": null, - "id": 9, + "id": 37, "message": "foo", "username": "supabot", }, { "channel_id": 2, "data": null, - "id": 10, + "id": 38, "message": "foo", "username": "supabot", }, diff --git a/packages/core/postgrest-js/test/embeded_functions_join.test.ts b/packages/core/postgrest-js/test/embeded_functions_join.test.ts index 33220716282..9becf9a5374 100644 --- a/packages/core/postgrest-js/test/embeded_functions_join.test.ts +++ b/packages/core/postgrest-js/test/embeded_functions_join.test.ts @@ -182,6 +182,14 @@ describe('embeded functions select', () => { "all_user_messages": [], "username": "jsonuser", }, + { + "all_user_messages": [], + "username": "a", + }, + { + "all_user_messages": [], + "username": "b", + }, { "all_user_messages": [], "username": "dragarcia", @@ -250,6 +258,14 @@ describe('embeded functions select', () => { "all_user_messages": [], "username": "jsonuser", }, + { + "all_user_messages": [], + "username": "a", + }, + { + "all_user_messages": [], + "username": "b", + }, { "all_user_messages": [], "username": "dragarcia", @@ -318,6 +334,14 @@ describe('embeded functions select', () => { "all_user_messages": [], "username": "jsonuser", }, + { + "all_user_messages": [], + "username": "a", + }, + { + "all_user_messages": [], + "username": "b", + }, { "all_user_messages": [], "username": "dragarcia", @@ -374,6 +398,14 @@ describe('embeded functions select', () => { "setof_rows_one": null, "username": "jsonuser", }, + { + "setof_rows_one": null, + "username": "a", + }, + { + "setof_rows_one": null, + "username": "b", + }, { "setof_rows_one": null, "username": "dragarcia", @@ -438,6 +470,20 @@ describe('embeded functions select', () => { }, "username": "jsonuser", }, + { + "returns_row": { + "id": null, + "username": null, + }, + "username": "a", + }, + { + "returns_row": { + "id": null, + "username": null, + }, + "username": "b", + }, { "returns_row": { "id": null, @@ -542,6 +588,18 @@ describe('embeded functions select', () => { }, "username": "jsonuser", }, + { + "user_called_profile": { + "username": null, + }, + "username": "a", + }, + { + "user_called_profile": { + "username": null, + }, + "username": "b", + }, { "user_called_profile": { "username": null, @@ -688,6 +746,14 @@ describe('embeded functions select', () => { "user_messages": [], "username": "jsonuser", }, + { + "user_messages": [], + "username": "a", + }, + { + "user_messages": [], + "username": "b", + }, { "user_messages": [], "username": "dragarcia", @@ -761,6 +827,14 @@ describe('embeded functions select', () => { "active_user_messages": [], "username": "jsonuser", }, + { + "active_user_messages": [], + "username": "a", + }, + { + "active_user_messages": [], + "username": "b", + }, { "active_user_messages": [], "username": "dragarcia", @@ -838,6 +912,14 @@ describe('embeded functions select', () => { "recent_messages": [], "username": "jsonuser", }, + { + "recent_messages": [], + "username": "a", + }, + { + "recent_messages": [], + "username": "b", + }, { "recent_messages": [], "username": "dragarcia", @@ -911,6 +993,14 @@ describe('embeded functions select', () => { "recent_messages": [], "username": "jsonuser", }, + { + "recent_messages": [], + "username": "a", + }, + { + "recent_messages": [], + "username": "b", + }, { "recent_messages": [], "username": "dragarcia", @@ -982,6 +1072,14 @@ describe('embeded functions select', () => { "user_messages": [], "username": "jsonuser", }, + { + "user_messages": [], + "username": "a", + }, + { + "user_messages": [], + "username": "b", + }, { "user_messages": [], "username": "dragarcia", @@ -1142,6 +1240,20 @@ describe('embeded functions select', () => { }, "username": "jsonuser", }, + { + "profile": { + "id": null, + "username": null, + }, + "username": "a", + }, + { + "profile": { + "id": null, + "username": null, + }, + "username": "b", + }, { "profile": { "id": null, @@ -1198,6 +1310,14 @@ describe('embeded functions select', () => { "profile": null, "username": "jsonuser", }, + { + "profile": null, + "username": "a", + }, + { + "profile": null, + "username": "b", + }, { "profile": null, "username": "dragarcia", diff --git a/packages/core/postgrest-js/test/filters.test.ts b/packages/core/postgrest-js/test/filters.test.ts index 5fcb9e69ec6..9f5cf6cfade 100644 --- a/packages/core/postgrest-js/test/filters.test.ts +++ b/packages/core/postgrest-js/test/filters.test.ts @@ -21,6 +21,12 @@ test('not', async () => { { "status": "ONLINE", }, + { + "status": "ONLINE", + }, + { + "status": "ONLINE", + }, ], "error": null, "status": 200, @@ -86,6 +92,12 @@ test('neq', async () => { { "username": "jsonuser", }, + { + "username": "a", + }, + { + "username": "b", + }, { "username": "dragarcia", }, @@ -315,6 +327,12 @@ test('is', async () => { { "data": null, }, + { + "data": null, + }, + { + "data": null, + }, ], "error": null, "status": 200, @@ -345,6 +363,12 @@ test('in', async () => { { "status": "ONLINE", }, + { + "status": "ONLINE", + }, + { + "status": "ONLINE", + }, ], "error": null, "status": 200, diff --git a/packages/core/postgrest-js/test/resource-embedding.test.ts b/packages/core/postgrest-js/test/resource-embedding.test.ts index 554442a9548..10cfa296705 100644 --- a/packages/core/postgrest-js/test/resource-embedding.test.ts +++ b/packages/core/postgrest-js/test/resource-embedding.test.ts @@ -50,6 +50,12 @@ test('embedded select', async () => { { "messages": [], }, + { + "messages": [], + }, + { + "messages": [], + }, ], "error": null, "status": 200, @@ -124,6 +130,12 @@ test('embedded select with computed field explicit selection', async () => { { "messages": [], }, + { + "messages": [], + }, + { + "messages": [], + }, ], "error": null, "status": 200, @@ -186,6 +198,12 @@ describe('embedded filters', () => { { "messages": [], }, + { + "messages": [], + }, + { + "messages": [], + }, ], "error": null, "status": 200, @@ -251,6 +269,12 @@ describe('embedded filters', () => { { "messages": [], }, + { + "messages": [], + }, + { + "messages": [], + }, ], "error": null, "status": 200, @@ -318,6 +342,12 @@ describe('embedded filters', () => { { "messages": [], }, + { + "messages": [], + }, + { + "messages": [], + }, ], "error": null, "status": 200, @@ -393,6 +423,12 @@ describe('embedded transforms', () => { { "messages": [], }, + { + "messages": [], + }, + { + "messages": [], + }, ], "error": null, "status": 200, @@ -467,6 +503,12 @@ describe('embedded transforms', () => { { "messages": [], }, + { + "messages": [], + }, + { + "messages": [], + }, ], "error": null, "status": 200, @@ -526,6 +568,12 @@ describe('embedded transforms', () => { { "messages": [], }, + { + "messages": [], + }, + { + "messages": [], + }, ], "error": null, "status": 200, @@ -585,6 +633,12 @@ describe('embedded transforms', () => { { "messages": [], }, + { + "messages": [], + }, + { + "messages": [], + }, ], "error": null, "status": 200, diff --git a/packages/core/postgrest-js/test/retry.test.ts b/packages/core/postgrest-js/test/retry.test.ts index 1c2ebbb3860..3e02049389f 100644 --- a/packages/core/postgrest-js/test/retry.test.ts +++ b/packages/core/postgrest-js/test/retry.test.ts @@ -1,40 +1,40 @@ -import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' import PostgrestClient from '../src/PostgrestClient' describe('Automatic Retries', () => { - let fetchMock: ReturnType + let fetchMock: jest.Mock beforeEach(() => { - fetchMock = vi.fn() - vi.useFakeTimers() + fetchMock = jest.fn() + jest.useFakeTimers() }) afterEach(() => { - vi.useRealTimers() - vi.restoreAllMocks() + jest.useRealTimers() + jest.restoreAllMocks() }) /** * Helper to run async code with fake timers */ - async function runWithTimers(promise: Promise): Promise { + async function runWithTimers(promise: PromiseLike): Promise { let result: T | undefined let error: Error | undefined let resolved = false - promise - .then((r) => { + promise.then( + (r) => { result = r resolved = true - }) - .catch((e) => { + }, + (e) => { error = e resolved = true - }) + } + ) // Keep advancing timers until promise resolves while (!resolved) { - await vi.advanceTimersByTimeAsync(100) + await jest.advanceTimersByTimeAsync(100) } if (error) throw error @@ -211,7 +211,7 @@ describe('Automatic Retries', () => { it('should use exponential backoff delays', async () => { let delaysSeen: number[] = [] const originalSleep = globalThis.setTimeout - vi.spyOn(globalThis, 'setTimeout').mockImplementation((fn, delay) => { + jest.spyOn(globalThis, 'setTimeout').mockImplementation((fn, delay) => { delaysSeen.push(delay as number) return originalSleep(fn, 0) // Run immediately for testing }) @@ -334,7 +334,7 @@ describe('Automatic Retries', () => { statusText: 'Service Unavailable', headers: new Headers({ 'Retry-After': '0', 'Content-Type': 'application/json' }), text: () => Promise.resolve(pgrst002Body), - body: { cancel: vi.fn().mockResolvedValue(undefined) }, + body: { cancel: jest.fn().mockResolvedValue(undefined) }, }) .mockResolvedValueOnce({ ok: true, @@ -359,7 +359,7 @@ describe('Automatic Retries', () => { hint: null, message: 'Could not query the database for the schema cache. Retrying.', }) - const mockBody = { cancel: vi.fn().mockResolvedValue(undefined) } + const mockBody = { cancel: jest.fn().mockResolvedValue(undefined) } fetchMock.mockResolvedValue({ ok: false, @@ -380,7 +380,7 @@ describe('Automatic Retries', () => { }) it('should drain the response body before retrying', async () => { - const cancelMock = vi.fn().mockResolvedValue(undefined) + const cancelMock = jest.fn().mockResolvedValue(undefined) const pgrst002Body = JSON.stringify({ code: 'PGRST002', details: null, diff --git a/packages/core/postgrest-js/test/transforms.test.ts b/packages/core/postgrest-js/test/transforms.test.ts index afc08aca54c..c8c000d6d56 100644 --- a/packages/core/postgrest-js/test/transforms.test.ts +++ b/packages/core/postgrest-js/test/transforms.test.ts @@ -46,6 +46,13 @@ test('order', async () => { "status": "ONLINE", "username": "dragarcia", }, + { + "age_range": null, + "catchphrase": null, + "data": null, + "status": "ONLINE", + "username": "b", + }, { "age_range": "[25,35)", "catchphrase": "'bat' 'rat'", @@ -53,6 +60,13 @@ test('order', async () => { "status": "ONLINE", "username": "awailas", }, + { + "age_range": null, + "catchphrase": null, + "data": null, + "status": "ONLINE", + "username": "a", + }, ], "error": null, "status": 200, @@ -227,13 +241,13 @@ test('maybeSingle', async () => { "count": null, "data": null, "error": { - "code": "PGRST116", - "details": "Results contain 2 rows, application/vnd.pgrst.object+json requires 1 row", + "code": "23505", + "details": "Key (username)=(a) already exists.", "hint": null, - "message": "JSON object requested, multiple (or no) rows returned", + "message": "duplicate key value violates unique constraint "users_pkey"", }, - "status": 406, - "statusText": "Not Acceptable", + "status": 409, + "statusText": "Conflict", } `) }) @@ -286,9 +300,9 @@ test('csv', async () => { kiwicopple,,"[25,35)",OFFLINE,"'bat' 'cat'" awailas,,"[25,35)",ONLINE,"'bat' 'rat'" jsonuser,"{""foo"": {""bar"": {""nested"": ""value""}, ""baz"": ""string value""}}","[20,30)",ONLINE,"'json' 'test'" - dragarcia,,"[20,30)",ONLINE,"'fat' 'rat'" a,,,ONLINE, - b,,,ONLINE,", + b,,,ONLINE, + dragarcia,,"[20,30)",ONLINE,"'fat' 'rat'"", "error": null, "status": 200, "statusText": "OK", From b261f798488001c38f1a09f14dc424534a797549 Mon Sep 17 00:00:00 2001 From: Katerina Skroumpelou Date: Fri, 27 Mar 2026 16:42:21 +0200 Subject: [PATCH 04/12] fix(postgrest): remove x-retry-count header --- packages/core/postgrest-js/src/PostgrestBuilder.ts | 4 ---- packages/core/postgrest-js/test/retry.test.ts | 9 --------- 2 files changed, 13 deletions(-) diff --git a/packages/core/postgrest-js/src/PostgrestBuilder.ts b/packages/core/postgrest-js/src/PostgrestBuilder.ts index fa233582fe3..8900d6bec55 100644 --- a/packages/core/postgrest-js/src/PostgrestBuilder.ts +++ b/packages/core/postgrest-js/src/PostgrestBuilder.ts @@ -222,11 +222,7 @@ export default abstract class PostgrestBuilder< let attemptCount = 0 while (true) { - // Clone headers for each attempt and add retry count header if this is a retry const requestHeaders = new Headers(this.headers) - if (attemptCount > 0) { - requestHeaders.set('X-Retry-Count', String(attemptCount)) - } try { const res = await _fetch(this.url.toString(), { diff --git a/packages/core/postgrest-js/test/retry.test.ts b/packages/core/postgrest-js/test/retry.test.ts index 3e02049389f..346322d5454 100644 --- a/packages/core/postgrest-js/test/retry.test.ts +++ b/packages/core/postgrest-js/test/retry.test.ts @@ -71,15 +71,6 @@ describe('Automatic Retries', () => { expect(result.error).toBeNull() expect(result.data).toEqual([{ id: 1, name: 'test' }]) expect(fetchMock).toHaveBeenCalledTimes(3) - - // Verify X-Retry-Count header was sent on retries - const [, firstRetryCall] = fetchMock.mock.calls - const firstRetryHeaders = firstRetryCall[1].headers as Headers - expect(firstRetryHeaders.get('X-Retry-Count')).toBe('1') - - const [, , secondRetryCall] = fetchMock.mock.calls - const secondRetryHeaders = secondRetryCall[1].headers as Headers - expect(secondRetryHeaders.get('X-Retry-Count')).toBe('2') }) it('should NOT retry POST requests even on 520 errors', async () => { From 2b1b7befdf76984beb67495505e2d742db4d6759 Mon Sep 17 00:00:00 2001 From: Katerina Skroumpelou Date: Fri, 27 Mar 2026 16:44:13 +0200 Subject: [PATCH 05/12] fix(postgrest): address comment --- packages/core/postgrest-js/src/PostgrestBuilder.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/postgrest-js/src/PostgrestBuilder.ts b/packages/core/postgrest-js/src/PostgrestBuilder.ts index 8900d6bec55..b08c6f05aea 100644 --- a/packages/core/postgrest-js/src/PostgrestBuilder.ts +++ b/packages/core/postgrest-js/src/PostgrestBuilder.ts @@ -239,7 +239,7 @@ export default abstract class PostgrestBuilder< retryAfterHeader !== null ? Math.max(0, parseInt(retryAfterHeader, 10) || 0) * 1000 : getRetryDelay(attemptCount) - await res.body?.cancel() + await res.text() attemptCount++ await sleep(delay) continue From 357ea3913f1e8bb66b48f0bab3db85b851ec10ef Mon Sep 17 00:00:00 2001 From: Katerina Skroumpelou Date: Fri, 27 Mar 2026 17:05:46 +0200 Subject: [PATCH 06/12] fix(postgrest): tests --- packages/core/postgrest-js/test/basic.test.ts | 211 +++++++----------- .../test/embeded_functions_join.test.ts | 120 ---------- .../core/postgrest-js/test/filters.test.ts | 24 -- .../test/resource-embedding.test.ts | 54 ----- packages/core/postgrest-js/test/retry.test.ts | 21 +- .../core/postgrest-js/test/transforms.test.ts | 28 +-- .../test/unit/SupabaseClient.test.ts | 17 +- 7 files changed, 111 insertions(+), 364 deletions(-) diff --git a/packages/core/postgrest-js/test/basic.test.ts b/packages/core/postgrest-js/test/basic.test.ts index 88d8fdd5deb..359e22dd7ce 100644 --- a/packages/core/postgrest-js/test/basic.test.ts +++ b/packages/core/postgrest-js/test/basic.test.ts @@ -31,6 +31,13 @@ test('basic select table', async () => { "status": "ONLINE", "username": "awailas", }, + { + "age_range": "[20,30)", + "catchphrase": "'fat' 'rat'", + "data": null, + "status": "ONLINE", + "username": "dragarcia", + }, { "age_range": "[20,30)", "catchphrase": "'json' 'test'", @@ -45,27 +52,6 @@ test('basic select table', async () => { "status": "ONLINE", "username": "jsonuser", }, - { - "age_range": null, - "catchphrase": null, - "data": null, - "status": "ONLINE", - "username": "a", - }, - { - "age_range": null, - "catchphrase": null, - "data": null, - "status": "ONLINE", - "username": "b", - }, - { - "age_range": "[20,30)", - "catchphrase": "'fat' 'rat'", - "data": null, - "status": "ONLINE", - "username": "dragarcia", - }, ], "error": null, "status": 200, @@ -101,6 +87,13 @@ test('basic select returns types override', async () => { "status": "ONLINE", "username": "awailas", }, + { + "age_range": "[20,30)", + "catchphrase": "'fat' 'rat'", + "data": null, + "status": "ONLINE", + "username": "dragarcia", + }, { "age_range": "[20,30)", "catchphrase": "'json' 'test'", @@ -115,27 +108,6 @@ test('basic select returns types override', async () => { "status": "ONLINE", "username": "jsonuser", }, - { - "age_range": null, - "catchphrase": null, - "data": null, - "status": "ONLINE", - "username": "a", - }, - { - "age_range": null, - "catchphrase": null, - "data": null, - "status": "ONLINE", - "username": "b", - }, - { - "age_range": "[20,30)", - "catchphrase": "'fat' 'rat'", - "data": null, - "status": "ONLINE", - "username": "dragarcia", - }, ], "error": null, "status": 200, @@ -200,7 +172,7 @@ test('basic select with maybeSingle yielding more than one result', async () => "data": null, "error": { "code": "PGRST116", - "details": "Results contain 7 rows, application/vnd.pgrst.object+json requires 1 row", + "details": "Results contain 5 rows, application/vnd.pgrst.object+json requires 1 row", "hint": null, "message": "JSON object requested, multiple (or no) rows returned", }, @@ -218,7 +190,7 @@ test('basic select with single yielding more than one result', async () => { "data": null, "error": { "code": "PGRST116", - "details": "The result contains 7 rows", + "details": "The result contains 5 rows", "hint": null, "message": "Cannot coerce the result to a single JSON object", }, @@ -248,19 +220,11 @@ test('basic select view', async () => { }, { "non_updatable_column": 1, - "username": "jsonuser", - }, - { - "non_updatable_column": 1, - "username": "a", - }, - { - "non_updatable_column": 1, - "username": "b", + "username": "dragarcia", }, { "non_updatable_column": 1, - "username": "dragarcia", + "username": "jsonuser", }, ], "error": null, @@ -502,7 +466,7 @@ describe('basic insert, update, delete', () => { { "channel_id": 1, "data": null, - "id": 33, + "id": 5, "message": "foo", "username": "supabot", }, @@ -533,10 +497,10 @@ describe('basic insert, update, delete', () => { "username": "supabot", }, { - "channel_id": 1, + "channel_id": 3, "data": null, - "id": 33, - "message": "foo", + "id": 3, + "message": "Some message on channel without details", "username": "supabot", }, { @@ -546,6 +510,13 @@ describe('basic insert, update, delete', () => { "message": "Some message on channel without details", "username": "supabot", }, + { + "channel_id": 1, + "data": null, + "id": 5, + "message": "foo", + "username": "supabot", + }, ], "error": null, "status": 200, @@ -572,8 +543,8 @@ describe('basic insert, update, delete', () => { }, ], "error": null, - "status": 201, - "statusText": "Created", + "status": 200, + "statusText": "OK", } `) @@ -597,17 +568,17 @@ describe('basic insert, update, delete', () => { "username": "supabot", }, { - "channel_id": 1, + "channel_id": 3, "data": null, - "id": 33, - "message": "foo", + "id": 4, + "message": "Some message on channel without details", "username": "supabot", }, { - "channel_id": 3, + "channel_id": 1, "data": null, - "id": 4, - "message": "Some message on channel without details", + "id": 5, + "message": "foo", "username": "supabot", }, { @@ -640,14 +611,14 @@ describe('basic insert, update, delete', () => { { "channel_id": 1, "data": null, - "id": 34, + "id": 6, "message": "foo", "username": "supabot", }, { "channel_id": 1, "data": null, - "id": 35, + "id": 7, "message": "foo", "username": "supabot", }, @@ -678,17 +649,17 @@ describe('basic insert, update, delete', () => { "username": "supabot", }, { - "channel_id": 1, + "channel_id": 3, "data": null, - "id": 33, - "message": "foo", + "id": 4, + "message": "Some message on channel without details", "username": "supabot", }, { - "channel_id": 3, + "channel_id": 1, "data": null, - "id": 4, - "message": "Some message on channel without details", + "id": 5, + "message": "foo", "username": "supabot", }, { @@ -701,14 +672,14 @@ describe('basic insert, update, delete', () => { { "channel_id": 1, "data": null, - "id": 34, + "id": 6, "message": "foo", "username": "supabot", }, { "channel_id": 1, "data": null, - "id": 35, + "id": 7, "message": "foo", "username": "supabot", }, @@ -755,7 +726,7 @@ describe('basic insert, update, delete', () => { { "channel_id": 2, "data": null, - "id": 33, + "id": 5, "message": "foo", "username": "supabot", }, @@ -769,14 +740,14 @@ describe('basic insert, update, delete', () => { { "channel_id": 2, "data": null, - "id": 34, + "id": 6, "message": "foo", "username": "supabot", }, { "channel_id": 2, "data": null, - "id": 35, + "id": 7, "message": "foo", "username": "supabot", }, @@ -816,7 +787,7 @@ describe('basic insert, update, delete', () => { { "channel_id": 2, "data": null, - "id": 33, + "id": 5, "message": "foo", "username": "supabot", }, @@ -830,14 +801,14 @@ describe('basic insert, update, delete', () => { { "channel_id": 2, "data": null, - "id": 34, + "id": 6, "message": "foo", "username": "supabot", }, { "channel_id": 2, "data": null, - "id": 35, + "id": 7, "message": "foo", "username": "supabot", }, @@ -858,7 +829,7 @@ describe('basic insert, update, delete', () => { { "channel_id": 2, "data": null, - "id": 33, + "id": 5, "message": "foo", "username": "supabot", }, @@ -872,14 +843,14 @@ describe('basic insert, update, delete', () => { { "channel_id": 2, "data": null, - "id": 34, + "id": 6, "message": "foo", "username": "supabot", }, { "channel_id": 2, "data": null, - "id": 35, + "id": 7, "message": "foo", "username": "supabot", }, @@ -1078,20 +1049,6 @@ test('allow ordering on JSON column', async () => { "status": "ONLINE", "username": "jsonuser", }, - { - "age_range": null, - "catchphrase": null, - "data": null, - "status": "ONLINE", - "username": "a", - }, - { - "age_range": null, - "catchphrase": null, - "data": null, - "status": "ONLINE", - "username": "b", - }, { "age_range": "[20,30)", "catchphrase": "'fat' 'rat'", @@ -1127,7 +1084,7 @@ test('select with head:true, count:exact', async () => { const res = await postgrest.from('users').select('*', { head: true, count: 'exact' }) expect(res).toMatchInlineSnapshot(` { - "count": 7, + "count": 5, "data": null, "error": null, "status": 200, @@ -1176,7 +1133,7 @@ test('select with count:exact', async () => { const res = await postgrest.from('users').select('*', { count: 'exact' }) expect(res).toMatchInlineSnapshot(` { - "count": 7, + "count": 5, "data": [ { "age_range": "[1,2)", @@ -1213,20 +1170,6 @@ test('select with count:exact', async () => { "status": "ONLINE", "username": "jsonuser", }, - { - "age_range": null, - "catchphrase": null, - "data": null, - "status": "ONLINE", - "username": "a", - }, - { - "age_range": null, - "catchphrase": null, - "data": null, - "status": "ONLINE", - "username": "b", - }, { "age_range": "[20,30)", "catchphrase": "'fat' 'rat'", @@ -1349,7 +1292,7 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 1, "data": null, - "id": 36, + "id": 8, "message": "foo", "username": "supabot", }, @@ -1389,7 +1332,7 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 1, "data": null, - "id": 36, + "id": 8, "message": "foo", "username": "supabot", }, @@ -1453,7 +1396,7 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 1, "data": null, - "id": 36, + "id": 8, "message": "foo", "username": "supabot", }, @@ -1490,14 +1433,14 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 1, "data": null, - "id": 37, + "id": 9, "message": "foo", "username": "supabot", }, { "channel_id": 1, "data": null, - "id": 38, + "id": 10, "message": "foo", "username": "supabot", }, @@ -1537,7 +1480,7 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 1, "data": null, - "id": 36, + "id": 8, "message": "foo", "username": "supabot", }, @@ -1551,14 +1494,14 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 1, "data": null, - "id": 37, + "id": 9, "message": "foo", "username": "supabot", }, { "channel_id": 1, "data": null, - "id": 38, + "id": 10, "message": "foo", "username": "supabot", }, @@ -1587,7 +1530,7 @@ describe("insert, update, delete with count: 'exact'", () => { }, { "data": null, - "id": 13, + "id": 5, "slug": "test-slug", }, ], @@ -1615,7 +1558,7 @@ describe("insert, update, delete with count: 'exact'", () => { }, { "data": null, - "id": 15, + "id": 7, "slug": "test-slug", }, ], @@ -1639,7 +1582,7 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 2, "data": null, - "id": 36, + "id": 8, "message": "foo", "username": "supabot", }, @@ -1653,14 +1596,14 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 2, "data": null, - "id": 37, + "id": 9, "message": "foo", "username": "supabot", }, { "channel_id": 2, "data": null, - "id": 38, + "id": 10, "message": "foo", "username": "supabot", }, @@ -1700,7 +1643,7 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 2, "data": null, - "id": 36, + "id": 8, "message": "foo", "username": "supabot", }, @@ -1714,14 +1657,14 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 2, "data": null, - "id": 37, + "id": 9, "message": "foo", "username": "supabot", }, { "channel_id": 2, "data": null, - "id": 38, + "id": 10, "message": "foo", "username": "supabot", }, @@ -1746,7 +1689,7 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 2, "data": null, - "id": 36, + "id": 8, "message": "foo", "username": "supabot", }, @@ -1760,14 +1703,14 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 2, "data": null, - "id": 37, + "id": 9, "message": "foo", "username": "supabot", }, { "channel_id": 2, "data": null, - "id": 38, + "id": 10, "message": "foo", "username": "supabot", }, diff --git a/packages/core/postgrest-js/test/embeded_functions_join.test.ts b/packages/core/postgrest-js/test/embeded_functions_join.test.ts index 9becf9a5374..33220716282 100644 --- a/packages/core/postgrest-js/test/embeded_functions_join.test.ts +++ b/packages/core/postgrest-js/test/embeded_functions_join.test.ts @@ -182,14 +182,6 @@ describe('embeded functions select', () => { "all_user_messages": [], "username": "jsonuser", }, - { - "all_user_messages": [], - "username": "a", - }, - { - "all_user_messages": [], - "username": "b", - }, { "all_user_messages": [], "username": "dragarcia", @@ -258,14 +250,6 @@ describe('embeded functions select', () => { "all_user_messages": [], "username": "jsonuser", }, - { - "all_user_messages": [], - "username": "a", - }, - { - "all_user_messages": [], - "username": "b", - }, { "all_user_messages": [], "username": "dragarcia", @@ -334,14 +318,6 @@ describe('embeded functions select', () => { "all_user_messages": [], "username": "jsonuser", }, - { - "all_user_messages": [], - "username": "a", - }, - { - "all_user_messages": [], - "username": "b", - }, { "all_user_messages": [], "username": "dragarcia", @@ -398,14 +374,6 @@ describe('embeded functions select', () => { "setof_rows_one": null, "username": "jsonuser", }, - { - "setof_rows_one": null, - "username": "a", - }, - { - "setof_rows_one": null, - "username": "b", - }, { "setof_rows_one": null, "username": "dragarcia", @@ -470,20 +438,6 @@ describe('embeded functions select', () => { }, "username": "jsonuser", }, - { - "returns_row": { - "id": null, - "username": null, - }, - "username": "a", - }, - { - "returns_row": { - "id": null, - "username": null, - }, - "username": "b", - }, { "returns_row": { "id": null, @@ -588,18 +542,6 @@ describe('embeded functions select', () => { }, "username": "jsonuser", }, - { - "user_called_profile": { - "username": null, - }, - "username": "a", - }, - { - "user_called_profile": { - "username": null, - }, - "username": "b", - }, { "user_called_profile": { "username": null, @@ -746,14 +688,6 @@ describe('embeded functions select', () => { "user_messages": [], "username": "jsonuser", }, - { - "user_messages": [], - "username": "a", - }, - { - "user_messages": [], - "username": "b", - }, { "user_messages": [], "username": "dragarcia", @@ -827,14 +761,6 @@ describe('embeded functions select', () => { "active_user_messages": [], "username": "jsonuser", }, - { - "active_user_messages": [], - "username": "a", - }, - { - "active_user_messages": [], - "username": "b", - }, { "active_user_messages": [], "username": "dragarcia", @@ -912,14 +838,6 @@ describe('embeded functions select', () => { "recent_messages": [], "username": "jsonuser", }, - { - "recent_messages": [], - "username": "a", - }, - { - "recent_messages": [], - "username": "b", - }, { "recent_messages": [], "username": "dragarcia", @@ -993,14 +911,6 @@ describe('embeded functions select', () => { "recent_messages": [], "username": "jsonuser", }, - { - "recent_messages": [], - "username": "a", - }, - { - "recent_messages": [], - "username": "b", - }, { "recent_messages": [], "username": "dragarcia", @@ -1072,14 +982,6 @@ describe('embeded functions select', () => { "user_messages": [], "username": "jsonuser", }, - { - "user_messages": [], - "username": "a", - }, - { - "user_messages": [], - "username": "b", - }, { "user_messages": [], "username": "dragarcia", @@ -1240,20 +1142,6 @@ describe('embeded functions select', () => { }, "username": "jsonuser", }, - { - "profile": { - "id": null, - "username": null, - }, - "username": "a", - }, - { - "profile": { - "id": null, - "username": null, - }, - "username": "b", - }, { "profile": { "id": null, @@ -1310,14 +1198,6 @@ describe('embeded functions select', () => { "profile": null, "username": "jsonuser", }, - { - "profile": null, - "username": "a", - }, - { - "profile": null, - "username": "b", - }, { "profile": null, "username": "dragarcia", diff --git a/packages/core/postgrest-js/test/filters.test.ts b/packages/core/postgrest-js/test/filters.test.ts index 9f5cf6cfade..5fcb9e69ec6 100644 --- a/packages/core/postgrest-js/test/filters.test.ts +++ b/packages/core/postgrest-js/test/filters.test.ts @@ -21,12 +21,6 @@ test('not', async () => { { "status": "ONLINE", }, - { - "status": "ONLINE", - }, - { - "status": "ONLINE", - }, ], "error": null, "status": 200, @@ -92,12 +86,6 @@ test('neq', async () => { { "username": "jsonuser", }, - { - "username": "a", - }, - { - "username": "b", - }, { "username": "dragarcia", }, @@ -327,12 +315,6 @@ test('is', async () => { { "data": null, }, - { - "data": null, - }, - { - "data": null, - }, ], "error": null, "status": 200, @@ -363,12 +345,6 @@ test('in', async () => { { "status": "ONLINE", }, - { - "status": "ONLINE", - }, - { - "status": "ONLINE", - }, ], "error": null, "status": 200, diff --git a/packages/core/postgrest-js/test/resource-embedding.test.ts b/packages/core/postgrest-js/test/resource-embedding.test.ts index 10cfa296705..554442a9548 100644 --- a/packages/core/postgrest-js/test/resource-embedding.test.ts +++ b/packages/core/postgrest-js/test/resource-embedding.test.ts @@ -50,12 +50,6 @@ test('embedded select', async () => { { "messages": [], }, - { - "messages": [], - }, - { - "messages": [], - }, ], "error": null, "status": 200, @@ -130,12 +124,6 @@ test('embedded select with computed field explicit selection', async () => { { "messages": [], }, - { - "messages": [], - }, - { - "messages": [], - }, ], "error": null, "status": 200, @@ -198,12 +186,6 @@ describe('embedded filters', () => { { "messages": [], }, - { - "messages": [], - }, - { - "messages": [], - }, ], "error": null, "status": 200, @@ -269,12 +251,6 @@ describe('embedded filters', () => { { "messages": [], }, - { - "messages": [], - }, - { - "messages": [], - }, ], "error": null, "status": 200, @@ -342,12 +318,6 @@ describe('embedded filters', () => { { "messages": [], }, - { - "messages": [], - }, - { - "messages": [], - }, ], "error": null, "status": 200, @@ -423,12 +393,6 @@ describe('embedded transforms', () => { { "messages": [], }, - { - "messages": [], - }, - { - "messages": [], - }, ], "error": null, "status": 200, @@ -503,12 +467,6 @@ describe('embedded transforms', () => { { "messages": [], }, - { - "messages": [], - }, - { - "messages": [], - }, ], "error": null, "status": 200, @@ -568,12 +526,6 @@ describe('embedded transforms', () => { { "messages": [], }, - { - "messages": [], - }, - { - "messages": [], - }, ], "error": null, "status": 200, @@ -633,12 +585,6 @@ describe('embedded transforms', () => { { "messages": [], }, - { - "messages": [], - }, - { - "messages": [], - }, ], "error": null, "status": 200, diff --git a/packages/core/postgrest-js/test/retry.test.ts b/packages/core/postgrest-js/test/retry.test.ts index 346322d5454..4298f06d0e3 100644 --- a/packages/core/postgrest-js/test/retry.test.ts +++ b/packages/core/postgrest-js/test/retry.test.ts @@ -371,13 +371,14 @@ describe('Automatic Retries', () => { }) it('should drain the response body before retrying', async () => { - const cancelMock = jest.fn().mockResolvedValue(undefined) - const pgrst002Body = JSON.stringify({ - code: 'PGRST002', - details: null, - hint: null, - message: 'err', - }) + const textMock = jest.fn().mockResolvedValue( + JSON.stringify({ + code: 'PGRST002', + details: null, + hint: null, + message: 'err', + }) + ) fetchMock .mockResolvedValueOnce({ @@ -385,8 +386,7 @@ describe('Automatic Retries', () => { status: 503, statusText: 'Service Unavailable', headers: new Headers({ 'Retry-After': '0', 'Content-Type': 'application/json' }), - text: () => Promise.resolve(pgrst002Body), - body: { cancel: cancelMock }, + text: textMock, }) .mockResolvedValueOnce({ ok: true, @@ -399,7 +399,8 @@ describe('Automatic Retries', () => { const client = new PostgrestClient('http://localhost:3000', { fetch: fetchMock }) await runWithTimers(client.from('users').select()) - expect(cancelMock).toHaveBeenCalledTimes(1) + // text() is called once to drain body before retry, once by processResponse for the retry + expect(textMock).toHaveBeenCalledTimes(1) }) }) diff --git a/packages/core/postgrest-js/test/transforms.test.ts b/packages/core/postgrest-js/test/transforms.test.ts index c8c000d6d56..afc08aca54c 100644 --- a/packages/core/postgrest-js/test/transforms.test.ts +++ b/packages/core/postgrest-js/test/transforms.test.ts @@ -46,13 +46,6 @@ test('order', async () => { "status": "ONLINE", "username": "dragarcia", }, - { - "age_range": null, - "catchphrase": null, - "data": null, - "status": "ONLINE", - "username": "b", - }, { "age_range": "[25,35)", "catchphrase": "'bat' 'rat'", @@ -60,13 +53,6 @@ test('order', async () => { "status": "ONLINE", "username": "awailas", }, - { - "age_range": null, - "catchphrase": null, - "data": null, - "status": "ONLINE", - "username": "a", - }, ], "error": null, "status": 200, @@ -241,13 +227,13 @@ test('maybeSingle', async () => { "count": null, "data": null, "error": { - "code": "23505", - "details": "Key (username)=(a) already exists.", + "code": "PGRST116", + "details": "Results contain 2 rows, application/vnd.pgrst.object+json requires 1 row", "hint": null, - "message": "duplicate key value violates unique constraint "users_pkey"", + "message": "JSON object requested, multiple (or no) rows returned", }, - "status": 409, - "statusText": "Conflict", + "status": 406, + "statusText": "Not Acceptable", } `) }) @@ -300,9 +286,9 @@ test('csv', async () => { kiwicopple,,"[25,35)",OFFLINE,"'bat' 'cat'" awailas,,"[25,35)",ONLINE,"'bat' 'rat'" jsonuser,"{""foo"": {""bar"": {""nested"": ""value""}, ""baz"": ""string value""}}","[20,30)",ONLINE,"'json' 'test'" + dragarcia,,"[20,30)",ONLINE,"'fat' 'rat'" a,,,ONLINE, - b,,,ONLINE, - dragarcia,,"[20,30)",ONLINE,"'fat' 'rat'"", + b,,,ONLINE,", "error": null, "status": 200, "statusText": "OK", diff --git a/packages/core/supabase-js/test/unit/SupabaseClient.test.ts b/packages/core/supabase-js/test/unit/SupabaseClient.test.ts index fe35dcf40f4..1dbb997f8f3 100644 --- a/packages/core/supabase-js/test/unit/SupabaseClient.test.ts +++ b/packages/core/supabase-js/test/unit/SupabaseClient.test.ts @@ -385,6 +385,10 @@ describe('SupabaseClient', () => { const expectedToken = 'test-fetch-token' const mockFetch = jest.fn().mockResolvedValue({ ok: true, + status: 200, + statusText: 'OK', + text: () => Promise.resolve('{}'), + headers: new Headers(), json: () => Promise.resolve({}), }) @@ -408,7 +412,14 @@ describe('SupabaseClient', () => { const expectedToken = 'test-multi-service-token' const mockFetch = jest .fn() - .mockResolvedValueOnce({ ok: true, json: () => Promise.resolve({}) }) // rest + .mockResolvedValueOnce({ + ok: true, + status: 200, + statusText: 'OK', + text: () => Promise.resolve('{}'), + headers: new Headers(), + json: () => Promise.resolve({}), + }) // rest .mockResolvedValueOnce({ ok: true, json: () => Promise.resolve({ data: [] }) }) // storage .mockResolvedValueOnce({ ok: true, @@ -438,6 +449,10 @@ describe('SupabaseClient', () => { test('should use supabaseKey fallback in fetchWithAuth', async () => { const mockFetch = jest.fn().mockResolvedValue({ ok: true, + status: 200, + statusText: 'OK', + text: () => Promise.resolve('{}'), + headers: new Headers(), json: () => Promise.resolve({}), }) From 68e98cf181d40b8e325d4c49aa39ab10e8703a31 Mon Sep 17 00:00:00 2001 From: Katerina Skroumpelou Date: Fri, 27 Mar 2026 17:31:41 +0200 Subject: [PATCH 07/12] fix(postgrest): update snaps --- .../postgrest-js/test/advanced_rpc.test.ts | 89 ++++++++++++++----- 1 file changed, 68 insertions(+), 21 deletions(-) diff --git a/packages/core/postgrest-js/test/advanced_rpc.test.ts b/packages/core/postgrest-js/test/advanced_rpc.test.ts index d2c1880d42b..7b126981654 100644 --- a/packages/core/postgrest-js/test/advanced_rpc.test.ts +++ b/packages/core/postgrest-js/test/advanced_rpc.test.ts @@ -120,6 +120,13 @@ describe('advanced rpc', () => { "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", "username": "supabot", }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, { "channel_id": 3, "data": null, @@ -192,6 +199,13 @@ describe('advanced rpc', () => { "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", "username": "supabot", }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, { "channel_id": 3, "data": null, @@ -240,6 +254,13 @@ describe('advanced rpc', () => { "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", "username": "supabot", }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, { "channel_id": 3, "data": null, @@ -288,6 +309,13 @@ describe('advanced rpc', () => { "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", "username": "supabot", }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, { "channel_id": 3, "data": null, @@ -328,6 +356,13 @@ describe('advanced rpc', () => { "message": "Some message on channel without details", "username": "supabot", }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, { "channel_id": 2, "data": null, @@ -369,6 +404,13 @@ describe('advanced rpc', () => { "message": "Some message on channel without details", "username": "supabot", }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, { "channel_id": 2, "data": null, @@ -405,6 +447,14 @@ describe('advanced rpc', () => { { "count": null, "data": [ + { + "channel_id": 3, + "message": "Some message on channel without details", + "users": { + "catchphrase": "'cat' 'fat'", + "username": "supabot", + }, + }, { "channel_id": 3, "message": "Some message on channel without details", @@ -673,6 +723,13 @@ describe('advanced rpc', () => { "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", "username": "supabot", }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, { "channel_id": 3, "data": null, @@ -1101,6 +1158,13 @@ describe('advanced rpc', () => { "status": "ONLINE", "username": "awailas", }, + { + "age_range": "[20,30)", + "catchphrase": "'fat' 'rat'", + "data": null, + "status": "ONLINE", + "username": "dragarcia", + }, { "age_range": "[20,30)", "catchphrase": "'json' 'test'", @@ -1115,27 +1179,6 @@ describe('advanced rpc', () => { "status": "ONLINE", "username": "jsonuser", }, - { - "age_range": null, - "catchphrase": null, - "data": null, - "status": "ONLINE", - "username": "a", - }, - { - "age_range": null, - "catchphrase": null, - "data": null, - "status": "ONLINE", - "username": "b", - }, - { - "age_range": "[20,30)", - "catchphrase": "'fat' 'rat'", - "data": null, - "status": "ONLINE", - "username": "dragarcia", - }, ], "error": null, "status": 200, @@ -1289,6 +1332,10 @@ test('RPC call with subselect and computed field', async () => { "blurb_message": "Som", "message": "Some message on channel without details", }, + { + "blurb_message": "Som", + "message": "Some message on channel without details", + }, ], "error": null, "status": 200, From 6eca983eb6b27ff8b99bc905716fe640f5334eb6 Mon Sep 17 00:00:00 2001 From: Katerina Skroumpelou Date: Mon, 30 Mar 2026 14:53:49 +0300 Subject: [PATCH 08/12] feat(postgrest): restore x-retry-count header --- .../core/postgrest-js/src/PostgrestBuilder.ts | 3 + .../postgrest-js/src/PostgrestQueryBuilder.ts | 17 +- .../postgrest-js/test/advanced_rpc.test.ts | 1130 +++++++++-------- packages/core/postgrest-js/test/retry.test.ts | 9 + packages/core/supabase-js/src/cors.ts | 9 +- .../core/supabase-js/test/unit/cors.test.ts | 1 + 6 files changed, 630 insertions(+), 539 deletions(-) diff --git a/packages/core/postgrest-js/src/PostgrestBuilder.ts b/packages/core/postgrest-js/src/PostgrestBuilder.ts index b08c6f05aea..7805eb210b7 100644 --- a/packages/core/postgrest-js/src/PostgrestBuilder.ts +++ b/packages/core/postgrest-js/src/PostgrestBuilder.ts @@ -223,6 +223,9 @@ export default abstract class PostgrestBuilder< while (true) { const requestHeaders = new Headers(this.headers) + if (attemptCount > 0) { + requestHeaders.set('X-Retry-Count', String(attemptCount)) + } try { const res = await _fetch(this.url.toString(), { diff --git a/packages/core/postgrest-js/src/PostgrestQueryBuilder.ts b/packages/core/postgrest-js/src/PostgrestQueryBuilder.ts index 0f2cbb4f5d7..3c890979b92 100644 --- a/packages/core/postgrest-js/src/PostgrestQueryBuilder.ts +++ b/packages/core/postgrest-js/src/PostgrestQueryBuilder.ts @@ -22,7 +22,12 @@ export default class PostgrestQueryBuilder< fetch?: Fetch urlLengthLimit: number - // Retry configuration + /** + * Enable or disable automatic retries for transient errors. + * When enabled, idempotent requests (GET/HEAD/OPTIONS) that fail with 520 or 503 errors + * are automatically retried with exponential backoff (up to 3 attempts). + * Defaults to `true` when not specified. + */ retry?: boolean /** @@ -30,13 +35,21 @@ export default class PostgrestQueryBuilder< * * @category Database * + * @param url - The URL for the query + * @param options - Named parameters + * @param options.headers - Custom headers + * @param options.schema - Postgres schema to use + * @param options.fetch - Custom fetch implementation + * @param options.urlLengthLimit - Maximum URL length before warning + * @param options.retry - Enable automatic retries for transient errors (default: true) + * * @example Creating a Postgrest query builder * ```ts * import { PostgrestQueryBuilder } from '@supabase/postgrest-js' * * const query = new PostgrestQueryBuilder( * new URL('https://xyzcompany.supabase.co/rest/v1/users'), - * { headers: { apikey: 'public-anon-key' } } + * { headers: { apikey: 'public-anon-key' }, retry: true } * ) * ``` */ diff --git a/packages/core/postgrest-js/test/advanced_rpc.test.ts b/packages/core/postgrest-js/test/advanced_rpc.test.ts index 7b126981654..9ffa20f82c1 100644 --- a/packages/core/postgrest-js/test/advanced_rpc.test.ts +++ b/packages/core/postgrest-js/test/advanced_rpc.test.ts @@ -71,18 +71,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": [ - { - "channel_id": 1, - "data": null, - "id": 1, - "message": "Hello World 👋", - "username": "supabot", - }, - ], - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) ExpectedSchema.parse(res.data) @@ -105,39 +107,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": [ - { - "channel_id": 1, - "data": null, - "id": 1, - "message": "Hello World 👋", - "username": "supabot", - }, - { - "channel_id": 2, - "data": null, - "id": 2, - "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", - "username": "supabot", - }, - { - "channel_id": 3, - "data": null, - "id": 3, - "message": "Some message on channel without details", - "username": "supabot", - }, - { - "channel_id": 3, - "data": null, - "id": 4, - "message": "Some message on channel without details", - "username": "supabot", - }, - ], - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) ExpectedSchema.parse(res.data) @@ -159,13 +142,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": { - "id": 1, - "username": "supabot", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", }, - "error": null, - "status": 200, - "statusText": "OK", + "status": 0, + "statusText": "", } `) UserProfileSchema.parse(res.data) @@ -184,39 +174,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": [ - { - "channel_id": 1, - "data": null, - "id": 1, - "message": "Hello World 👋", - "username": "supabot", - }, - { - "channel_id": 2, - "data": null, - "id": 2, - "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", - "username": "supabot", - }, - { - "channel_id": 3, - "data": null, - "id": 3, - "message": "Some message on channel without details", - "username": "supabot", - }, - { - "channel_id": 3, - "data": null, - "id": 4, - "message": "Some message on channel without details", - "username": "supabot", - }, - ], - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) ExpectedSchema.parse(res.data) @@ -239,39 +210,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": [ - { - "channel_id": 1, - "data": null, - "id": 1, - "message": "Hello World 👋", - "username": "supabot", - }, - { - "channel_id": 2, - "data": null, - "id": 2, - "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", - "username": "supabot", - }, - { - "channel_id": 3, - "data": null, - "id": 3, - "message": "Some message on channel without details", - "username": "supabot", - }, - { - "channel_id": 3, - "data": null, - "id": 4, - "message": "Some message on channel without details", - "username": "supabot", - }, - ], - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) ExpectedSchema.parse(res.data) @@ -294,39 +246,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": [ - { - "channel_id": 1, - "data": null, - "id": 1, - "message": "Hello World 👋", - "username": "supabot", - }, - { - "channel_id": 2, - "data": null, - "id": 2, - "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", - "username": "supabot", - }, - { - "channel_id": 3, - "data": null, - "id": 3, - "message": "Some message on channel without details", - "username": "supabot", - }, - { - "channel_id": 3, - "data": null, - "id": 4, - "message": "Some message on channel without details", - "username": "supabot", - }, - ], - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) ExpectedSchema.parse(res.data) @@ -348,39 +281,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": [ - { - "channel_id": 3, - "data": null, - "id": 4, - "message": "Some message on channel without details", - "username": "supabot", - }, - { - "channel_id": 3, - "data": null, - "id": 3, - "message": "Some message on channel without details", - "username": "supabot", - }, - { - "channel_id": 2, - "data": null, - "id": 2, - "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", - "username": "supabot", - }, - { - "channel_id": 1, - "data": null, - "id": 1, - "message": "Hello World 👋", - "username": "supabot", - }, - ], - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) RecentMessagesSchema.array().parse(res.data) @@ -396,39 +310,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": [ - { - "channel_id": 3, - "data": null, - "id": 4, - "message": "Some message on channel without details", - "username": "supabot", - }, - { - "channel_id": 3, - "data": null, - "id": 3, - "message": "Some message on channel without details", - "username": "supabot", - }, - { - "channel_id": 2, - "data": null, - "id": 2, - "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", - "username": "supabot", - }, - { - "channel_id": 1, - "data": null, - "id": 1, - "message": "Hello World 👋", - "username": "supabot", - }, - ], - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) RecentMessagesSchema.array().parse(res.data) @@ -446,43 +341,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": [ - { - "channel_id": 3, - "message": "Some message on channel without details", - "users": { - "catchphrase": "'cat' 'fat'", - "username": "supabot", - }, - }, - { - "channel_id": 3, - "message": "Some message on channel without details", - "users": { - "catchphrase": "'cat' 'fat'", - "username": "supabot", - }, - }, - { - "channel_id": 2, - "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", - "users": { - "catchphrase": "'cat' 'fat'", - "username": "supabot", - }, - }, - { - "channel_id": 1, - "message": "Hello World 👋", - "users": { - "catchphrase": "'cat' 'fat'", - "username": "supabot", - }, - }, - ], - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) SelectWithUsersSchema.array().parse(res.data) @@ -506,17 +378,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": { - "id": 1, - "username": "supabot", - "users": { - "catchphrase": "'cat' 'fat'", - "username": "supabot", - }, + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", }, - "error": null, - "status": 200, - "statusText": "OK", + "status": 0, + "statusText": "", } `) SelectWithUsersProfileSchema.parse(res.data) @@ -531,9 +406,19 @@ describe('advanced rpc', () => { { "count": null, "data": null, - "error": null, - "status": 204, - "statusText": "No Content", + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) }) @@ -551,13 +436,18 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "PGRST203", - "details": null, - "hint": "Try renaming the parameters or the function itself in the database so function overloading can be resolved", - "message": "Could not choose the best candidate function between: public.postgrest_unresolvable_function(a => integer), public.postgrest_unresolvable_function(a => text)", + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", }, - "status": 300, - "statusText": "Multiple Choices", + "status": 0, + "statusText": "", } `) }) @@ -575,13 +465,18 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "PGRST203", - "details": null, - "hint": "Try renaming the parameters or the function itself in the database so function overloading can be resolved", - "message": "Could not choose the best candidate function between: public.postgrest_unresolvable_function(a => integer), public.postgrest_unresolvable_function(a => text)", + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", }, - "status": 300, - "statusText": "Multiple Choices", + "status": 0, + "statusText": "", } `) }) @@ -595,9 +490,19 @@ describe('advanced rpc', () => { { "count": null, "data": null, - "error": null, - "status": 204, - "statusText": "No Content", + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) }) @@ -612,10 +517,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": 1, - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) }) @@ -630,10 +545,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": "foo", - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) }) @@ -648,15 +573,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": [ - { - "id": 1, - "username": "supabot", - }, - ], - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) UserProfileSchema.array().parse(res.data) @@ -674,18 +604,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": [ - { - "channel_id": 1, - "data": null, - "id": 1, - "message": "Hello World 👋", - "username": "supabot", - }, - ], - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) ExpectedSchema.parse(res.data) @@ -708,39 +640,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": [ - { - "channel_id": 1, - "data": null, - "id": 1, - "message": "Hello World 👋", - "username": "supabot", - }, - { - "channel_id": 2, - "data": null, - "id": 2, - "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", - "username": "supabot", - }, - { - "channel_id": 3, - "data": null, - "id": 3, - "message": "Some message on channel without details", - "username": "supabot", - }, - { - "channel_id": 3, - "data": null, - "id": 4, - "message": "Some message on channel without details", - "username": "supabot", - }, - ], - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) ExpectedSchema.parse(res.data) @@ -756,10 +669,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": "foo", - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) }) @@ -772,10 +695,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": "foo", - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) }) @@ -793,13 +726,18 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "PGRST202", - "details": "Searched for the function public.polymorphic_function_with_unnamed_integer with parameter or with a single unnamed json/jsonb parameter, but no matches were found in the schema cache.", - "hint": "Perhaps you meant to call the function public.polymorphic_function_with_unnamed_text", - "message": "Could not find the function public.polymorphic_function_with_unnamed_integer() in the schema cache", + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", }, - "status": 404, - "statusText": "Not Found", + "status": 0, + "statusText": "", } `) }) @@ -814,10 +752,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": 1, - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) }) @@ -832,10 +780,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": 1, - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) }) @@ -850,10 +808,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": 1, - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) }) @@ -866,10 +834,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": 1, - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) }) @@ -884,10 +862,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": "foo", - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) }) @@ -902,10 +890,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": "foo", - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) }) @@ -920,13 +918,18 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "PGRST203", - "details": null, - "hint": "Try renaming the parameters or the function itself in the database so function overloading can be resolved", - "message": "Could not choose the best candidate function between: public.polymorphic_function_with_unnamed_default(), public.polymorphic_function_with_unnamed_default( => text)", + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", }, - "status": 300, - "statusText": "Multiple Choices", + "status": 0, + "statusText": "", } `) }) @@ -943,13 +946,18 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "PGRST203", - "details": null, - "hint": "Try renaming the parameters or the function itself in the database so function overloading can be resolved", - "message": "Could not choose the best candidate function between: public.polymorphic_function_with_unnamed_default(), public.polymorphic_function_with_unnamed_default( => text)", + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", }, - "status": 300, - "statusText": "Multiple Choices", + "status": 0, + "statusText": "", } `) }) @@ -964,10 +972,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": "foo", - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) }) @@ -982,13 +1000,18 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "PGRST203", - "details": null, - "hint": "Try renaming the parameters or the function itself in the database so function overloading can be resolved", - "message": "Could not choose the best candidate function between: public.polymorphic_function_with_unnamed_default_overload(), public.polymorphic_function_with_unnamed_default_overload( => text)", + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", }, - "status": 300, - "statusText": "Multiple Choices", + "status": 0, + "statusText": "", } `) }) @@ -1005,13 +1028,18 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "PGRST203", - "details": null, - "hint": "Try renaming the parameters or the function itself in the database so function overloading can be resolved", - "message": "Could not choose the best candidate function between: public.polymorphic_function_with_unnamed_default_overload(), public.polymorphic_function_with_unnamed_default_overload( => text)", + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", }, - "status": 300, - "statusText": "Multiple Choices", + "status": 0, + "statusText": "", } `) }) @@ -1026,10 +1054,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": "foo", - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) }) @@ -1045,10 +1083,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": "foo", - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) }) @@ -1063,13 +1111,18 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "PGRST202", - "details": "Searched for the function public.blurb_message without parameters or with a single unnamed json/jsonb parameter, but no matches were found in the schema cache.", - "hint": "Perhaps you meant to call the function public.get_messages", - "message": "Could not find the function public.blurb_message without parameters in the schema cache", + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", }, - "status": 404, - "statusText": "Not Found", + "status": 0, + "statusText": "", } `) }) @@ -1093,13 +1146,18 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "PGRST202", - "details": "Searched for the function public.blurb_message with parameter or with a single unnamed json/jsonb parameter, but no matches were found in the schema cache.", - "hint": "Perhaps you meant to call the function public.get_messages", - "message": "Could not find the function public.blurb_message() in the schema cache", + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", }, - "status": 404, - "statusText": "Not Found", + "status": 0, + "statusText": "", } `) }) @@ -1112,16 +1170,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": { - "age_range": "[1,2)", - "catchphrase": "'cat' 'fat'", - "data": null, - "status": "ONLINE", - "username": "supabot", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", }, - "error": null, - "status": 200, - "statusText": "OK", + "status": 0, + "statusText": "", } `) FunctionReturningRowSchema.parse(res.data) @@ -1136,53 +1198,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": [ - { - "age_range": "[1,2)", - "catchphrase": "'cat' 'fat'", - "data": null, - "status": "ONLINE", - "username": "supabot", - }, - { - "age_range": "[25,35)", - "catchphrase": "'bat' 'cat'", - "data": null, - "status": "OFFLINE", - "username": "kiwicopple", - }, - { - "age_range": "[25,35)", - "catchphrase": "'bat' 'rat'", - "data": null, - "status": "ONLINE", - "username": "awailas", - }, - { - "age_range": "[20,30)", - "catchphrase": "'fat' 'rat'", - "data": null, - "status": "ONLINE", - "username": "dragarcia", - }, - { - "age_range": "[20,30)", - "catchphrase": "'json' 'test'", - "data": { - "foo": { - "bar": { - "nested": "value", - }, - "baz": "string value", - }, - }, - "status": "ONLINE", - "username": "jsonuser", - }, - ], - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) ExpectedSchema.parse(res.data) @@ -1205,15 +1234,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": [ - { - "id": 1, - "username": "supabot", - }, - ], - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) ExpectedSchema.parse(res.data) @@ -1235,13 +1269,20 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": { - "id": 1, - "username": "supabot", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", }, - "error": null, - "status": 200, - "statusText": "OK", + "status": 0, + "statusText": "", } `) UserProfileSchema.parse(res.data) @@ -1261,9 +1302,19 @@ test('should be able to filter before and after select rpc', async () => { { "count": null, "data": null, - "error": null, - "status": 200, - "statusText": "OK", + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) const res2 = await postgrest @@ -1279,9 +1330,19 @@ test('should be able to filter before and after select rpc', async () => { { "count": null, "data": null, - "error": null, - "status": 200, - "statusText": "OK", + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) const res3 = await postgrest @@ -1296,16 +1357,20 @@ test('should be able to filter before and after select rpc', async () => { expect(res3).toMatchInlineSnapshot(` { "count": null, - "data": { - "username": "supabot", - "users": { - "catchphrase": "'cat' 'fat'", - "username": "supabot", - }, + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", }, - "error": null, - "status": 200, - "statusText": "OK", + "status": 0, + "statusText": "", } `) }) @@ -1319,27 +1384,20 @@ test('RPC call with subselect and computed field', async () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": [ - { - "blurb_message": "Hel", - "message": "Hello World 👋", - }, - { - "blurb_message": "Per", - "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", - }, - { - "blurb_message": "Som", - "message": "Some message on channel without details", - }, - { - "blurb_message": "Som", - "message": "Some message on channel without details", - }, - ], - "error": null, - "status": 200, - "statusText": "OK", + "data": null, + "error": { + "code": "", + "details": "TypeError: fetch failed + + Caused by: AggregateError: (ECONNREFUSED) + AggregateError + at internalConnectMultiple (node:net:1122:18) + at afterConnectMultiple (node:net:1689:7)", + "hint": "", + "message": "TypeError: fetch failed", + }, + "status": 0, + "statusText": "", } `) let result: Exclude diff --git a/packages/core/postgrest-js/test/retry.test.ts b/packages/core/postgrest-js/test/retry.test.ts index 4298f06d0e3..e308e4c52f1 100644 --- a/packages/core/postgrest-js/test/retry.test.ts +++ b/packages/core/postgrest-js/test/retry.test.ts @@ -71,6 +71,15 @@ describe('Automatic Retries', () => { expect(result.error).toBeNull() expect(result.data).toEqual([{ id: 1, name: 'test' }]) expect(fetchMock).toHaveBeenCalledTimes(3) + + // Verify X-Retry-Count header was sent on retries + const [, firstRetryCall] = fetchMock.mock.calls + const firstRetryHeaders = firstRetryCall[1].headers as Headers + expect(firstRetryHeaders.get('X-Retry-Count')).toBe('1') + + const [, , secondRetryCall] = fetchMock.mock.calls + const secondRetryHeaders = secondRetryCall[1].headers as Headers + expect(secondRetryHeaders.get('X-Retry-Count')).toBe('2') }) it('should NOT retry POST requests even on 520 errors', async () => { diff --git a/packages/core/supabase-js/src/cors.ts b/packages/core/supabase-js/src/cors.ts index 2427e7f9b0d..54278ee0681 100644 --- a/packages/core/supabase-js/src/cors.ts +++ b/packages/core/supabase-js/src/cors.ts @@ -33,8 +33,15 @@ * - x-client-info: Library version information * - apikey: Project API key * - content-type: Standard HTTP content type + * - x-retry-count: Retry attempt number sent by postgrest-js on retried requests */ -const SUPABASE_HEADERS = ['authorization', 'x-client-info', 'apikey', 'content-type'].join(', ') +const SUPABASE_HEADERS = [ + 'authorization', + 'x-client-info', + 'apikey', + 'content-type', + 'x-retry-count', +].join(', ') /** * All HTTP methods used by the Supabase SDK diff --git a/packages/core/supabase-js/test/unit/cors.test.ts b/packages/core/supabase-js/test/unit/cors.test.ts index bcb1fc4c483..3a923c824cb 100644 --- a/packages/core/supabase-js/test/unit/cors.test.ts +++ b/packages/core/supabase-js/test/unit/cors.test.ts @@ -21,6 +21,7 @@ describe('CORS Module', () => { expect(allowedHeaders).toContain('x-client-info') expect(allowedHeaders).toContain('apikey') expect(allowedHeaders).toContain('content-type') + expect(allowedHeaders).toContain('x-retry-count') }) it('should include all HTTP methods including OPTIONS', () => { From 21a0b5bc14535a0337c162f073991c4fc138734e Mon Sep 17 00:00:00 2001 From: Katerina Skroumpelou Date: Mon, 30 Mar 2026 15:13:29 +0300 Subject: [PATCH 09/12] fix(postgrest): update snapshots --- .../postgrest-js/test/advanced_rpc.test.ts | 1130 ++++++++--------- 1 file changed, 536 insertions(+), 594 deletions(-) diff --git a/packages/core/postgrest-js/test/advanced_rpc.test.ts b/packages/core/postgrest-js/test/advanced_rpc.test.ts index 9ffa20f82c1..7b126981654 100644 --- a/packages/core/postgrest-js/test/advanced_rpc.test.ts +++ b/packages/core/postgrest-js/test/advanced_rpc.test.ts @@ -71,20 +71,18 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": [ + { + "channel_id": 1, + "data": null, + "id": 1, + "message": "Hello World 👋", + "username": "supabot", + }, + ], + "error": null, + "status": 200, + "statusText": "OK", } `) ExpectedSchema.parse(res.data) @@ -107,20 +105,39 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": [ + { + "channel_id": 1, + "data": null, + "id": 1, + "message": "Hello World 👋", + "username": "supabot", + }, + { + "channel_id": 2, + "data": null, + "id": 2, + "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", + "username": "supabot", + }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, + { + "channel_id": 3, + "data": null, + "id": 4, + "message": "Some message on channel without details", + "username": "supabot", + }, + ], + "error": null, + "status": 200, + "statusText": "OK", } `) ExpectedSchema.parse(res.data) @@ -142,20 +159,13 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", + "data": { + "id": 1, + "username": "supabot", }, - "status": 0, - "statusText": "", + "error": null, + "status": 200, + "statusText": "OK", } `) UserProfileSchema.parse(res.data) @@ -174,20 +184,39 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": [ + { + "channel_id": 1, + "data": null, + "id": 1, + "message": "Hello World 👋", + "username": "supabot", + }, + { + "channel_id": 2, + "data": null, + "id": 2, + "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", + "username": "supabot", + }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, + { + "channel_id": 3, + "data": null, + "id": 4, + "message": "Some message on channel without details", + "username": "supabot", + }, + ], + "error": null, + "status": 200, + "statusText": "OK", } `) ExpectedSchema.parse(res.data) @@ -210,20 +239,39 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": [ + { + "channel_id": 1, + "data": null, + "id": 1, + "message": "Hello World 👋", + "username": "supabot", + }, + { + "channel_id": 2, + "data": null, + "id": 2, + "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", + "username": "supabot", + }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, + { + "channel_id": 3, + "data": null, + "id": 4, + "message": "Some message on channel without details", + "username": "supabot", + }, + ], + "error": null, + "status": 200, + "statusText": "OK", } `) ExpectedSchema.parse(res.data) @@ -246,20 +294,39 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": [ + { + "channel_id": 1, + "data": null, + "id": 1, + "message": "Hello World 👋", + "username": "supabot", + }, + { + "channel_id": 2, + "data": null, + "id": 2, + "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", + "username": "supabot", + }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, + { + "channel_id": 3, + "data": null, + "id": 4, + "message": "Some message on channel without details", + "username": "supabot", + }, + ], + "error": null, + "status": 200, + "statusText": "OK", } `) ExpectedSchema.parse(res.data) @@ -281,20 +348,39 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": [ + { + "channel_id": 3, + "data": null, + "id": 4, + "message": "Some message on channel without details", + "username": "supabot", + }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, + { + "channel_id": 2, + "data": null, + "id": 2, + "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", + "username": "supabot", + }, + { + "channel_id": 1, + "data": null, + "id": 1, + "message": "Hello World 👋", + "username": "supabot", + }, + ], + "error": null, + "status": 200, + "statusText": "OK", } `) RecentMessagesSchema.array().parse(res.data) @@ -310,20 +396,39 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": [ + { + "channel_id": 3, + "data": null, + "id": 4, + "message": "Some message on channel without details", + "username": "supabot", + }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, + { + "channel_id": 2, + "data": null, + "id": 2, + "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", + "username": "supabot", + }, + { + "channel_id": 1, + "data": null, + "id": 1, + "message": "Hello World 👋", + "username": "supabot", + }, + ], + "error": null, + "status": 200, + "statusText": "OK", } `) RecentMessagesSchema.array().parse(res.data) @@ -341,20 +446,43 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": [ + { + "channel_id": 3, + "message": "Some message on channel without details", + "users": { + "catchphrase": "'cat' 'fat'", + "username": "supabot", + }, + }, + { + "channel_id": 3, + "message": "Some message on channel without details", + "users": { + "catchphrase": "'cat' 'fat'", + "username": "supabot", + }, + }, + { + "channel_id": 2, + "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", + "users": { + "catchphrase": "'cat' 'fat'", + "username": "supabot", + }, + }, + { + "channel_id": 1, + "message": "Hello World 👋", + "users": { + "catchphrase": "'cat' 'fat'", + "username": "supabot", + }, + }, + ], + "error": null, + "status": 200, + "statusText": "OK", } `) SelectWithUsersSchema.array().parse(res.data) @@ -378,20 +506,17 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", + "data": { + "id": 1, + "username": "supabot", + "users": { + "catchphrase": "'cat' 'fat'", + "username": "supabot", + }, }, - "status": 0, - "statusText": "", + "error": null, + "status": 200, + "statusText": "OK", } `) SelectWithUsersProfileSchema.parse(res.data) @@ -406,19 +531,9 @@ describe('advanced rpc', () => { { "count": null, "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "error": null, + "status": 204, + "statusText": "No Content", } `) }) @@ -436,18 +551,13 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", + "code": "PGRST203", + "details": null, + "hint": "Try renaming the parameters or the function itself in the database so function overloading can be resolved", + "message": "Could not choose the best candidate function between: public.postgrest_unresolvable_function(a => integer), public.postgrest_unresolvable_function(a => text)", }, - "status": 0, - "statusText": "", + "status": 300, + "statusText": "Multiple Choices", } `) }) @@ -465,18 +575,13 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", + "code": "PGRST203", + "details": null, + "hint": "Try renaming the parameters or the function itself in the database so function overloading can be resolved", + "message": "Could not choose the best candidate function between: public.postgrest_unresolvable_function(a => integer), public.postgrest_unresolvable_function(a => text)", }, - "status": 0, - "statusText": "", + "status": 300, + "statusText": "Multiple Choices", } `) }) @@ -490,19 +595,9 @@ describe('advanced rpc', () => { { "count": null, "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "error": null, + "status": 204, + "statusText": "No Content", } `) }) @@ -517,20 +612,10 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": 1, + "error": null, + "status": 200, + "statusText": "OK", } `) }) @@ -545,20 +630,10 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": "foo", + "error": null, + "status": 200, + "statusText": "OK", } `) }) @@ -573,20 +648,15 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": [ + { + "id": 1, + "username": "supabot", + }, + ], + "error": null, + "status": 200, + "statusText": "OK", } `) UserProfileSchema.array().parse(res.data) @@ -604,20 +674,18 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": [ + { + "channel_id": 1, + "data": null, + "id": 1, + "message": "Hello World 👋", + "username": "supabot", + }, + ], + "error": null, + "status": 200, + "statusText": "OK", } `) ExpectedSchema.parse(res.data) @@ -640,20 +708,39 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": [ + { + "channel_id": 1, + "data": null, + "id": 1, + "message": "Hello World 👋", + "username": "supabot", + }, + { + "channel_id": 2, + "data": null, + "id": 2, + "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", + "username": "supabot", + }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, + { + "channel_id": 3, + "data": null, + "id": 4, + "message": "Some message on channel without details", + "username": "supabot", + }, + ], + "error": null, + "status": 200, + "statusText": "OK", } `) ExpectedSchema.parse(res.data) @@ -669,20 +756,10 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": "foo", + "error": null, + "status": 200, + "statusText": "OK", } `) }) @@ -695,20 +772,10 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": "foo", + "error": null, + "status": 200, + "statusText": "OK", } `) }) @@ -726,18 +793,13 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", + "code": "PGRST202", + "details": "Searched for the function public.polymorphic_function_with_unnamed_integer with parameter or with a single unnamed json/jsonb parameter, but no matches were found in the schema cache.", + "hint": "Perhaps you meant to call the function public.polymorphic_function_with_unnamed_text", + "message": "Could not find the function public.polymorphic_function_with_unnamed_integer() in the schema cache", }, - "status": 0, - "statusText": "", + "status": 404, + "statusText": "Not Found", } `) }) @@ -752,20 +814,10 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": 1, + "error": null, + "status": 200, + "statusText": "OK", } `) }) @@ -780,20 +832,10 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": 1, + "error": null, + "status": 200, + "statusText": "OK", } `) }) @@ -808,20 +850,10 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": 1, + "error": null, + "status": 200, + "statusText": "OK", } `) }) @@ -834,20 +866,10 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": 1, + "error": null, + "status": 200, + "statusText": "OK", } `) }) @@ -862,20 +884,10 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": "foo", + "error": null, + "status": 200, + "statusText": "OK", } `) }) @@ -890,20 +902,10 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": "foo", + "error": null, + "status": 200, + "statusText": "OK", } `) }) @@ -918,18 +920,13 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", + "code": "PGRST203", + "details": null, + "hint": "Try renaming the parameters or the function itself in the database so function overloading can be resolved", + "message": "Could not choose the best candidate function between: public.polymorphic_function_with_unnamed_default(), public.polymorphic_function_with_unnamed_default( => text)", }, - "status": 0, - "statusText": "", + "status": 300, + "statusText": "Multiple Choices", } `) }) @@ -946,18 +943,13 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", + "code": "PGRST203", + "details": null, + "hint": "Try renaming the parameters or the function itself in the database so function overloading can be resolved", + "message": "Could not choose the best candidate function between: public.polymorphic_function_with_unnamed_default(), public.polymorphic_function_with_unnamed_default( => text)", }, - "status": 0, - "statusText": "", + "status": 300, + "statusText": "Multiple Choices", } `) }) @@ -972,20 +964,10 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": "foo", + "error": null, + "status": 200, + "statusText": "OK", } `) }) @@ -1000,18 +982,13 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", + "code": "PGRST203", + "details": null, + "hint": "Try renaming the parameters or the function itself in the database so function overloading can be resolved", + "message": "Could not choose the best candidate function between: public.polymorphic_function_with_unnamed_default_overload(), public.polymorphic_function_with_unnamed_default_overload( => text)", }, - "status": 0, - "statusText": "", + "status": 300, + "statusText": "Multiple Choices", } `) }) @@ -1028,18 +1005,13 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", + "code": "PGRST203", + "details": null, + "hint": "Try renaming the parameters or the function itself in the database so function overloading can be resolved", + "message": "Could not choose the best candidate function between: public.polymorphic_function_with_unnamed_default_overload(), public.polymorphic_function_with_unnamed_default_overload( => text)", }, - "status": 0, - "statusText": "", + "status": 300, + "statusText": "Multiple Choices", } `) }) @@ -1054,20 +1026,10 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": "foo", + "error": null, + "status": 200, + "statusText": "OK", } `) }) @@ -1083,20 +1045,10 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": "foo", + "error": null, + "status": 200, + "statusText": "OK", } `) }) @@ -1111,18 +1063,13 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", + "code": "PGRST202", + "details": "Searched for the function public.blurb_message without parameters or with a single unnamed json/jsonb parameter, but no matches were found in the schema cache.", + "hint": "Perhaps you meant to call the function public.get_messages", + "message": "Could not find the function public.blurb_message without parameters in the schema cache", }, - "status": 0, - "statusText": "", + "status": 404, + "statusText": "Not Found", } `) }) @@ -1146,18 +1093,13 @@ describe('advanced rpc', () => { "count": null, "data": null, "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", + "code": "PGRST202", + "details": "Searched for the function public.blurb_message with parameter or with a single unnamed json/jsonb parameter, but no matches were found in the schema cache.", + "hint": "Perhaps you meant to call the function public.get_messages", + "message": "Could not find the function public.blurb_message() in the schema cache", }, - "status": 0, - "statusText": "", + "status": 404, + "statusText": "Not Found", } `) }) @@ -1170,20 +1112,16 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", + "data": { + "age_range": "[1,2)", + "catchphrase": "'cat' 'fat'", + "data": null, + "status": "ONLINE", + "username": "supabot", }, - "status": 0, - "statusText": "", + "error": null, + "status": 200, + "statusText": "OK", } `) FunctionReturningRowSchema.parse(res.data) @@ -1198,20 +1136,53 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": [ + { + "age_range": "[1,2)", + "catchphrase": "'cat' 'fat'", + "data": null, + "status": "ONLINE", + "username": "supabot", + }, + { + "age_range": "[25,35)", + "catchphrase": "'bat' 'cat'", + "data": null, + "status": "OFFLINE", + "username": "kiwicopple", + }, + { + "age_range": "[25,35)", + "catchphrase": "'bat' 'rat'", + "data": null, + "status": "ONLINE", + "username": "awailas", + }, + { + "age_range": "[20,30)", + "catchphrase": "'fat' 'rat'", + "data": null, + "status": "ONLINE", + "username": "dragarcia", + }, + { + "age_range": "[20,30)", + "catchphrase": "'json' 'test'", + "data": { + "foo": { + "bar": { + "nested": "value", + }, + "baz": "string value", + }, + }, + "status": "ONLINE", + "username": "jsonuser", + }, + ], + "error": null, + "status": 200, + "statusText": "OK", } `) ExpectedSchema.parse(res.data) @@ -1234,20 +1205,15 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": [ + { + "id": 1, + "username": "supabot", + }, + ], + "error": null, + "status": 200, + "statusText": "OK", } `) ExpectedSchema.parse(res.data) @@ -1269,20 +1235,13 @@ describe('advanced rpc', () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", + "data": { + "id": 1, + "username": "supabot", }, - "status": 0, - "statusText": "", + "error": null, + "status": 200, + "statusText": "OK", } `) UserProfileSchema.parse(res.data) @@ -1302,19 +1261,9 @@ test('should be able to filter before and after select rpc', async () => { { "count": null, "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "error": null, + "status": 200, + "statusText": "OK", } `) const res2 = await postgrest @@ -1330,19 +1279,9 @@ test('should be able to filter before and after select rpc', async () => { { "count": null, "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "error": null, + "status": 200, + "statusText": "OK", } `) const res3 = await postgrest @@ -1357,20 +1296,16 @@ test('should be able to filter before and after select rpc', async () => { expect(res3).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", + "data": { + "username": "supabot", + "users": { + "catchphrase": "'cat' 'fat'", + "username": "supabot", + }, }, - "status": 0, - "statusText": "", + "error": null, + "status": 200, + "statusText": "OK", } `) }) @@ -1384,20 +1319,27 @@ test('RPC call with subselect and computed field', async () => { expect(res).toMatchInlineSnapshot(` { "count": null, - "data": null, - "error": { - "code": "", - "details": "TypeError: fetch failed - - Caused by: AggregateError: (ECONNREFUSED) - AggregateError - at internalConnectMultiple (node:net:1122:18) - at afterConnectMultiple (node:net:1689:7)", - "hint": "", - "message": "TypeError: fetch failed", - }, - "status": 0, - "statusText": "", + "data": [ + { + "blurb_message": "Hel", + "message": "Hello World 👋", + }, + { + "blurb_message": "Per", + "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", + }, + { + "blurb_message": "Som", + "message": "Some message on channel without details", + }, + { + "blurb_message": "Som", + "message": "Some message on channel without details", + }, + ], + "error": null, + "status": 200, + "statusText": "OK", } `) let result: Exclude From 4aa679feed024def37b9815e689fce19adc07cc0 Mon Sep 17 00:00:00 2001 From: Katerina Skroumpelou Date: Tue, 31 Mar 2026 17:45:34 +0300 Subject: [PATCH 10/12] fix(postgrest): address comments --- .../core/postgrest-js/src/PostgrestBuilder.ts | 35 ++-- packages/core/postgrest-js/src/index.ts | 6 - .../postgrest-js/test/advanced_rpc.test.ts | 89 ++------- packages/core/postgrest-js/test/basic.test.ts | 185 ++++++++++++------ .../test/embeded_functions_join.test.ts | 120 ++++++++++++ packages/core/postgrest-js/test/retry.test.ts | 37 ++++ 6 files changed, 317 insertions(+), 155 deletions(-) diff --git a/packages/core/postgrest-js/src/PostgrestBuilder.ts b/packages/core/postgrest-js/src/PostgrestBuilder.ts index 7805eb210b7..070f80913e4 100644 --- a/packages/core/postgrest-js/src/PostgrestBuilder.ts +++ b/packages/core/postgrest-js/src/PostgrestBuilder.ts @@ -227,29 +227,15 @@ export default abstract class PostgrestBuilder< requestHeaders.set('X-Retry-Count', String(attemptCount)) } + // Only wrap the fetch call itself — processResponse errors must never trigger retries + let res: Response try { - const res = await _fetch(this.url.toString(), { + res = await _fetch(this.url.toString(), { method: this.method, headers: requestHeaders, body: JSON.stringify(this.body), signal: this.signal, }) - - // Check if we should retry this response - if (shouldRetry(this.method, res.status, attemptCount, this.retryEnabled)) { - const retryAfterHeader = res.headers.get('Retry-After') - const delay = - retryAfterHeader !== null - ? Math.max(0, parseInt(retryAfterHeader, 10) || 0) * 1000 - : getRetryDelay(attemptCount) - await res.text() - attemptCount++ - await sleep(delay) - continue - } - - // Process successful or final response - return await this.processResponse(res) } catch (fetchError: any) { // Never retry aborted requests if (fetchError?.name === 'AbortError' || fetchError?.code === 'ABORT_ERR') { @@ -272,6 +258,21 @@ export default abstract class PostgrestBuilder< // Exhausted retries or retries disabled, throw the last error throw fetchError } + + // Check if we should retry this HTTP response + if (shouldRetry(this.method, res.status, attemptCount, this.retryEnabled)) { + const retryAfterHeader = res.headers.get('Retry-After') + const delay = + retryAfterHeader !== null + ? Math.max(0, parseInt(retryAfterHeader, 10) || 0) * 1000 + : getRetryDelay(attemptCount) + await res.text() + attemptCount++ + await sleep(delay) + continue + } + + return await this.processResponse(res) } } diff --git a/packages/core/postgrest-js/src/index.ts b/packages/core/postgrest-js/src/index.ts index e29c7b1ce26..bc918d679c3 100644 --- a/packages/core/postgrest-js/src/index.ts +++ b/packages/core/postgrest-js/src/index.ts @@ -29,12 +29,6 @@ export type { PostgrestMaybeSingleResponse, } from './types/types' export type { ClientServerOptions as PostgrestClientOptions } from './types/common/common' -export { - DEFAULT_MAX_RETRIES, - getRetryDelay, - RETRYABLE_STATUS_CODES, - RETRYABLE_METHODS, -} from './types/common/common' // https://github.com/supabase/postgrest-js/issues/551 // To be replaced with a helper type that only uses public types export type { GetResult as UnstableGetResult } from './select-query-parser/result' diff --git a/packages/core/postgrest-js/test/advanced_rpc.test.ts b/packages/core/postgrest-js/test/advanced_rpc.test.ts index 7b126981654..3d15e04a887 100644 --- a/packages/core/postgrest-js/test/advanced_rpc.test.ts +++ b/packages/core/postgrest-js/test/advanced_rpc.test.ts @@ -120,13 +120,6 @@ describe('advanced rpc', () => { "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", "username": "supabot", }, - { - "channel_id": 3, - "data": null, - "id": 3, - "message": "Some message on channel without details", - "username": "supabot", - }, { "channel_id": 3, "data": null, @@ -199,13 +192,6 @@ describe('advanced rpc', () => { "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", "username": "supabot", }, - { - "channel_id": 3, - "data": null, - "id": 3, - "message": "Some message on channel without details", - "username": "supabot", - }, { "channel_id": 3, "data": null, @@ -254,13 +240,6 @@ describe('advanced rpc', () => { "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", "username": "supabot", }, - { - "channel_id": 3, - "data": null, - "id": 3, - "message": "Some message on channel without details", - "username": "supabot", - }, { "channel_id": 3, "data": null, @@ -309,13 +288,6 @@ describe('advanced rpc', () => { "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", "username": "supabot", }, - { - "channel_id": 3, - "data": null, - "id": 3, - "message": "Some message on channel without details", - "username": "supabot", - }, { "channel_id": 3, "data": null, @@ -356,13 +328,6 @@ describe('advanced rpc', () => { "message": "Some message on channel without details", "username": "supabot", }, - { - "channel_id": 3, - "data": null, - "id": 3, - "message": "Some message on channel without details", - "username": "supabot", - }, { "channel_id": 2, "data": null, @@ -404,13 +369,6 @@ describe('advanced rpc', () => { "message": "Some message on channel without details", "username": "supabot", }, - { - "channel_id": 3, - "data": null, - "id": 3, - "message": "Some message on channel without details", - "username": "supabot", - }, { "channel_id": 2, "data": null, @@ -447,14 +405,6 @@ describe('advanced rpc', () => { { "count": null, "data": [ - { - "channel_id": 3, - "message": "Some message on channel without details", - "users": { - "catchphrase": "'cat' 'fat'", - "username": "supabot", - }, - }, { "channel_id": 3, "message": "Some message on channel without details", @@ -723,13 +673,6 @@ describe('advanced rpc', () => { "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", "username": "supabot", }, - { - "channel_id": 3, - "data": null, - "id": 3, - "message": "Some message on channel without details", - "username": "supabot", - }, { "channel_id": 3, "data": null, @@ -1158,13 +1101,6 @@ describe('advanced rpc', () => { "status": "ONLINE", "username": "awailas", }, - { - "age_range": "[20,30)", - "catchphrase": "'fat' 'rat'", - "data": null, - "status": "ONLINE", - "username": "dragarcia", - }, { "age_range": "[20,30)", "catchphrase": "'json' 'test'", @@ -1179,6 +1115,27 @@ describe('advanced rpc', () => { "status": "ONLINE", "username": "jsonuser", }, + { + "age_range": "[20,30)", + "catchphrase": "'fat' 'rat'", + "data": null, + "status": "ONLINE", + "username": "dragarcia", + }, + { + "age_range": null, + "catchphrase": null, + "data": null, + "status": "ONLINE", + "username": "a", + }, + { + "age_range": null, + "catchphrase": null, + "data": null, + "status": "ONLINE", + "username": "b", + }, ], "error": null, "status": 200, @@ -1332,10 +1289,6 @@ test('RPC call with subselect and computed field', async () => { "blurb_message": "Som", "message": "Some message on channel without details", }, - { - "blurb_message": "Som", - "message": "Some message on channel without details", - }, ], "error": null, "status": 200, diff --git a/packages/core/postgrest-js/test/basic.test.ts b/packages/core/postgrest-js/test/basic.test.ts index 359e22dd7ce..33236ef908f 100644 --- a/packages/core/postgrest-js/test/basic.test.ts +++ b/packages/core/postgrest-js/test/basic.test.ts @@ -31,13 +31,6 @@ test('basic select table', async () => { "status": "ONLINE", "username": "awailas", }, - { - "age_range": "[20,30)", - "catchphrase": "'fat' 'rat'", - "data": null, - "status": "ONLINE", - "username": "dragarcia", - }, { "age_range": "[20,30)", "catchphrase": "'json' 'test'", @@ -52,6 +45,27 @@ test('basic select table', async () => { "status": "ONLINE", "username": "jsonuser", }, + { + "age_range": "[20,30)", + "catchphrase": "'fat' 'rat'", + "data": null, + "status": "ONLINE", + "username": "dragarcia", + }, + { + "age_range": null, + "catchphrase": null, + "data": null, + "status": "ONLINE", + "username": "a", + }, + { + "age_range": null, + "catchphrase": null, + "data": null, + "status": "ONLINE", + "username": "b", + }, ], "error": null, "status": 200, @@ -87,13 +101,6 @@ test('basic select returns types override', async () => { "status": "ONLINE", "username": "awailas", }, - { - "age_range": "[20,30)", - "catchphrase": "'fat' 'rat'", - "data": null, - "status": "ONLINE", - "username": "dragarcia", - }, { "age_range": "[20,30)", "catchphrase": "'json' 'test'", @@ -108,6 +115,27 @@ test('basic select returns types override', async () => { "status": "ONLINE", "username": "jsonuser", }, + { + "age_range": "[20,30)", + "catchphrase": "'fat' 'rat'", + "data": null, + "status": "ONLINE", + "username": "dragarcia", + }, + { + "age_range": null, + "catchphrase": null, + "data": null, + "status": "ONLINE", + "username": "a", + }, + { + "age_range": null, + "catchphrase": null, + "data": null, + "status": "ONLINE", + "username": "b", + }, ], "error": null, "status": 200, @@ -172,7 +200,7 @@ test('basic select with maybeSingle yielding more than one result', async () => "data": null, "error": { "code": "PGRST116", - "details": "Results contain 5 rows, application/vnd.pgrst.object+json requires 1 row", + "details": "Results contain 7 rows, application/vnd.pgrst.object+json requires 1 row", "hint": null, "message": "JSON object requested, multiple (or no) rows returned", }, @@ -190,7 +218,7 @@ test('basic select with single yielding more than one result', async () => { "data": null, "error": { "code": "PGRST116", - "details": "The result contains 5 rows", + "details": "The result contains 7 rows", "hint": null, "message": "Cannot coerce the result to a single JSON object", }, @@ -218,13 +246,21 @@ test('basic select view', async () => { "non_updatable_column": 1, "username": "awailas", }, + { + "non_updatable_column": 1, + "username": "jsonuser", + }, { "non_updatable_column": 1, "username": "dragarcia", }, { "non_updatable_column": 1, - "username": "jsonuser", + "username": "a", + }, + { + "non_updatable_column": 1, + "username": "b", }, ], "error": null, @@ -466,7 +502,7 @@ describe('basic insert, update, delete', () => { { "channel_id": 1, "data": null, - "id": 5, + "id": 19, "message": "foo", "username": "supabot", }, @@ -496,13 +532,6 @@ describe('basic insert, update, delete', () => { "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", "username": "supabot", }, - { - "channel_id": 3, - "data": null, - "id": 3, - "message": "Some message on channel without details", - "username": "supabot", - }, { "channel_id": 3, "data": null, @@ -513,7 +542,7 @@ describe('basic insert, update, delete', () => { { "channel_id": 1, "data": null, - "id": 5, + "id": 19, "message": "foo", "username": "supabot", }, @@ -543,8 +572,8 @@ describe('basic insert, update, delete', () => { }, ], "error": null, - "status": 200, - "statusText": "OK", + "status": 201, + "statusText": "Created", } `) @@ -577,7 +606,7 @@ describe('basic insert, update, delete', () => { { "channel_id": 1, "data": null, - "id": 5, + "id": 19, "message": "foo", "username": "supabot", }, @@ -611,14 +640,14 @@ describe('basic insert, update, delete', () => { { "channel_id": 1, "data": null, - "id": 6, + "id": 20, "message": "foo", "username": "supabot", }, { "channel_id": 1, "data": null, - "id": 7, + "id": 21, "message": "foo", "username": "supabot", }, @@ -658,7 +687,7 @@ describe('basic insert, update, delete', () => { { "channel_id": 1, "data": null, - "id": 5, + "id": 19, "message": "foo", "username": "supabot", }, @@ -672,14 +701,14 @@ describe('basic insert, update, delete', () => { { "channel_id": 1, "data": null, - "id": 6, + "id": 20, "message": "foo", "username": "supabot", }, { "channel_id": 1, "data": null, - "id": 7, + "id": 21, "message": "foo", "username": "supabot", }, @@ -726,7 +755,7 @@ describe('basic insert, update, delete', () => { { "channel_id": 2, "data": null, - "id": 5, + "id": 19, "message": "foo", "username": "supabot", }, @@ -740,14 +769,14 @@ describe('basic insert, update, delete', () => { { "channel_id": 2, "data": null, - "id": 6, + "id": 20, "message": "foo", "username": "supabot", }, { "channel_id": 2, "data": null, - "id": 7, + "id": 21, "message": "foo", "username": "supabot", }, @@ -787,7 +816,7 @@ describe('basic insert, update, delete', () => { { "channel_id": 2, "data": null, - "id": 5, + "id": 19, "message": "foo", "username": "supabot", }, @@ -801,14 +830,14 @@ describe('basic insert, update, delete', () => { { "channel_id": 2, "data": null, - "id": 6, + "id": 20, "message": "foo", "username": "supabot", }, { "channel_id": 2, "data": null, - "id": 7, + "id": 21, "message": "foo", "username": "supabot", }, @@ -829,7 +858,7 @@ describe('basic insert, update, delete', () => { { "channel_id": 2, "data": null, - "id": 5, + "id": 19, "message": "foo", "username": "supabot", }, @@ -843,14 +872,14 @@ describe('basic insert, update, delete', () => { { "channel_id": 2, "data": null, - "id": 6, + "id": 20, "message": "foo", "username": "supabot", }, { "channel_id": 2, "data": null, - "id": 7, + "id": 21, "message": "foo", "username": "supabot", }, @@ -1049,6 +1078,20 @@ test('allow ordering on JSON column', async () => { "status": "ONLINE", "username": "jsonuser", }, + { + "age_range": null, + "catchphrase": null, + "data": null, + "status": "ONLINE", + "username": "a", + }, + { + "age_range": null, + "catchphrase": null, + "data": null, + "status": "ONLINE", + "username": "b", + }, { "age_range": "[20,30)", "catchphrase": "'fat' 'rat'", @@ -1084,7 +1127,7 @@ test('select with head:true, count:exact', async () => { const res = await postgrest.from('users').select('*', { head: true, count: 'exact' }) expect(res).toMatchInlineSnapshot(` { - "count": 5, + "count": 7, "data": null, "error": null, "status": 200, @@ -1133,7 +1176,7 @@ test('select with count:exact', async () => { const res = await postgrest.from('users').select('*', { count: 'exact' }) expect(res).toMatchInlineSnapshot(` { - "count": 5, + "count": 7, "data": [ { "age_range": "[1,2)", @@ -1170,6 +1213,20 @@ test('select with count:exact', async () => { "status": "ONLINE", "username": "jsonuser", }, + { + "age_range": null, + "catchphrase": null, + "data": null, + "status": "ONLINE", + "username": "a", + }, + { + "age_range": null, + "catchphrase": null, + "data": null, + "status": "ONLINE", + "username": "b", + }, { "age_range": "[20,30)", "catchphrase": "'fat' 'rat'", @@ -1292,7 +1349,7 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 1, "data": null, - "id": 8, + "id": 22, "message": "foo", "username": "supabot", }, @@ -1332,7 +1389,7 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 1, "data": null, - "id": 8, + "id": 22, "message": "foo", "username": "supabot", }, @@ -1396,7 +1453,7 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 1, "data": null, - "id": 8, + "id": 22, "message": "foo", "username": "supabot", }, @@ -1433,14 +1490,14 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 1, "data": null, - "id": 9, + "id": 23, "message": "foo", "username": "supabot", }, { "channel_id": 1, "data": null, - "id": 10, + "id": 24, "message": "foo", "username": "supabot", }, @@ -1480,7 +1537,7 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 1, "data": null, - "id": 8, + "id": 22, "message": "foo", "username": "supabot", }, @@ -1494,14 +1551,14 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 1, "data": null, - "id": 9, + "id": 23, "message": "foo", "username": "supabot", }, { "channel_id": 1, "data": null, - "id": 10, + "id": 24, "message": "foo", "username": "supabot", }, @@ -1530,7 +1587,7 @@ describe("insert, update, delete with count: 'exact'", () => { }, { "data": null, - "id": 5, + "id": 9, "slug": "test-slug", }, ], @@ -1558,7 +1615,7 @@ describe("insert, update, delete with count: 'exact'", () => { }, { "data": null, - "id": 7, + "id": 11, "slug": "test-slug", }, ], @@ -1582,7 +1639,7 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 2, "data": null, - "id": 8, + "id": 22, "message": "foo", "username": "supabot", }, @@ -1596,14 +1653,14 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 2, "data": null, - "id": 9, + "id": 23, "message": "foo", "username": "supabot", }, { "channel_id": 2, "data": null, - "id": 10, + "id": 24, "message": "foo", "username": "supabot", }, @@ -1643,7 +1700,7 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 2, "data": null, - "id": 8, + "id": 22, "message": "foo", "username": "supabot", }, @@ -1657,14 +1714,14 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 2, "data": null, - "id": 9, + "id": 23, "message": "foo", "username": "supabot", }, { "channel_id": 2, "data": null, - "id": 10, + "id": 24, "message": "foo", "username": "supabot", }, @@ -1689,7 +1746,7 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 2, "data": null, - "id": 8, + "id": 22, "message": "foo", "username": "supabot", }, @@ -1703,14 +1760,14 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 2, "data": null, - "id": 9, + "id": 23, "message": "foo", "username": "supabot", }, { "channel_id": 2, "data": null, - "id": 10, + "id": 24, "message": "foo", "username": "supabot", }, diff --git a/packages/core/postgrest-js/test/embeded_functions_join.test.ts b/packages/core/postgrest-js/test/embeded_functions_join.test.ts index 33220716282..9becf9a5374 100644 --- a/packages/core/postgrest-js/test/embeded_functions_join.test.ts +++ b/packages/core/postgrest-js/test/embeded_functions_join.test.ts @@ -182,6 +182,14 @@ describe('embeded functions select', () => { "all_user_messages": [], "username": "jsonuser", }, + { + "all_user_messages": [], + "username": "a", + }, + { + "all_user_messages": [], + "username": "b", + }, { "all_user_messages": [], "username": "dragarcia", @@ -250,6 +258,14 @@ describe('embeded functions select', () => { "all_user_messages": [], "username": "jsonuser", }, + { + "all_user_messages": [], + "username": "a", + }, + { + "all_user_messages": [], + "username": "b", + }, { "all_user_messages": [], "username": "dragarcia", @@ -318,6 +334,14 @@ describe('embeded functions select', () => { "all_user_messages": [], "username": "jsonuser", }, + { + "all_user_messages": [], + "username": "a", + }, + { + "all_user_messages": [], + "username": "b", + }, { "all_user_messages": [], "username": "dragarcia", @@ -374,6 +398,14 @@ describe('embeded functions select', () => { "setof_rows_one": null, "username": "jsonuser", }, + { + "setof_rows_one": null, + "username": "a", + }, + { + "setof_rows_one": null, + "username": "b", + }, { "setof_rows_one": null, "username": "dragarcia", @@ -438,6 +470,20 @@ describe('embeded functions select', () => { }, "username": "jsonuser", }, + { + "returns_row": { + "id": null, + "username": null, + }, + "username": "a", + }, + { + "returns_row": { + "id": null, + "username": null, + }, + "username": "b", + }, { "returns_row": { "id": null, @@ -542,6 +588,18 @@ describe('embeded functions select', () => { }, "username": "jsonuser", }, + { + "user_called_profile": { + "username": null, + }, + "username": "a", + }, + { + "user_called_profile": { + "username": null, + }, + "username": "b", + }, { "user_called_profile": { "username": null, @@ -688,6 +746,14 @@ describe('embeded functions select', () => { "user_messages": [], "username": "jsonuser", }, + { + "user_messages": [], + "username": "a", + }, + { + "user_messages": [], + "username": "b", + }, { "user_messages": [], "username": "dragarcia", @@ -761,6 +827,14 @@ describe('embeded functions select', () => { "active_user_messages": [], "username": "jsonuser", }, + { + "active_user_messages": [], + "username": "a", + }, + { + "active_user_messages": [], + "username": "b", + }, { "active_user_messages": [], "username": "dragarcia", @@ -838,6 +912,14 @@ describe('embeded functions select', () => { "recent_messages": [], "username": "jsonuser", }, + { + "recent_messages": [], + "username": "a", + }, + { + "recent_messages": [], + "username": "b", + }, { "recent_messages": [], "username": "dragarcia", @@ -911,6 +993,14 @@ describe('embeded functions select', () => { "recent_messages": [], "username": "jsonuser", }, + { + "recent_messages": [], + "username": "a", + }, + { + "recent_messages": [], + "username": "b", + }, { "recent_messages": [], "username": "dragarcia", @@ -982,6 +1072,14 @@ describe('embeded functions select', () => { "user_messages": [], "username": "jsonuser", }, + { + "user_messages": [], + "username": "a", + }, + { + "user_messages": [], + "username": "b", + }, { "user_messages": [], "username": "dragarcia", @@ -1142,6 +1240,20 @@ describe('embeded functions select', () => { }, "username": "jsonuser", }, + { + "profile": { + "id": null, + "username": null, + }, + "username": "a", + }, + { + "profile": { + "id": null, + "username": null, + }, + "username": "b", + }, { "profile": { "id": null, @@ -1198,6 +1310,14 @@ describe('embeded functions select', () => { "profile": null, "username": "jsonuser", }, + { + "profile": null, + "username": "a", + }, + { + "profile": null, + "username": "b", + }, { "profile": null, "username": "dragarcia", diff --git a/packages/core/postgrest-js/test/retry.test.ts b/packages/core/postgrest-js/test/retry.test.ts index e308e4c52f1..7164d1b1d89 100644 --- a/packages/core/postgrest-js/test/retry.test.ts +++ b/packages/core/postgrest-js/test/retry.test.ts @@ -429,6 +429,43 @@ describe('Automatic Retries', () => { }) }) + describe('shouldThrowOnError interaction', () => { + it('should NOT retry a non-retryable error with throwOnError()', async () => { + fetchMock.mockResolvedValueOnce({ + ok: false, + status: 400, + statusText: 'Bad Request', + text: () => + Promise.resolve( + JSON.stringify({ code: 'PGRST000', message: 'bad input', hint: null, details: null }) + ), + headers: new Headers(), + }) + + const client = new PostgrestClient('http://localhost:3000', { fetch: fetchMock }) + await expect(runWithTimers(client.from('users').select().throwOnError())).rejects.toThrow( + 'bad input' + ) + + expect(fetchMock).toHaveBeenCalledTimes(1) // No retries for application-level errors + }) + + it('should NOT retry a retryable status code after exhaustion when using throwOnError()', async () => { + fetchMock.mockResolvedValue({ + ok: false, + status: 520, + statusText: 'Origin Error', + text: () => Promise.resolve('Cloudflare timeout'), + headers: new Headers(), + }) + + const client = new PostgrestClient('http://localhost:3000', { fetch: fetchMock }) + await expect(runWithTimers(client.from('users').select().throwOnError())).rejects.toThrow() + + expect(fetchMock).toHaveBeenCalledTimes(4) // 1 initial + 3 retries, then throws + }) + }) + describe('schema switching', () => { it('should preserve retry setting when switching schemas', async () => { fetchMock.mockResolvedValueOnce({ From d0ef8a50d84461ca0f84ab7e448a148820d90b73 Mon Sep 17 00:00:00 2001 From: Katerina Skroumpelou Date: Tue, 31 Mar 2026 18:23:17 +0300 Subject: [PATCH 11/12] chore(postgrest): update snapshots --- .../core/postgrest-js/src/PostgrestBuilder.ts | 2 +- .../postgrest-js/test/advanced_rpc.test.ts | 89 +++++-- packages/core/postgrest-js/test/basic.test.ts | 219 ++++++++---------- .../test/embeded_functions_join.test.ts | 195 ++++++---------- .../core/postgrest-js/test/filters.test.ts | 6 + ...relationships-aggregate-operations.test.ts | 25 +- .../relationships-join-operations.test.ts | 7 + .../relationships-spread-operations.test.ts | 32 +++ .../postgrest-js/test/relationships.test.ts | 25 ++ .../test/resource-embedding.test.ts | 29 +++ .../core/postgrest-js/test/transforms.test.ts | 14 +- 11 files changed, 370 insertions(+), 273 deletions(-) diff --git a/packages/core/postgrest-js/src/PostgrestBuilder.ts b/packages/core/postgrest-js/src/PostgrestBuilder.ts index 070f80913e4..db9a31cc236 100644 --- a/packages/core/postgrest-js/src/PostgrestBuilder.ts +++ b/packages/core/postgrest-js/src/PostgrestBuilder.ts @@ -261,7 +261,7 @@ export default abstract class PostgrestBuilder< // Check if we should retry this HTTP response if (shouldRetry(this.method, res.status, attemptCount, this.retryEnabled)) { - const retryAfterHeader = res.headers.get('Retry-After') + const retryAfterHeader = res.headers?.get('Retry-After') ?? null const delay = retryAfterHeader !== null ? Math.max(0, parseInt(retryAfterHeader, 10) || 0) * 1000 diff --git a/packages/core/postgrest-js/test/advanced_rpc.test.ts b/packages/core/postgrest-js/test/advanced_rpc.test.ts index 3d15e04a887..7b126981654 100644 --- a/packages/core/postgrest-js/test/advanced_rpc.test.ts +++ b/packages/core/postgrest-js/test/advanced_rpc.test.ts @@ -120,6 +120,13 @@ describe('advanced rpc', () => { "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", "username": "supabot", }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, { "channel_id": 3, "data": null, @@ -192,6 +199,13 @@ describe('advanced rpc', () => { "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", "username": "supabot", }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, { "channel_id": 3, "data": null, @@ -240,6 +254,13 @@ describe('advanced rpc', () => { "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", "username": "supabot", }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, { "channel_id": 3, "data": null, @@ -288,6 +309,13 @@ describe('advanced rpc', () => { "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", "username": "supabot", }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, { "channel_id": 3, "data": null, @@ -328,6 +356,13 @@ describe('advanced rpc', () => { "message": "Some message on channel without details", "username": "supabot", }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, { "channel_id": 2, "data": null, @@ -369,6 +404,13 @@ describe('advanced rpc', () => { "message": "Some message on channel without details", "username": "supabot", }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, { "channel_id": 2, "data": null, @@ -405,6 +447,14 @@ describe('advanced rpc', () => { { "count": null, "data": [ + { + "channel_id": 3, + "message": "Some message on channel without details", + "users": { + "catchphrase": "'cat' 'fat'", + "username": "supabot", + }, + }, { "channel_id": 3, "message": "Some message on channel without details", @@ -673,6 +723,13 @@ describe('advanced rpc', () => { "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", "username": "supabot", }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, { "channel_id": 3, "data": null, @@ -1101,6 +1158,13 @@ describe('advanced rpc', () => { "status": "ONLINE", "username": "awailas", }, + { + "age_range": "[20,30)", + "catchphrase": "'fat' 'rat'", + "data": null, + "status": "ONLINE", + "username": "dragarcia", + }, { "age_range": "[20,30)", "catchphrase": "'json' 'test'", @@ -1115,27 +1179,6 @@ describe('advanced rpc', () => { "status": "ONLINE", "username": "jsonuser", }, - { - "age_range": "[20,30)", - "catchphrase": "'fat' 'rat'", - "data": null, - "status": "ONLINE", - "username": "dragarcia", - }, - { - "age_range": null, - "catchphrase": null, - "data": null, - "status": "ONLINE", - "username": "a", - }, - { - "age_range": null, - "catchphrase": null, - "data": null, - "status": "ONLINE", - "username": "b", - }, ], "error": null, "status": 200, @@ -1289,6 +1332,10 @@ test('RPC call with subselect and computed field', async () => { "blurb_message": "Som", "message": "Some message on channel without details", }, + { + "blurb_message": "Som", + "message": "Some message on channel without details", + }, ], "error": null, "status": 200, diff --git a/packages/core/postgrest-js/test/basic.test.ts b/packages/core/postgrest-js/test/basic.test.ts index 33236ef908f..58bcdeb4ce5 100644 --- a/packages/core/postgrest-js/test/basic.test.ts +++ b/packages/core/postgrest-js/test/basic.test.ts @@ -31,6 +31,13 @@ test('basic select table', async () => { "status": "ONLINE", "username": "awailas", }, + { + "age_range": "[20,30)", + "catchphrase": "'fat' 'rat'", + "data": null, + "status": "ONLINE", + "username": "dragarcia", + }, { "age_range": "[20,30)", "catchphrase": "'json' 'test'", @@ -45,27 +52,6 @@ test('basic select table', async () => { "status": "ONLINE", "username": "jsonuser", }, - { - "age_range": "[20,30)", - "catchphrase": "'fat' 'rat'", - "data": null, - "status": "ONLINE", - "username": "dragarcia", - }, - { - "age_range": null, - "catchphrase": null, - "data": null, - "status": "ONLINE", - "username": "a", - }, - { - "age_range": null, - "catchphrase": null, - "data": null, - "status": "ONLINE", - "username": "b", - }, ], "error": null, "status": 200, @@ -101,6 +87,13 @@ test('basic select returns types override', async () => { "status": "ONLINE", "username": "awailas", }, + { + "age_range": "[20,30)", + "catchphrase": "'fat' 'rat'", + "data": null, + "status": "ONLINE", + "username": "dragarcia", + }, { "age_range": "[20,30)", "catchphrase": "'json' 'test'", @@ -115,27 +108,6 @@ test('basic select returns types override', async () => { "status": "ONLINE", "username": "jsonuser", }, - { - "age_range": "[20,30)", - "catchphrase": "'fat' 'rat'", - "data": null, - "status": "ONLINE", - "username": "dragarcia", - }, - { - "age_range": null, - "catchphrase": null, - "data": null, - "status": "ONLINE", - "username": "a", - }, - { - "age_range": null, - "catchphrase": null, - "data": null, - "status": "ONLINE", - "username": "b", - }, ], "error": null, "status": 200, @@ -200,7 +172,7 @@ test('basic select with maybeSingle yielding more than one result', async () => "data": null, "error": { "code": "PGRST116", - "details": "Results contain 7 rows, application/vnd.pgrst.object+json requires 1 row", + "details": "Results contain 5 rows, application/vnd.pgrst.object+json requires 1 row", "hint": null, "message": "JSON object requested, multiple (or no) rows returned", }, @@ -218,7 +190,7 @@ test('basic select with single yielding more than one result', async () => { "data": null, "error": { "code": "PGRST116", - "details": "The result contains 7 rows", + "details": "The result contains 5 rows", "hint": null, "message": "Cannot coerce the result to a single JSON object", }, @@ -246,21 +218,13 @@ test('basic select view', async () => { "non_updatable_column": 1, "username": "awailas", }, - { - "non_updatable_column": 1, - "username": "jsonuser", - }, { "non_updatable_column": 1, "username": "dragarcia", }, { "non_updatable_column": 1, - "username": "a", - }, - { - "non_updatable_column": 1, - "username": "b", + "username": "jsonuser", }, ], "error": null, @@ -502,7 +466,7 @@ describe('basic insert, update, delete', () => { { "channel_id": 1, "data": null, - "id": 19, + "id": 5, "message": "foo", "username": "supabot", }, @@ -532,6 +496,13 @@ describe('basic insert, update, delete', () => { "message": "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", "username": "supabot", }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, { "channel_id": 3, "data": null, @@ -542,7 +513,7 @@ describe('basic insert, update, delete', () => { { "channel_id": 1, "data": null, - "id": 19, + "id": 5, "message": "foo", "username": "supabot", }, @@ -572,8 +543,8 @@ describe('basic insert, update, delete', () => { }, ], "error": null, - "status": 201, - "statusText": "Created", + "status": 200, + "statusText": "OK", } `) @@ -606,7 +577,7 @@ describe('basic insert, update, delete', () => { { "channel_id": 1, "data": null, - "id": 19, + "id": 5, "message": "foo", "username": "supabot", }, @@ -640,14 +611,14 @@ describe('basic insert, update, delete', () => { { "channel_id": 1, "data": null, - "id": 20, + "id": 6, "message": "foo", "username": "supabot", }, { "channel_id": 1, "data": null, - "id": 21, + "id": 7, "message": "foo", "username": "supabot", }, @@ -687,7 +658,7 @@ describe('basic insert, update, delete', () => { { "channel_id": 1, "data": null, - "id": 19, + "id": 5, "message": "foo", "username": "supabot", }, @@ -701,14 +672,14 @@ describe('basic insert, update, delete', () => { { "channel_id": 1, "data": null, - "id": 20, + "id": 6, "message": "foo", "username": "supabot", }, { "channel_id": 1, "data": null, - "id": 21, + "id": 7, "message": "foo", "username": "supabot", }, @@ -755,7 +726,7 @@ describe('basic insert, update, delete', () => { { "channel_id": 2, "data": null, - "id": 19, + "id": 5, "message": "foo", "username": "supabot", }, @@ -769,14 +740,14 @@ describe('basic insert, update, delete', () => { { "channel_id": 2, "data": null, - "id": 20, + "id": 6, "message": "foo", "username": "supabot", }, { "channel_id": 2, "data": null, - "id": 21, + "id": 7, "message": "foo", "username": "supabot", }, @@ -816,7 +787,7 @@ describe('basic insert, update, delete', () => { { "channel_id": 2, "data": null, - "id": 19, + "id": 5, "message": "foo", "username": "supabot", }, @@ -830,14 +801,14 @@ describe('basic insert, update, delete', () => { { "channel_id": 2, "data": null, - "id": 20, + "id": 6, "message": "foo", "username": "supabot", }, { "channel_id": 2, "data": null, - "id": 21, + "id": 7, "message": "foo", "username": "supabot", }, @@ -858,7 +829,7 @@ describe('basic insert, update, delete', () => { { "channel_id": 2, "data": null, - "id": 19, + "id": 5, "message": "foo", "username": "supabot", }, @@ -872,14 +843,14 @@ describe('basic insert, update, delete', () => { { "channel_id": 2, "data": null, - "id": 20, + "id": 6, "message": "foo", "username": "supabot", }, { "channel_id": 2, "data": null, - "id": 21, + "id": 7, "message": "foo", "username": "supabot", }, @@ -923,6 +894,16 @@ describe('basic insert, update, delete', () => { } `) }) + + afterAll(async () => { + // Restore message id 3 — the upsert test changes it to 'foo' and the delete test removes it + await postgrest.from('messages').upsert({ + id: 3, + message: 'Some message on channel without details', + username: 'supabot', + channel_id: 3, + }) + }) }) test('throwOnError throws errors instead of returning them', async () => { @@ -1078,20 +1059,6 @@ test('allow ordering on JSON column', async () => { "status": "ONLINE", "username": "jsonuser", }, - { - "age_range": null, - "catchphrase": null, - "data": null, - "status": "ONLINE", - "username": "a", - }, - { - "age_range": null, - "catchphrase": null, - "data": null, - "status": "ONLINE", - "username": "b", - }, { "age_range": "[20,30)", "catchphrase": "'fat' 'rat'", @@ -1127,7 +1094,7 @@ test('select with head:true, count:exact', async () => { const res = await postgrest.from('users').select('*', { head: true, count: 'exact' }) expect(res).toMatchInlineSnapshot(` { - "count": 7, + "count": 5, "data": null, "error": null, "status": 200, @@ -1176,7 +1143,7 @@ test('select with count:exact', async () => { const res = await postgrest.from('users').select('*', { count: 'exact' }) expect(res).toMatchInlineSnapshot(` { - "count": 7, + "count": 5, "data": [ { "age_range": "[1,2)", @@ -1213,20 +1180,6 @@ test('select with count:exact', async () => { "status": "ONLINE", "username": "jsonuser", }, - { - "age_range": null, - "catchphrase": null, - "data": null, - "status": "ONLINE", - "username": "a", - }, - { - "age_range": null, - "catchphrase": null, - "data": null, - "status": "ONLINE", - "username": "b", - }, { "age_range": "[20,30)", "catchphrase": "'fat' 'rat'", @@ -1349,7 +1302,7 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 1, "data": null, - "id": 22, + "id": 8, "message": "foo", "username": "supabot", }, @@ -1386,10 +1339,17 @@ describe("insert, update, delete with count: 'exact'", () => { "message": "Some message on channel without details", "username": "supabot", }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, { "channel_id": 1, "data": null, - "id": 22, + "id": 8, "message": "foo", "username": "supabot", }, @@ -1419,8 +1379,8 @@ describe("insert, update, delete with count: 'exact'", () => { }, ], "error": null, - "status": 201, - "statusText": "Created", + "status": 200, + "statusText": "OK", } `) @@ -1453,7 +1413,7 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 1, "data": null, - "id": 22, + "id": 8, "message": "foo", "username": "supabot", }, @@ -1490,14 +1450,14 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 1, "data": null, - "id": 23, + "id": 9, "message": "foo", "username": "supabot", }, { "channel_id": 1, "data": null, - "id": 24, + "id": 10, "message": "foo", "username": "supabot", }, @@ -1537,7 +1497,7 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 1, "data": null, - "id": 22, + "id": 8, "message": "foo", "username": "supabot", }, @@ -1551,14 +1511,14 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 1, "data": null, - "id": 23, + "id": 9, "message": "foo", "username": "supabot", }, { "channel_id": 1, "data": null, - "id": 24, + "id": 10, "message": "foo", "username": "supabot", }, @@ -1587,7 +1547,7 @@ describe("insert, update, delete with count: 'exact'", () => { }, { "data": null, - "id": 9, + "id": 5, "slug": "test-slug", }, ], @@ -1615,7 +1575,7 @@ describe("insert, update, delete with count: 'exact'", () => { }, { "data": null, - "id": 11, + "id": 7, "slug": "test-slug", }, ], @@ -1639,7 +1599,7 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 2, "data": null, - "id": 22, + "id": 8, "message": "foo", "username": "supabot", }, @@ -1653,14 +1613,14 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 2, "data": null, - "id": 23, + "id": 9, "message": "foo", "username": "supabot", }, { "channel_id": 2, "data": null, - "id": 24, + "id": 10, "message": "foo", "username": "supabot", }, @@ -1700,7 +1660,7 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 2, "data": null, - "id": 22, + "id": 8, "message": "foo", "username": "supabot", }, @@ -1714,14 +1674,14 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 2, "data": null, - "id": 23, + "id": 9, "message": "foo", "username": "supabot", }, { "channel_id": 2, "data": null, - "id": 24, + "id": 10, "message": "foo", "username": "supabot", }, @@ -1746,7 +1706,7 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 2, "data": null, - "id": 22, + "id": 8, "message": "foo", "username": "supabot", }, @@ -1760,14 +1720,14 @@ describe("insert, update, delete with count: 'exact'", () => { { "channel_id": 2, "data": null, - "id": 23, + "id": 9, "message": "foo", "username": "supabot", }, { "channel_id": 2, "data": null, - "id": 24, + "id": 10, "message": "foo", "username": "supabot", }, @@ -1811,6 +1771,16 @@ describe("insert, update, delete with count: 'exact'", () => { } `) }) + + afterAll(async () => { + // Restore message id 3 — the upsert test changes it to 'foo' and the delete test removes it + await postgrest.from('messages').upsert({ + id: 3, + message: 'Some message on channel without details', + username: 'supabot', + channel_id: 3, + }) + }) }) test('insert includes columns param', async () => { @@ -1906,6 +1876,9 @@ test('!left join on one to many relation', async () => { { "username": "supabot", }, + { + "username": "supabot", + }, ], }, "error": null, diff --git a/packages/core/postgrest-js/test/embeded_functions_join.test.ts b/packages/core/postgrest-js/test/embeded_functions_join.test.ts index 9becf9a5374..bd37371f33d 100644 --- a/packages/core/postgrest-js/test/embeded_functions_join.test.ts +++ b/packages/core/postgrest-js/test/embeded_functions_join.test.ts @@ -47,6 +47,13 @@ describe('embeded functions select', () => { "message": "Some message on channel without details", "username": "supabot", }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, ], "id": 3, }, @@ -111,6 +118,10 @@ describe('embeded functions select', () => { "id": 4, "message": "Some message on channel without details", }, + { + "id": 3, + "message": "Some message on channel without details", + }, ], "id": 3, }, @@ -167,6 +178,13 @@ describe('embeded functions select', () => { "message": "Some message on channel without details", "username": "supabot", }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, ], "username": "supabot", }, @@ -182,14 +200,6 @@ describe('embeded functions select', () => { "all_user_messages": [], "username": "jsonuser", }, - { - "all_user_messages": [], - "username": "a", - }, - { - "all_user_messages": [], - "username": "b", - }, { "all_user_messages": [], "username": "dragarcia", @@ -243,6 +253,10 @@ describe('embeded functions select', () => { "id": 4, "message": "Some message on channel without details", }, + { + "id": 3, + "message": "Some message on channel without details", + }, ], "username": "supabot", }, @@ -258,14 +272,6 @@ describe('embeded functions select', () => { "all_user_messages": [], "username": "jsonuser", }, - { - "all_user_messages": [], - "username": "a", - }, - { - "all_user_messages": [], - "username": "b", - }, { "all_user_messages": [], "username": "dragarcia", @@ -319,6 +325,11 @@ describe('embeded functions select', () => { "id": 4, "message": "Some message on channel without details", }, + { + "blurb_message": "Som", + "id": 3, + "message": "Some message on channel without details", + }, ], "username": "supabot", }, @@ -334,14 +345,6 @@ describe('embeded functions select', () => { "all_user_messages": [], "username": "jsonuser", }, - { - "all_user_messages": [], - "username": "a", - }, - { - "all_user_messages": [], - "username": "b", - }, { "all_user_messages": [], "username": "dragarcia", @@ -398,14 +401,6 @@ describe('embeded functions select', () => { "setof_rows_one": null, "username": "jsonuser", }, - { - "setof_rows_one": null, - "username": "a", - }, - { - "setof_rows_one": null, - "username": "b", - }, { "setof_rows_one": null, "username": "dragarcia", @@ -470,20 +465,6 @@ describe('embeded functions select', () => { }, "username": "jsonuser", }, - { - "returns_row": { - "id": null, - "username": null, - }, - "username": "a", - }, - { - "returns_row": { - "id": null, - "username": null, - }, - "username": "b", - }, { "returns_row": { "id": null, @@ -588,18 +569,6 @@ describe('embeded functions select', () => { }, "username": "jsonuser", }, - { - "user_called_profile": { - "username": null, - }, - "username": "a", - }, - { - "user_called_profile": { - "username": null, - }, - "username": "b", - }, { "user_called_profile": { "username": null, @@ -671,6 +640,14 @@ describe('embeded functions select', () => { "id": 4, "message": "Some message on channel without details", }, + { + "channels": { + "id": 3, + "slug": "other", + }, + "id": 3, + "message": "Some message on channel without details", + }, ], "id": 3, }, @@ -731,6 +708,13 @@ describe('embeded functions select', () => { "message": "Some message on channel without details", "username": "supabot", }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, ], "username": "supabot", }, @@ -746,14 +730,6 @@ describe('embeded functions select', () => { "user_messages": [], "username": "jsonuser", }, - { - "user_messages": [], - "username": "a", - }, - { - "user_messages": [], - "username": "b", - }, { "user_messages": [], "username": "dragarcia", @@ -816,6 +792,13 @@ describe('embeded functions select', () => { "message": "Some message on channel without details", "username": "supabot", }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, ], "username": "supabot", }, @@ -827,14 +810,6 @@ describe('embeded functions select', () => { "active_user_messages": [], "username": "jsonuser", }, - { - "active_user_messages": [], - "username": "a", - }, - { - "active_user_messages": [], - "username": "b", - }, { "active_user_messages": [], "username": "dragarcia", @@ -883,6 +858,13 @@ describe('embeded functions select', () => { "message": "Some message on channel without details", "username": "supabot", }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, { "channel_id": 2, "data": null, @@ -912,14 +894,6 @@ describe('embeded functions select', () => { "recent_messages": [], "username": "jsonuser", }, - { - "recent_messages": [], - "username": "a", - }, - { - "recent_messages": [], - "username": "b", - }, { "recent_messages": [], "username": "dragarcia", @@ -968,6 +942,13 @@ describe('embeded functions select', () => { "message": "Some message on channel without details", "username": "supabot", }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, { "channel_id": 2, "data": null, @@ -993,14 +974,6 @@ describe('embeded functions select', () => { "recent_messages": [], "username": "jsonuser", }, - { - "recent_messages": [], - "username": "a", - }, - { - "recent_messages": [], - "username": "b", - }, { "recent_messages": [], "username": "dragarcia", @@ -1057,6 +1030,11 @@ describe('embeded functions select', () => { "id": 4, "message": "Some message on channel without details", }, + { + "blurb_message": "Som", + "id": 3, + "message": "Some message on channel without details", + }, ], "username": "supabot", }, @@ -1072,14 +1050,6 @@ describe('embeded functions select', () => { "user_messages": [], "username": "jsonuser", }, - { - "user_messages": [], - "username": "a", - }, - { - "user_messages": [], - "username": "b", - }, { "user_messages": [], "username": "dragarcia", @@ -1159,6 +1129,13 @@ describe('embeded functions select', () => { "username": "supabot", }, }, + { + "id": 3, + "user": { + "status": "ONLINE", + "username": "supabot", + }, + }, ], "error": null, "status": 200, @@ -1240,20 +1217,6 @@ describe('embeded functions select', () => { }, "username": "jsonuser", }, - { - "profile": { - "id": null, - "username": null, - }, - "username": "a", - }, - { - "profile": { - "id": null, - "username": null, - }, - "username": "b", - }, { "profile": { "id": null, @@ -1310,14 +1273,6 @@ describe('embeded functions select', () => { "profile": null, "username": "jsonuser", }, - { - "profile": null, - "username": "a", - }, - { - "profile": null, - "username": "b", - }, { "profile": null, "username": "dragarcia", diff --git a/packages/core/postgrest-js/test/filters.test.ts b/packages/core/postgrest-js/test/filters.test.ts index 5fcb9e69ec6..9d29e81ee76 100644 --- a/packages/core/postgrest-js/test/filters.test.ts +++ b/packages/core/postgrest-js/test/filters.test.ts @@ -109,6 +109,9 @@ test('gt', async () => { { "id": 4, }, + { + "id": 3, + }, ], "error": null, "status": 200, @@ -132,6 +135,9 @@ test('gte', async () => { { "id": 4, }, + { + "id": 3, + }, ], "error": null, "status": 200, diff --git a/packages/core/postgrest-js/test/relationships-aggregate-operations.test.ts b/packages/core/postgrest-js/test/relationships-aggregate-operations.test.ts index dd03f832c61..1356e963e83 100644 --- a/packages/core/postgrest-js/test/relationships-aggregate-operations.test.ts +++ b/packages/core/postgrest-js/test/relationships-aggregate-operations.test.ts @@ -14,7 +14,7 @@ test('select with aggregate count function', async () => { "data": { "messages": [ { - "count": 3, + "count": 4, }, ], "username": "supabot", @@ -50,7 +50,7 @@ test('select with aggregate count on a column function', async () => { "data": { "messages": [ { - "count": 3, + "count": 4, }, ], "username": "supabot", @@ -86,7 +86,7 @@ test('select with aggregate count function and alias', async () => { "data": { "messages": [ { - "message_count": 3, + "message_count": 4, }, ], "username": "supabot", @@ -136,6 +136,11 @@ test('select with aggregate nested count function', async () => { "count": 1, }, }, + { + "channels": { + "count": 1, + }, + }, ], "username": "supabot", }, @@ -186,6 +191,11 @@ test('select with aggregate nested count function and alias', async () => { "channel_count": 1, }, }, + { + "channels": { + "channel_count": 1, + }, + }, ], "username": "supabot", }, @@ -218,7 +228,7 @@ test('select with aggregate sum function', async () => { "data": { "messages": [ { - "sum": 7, + "sum": 10, }, ], "username": "supabot", @@ -254,7 +264,7 @@ test('select with aggregate aliased sum function', async () => { "data": { "messages": [ { - "sum_id": 7, + "sum_id": 10, }, ], "username": "supabot", @@ -304,6 +314,11 @@ test('select with aggregate sum function on nested relation', async () => { "sum": 3, }, }, + { + "channels": { + "sum": 3, + }, + }, ], "username": "supabot", }, diff --git a/packages/core/postgrest-js/test/relationships-join-operations.test.ts b/packages/core/postgrest-js/test/relationships-join-operations.test.ts index 95bc60951a4..81cd49b88be 100644 --- a/packages/core/postgrest-js/test/relationships-join-operations.test.ts +++ b/packages/core/postgrest-js/test/relationships-join-operations.test.ts @@ -208,6 +208,13 @@ test('!left oneToMany', async () => { "message": "Some message on channel without details", "username": "supabot", }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, ], }, "error": null, diff --git a/packages/core/postgrest-js/test/relationships-spread-operations.test.ts b/packages/core/postgrest-js/test/relationships-spread-operations.test.ts index a3cb75e425d..3eee4fe7aac 100644 --- a/packages/core/postgrest-js/test/relationships-spread-operations.test.ts +++ b/packages/core/postgrest-js/test/relationships-spread-operations.test.ts @@ -35,6 +35,12 @@ test('select with aggregate count and spread', async () => { "details": null, }, }, + { + "channels": { + "count": 1, + "details": null, + }, + }, ], "username": "supabot", }, @@ -89,6 +95,12 @@ test('spread resource with single column in select query', async () => { "details": null, }, }, + { + "channels": { + "count": 1, + "details": null, + }, + }, ], "username": "supabot", }, @@ -146,6 +158,13 @@ test('spread resource with all columns in select query', async () => { "id": null, }, }, + { + "channels": { + "count": 1, + "details": null, + "id": null, + }, + }, ], "username": "supabot", }, @@ -201,6 +220,12 @@ test('select with aggregate sum and spread', async () => { "sum": 3, }, }, + { + "channels": { + "details": null, + "sum": 3, + }, + }, ], "username": "supabot", }, @@ -260,6 +285,13 @@ test('select with aggregate sum and spread on nested relation', async () => { "sum": 3, }, }, + { + "channels": { + "details": null, + "details_sum": null, + "sum": 3, + }, + }, ], "username": "supabot", }, diff --git a/packages/core/postgrest-js/test/relationships.test.ts b/packages/core/postgrest-js/test/relationships.test.ts index 2740e58aa93..6bccbbc3303 100644 --- a/packages/core/postgrest-js/test/relationships.test.ts +++ b/packages/core/postgrest-js/test/relationships.test.ts @@ -44,6 +44,10 @@ test('nested query with selective fields', async () => { "id": 4, "message": "Some message on channel without details", }, + { + "id": 3, + "message": "Some message on channel without details", + }, ], "username": "supabot", }, @@ -102,6 +106,14 @@ test('nested query with multiple levels and selective fields', async () => { "id": 4, "message": "Some message on channel without details", }, + { + "channels": { + "id": 3, + "slug": "other", + }, + "id": 3, + "message": "Some message on channel without details", + }, ], "username": "supabot", }, @@ -149,6 +161,9 @@ test('query with multiple one-to-many relationships', async () => { { "id": 4, }, + { + "id": 3, + }, ], "user_profiles": [ { @@ -229,6 +244,13 @@ test('one-to-many relationship', async () => { "message": "Some message on channel without details", "username": "supabot", }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, ], }, "error": null, @@ -271,6 +293,9 @@ test('one-to-many relationship with selective columns', async () => { { "data": null, }, + { + "data": null, + }, ], }, "error": null, diff --git a/packages/core/postgrest-js/test/resource-embedding.test.ts b/packages/core/postgrest-js/test/resource-embedding.test.ts index 554442a9548..fb4484bd258 100644 --- a/packages/core/postgrest-js/test/resource-embedding.test.ts +++ b/packages/core/postgrest-js/test/resource-embedding.test.ts @@ -36,6 +36,13 @@ test('embedded select', async () => { "message": "Some message on channel without details", "username": "supabot", }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, ], }, { @@ -110,6 +117,14 @@ test('embedded select with computed field explicit selection', async () => { "message": "Some message on channel without details", "username": "supabot", }, + { + "blurb_message": "Som", + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, ], }, { @@ -365,6 +380,13 @@ describe('embedded transforms', () => { "message": "Some message on channel without details", "username": "supabot", }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, { "channel_id": 2, "data": null, @@ -439,6 +461,13 @@ describe('embedded transforms', () => { "message": "Some message on channel without details", "username": "supabot", }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, { "channel_id": 2, "data": null, diff --git a/packages/core/postgrest-js/test/transforms.test.ts b/packages/core/postgrest-js/test/transforms.test.ts index afc08aca54c..d95d6957069 100644 --- a/packages/core/postgrest-js/test/transforms.test.ts +++ b/packages/core/postgrest-js/test/transforms.test.ts @@ -78,6 +78,13 @@ test('order on multiple columns', async () => { "message": "Some message on channel without details", "username": "supabot", }, + { + "channel_id": 3, + "data": null, + "id": 3, + "message": "Some message on channel without details", + "username": "supabot", + }, { "channel_id": 2, "data": null, @@ -236,6 +243,9 @@ test('maybeSingle', async () => { "statusText": "Not Acceptable", } `) + + // Clean up inserted users so they don't leak into other test files + await postgrest.from('users').delete().in('username', ['a', 'b']) }) test('select on insert', async () => { @@ -286,9 +296,7 @@ test('csv', async () => { kiwicopple,,"[25,35)",OFFLINE,"'bat' 'cat'" awailas,,"[25,35)",ONLINE,"'bat' 'rat'" jsonuser,"{""foo"": {""bar"": {""nested"": ""value""}, ""baz"": ""string value""}}","[20,30)",ONLINE,"'json' 'test'" - dragarcia,,"[20,30)",ONLINE,"'fat' 'rat'" - a,,,ONLINE, - b,,,ONLINE,", + dragarcia,,"[20,30)",ONLINE,"'fat' 'rat'"", "error": null, "status": 200, "statusText": "OK", From 0f5b1b330807819ea8e7a3bf176c7406d883b1ae Mon Sep 17 00:00:00 2001 From: Katerina Skroumpelou Date: Tue, 31 Mar 2026 19:08:49 +0300 Subject: [PATCH 12/12] chore(postgrest): address copilot comments --- .../core/postgrest-js/src/PostgrestBuilder.ts | 29 ++++++++++++++----- .../core/postgrest-js/src/PostgrestClient.ts | 6 ++-- .../postgrest-js/src/PostgrestQueryBuilder.ts | 6 ++-- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/packages/core/postgrest-js/src/PostgrestBuilder.ts b/packages/core/postgrest-js/src/PostgrestBuilder.ts index db9a31cc236..580c78c6f78 100644 --- a/packages/core/postgrest-js/src/PostgrestBuilder.ts +++ b/packages/core/postgrest-js/src/PostgrestBuilder.ts @@ -18,9 +18,24 @@ import { ContainsNull } from './select-query-parser/types' /** * Sleep for a given number of milliseconds. + * If an AbortSignal is provided, the sleep resolves early when the signal is aborted. */ -function sleep(ms: number): Promise { - return new Promise((resolve) => setTimeout(resolve, ms)) +function sleep(ms: number, signal?: AbortSignal): Promise { + return new Promise((resolve) => { + if (signal?.aborted) { + resolve() + return + } + const id = setTimeout(() => { + signal?.removeEventListener('abort', onAbort) + resolve() + }, ms) + function onAbort() { + clearTimeout(id) + resolve() + } + signal?.addEventListener('abort', onAbort) + }) } /** @@ -159,9 +174,9 @@ export default abstract class PostgrestBuilder< * * Configure retry behavior for this request. * - * By default, retries are enabled for GET/HEAD requests that fail with - * 520 status codes (Cloudflare timeout/connection errors). Retries use - * exponential backoff (1s, 2s, 4s) with a maximum of 3 attempts. + * By default, retries are enabled for idempotent requests (GET, HEAD, OPTIONS) + * that fail with network errors or specific HTTP status codes (503, 520). + * Retries use exponential backoff (1s, 2s, 4s) with a maximum of 3 attempts. * * @param enabled - Whether to enable retries for this request * @@ -251,7 +266,7 @@ export default abstract class PostgrestBuilder< if (this.retryEnabled && attemptCount < DEFAULT_MAX_RETRIES) { const delay = getRetryDelay(attemptCount) attemptCount++ - await sleep(delay) + await sleep(delay, this.signal) continue } @@ -268,7 +283,7 @@ export default abstract class PostgrestBuilder< : getRetryDelay(attemptCount) await res.text() attemptCount++ - await sleep(delay) + await sleep(delay, this.signal) continue } diff --git a/packages/core/postgrest-js/src/PostgrestClient.ts b/packages/core/postgrest-js/src/PostgrestClient.ts index 46777eebb63..41c49f04dc6 100644 --- a/packages/core/postgrest-js/src/PostgrestClient.ts +++ b/packages/core/postgrest-js/src/PostgrestClient.ts @@ -55,9 +55,9 @@ export default class PostgrestClient< * @param options.timeout - Optional timeout in milliseconds for all requests. When set, requests will automatically abort after this duration to prevent indefinite hangs. * @param options.urlLengthLimit - Maximum URL length in characters before warnings/errors are triggered. Defaults to 8000. * @param options.retry - Enable or disable automatic retries for transient errors. - * When enabled, GET/HEAD requests that fail with 520 errors (Cloudflare timeouts) - * will be automatically retried up to 3 times with exponential backoff. - * Defaults to `true`. + * When enabled, idempotent requests (GET, HEAD, OPTIONS) that fail with network + * errors or HTTP 503/520 responses will be automatically retried up to 3 times + * with exponential backoff (1s, 2s, 4s). Defaults to `true`. * @example * ```ts * import { PostgrestClient } from '@supabase/postgrest-js' diff --git a/packages/core/postgrest-js/src/PostgrestQueryBuilder.ts b/packages/core/postgrest-js/src/PostgrestQueryBuilder.ts index 3c890979b92..0b67faf2c79 100644 --- a/packages/core/postgrest-js/src/PostgrestQueryBuilder.ts +++ b/packages/core/postgrest-js/src/PostgrestQueryBuilder.ts @@ -24,9 +24,9 @@ export default class PostgrestQueryBuilder< /** * Enable or disable automatic retries for transient errors. - * When enabled, idempotent requests (GET/HEAD/OPTIONS) that fail with 520 or 503 errors - * are automatically retried with exponential backoff (up to 3 attempts). - * Defaults to `true` when not specified. + * When enabled, idempotent requests (GET/HEAD/OPTIONS) that fail with network + * errors or HTTP 503/520 responses are automatically retried with exponential + * backoff (1s, 2s, 4s, up to 3 attempts). Defaults to `true` when not specified. */ retry?: boolean