Skip to content

feat(postgrest): add success discriminator field to PostgREST response types - #2198

Merged
mandarini merged 4 commits into
supabase:masterfrom
claygeo:feat/add-success-discriminator
Apr 2, 2026
Merged

mandarini merged 4 commits into
supabase:masterfrom
claygeo:feat/add-success-discriminator

Conversation

@claygeo

@claygeo claygeo commented Mar 27, 2026

Copy link
Copy Markdown
Contributor

Problem

PostgrestResponseSuccess<T> and PostgrestResponseFailure form a union type but lack a boolean discriminator field. Users must narrow via if (res.error) rather than a clean boolean check, and TypeScript doesn't guarantee exhaustive narrowing.

Solution

Add a success boolean literal field as a discriminated union discriminator:

const res = await supabase.from('users').select()
if (res.success) {
  res.data   // T (narrowed)
  res.error  // null (narrowed)
} else {
  res.error  // PostgrestError (narrowed)
  res.data   // null (narrowed)
}

The field name success aligns with Zod's safeParse() result type, which uses the same data/error pattern with a success boolean discriminator. This was the community consensus in #1905.

Changes

File Change
packages/core/postgrest-js/src/types/types.ts Added success: true to Success, success: false to Failure
packages/core/postgrest-js/src/PostgrestBuilder.ts Set success at runtime in both success and fetch error catch paths

Backwards Compatibility

Additive only. Existing code that narrows via if (res.error) continues to work unchanged. No existing fields are modified or removed.

Ref: #1905

@claygeo
claygeo requested review from a team as code owners March 27, 2026 18:26
steve-chavez
steve-chavez previously approved these changes Mar 30, 2026
@pkg-pr-new

pkg-pr-new Bot commented Mar 30, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@supabase/auth-js

npm i https://pkg.pr.new/@supabase/auth-js@2198

@supabase/functions-js

npm i https://pkg.pr.new/@supabase/functions-js@2198

@supabase/postgrest-js

npm i https://pkg.pr.new/@supabase/postgrest-js@2198

@supabase/realtime-js

npm i https://pkg.pr.new/@supabase/realtime-js@2198

@supabase/storage-js

npm i https://pkg.pr.new/@supabase/storage-js@2198

@supabase/supabase-js

npm i https://pkg.pr.new/@supabase/supabase-js@2198

commit: 46e268a

@claygeo

claygeo commented Mar 30, 2026

Copy link
Copy Markdown
Contributor Author

Good catch @mahdirajaee — updated success: !error to success: error === null for the explicit null check. The !error approach would silently break the discriminant if PostgREST ever returned a falsy-but-non-null error shape, whereas === null directly matches the declared PostgrestError | null type and won't misbehave on edge cases.

mandarini
mandarini previously approved these changes Apr 1, 2026
@mandarini mandarini changed the title feat: add success discriminator field to PostgREST response types feat(postgrest): add success discriminator field to PostgREST response types Apr 1, 2026
@mandarini mandarini self-assigned this Apr 1, 2026
kmaclip and others added 3 commits April 2, 2026 15:34
PostgrestResponseSuccess and PostgrestResponseFailure form a union
type but lack a boolean discriminator, forcing users to narrow via
error checking rather than a simple boolean check.

Adds success: true/false literal field to both interfaces, enabling
TypeScript discriminated union narrowing:

  const res = await supabase.from('users').select()
  if (res.success) {
    res.data  // T (narrowed)
  } else {
    res.error // PostgrestError (narrowed)
  }

The field name "success" aligns with Zod's safeParse() convention.

Additive change only, fully backwards compatible.

Ref: #1905
grdsdev
grdsdev previously approved these changes Apr 2, 2026
@mandarini mandarini linked an issue Apr 2, 2026 that may be closed by this pull request
4 tasks done
mandarini pushed a commit to supabase/ssr that referenced this pull request Apr 7, 2026
This PR updates `@supabase/supabase-js` to v2.102.1.

**Source**: supabase-js-stable-release

---

## Release Notes

## v2.102.1

## 2.102.1 (2026-04-07)

