import { OAuthScope } from '@supabase/shared-types/out/constants' import { useQuery } from '@tanstack/react-query' import { components } from 'api-types' import { z } from 'zod' import { oauthAppKeys } from './keys' import { get, handleError } from '@/data/fetchers' import type { StudioError } from '@/lib/errors/studio-errors' import type { UseCustomQueryOptions } from '@/types' export type OAuthAppsVariables = { slug?: string } export type OAuthApp = Omit & { scopes?: OAuthScope[] } 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.map((app): OAuthApp => ({ ...app, scopes: z.array(z.nativeEnum(OAuthScope)).optional().parse(app.scopes), })) } export type OAuthAppsData = Awaited> export type OAuthAppsError = StudioError export const useOAuthAppsQuery = ( { slug }: OAuthAppsVariables, { enabled = true, ...options }: UseCustomQueryOptions = {} ) => useQuery({ queryKey: oauthAppKeys.oauthApps(slug), queryFn: () => getOAuthApps({ slug }), enabled: enabled && typeof slug !== 'undefined', ...options, })