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.
67 lines
2.6 KiB
TypeScript
67 lines
2.6 KiB
TypeScript
import { AuthError, type CustomOAuthProvider } from '@supabase/auth-js'
|
|
import { useQuery } from '@tanstack/react-query'
|
|
|
|
import { oAuthCustomProvidersKeys } from './keys'
|
|
import { useAuthConfigQuery } from '@/data/auth/auth-config-query'
|
|
import { useProjectApiUrl } from '@/data/config/project-endpoint-query'
|
|
import { handleError } from '@/data/fetchers'
|
|
import type { StudioError } from '@/lib/errors/studio-errors'
|
|
import { createProjectSupabaseClient } from '@/lib/project-supabase-client'
|
|
import type { UseCustomQueryOptions } from '@/types'
|
|
|
|
export type OAuthCustomProvidersVariables = {
|
|
projectRef: string | undefined
|
|
page?: number
|
|
}
|
|
|
|
export async function getOAuthCustomProviders({
|
|
projectRef,
|
|
clientEndpoint,
|
|
}: OAuthCustomProvidersVariables & { clientEndpoint: string | undefined }) {
|
|
if (!projectRef) throw new Error('Project reference is required')
|
|
if (!clientEndpoint) throw new Error('Client endpoint is required')
|
|
|
|
const supabaseClient = await createProjectSupabaseClient(projectRef, clientEndpoint)
|
|
|
|
const { data, error } = await supabaseClient.auth.admin.customProviders.listProviders()
|
|
|
|
if (error) {
|
|
// Non-JSON responses from the API indicate custom providers aren't enabled.
|
|
// Different browsers/SDK versions produce different JSON parse error messages,
|
|
// so we check broadly for JSON parse indicators.
|
|
if (/JSON\.parse|Unexpected token|unexpected.*character/i.test(error.message)) {
|
|
handleError(new AuthError('Custom providers are not enabled for this project'))
|
|
} else {
|
|
handleError(error)
|
|
}
|
|
}
|
|
return data.providers as CustomOAuthProvider[]
|
|
}
|
|
|
|
export type OAuthCustomProvidersData = Awaited<ReturnType<typeof getOAuthCustomProviders>>
|
|
export type OAuthCustomProvidersError = StudioError
|
|
|
|
export const useOAuthCustomProvidersQuery = <TData = OAuthCustomProvidersData>(
|
|
{ projectRef }: OAuthCustomProvidersVariables,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: UseCustomQueryOptions<OAuthCustomProvidersData, OAuthCustomProvidersError, TData> = {}
|
|
) => {
|
|
const { hostEndpoint: clientEndpoint } = useProjectApiUrl({ projectRef })
|
|
const { data: authConfig, isSuccess: isSuccessConfig } = useAuthConfigQuery({ projectRef })
|
|
const isOAuthCustomProvidersEnabled = !!authConfig?.CUSTOM_OAUTH_ENABLED
|
|
|
|
return useQuery<OAuthCustomProvidersData, OAuthCustomProvidersError, TData>({
|
|
queryKey: oAuthCustomProvidersKeys.list(projectRef, clientEndpoint),
|
|
queryFn: () => getOAuthCustomProviders({ projectRef, clientEndpoint }),
|
|
enabled:
|
|
enabled &&
|
|
typeof projectRef !== 'undefined' &&
|
|
!!clientEndpoint &&
|
|
isSuccessConfig &&
|
|
isOAuthCustomProvidersEnabled,
|
|
...options,
|
|
})
|
|
}
|