### 🩹 Fixes

- **functions:** add toJSON to FunctionsError for correct JSON
serialization
([#2226](supabase/supabase-js#2226))

### ❤️ Thank You

- oniani1
## v2.102.0

## 2.102.0 (2026-04-07)

### 🚀 Features

- **postgrest:** add automatic retries for transient errors
([#2072](supabase/supabase-js#2072))
- **postgrest:** add success discriminator field to PostgREST response
types ([#2198](supabase/supabase-js#2198))
- **supabase:** export PostgrestFilterBuilder and StorageApiError from
supabase-js ([#2222](supabase/supabase-js#2222))

### 🩹 Fixes

- **auth:** downgrade console.error to console.warn for missing session
([#2214](supabase/supabase-js#2214))
- **ci:** add --ignore-scripts to platform test installs to block post
install attacks
([#2206](supabase/supabase-js#2206))
- **postgrest:** add toJSON to PostgrestError for correct JSON
serialization
([#2212](supabase/supabase-js#2212))
- **postgrest:** reject excess properties in insert, update, and upsert
([#2186](supabase/supabase-js#2186))
- **storage:** set correct content-type for uploads
([#2211](supabase/supabase-js#2211))
- **storage:** avoid duplicate content-type headers in vector requests
([#2220](supabase/supabase-js#2220))

### ❤️ Thank You

- Clay
- Guilherme Souza
- Katerina Skroumpelou @mandarini
- oniani1
- Vaibhav @7ttp

This PR was created automatically.

Co-authored-by: supabase-workflow-trigger[bot] <266661614+supabase-workflow-trigger[bot]@users.noreply.github.com>
mandarini pushed a commit to supabase/supabase that referenced this pull request Apr 7, 2026
This PR updates @supabase/*-js libraries to version 2.102.1.

**Source**: supabase-js-stable-release

**Changes**:
- Updated @supabase/supabase-js to 2.102.1
- Updated @supabase/auth-js to 2.102.1
- Updated @supabase/realtime-js to 2.102.1
- Updated @supabase/postgest-js to 2.102.1
- Refreshed pnpm-lock.yaml

---

## Release Notes

## v2.102.1

## 2.102.1 (2026-04-07)

### 🩹 Fixes

- **functions:** add toJSON to FunctionsError for correct JSON
serialization
([#2226](supabase/supabase-js#2226))

### ❤️ Thank You

- oniani1
## v2.102.0

## 2.102.0 (2026-04-07)

### 🚀 Features

- **postgrest:** add automatic retries for transient errors
([#2072](supabase/supabase-js#2072))
- **postgrest:** add success discriminator field to PostgREST response
types ([#2198](supabase/supabase-js#2198))
- **supabase:** export PostgrestFilterBuilder and StorageApiError from
supabase-js ([#2222](supabase/supabase-js#2222))

### 🩹 Fixes

- **auth:** downgrade console.error to console.warn for missing session
([#2214](supabase/supabase-js#2214))
- **ci:** add --ignore-scripts to platform test installs to block post
install attacks
([#2206](supabase/supabase-js#2206))
- **postgrest:** add toJSON to PostgrestError for correct JSON
serialization
([#2212](supabase/supabase-js#2212))
- **postgrest:** reject excess properties in insert, update, and upsert
([#2186](supabase/supabase-js#2186))
- **storage:** set correct content-type for uploads
([#2211](supabase/supabase-js#2211))
- **storage:** avoid duplicate content-type headers in vector requests
([#2220](supabase/supabase-js#2220))

### ❤️ Thank You

- Clay
- Guilherme Souza
- Katerina Skroumpelou @mandarini
- oniani1
- Vaibhav @7ttp
## v2.101.1

## 2.101.1 (2026-03-31)

### 🩹 Fixes

- **storage:** support exactOptionalPropertyTypes
([#2200](supabase/supabase-js#2200))

### ❤️ Thank You

- Vaibhav @7ttp

This PR was created automatically.

Co-authored-by: supabase-workflow-trigger[bot] <266661614+supabase-workflow-trigger[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a type discriminator to result types

4 participants