Skip to content

Commit 7799e2d

Browse files
authored
fix(postgrest): fix maybeSingle for all request methods by removing Accept header override (#2182)
1 parent 15254fa commit 7799e2d

4 files changed

Lines changed: 11 additions & 42 deletions

File tree

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,8 @@ export default abstract class PostgrestBuilder<
176176
count = parseInt(contentRange[1])
177177
}
178178

179-
// Temporary partial fix for https://github.com/supabase/postgrest-js/issues/361
180-
// Issue persists e.g. for `.insert([...]).select().maybeSingle()`
181-
if (this.isMaybeSingle && this.method === 'GET' && Array.isArray(data)) {
179+
// Fix for https://github.com/supabase/postgrest-js/issues/361 — applies to all methods.
180+
if (this.isMaybeSingle && Array.isArray(data)) {
182181
if (data.length > 1) {
183182
error = {
184183
// https://github.com/PostgREST/postgrest/blob/a867d79c42419af16c18c3fb019eba8df992626f/src/PostgREST/Error.hs#L553
@@ -222,12 +221,6 @@ export default abstract class PostgrestBuilder<
222221
}
223222
}
224223

225-
if (error && this.isMaybeSingle && error?.details?.includes('0 rows')) {
226-
error = null
227-
status = 200
228-
statusText = 'OK'
229-
}
230-
231224
if (error && this.shouldThrowOnError) {
232225
throw new PostgrestError(error)
233226
}

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -685,13 +685,8 @@ export default class PostgrestTransformBuilder<
685685
maybeSingle<
686686
ResultOne = Result extends (infer ResultOne)[] ? ResultOne : never,
687687
>(): PostgrestBuilder<ClientOptions, ResultOne | null> {
688-
// Temporary partial fix for https://github.com/supabase/postgrest-js/issues/361
689-
// Issue persists e.g. for `.insert([...]).select().maybeSingle()`
690-
if (this.method === 'GET') {
691-
this.headers.set('Accept', 'application/json')
692-
} else {
693-
this.headers.set('Accept', 'application/vnd.pgrst.object+json')
694-
}
688+
// No Accept header override — we fetch as a list and enforce cardinality client-side.
689+
// Fixes https://github.com/supabase/postgrest-js/issues/361 for all request methods.
695690
this.isMaybeSingle = true
696691
return this as unknown as PostgrestBuilder<ClientOptions, ResultOne | null>
697692
}

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

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2157,29 +2157,8 @@ test('handles empty body with 404 status', async () => {
21572157
`)
21582158
})
21592159

2160-
test('maybeSingle handles zero rows error', async () => {
2161-
const customFetch = jest.fn().mockImplementation(() =>
2162-
Promise.resolve({
2163-
ok: false,
2164-
status: 406,
2165-
statusText: 'Not Acceptable',
2166-
text: () =>
2167-
Promise.resolve(
2168-
JSON.stringify({
2169-
code: 'PGRST116',
2170-
details: '0 rows',
2171-
hint: null,
2172-
message: 'JSON object requested, multiple (or no) rows returned',
2173-
})
2174-
),
2175-
})
2176-
)
2177-
2178-
const postgrestWithCustomFetch = new PostgrestClient<Database>(REST_URL, {
2179-
fetch: customFetch,
2180-
})
2181-
2182-
const res = await postgrestWithCustomFetch.from('users').select().maybeSingle()
2160+
test('maybeSingle returns null for zero rows (GET)', async () => {
2161+
const res = await postgrest.from('users').select().eq('username', 'does_not_exist').maybeSingle()
21832162

21842163
expect(res).toMatchInlineSnapshot(`
21852164
{

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,9 @@ test('maybeSingle', async () => {
228228
"data": null,
229229
"error": {
230230
"code": "PGRST116",
231-
"details": "The result contains 2 rows",
231+
"details": "Results contain 2 rows, application/vnd.pgrst.object+json requires 1 row",
232232
"hint": null,
233-
"message": "Cannot coerce the result to a single JSON object",
233+
"message": "JSON object requested, multiple (or no) rows returned",
234234
},
235235
"status": 406,
236236
"statusText": "Not Acceptable",
@@ -286,7 +286,9 @@ test('csv', async () => {
286286
kiwicopple,,"[25,35)",OFFLINE,"'bat' 'cat'"
287287
awailas,,"[25,35)",ONLINE,"'bat' 'rat'"
288288
jsonuser,"{""foo"": {""bar"": {""nested"": ""value""}, ""baz"": ""string value""}}","[20,30)",ONLINE,"'json' 'test'"
289-
dragarcia,,"[20,30)",ONLINE,"'fat' 'rat'"",
289+
dragarcia,,"[20,30)",ONLINE,"'fat' 'rat'"
290+
a,,,ONLINE,
291+
b,,,ONLINE,",
290292
"error": null,
291293
"status": 200,
292294
"statusText": "OK",

0 commit comments

Comments
 (0)