-
-
Notifications
You must be signed in to change notification settings - Fork 174
fix: rpc method #156
fix: rpc method #156
Conversation
I'm trying to fix this issue supabase/supabase-js#132 |
It looks like the function was previously working. I wonder if we added something that broke it?
I'm guessing the library is returning an error because we are trying to do some sort of |
It seems to be a postgrest error. For void function, postgrest doesn't return anything. my test function CREATE FUNCTION public.void_func()
RETURNS void AS $$
$$ LANGUAGE SQL; When i create a unit test. It returns properly for my PR and the current However, when I test with a code playground and a real supabase project. postgrest doesn't complete the request for a void function. |
Let's wait to see what @steve-chavez thinks. I believe we're using the nightly version in prod so that we can update the config without downtime |
I see, looks like PostgREST 7.0.1 is returning $ curl -s 'http://localhost:3000/rpc/void_func' -H 'Content-Type: application/json' -H 'Accept: application/json' -d '{"param":"foo"}'
null But it returns nothing on nightly. |
Yes, this was a side-effect of fixing functions that return Didn't realize it would break
Postgrest do supports HEAD on RPC, but the |
Another suggestion for fixing the issue: if (body === '') return undefined
return JSON.parse(body) |
i improve fetch response parsing method. It can handle empty result |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a couple comments, otherwise lgtm 👍
src/lib/types.ts
Outdated
if (text && text !== '') { | ||
try { | ||
data = JSON.parse(text) | ||
} catch (_) { | ||
error = { message: 'Failed to parse json response' } | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should just do nothing in catch? Not sure if it's a good idea to "forge" PostgREST errors. Otherwise you'll also need other fields in PostgrestError:
Lines 8 to 13 in 9ecb985
interface PostgrestError { | |
message: string | |
details: string | |
hint: string | |
code: string | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because users always expect json response from PostgREST. So if the response.text has content and we can't parse it to json. An error message is reasonable.
Actually, the error message is for us. I don't think users can do anything with this error. It's either PostgREST bug or postgrest-js bug.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, either way the user gets an unexpected error and files a bug. I guess we can keep old behavior and add special case for text === ''
? Loud and explicit error > silent error IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let do less. I remove try/catch block.
src/lib/types.ts
Outdated
try { | ||
error = JSON.parse(text) | ||
} catch (_) { | ||
error = { message: text } | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can revert this to the original error = await res.json()
.
Just want to be safe here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reverted to error = await res.json()
data = null | ||
const isReturnMinimal = this.headers['Prefer']?.split(',').includes('return=minimal') | ||
if (this.method !== 'HEAD' && !isReturnMinimal) { | ||
const text = await res.text() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will res.text
ever be undefined here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice one, thanks @phamhieu 👍
🎉 This PR is included in version 0.24.4 🎉 The release is available on: Your semantic-release bot 📦🚀 |
What kind of change does this PR introduce?
rpc
method from PostgrestQueryBuilder to PostgrestRpcBuilderPostgrestClient.rpc()
options paramWhat is the current behavior?
rpc
method insides PostgrestQueryBuilder. User can create this chainlet res = await postgrest.from('users').rpc({ name_param: 'supabot'})
It's wrong.
What is the new behavior?
postgrest.rpc
usage is the same after moving it to PostgrestRpcBuilderrpc
options param