Files
supabase/apps/studio/data/oauth-server-apps/oauth-openid-configuration-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

93 lines
2.7 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { useAuthConfigQuery } from '../auth/auth-config-query'
import { oauthServerAppKeys } from './keys'
import { useProjectApiUrl } from '@/data/config/project-endpoint-query'
import { handleError } from '@/data/fetchers'
import type { StudioError } from '@/lib/errors/studio-errors'
import type { UseCustomQueryOptions } from '@/types'
export type OpenIDConfigurationVariables = {
projectRef: string | undefined
}
/**
* OpenID Connect Discovery response
* @see https://openid.net/specs/openid-connect-discovery-1_0.html
*/
export type OpenIDConfiguration = {
issuer: string
authorization_endpoint: string
token_endpoint: string
userinfo_endpoint?: string
jwks_uri: string
registration_endpoint?: string
scopes_supported?: string[]
response_types_supported?: string[]
grant_types_supported?: string[]
subject_types_supported?: string[]
id_token_signing_alg_values_supported?: string[]
token_endpoint_auth_methods_supported?: string[]
code_challenge_methods_supported?: string[]
}
export async function getOpenIDConfiguration({
endpoint,
}: {
endpoint: string | undefined
}): Promise<OpenIDConfiguration> {
if (!endpoint) throw new Error('Client endpoint is required')
const response = await fetch(`${endpoint}/auth/v1/.well-known/openid-configuration`)
if (!response.ok) {
handleError({ message: `Failed to fetch OpenID configuration: ${response.statusText}` })
}
return response.json()
}
export type OpenIDConfigurationData = Awaited<ReturnType<typeof getOpenIDConfiguration>>
export type OpenIDConfigurationError = StudioError
export const useOpenIDConfigurationQuery = <TData = OpenIDConfigurationData>(
{ projectRef }: OpenIDConfigurationVariables,
{
enabled = true,
...options
}: UseCustomQueryOptions<OpenIDConfigurationData, OpenIDConfigurationError, TData> = {}
) => {
const { hostEndpoint, isPending: isEndpointLoading } = useProjectApiUrl({
projectRef,
})
const {
data: authConfig,
isSuccess: isSuccessConfig,
isLoading: isAuthConfigLoading,
} = useAuthConfigQuery({ projectRef })
const isOAuthServerEnabled = !!authConfig?.OAUTH_SERVER_ENABLED
const isQueryEnabled =
enabled &&
typeof projectRef !== 'undefined' &&
!!hostEndpoint &&
isSuccessConfig &&
isOAuthServerEnabled
const query = useQuery<OpenIDConfigurationData, OpenIDConfigurationError, TData>({
queryKey: oauthServerAppKeys.openidConfiguration(projectRef),
queryFn: () => getOpenIDConfiguration({ endpoint: hostEndpoint }),
enabled: isQueryEnabled,
...options,
})
// Include loading states from dependencies
const isLoading = query.isLoading || isEndpointLoading || isAuthConfigLoading
return {
...query,
isLoading,
}
}