Files
supabase/apps/studio/data/database-integrations/stripe/stripe-sync-install-mutation.ts
Charis df742bdb3b refactor(studio): rename ResponseError to StudioError (#52707)
## 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.
2026-11-03 11:09:30 -05:00

96 lines
2.6 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { getAccessToken } from 'common'
import { toast } from 'sonner'
import { stripeSyncKeys } from './keys'
import { databaseKeys } from '@/data/database/keys'
import { BASE_PATH } from '@/lib/constants'
import type { StudioError } from '@/lib/errors/studio-errors'
import { useTrack } from '@/lib/telemetry/track'
export type StripeSyncInstallVariables = {
projectRef: string
stripeSecretKey: string
startTime: number
}
export type StripeSyncInstallResponse = {
data: { message: string } | null
error: { message: string } | null
}
export async function installStripeSync({
projectRef,
stripeSecretKey,
startTime,
}: StripeSyncInstallVariables): Promise<StripeSyncInstallResponse> {
const accessToken = await getAccessToken()
const response = await fetch(`${BASE_PATH}/api/integrations/stripe-sync`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify({
projectRef,
stripeSecretKey,
startTime,
}),
})
const result = await response.json()
if (!response.ok) {
throw new Error(result.error?.message || 'Failed to install Stripe Sync')
}
return result
}
type StripeSyncInstallData = Awaited<ReturnType<typeof installStripeSync>>
export const useStripeSyncInstallMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<StripeSyncInstallData, StudioError, StripeSyncInstallVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
const track = useTrack()
return useMutation<StripeSyncInstallData, StudioError, StripeSyncInstallVariables>({
mutationFn: (vars) => installStripeSync(vars),
async onSuccess(data, variables, context, mutationContext) {
const { projectRef } = variables
track('integration_install_submitted', {
integrationName: 'stripe_sync_engine',
method: 'template',
})
// Invalidate schemas query to refresh installation status
await queryClient.invalidateQueries({
queryKey: databaseKeys.schemas(projectRef),
})
// Also invalidate any stripe sync related queries
await queryClient.invalidateQueries({
queryKey: stripeSyncKeys.all,
})
await onSuccess?.(data, variables, context, mutationContext)
},
async onError(data, variables, context, mutationContext) {
if (onError === undefined) {
toast.error(`Failed to install Stripe Sync: ${data.message}`)
} else {
onError(data, variables, context, mutationContext)
}
},
...options,
})
}