mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +00:00
## What
Moves the `ResponseError` class (`types/base.ts`) and the classified
error classes (`types/api-errors.ts`) into
`lib/errors/studio-errors.ts`, and renames `ResponseError` to
`StudioError`.
## Why
Studio's error classification is being expanded to cover errors that
don't come from API responses (thrown network failures, aborted
requests, unexpected client errors). The API-specific name and location
no longer fit. Landing the rename on its own keeps the follow-up PR,
which introduces the classified error model, small and reviewable.
## Notes for reviewers
- Mechanical change, no behavior change. Most of the diff is a one-line
import change per file plus import re-sorting by Prettier.
- Files that only use the type now use `import type { StudioError }`.
- `ResponseError` is no longer re-exported from `@/types`; there are no
compatibility shims at the old paths.
- Unrelated identifiers like `QueryResponseError` and
`parseResponseErrors` are unchanged.
158 lines
4.7 KiB
TypeScript
158 lines
4.7 KiB
TypeScript
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { organizationKeys } from './keys'
|
|
import type { OrganizationsData } from './organizations-query'
|
|
import type { CustomerAddress, CustomerTaxId } from './types'
|
|
import { handleError, put } from '@/data/fetchers'
|
|
import type { StudioError } from '@/lib/errors/studio-errors'
|
|
import type { UseCustomMutationOptions } from '@/types'
|
|
|
|
export type OrganizationCustomerProfileUpdateVariables = {
|
|
slug?: string
|
|
address?: CustomerAddress
|
|
billing_name?: string
|
|
/** Pass a tax ID object to set/update, `null` to clear, or `undefined` to leave unchanged */
|
|
tax_id?: CustomerTaxId | null
|
|
email?: string
|
|
additional_emails?: string[]
|
|
indirect_tax_registration_declaration?: 'yes' | 'no'
|
|
/** When true, validates the request without persisting changes */
|
|
dry_run?: boolean
|
|
}
|
|
|
|
export async function updateOrganizationCustomerProfile({
|
|
slug,
|
|
address,
|
|
billing_name,
|
|
tax_id,
|
|
email,
|
|
additional_emails,
|
|
indirect_tax_registration_declaration,
|
|
dry_run,
|
|
}: OrganizationCustomerProfileUpdateVariables) {
|
|
if (!slug) return console.error('Slug is required')
|
|
|
|
const { data, error } = await put(`/platform/organizations/{slug}/customer`, {
|
|
params: {
|
|
path: {
|
|
slug,
|
|
},
|
|
},
|
|
body: {
|
|
address: address != null ? address : undefined,
|
|
billing_name,
|
|
...(tax_id === null
|
|
? { clear_tax_id: true as const }
|
|
: tax_id !== undefined
|
|
? { tax_id }
|
|
: {}),
|
|
email,
|
|
additional_emails,
|
|
indirect_tax_registration_declaration,
|
|
...(dry_run ? { dry_run } : {}),
|
|
},
|
|
})
|
|
if (error) throw handleError(error)
|
|
return data
|
|
}
|
|
|
|
type OrganizationCustomerProfileUpdateData = Awaited<
|
|
ReturnType<typeof updateOrganizationCustomerProfile>
|
|
>
|
|
|
|
export const useOrganizationCustomerProfileUpdateMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<
|
|
OrganizationCustomerProfileUpdateData,
|
|
StudioError,
|
|
OrganizationCustomerProfileUpdateVariables
|
|
>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<
|
|
OrganizationCustomerProfileUpdateData,
|
|
StudioError,
|
|
OrganizationCustomerProfileUpdateVariables
|
|
>({
|
|
mutationFn: (vars) => updateOrganizationCustomerProfile(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const {
|
|
address,
|
|
slug,
|
|
billing_name,
|
|
tax_id,
|
|
email,
|
|
additional_emails,
|
|
indirect_tax_registration_declaration,
|
|
dry_run,
|
|
} = variables
|
|
|
|
if (dry_run) {
|
|
await onSuccess?.(data, variables, context)
|
|
return
|
|
}
|
|
|
|
// Optimistically update the cache for immediate UI consistency. Only patch the fields
|
|
// that were actually part of this mutation's variables - each caller (e.g. BillingEmail,
|
|
// BillingCustomerData) only sends the subset it owns, so an unconditional overwrite here
|
|
// would wipe out the other fields in the shared cache entry.
|
|
queryClient.setQueriesData(
|
|
{ queryKey: organizationKeys.customerProfile(slug) },
|
|
(prev: any) => {
|
|
if (!prev) return prev
|
|
return {
|
|
...prev,
|
|
...(billing_name !== undefined ? { billing_name } : {}),
|
|
...(address !== undefined ? { address } : {}),
|
|
...(email !== undefined ? { email } : {}),
|
|
...(additional_emails !== undefined ? { additional_emails: additional_emails } : {}),
|
|
}
|
|
}
|
|
)
|
|
|
|
if (tax_id !== undefined) {
|
|
queryClient.setQueryData(organizationKeys.taxId(slug), tax_id)
|
|
}
|
|
|
|
if (indirect_tax_registration_declaration !== undefined) {
|
|
queryClient.setQueryData<OrganizationsData>(organizationKeys.list(), (previous) =>
|
|
previous?.map((organization) =>
|
|
organization.slug === slug
|
|
? { ...organization, requires_indirect_tax_declaration: false }
|
|
: organization
|
|
)
|
|
)
|
|
}
|
|
|
|
// Refetch after a delay to pick up server-canonical values (e.g. normalized tax IDs).
|
|
// The GET endpoint can be stale for 1-2 seconds after an update.
|
|
setTimeout(() => {
|
|
queryClient.invalidateQueries({
|
|
queryKey: organizationKeys.customerProfile(slug),
|
|
})
|
|
if (tax_id !== undefined) {
|
|
queryClient.invalidateQueries({
|
|
queryKey: organizationKeys.taxId(slug),
|
|
})
|
|
}
|
|
}, 3000)
|
|
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to update customer profile: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|