mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +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.
67 lines
2.4 KiB
TypeScript
67 lines
2.4 KiB
TypeScript
import { useQuery } from '@tanstack/react-query'
|
|
|
|
import { configKeys } from './keys'
|
|
import type { components } from '@/data/api'
|
|
import { get, handleError } from '@/data/fetchers'
|
|
import { FGA_PERMISSIONS, useAsyncCheckPermissionsV2 } from '@/hooks/misc/useCheckPermissionsV2'
|
|
import type { StudioError } from '@/lib/errors/studio-errors'
|
|
import type { UseCustomQueryOptions } from '@/types'
|
|
|
|
export type ProjectSettingsVariables = { projectRef?: string }
|
|
|
|
// Manually add the protocol property to the response - specifically just for the local/CLI environment
|
|
type ProjectAppConfig = components['schemas']['ProjectSettingsResponse_Output']['app_config'] & {
|
|
protocol?: string
|
|
}
|
|
export type ProjectSettings = components['schemas']['ProjectSettingsResponse_Output'] & {
|
|
app_config?: ProjectAppConfig
|
|
}
|
|
|
|
export async function getProjectSettings(
|
|
{ projectRef }: ProjectSettingsVariables,
|
|
signal?: AbortSignal,
|
|
headers?: Record<string, string>
|
|
) {
|
|
if (!projectRef) throw new Error('projectRef is required')
|
|
|
|
const { data, error } = await get('/platform/projects/{ref}/settings', {
|
|
params: { path: { ref: projectRef } },
|
|
signal,
|
|
headers,
|
|
})
|
|
|
|
if (error) handleError(error)
|
|
return data as unknown as ProjectSettings
|
|
}
|
|
|
|
type ProjectSettingsData = Awaited<ReturnType<typeof getProjectSettings>>
|
|
type ProjectSettingsError = StudioError
|
|
|
|
export const useProjectSettingsV2Query = <TData = ProjectSettingsData>(
|
|
{ projectRef }: ProjectSettingsVariables,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: UseCustomQueryOptions<ProjectSettingsData, ProjectSettingsError, TData> = {}
|
|
) => {
|
|
// [Joshen] Sync with API perms checking here - shouldReturnApiKeys
|
|
// https://github.com/supabase/platform/blob/develop/api/src/routes/platform/projects/ref/settings.controller.ts#L92
|
|
const { can: canReadAPIKeys } = useAsyncCheckPermissionsV2(
|
|
FGA_PERMISSIONS.PROJECT.API_GATEWAY_KEYS_READ
|
|
)
|
|
|
|
return useQuery<ProjectSettingsData, ProjectSettingsError, TData>({
|
|
queryKey: configKeys.settingsV2(projectRef),
|
|
queryFn: ({ signal }) => getProjectSettings({ projectRef }, signal),
|
|
enabled: enabled && typeof projectRef !== 'undefined',
|
|
refetchInterval: (query) => {
|
|
const data = query.state.data
|
|
const apiKeys = data?.service_api_keys ?? []
|
|
const interval =
|
|
canReadAPIKeys && data?.status !== 'INACTIVE' && apiKeys.length === 0 ? 2000 : 0
|
|
return interval
|
|
},
|
|
...options,
|
|
})
|
|
}
|