Files
supabase/apps/studio/data/ai/check-api-key-query.ts
T
Ivan Vasilov 8b657165b5 chore: Migrate to use custom type for ReactQuery queries and mutations (#40073)
* Add custom types for queries, mutations and infinite queries.

* Migrate all queries to use the new type.

* Migrate all infinite queries to useCustomInfiniteQueryOptions.

* Migrate all mutations to use useCustomMutationOptions.

* Add type to all imports in `types` folder.
2025-11-03 13:18:13 +01:00

43 lines
1.3 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { constructHeaders, fetchHandler } from 'data/fetchers'
import { BASE_PATH, IS_PLATFORM } from 'lib/constants'
import { ResponseError, UseCustomQueryOptions } from 'types'
import { aiKeys } from './keys'
// check to see if the OPENAI_API_KEY env var is set in self-hosted
// so we can disable the chat editor and add a warning about manually adding the key
export async function checkOpenAIKey(signal?: AbortSignal) {
const headers = await constructHeaders()
const response = await fetchHandler(`${BASE_PATH}/api/ai/sql/check-api-key`, {
headers,
signal,
})
let body: any
try {
body = await response.json()
} catch {}
if (!response.ok) {
throw new ResponseError(body?.message, response.status)
}
return body as { hasKey: boolean }
}
export type ResourceData = Awaited<ReturnType<typeof checkOpenAIKey>>
export type ResourceError = { errorEventId: string; message: string }
export const useCheckOpenAIKeyQuery = <TData = ResourceData>({
enabled = true,
...options
}: UseCustomQueryOptions<ResourceData, ResourceError, TData> = {}) =>
useQuery<ResourceData, ResourceError, TData>({
queryKey: aiKeys.apiKey(),
queryFn: ({ signal }) => checkOpenAIKey(signal),
enabled: !IS_PLATFORM && enabled,
...options,
})