import { useQuery } from '@tanstack/react-query' import { jitDbAccessKeys } from './keys' import { get, handleError, unguarded } from '@/data/fetchers' import type { StudioError } from '@/lib/errors/studio-errors' import type { UseCustomQueryOptions } from '@/types' type JitDbAccessVariables = { projectRef?: string } async function getJitDbAccessConfiguration( { projectRef }: JitDbAccessVariables, signal?: AbortSignal ) { if (!projectRef) throw new Error('projectRef is required') const { data, error } = await get(`/v1/projects/{ref}/jit-access`, { params: { path: { ref: projectRef } }, signal, grant: unguarded.migrating, }) if (error) handleError(error) return data } type JitDbAccessData = Awaited> export const useJitDbAccessQuery = ( { projectRef }: JitDbAccessVariables, { enabled = true, ...options }: UseCustomQueryOptions = {} ) => useQuery({ queryKey: jitDbAccessKeys.list(projectRef), queryFn: ({ signal }) => getJitDbAccessConfiguration({ projectRef }, signal), enabled: enabled && typeof projectRef !== 'undefined', ...options, })