Files
supabase/apps/studio/data/realtime/realtime-config-query.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

68 lines
2.2 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import type { components } from 'api-types'
import { realtimeKeys } from './keys'
import { get, handleError } from '@/data/fetchers'
import { IS_PLATFORM } from '@/lib/constants'
import type { StudioError } from '@/lib/errors/studio-errors'
import type { UseCustomQueryOptions } from '@/types'
type RealtimeConfigResponse = components['schemas']['RealtimeConfigResponse_Output']
export type RealtimeConfigurationVariables = {
projectRef?: string
}
export const REALTIME_DEFAULT_CONFIG = {
private_only: false,
connection_pool: 2,
postgres_changes_pool: 2,
max_concurrent_users: 200,
max_events_per_second: 100,
max_bytes_per_second: 100000,
max_channels_per_client: 100,
max_joins_per_second: 100,
max_presence_events_per_second: 100,
max_payload_size_in_kb: 100,
suspend: false,
presence_enabled: true,
admin_suspended_at: null,
} as const satisfies RealtimeConfigResponse
export async function getRealtimeConfiguration(
{ projectRef }: RealtimeConfigurationVariables,
signal?: AbortSignal
) {
if (!projectRef) throw new Error('Project ref is required')
const { data, error } = await get(`/platform/projects/{ref}/config/realtime`, {
params: { path: { ref: projectRef } },
signal,
})
if (error) {
if ((error as StudioError).message === 'Custom realtime config for a project not found') {
return REALTIME_DEFAULT_CONFIG
} else {
handleError(error)
}
}
return data
}
export type RealtimeConfigurationData = Awaited<ReturnType<typeof getRealtimeConfiguration>>
export type RealtimeConfigurationError = StudioError
export const useRealtimeConfigurationQuery = <TData = RealtimeConfigurationData>(
{ projectRef }: RealtimeConfigurationVariables,
{
enabled = true,
...options
}: UseCustomQueryOptions<RealtimeConfigurationData, RealtimeConfigurationError, TData> = {}
) =>
useQuery<RealtimeConfigurationData, RealtimeConfigurationError, TData>({
queryKey: realtimeKeys.configuration(projectRef),
queryFn: ({ signal }) => getRealtimeConfiguration({ projectRef }, signal),
enabled: enabled && IS_PLATFORM && typeof projectRef !== 'undefined',
...options,
})