Files
supabase/apps/studio/data/api-keys/api-key-id-query.ts

47 lines
1.3 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { apiKeysKeys } from './keys'
import { get, handleError } from '@/data/fetchers'
import type { ResponseError, UseCustomQueryOptions } from '@/types'
export interface APIKeyVariables {
projectRef?: string
id?: string
reveal: boolean
}
export async function getAPIKeysById(
{ projectRef, id, reveal }: APIKeyVariables,
signal?: AbortSignal
) {
if (typeof projectRef === 'undefined') throw new Error('projectRef is required')
if (typeof id === 'undefined') throw new Error('Content ID is required')
const { data, error } = await get('/v1/projects/{ref}/api-keys/{id}', {
params: {
path: { ref: projectRef, id },
query: { reveal },
},
signal,
})
if (error) {
handleError(error)
}
return data
}
export type APIKeyIdData = Awaited<ReturnType<typeof getAPIKeysById>>
export const useAPIKeyIdQuery = <TData = APIKeyIdData>(
{ projectRef, id, reveal }: APIKeyVariables,
{ enabled = true, ...options }: UseCustomQueryOptions<APIKeyIdData, ResponseError, TData> = {}
) =>
useQuery<APIKeyIdData, ResponseError, TData>({
queryKey: apiKeysKeys.single(projectRef, id),
queryFn: ({ signal }) => getAPIKeysById({ projectRef, id, reveal }, signal),
enabled: enabled && typeof projectRef !== 'undefined' && typeof id !== 'undefined',
...options,
})