import { UseQueryOptions, useQuery } from '@tanstack/react-query' import { constructHeaders, fetchHandler, handleError } from 'data/fetchers' import type { ResponseError } from 'types' import { storageKeys } from './keys' type GetNamespacesVariables = { catalogUri: string warehouse: string token: string } async function getNamespaces({ catalogUri, warehouse, token }: GetNamespacesVariables) { let headers = new Headers() // handle both secret key and service role key if (token.startsWith('sb_secret_')) { headers = await constructHeaders({ 'Content-Type': 'application/json', apikey: `${token}`, }) headers.delete('Authorization') } else { headers = await constructHeaders({ 'Content-Type': 'application/json', Authorization: `Bearer ${token}`, }) } const url = `${catalogUri}/v1/${warehouse}/namespaces`.replaceAll(/(?> export type IcebergNamespacesError = ResponseError export const useIcebergNamespacesQuery = ( params: GetNamespacesVariables, { ...options }: UseQueryOptions = {} ) => { return useQuery( storageKeys.icebergNamespaces(params.catalogUri, params.warehouse), () => getNamespaces(params), { ...options } ) }