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> export type OAuthAppsError = ResponseError export const useOAuthAppsQuery = ( { slug }: OAuthAppsVariables, { enabled = true, ...options }: UseCustomQueryOptions = {} ) => useQuery({ queryKey: oauthAppKeys.oauthApps(slug), queryFn: () => getOAuthApps({ slug }), enabled: enabled && typeof slug !== 'undefined', ...options, })