Files
supabase/apps/studio/data/api-keys/api-key-id-query.ts
Joshen Lim 27188c147c Support creating multiple publishable keys, and deleting publishable keys (#41186)
* Support creating multiple publishable keys, and deleting publishable keys

* FIx types

* Smol

* Smol fix

* Address issues

* Update comment

* Replace all usage of useApiKeysVisiblity for checking permissions to just call useAsyncCheckPermissions directly

* Clean up and deprecate useApiKeysVisibility hook

* ADdress
2025-12-12 16:07:36 +08:00

46 lines
1.3 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { get, handleError } from 'data/fetchers'
import type { ResponseError, UseCustomQueryOptions } from 'types'
import { apiKeysKeys } from './keys'
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,
})