-
-
Notifications
You must be signed in to change notification settings - Fork 269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
throwOnError
with await returns nullable type
#801
Comments
Thank you for opening this issue. I was about to open an issue for this exact thing. |
One workaround until it's resolved: const get = async (id: string) => {
const entity = await supabase.client.from('auth.users').select(`*`)
.eq('id', id)
.single()
.throwOnError();
return entity.data as NonNullable<typeof entity.data>;
} |
I opened a PR to support this: supabase/postgrest-js#494 I got impatient so I published it under @rebundled/postgrest-js. Here's how you can use it with npm install @rebundled/postgrest-js import { CookieOptions, createServerClient } from '@supabase/ssr'
import { PostgrestClient } from '@rebundled/postgrest-js'
import { Database } from '~/db'
export const createClient = () => {
const client = createServerClient<Database>(
process.env.NEXT_PUBLIC_SUPABASE_URL,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
)
// @ts-expect-error `.rest` is protected
const { rest } = client
const wrapper = new PostgrestClient<Database>(rest.url, {
fetch: rest.fetch,
headers: rest.headers,
schema: rest.schemaName,
}).throwOnError()
return Object.assign(wrapper, {
rest,
realtime: client.realtime,
auth: client.auth,
})
} You can then use like: import {createClient} from '..'
export default async () => {
const client = createClient()
const { data: user } = await client.from('users').select('*').single()
console.log(`Hello ${user.username}`) // no need to check truthiness/ throw manually
} If you need access to the non-throwing client, you can use |
+1. this is kinda annoying tbh |
Would changing the functionality of One alternative could just be more chainable throw methods, such as: throwOnEmpty()
throwOnFalsy()
throwOn( function(){} ) |
I don't see why it would be backwards compatible to return non-nullable when returning an array of items. |
Any update on this feature ? |
In addition to supabase/postgrest-js#494 there is also supabase/postgrest-js#364. That PR is also in limbo, linking here just because it is relevant to the issue at hand. Out of curiosity, does anyone see the issue with returning directly the value of |
Any update on this feature? |
Bug report
Describe the bug
.throwOnError
should return non-nullable type because since in a case of error it will throw instead of returning anerror
object with nulldata
object.To Reproduce
See below code for example:
In this example return type is nullabe
{ data: { id: string } } | null
but it shouldn't because it'll throw on error.In this case I have to do below to correct typing which doesn't make any sense to do extra if checks even though it's unnecessary:
Expected behavior
If
.throwOnError
called return type must be{ data: NonNullable<...>, error: null }
or even without error object if possible.Screenshots
System information
The text was updated successfully, but these errors were encountered: