The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Refactor error handling for api authorization mutations
Joshen Limc3b1c4c32109
diff --git a/studio/data/__templates/README.md b/studio/data/__templates/README.mdindex 6fcdca24e5f..04c002df983 100644--- a/studio/data/__templates/README.md+++ b/studio/data/__templates/README.md@@ -72,7 +72,7 @@ return (### Mutations-The parameter `onError` can be passed when intializing the mutation, where an appropriate UI behavior should be triggered (usually a toast will be fine). If `onError` is not provided, we will then default to a toast message that will be called from within the mutation itself which should handle most cases. However, if there is a specific way that the error should be handled, then this should be passed via the `onError` parameter. (There's no need to repeat the error handling if the default behaviour is sufficient)+The parameter `onError` can be passed when intializing the mutation, where an appropriate UI behavior should be triggered (usually a toast will be fine). If `onError` is not provided, we will then default to a toast message that will be called from within the mutation itself which should handle most cases. However, if there is a specific way that the error should be handled, then this should be passed via the `onError` parameter. (There's no need to repeat the error handling if the default behavior is sufficient)```jsxconst { mutateAsync: someAction } = useMutation({diff --git a/studio/data/api-authorization/api-authorization-approve-mutation.ts b/studio/data/api-authorization/api-authorization-approve-mutation.tsindex 5ac01111adc..1cb0e4e0636 100644--- a/studio/data/api-authorization/api-authorization-approve-mutation.ts+++ b/studio/data/api-authorization/api-authorization-approve-mutation.ts@@ -1,6 +1,9 @@import { useMutation, UseMutationOptions } from '@tanstack/react-query'+import toast from 'react-hot-toast'+import { post } from 'lib/common/fetch'import { API_ADMIN_URL } from 'lib/constants'+import { ResponseError } from 'types'export type ApiAuthorizationApproveVariables = {id: string@@ -30,13 +33,26 @@ type ApiAuthorizationApproveData = Awaited<ReturnType<typeof approveApiAuthorizaexport const useApiAuthorizationApproveMutation = ({onSuccess,+ onError,...options}: Omit<- UseMutationOptions<ApiAuthorizationApproveData, unknown, ApiAuthorizationApproveVariables>,+ UseMutationOptions<ApiAuthorizationApproveData, ResponseError, ApiAuthorizationApproveVariables>,'mutationFn'> = {}) => {- return useMutation<ApiAuthorizationApproveData, unknown, ApiAuthorizationApproveVariables>(+ return useMutation<ApiAuthorizationApproveData, ResponseError, ApiAuthorizationApproveVariables>((vars) => approveApiAuthorization(vars),- options+ {+ async onSuccess(data, variables, context) {+ await onSuccess?.(data, variables, context)+ },+ async onError(data, variables, context) {+ if (onError === undefined) {+ toast.error(`Failed to approve authorization request: ${data.message}`)+ } else {+ onError(data, variables, context)+ }+ },+ ...options,+ })}diff --git a/studio/data/api-authorization/api-authorization-decline-mutation.ts b/studio/data/api-authorization/api-authorization-decline-mutation.tsindex 7825bb01688..47ad7429c9e 100644--- a/studio/data/api-authorization/api-authorization-decline-mutation.ts+++ b/studio/data/api-authorization/api-authorization-decline-mutation.ts@@ -1,6 +1,9 @@import { useMutation, UseMutationOptions } from '@tanstack/react-query'+import toast from 'react-hot-toast'+import { delete_ } from 'lib/common/fetch'import { API_ADMIN_URL } from 'lib/constants'+import { ResponseError } from 'types'export type ApiAuthorizationDeclineVariables = {id: string@@ -22,13 +25,26 @@ type ApiAuthorizationDeclineData = Awaited<ReturnType<typeof declineApiAuthorizaexport const useApiAuthorizationDeclineMutation = ({onSuccess,+ onError,...options}: Omit<- UseMutationOptions<ApiAuthorizationDeclineData, unknown, ApiAuthorizationDeclineVariables>,+ UseMutationOptions<ApiAuthorizationDeclineData, ResponseError, ApiAuthorizationDeclineVariables>,'mutationFn'> = {}) => {- return useMutation<ApiAuthorizationDeclineData, unknown, ApiAuthorizationDeclineVariables>(+ return useMutation<ApiAuthorizationDeclineData, ResponseError, ApiAuthorizationDeclineVariables>((vars) => declineApiAuthorization(vars),- options+ {+ async onSuccess(data, variables, context) {+ await onSuccess?.(data, variables, context)+ },+ async onError(data, variables, context) {+ if (onError === undefined) {+ toast.error(`Failed to decline authorization request: ${data.message}`)+ } else {+ onError(data, variables, context)+ }+ },+ ...options,+ })}diff --git a/studio/pages/authorize.tsx b/studio/pages/authorize.tsxindex 4d28fb24a67..41faa94974e 100644--- a/studio/pages/authorize.tsx+++ b/studio/pages/authorize.tsx@@ -22,7 +22,6 @@ const APIAuthorizationPage: NextPageWithLayout = () => {const { ui } = useStore()const router = useRouter()const { auth_id } = useParams()- const [isSubmitting, setIsSubmitting] = useState(false)const [selectedOrg, setSelectedOrg] = useState<string>()const { data: organizations, isLoading: isLoadingOrganizations } = useOrganizationsQuery()@@ -30,8 +29,11 @@ const APIAuthorizationPage: NextPageWithLayout = () => {const isApproved = requester?.approved_at !== nullconst isExpired = dayjs().isAfter(dayjs(requester?.expires_at))- const { mutateAsync: approveRequest } = useApiAuthorizationApproveMutation()- const { mutateAsync: declineRequest } = useApiAuthorizationDeclineMutation()+ const { mutateAsync: approveRequest, isLoading: isApproving } =+ useApiAuthorizationApproveMutation()+ const { mutateAsync: declineRequest, isLoading: isDeclining } =+ useApiAuthorizationDeclineMutation()+ const isSubmitting = isApproving || isDeclininguseEffect(() => {if (!isLoadingOrganizations) {@@ -53,17 +55,8 @@ const APIAuthorizationPage: NextPageWithLayout = () => {})}- try {- setIsSubmitting(true)- const res = await approveRequest({ id: auth_id, organization_id: selectedOrg })- router.push(res.url)- } catch (error: any) {- ui.setNotification({- category: 'error',- message: `Failed to approve request: ${error.message}`,- })- setIsSubmitting(false)- }+ const res = await approveRequest({ id: auth_id, organization_id: selectedOrg })+ router.push(res.url)}const onDeclineRequest = async () => {@@ -73,18 +66,9 @@ const APIAuthorizationPage: NextPageWithLayout = () => {message: 'Unable to decline request: auth_id is missing ',})- try {- setIsSubmitting(true)- await declineRequest({ id: auth_id })- ui.setNotification({ category: 'success', message: 'Declined API authorization request' })- router.push('/projects')- } catch (error: any) {- ui.setNotification({- category: 'error',- message: `Failed to decline request: ${error.message}`,- })- setIsSubmitting(false)- }+ await declineRequest({ id: auth_id })+ ui.setNotification({ category: 'success', message: 'Declined API authorization request' })+ router.push('/projects')}if (isLoading) {