Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

1.2 KB
Raw Blame
import { useQuery } from '@tanstack/react-query'
import { components } from 'api-types'

import { oauthAppKeys } from './keys'
import { get, handleError } from '@/data/fetchers'
import type { ResponseError, UseCustomQueryOptions } from '@/types'

export type OAuthAppsVariables = {
  slug?: string
}

export type OAuthApp = components['schemas']['OAuthAppResponse_Output']

export async function getOAuthApps({ slug }: OAuthAppsVariables) {
  if (!slug) throw new Error('Organization slug is required')

  const { data, error } = await get('/platform/organizations/{slug}/oauth/apps', {
    params: { path: { slug }, query: { type: 'published' } },
  })

  if (error) handleError(error)
  return data
}

export type OAuthAppsData = Awaited<ReturnType<typeof getOAuthApps>>
export type OAuthAppsError = ResponseError

export const useOAuthAppsQuery = <TData = OAuthAppsData>(
  { slug }: OAuthAppsVariables,
  { enabled = true, ...options }: UseCustomQueryOptions<OAuthAppsData, OAuthAppsError, TData> = {}
) =>
  useQuery<OAuthAppsData, OAuthAppsError, TData>({
    queryKey: oauthAppKeys.oauthApps(slug),
    queryFn: () => getOAuthApps({ slug }),
    enabled: enabled && typeof slug !== 'undefined',
    ...options,
  })