mirror of
https://github.com/supabase/supabase.git
synced 2026-10-07 10:25:06 +03:00
65 lines
2.1 KiB
TypeScript
65 lines
2.1 KiB
TypeScript
import { useQuery } from '@tanstack/react-query'
|
|
import { components } from 'api-types'
|
|
|
|
import { oauthServerAppKeys } from './keys'
|
|
import { useAuthConfigQuery } from '@/data/auth/auth-config-query'
|
|
import { useProjectApiUrl } from '@/data/config/project-endpoint-query'
|
|
import { handleError } from '@/data/fetchers'
|
|
import { createProjectSupabaseClient } from '@/lib/project-supabase-client'
|
|
import type { ResponseError, UseCustomQueryOptions } from '@/types'
|
|
|
|
export type OAuthServerAppsVariables = {
|
|
projectRef: string | undefined
|
|
page?: number
|
|
}
|
|
|
|
const APPS_PER_PAGE = 100
|
|
|
|
export type OAuthApp = components['schemas']['OAuthAppResponse']
|
|
|
|
export async function getOAuthServerApps({
|
|
projectRef,
|
|
clientEndpoint,
|
|
page = 1,
|
|
}: OAuthServerAppsVariables & { 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.oauth.listClients({
|
|
page,
|
|
perPage: APPS_PER_PAGE,
|
|
})
|
|
|
|
if (error) handleError(error)
|
|
return data
|
|
}
|
|
|
|
export type OAuthServerAppsData = Awaited<ReturnType<typeof getOAuthServerApps>>
|
|
export type OAuthServerAppsError = ResponseError
|
|
|
|
export const useOAuthServerAppsQuery = <TData = OAuthServerAppsData>(
|
|
{ projectRef }: OAuthServerAppsVariables,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: UseCustomQueryOptions<OAuthServerAppsData, OAuthServerAppsError, TData> = {}
|
|
) => {
|
|
const { hostEndpoint: clientEndpoint } = useProjectApiUrl({ projectRef })
|
|
const { data: authConfig, isSuccess: isSuccessConfig } = useAuthConfigQuery({ projectRef })
|
|
const isOAuthServerEnabled = !!authConfig?.OAUTH_SERVER_ENABLED
|
|
|
|
return useQuery<OAuthServerAppsData, OAuthServerAppsError, TData>({
|
|
queryKey: oauthServerAppKeys.list(projectRef, clientEndpoint),
|
|
queryFn: () => getOAuthServerApps({ projectRef, clientEndpoint }),
|
|
enabled:
|
|
enabled &&
|
|
typeof projectRef !== 'undefined' &&
|
|
!!clientEndpoint &&
|
|
isSuccessConfig &&
|
|
isOAuthServerEnabled,
|
|
...options,
|
|
})
|
|
}
|