Files
supabase/apps/studio/data/banned-ips/banned-ips-query.ts
T
Joshen LimandIvan Vasilov 163263c3c5 First round of wrapping RQ errors with handleError (#26384)
* First round of wrapping RQ errors with handleError

* Remove the throw before the handleError usage.

* Make the handling of an API error more versatile. Add logging in Sentry if the error is of unknown type.

* Remove throwing of the handleError function.

* Add return type to the handleError function to be never so that we're sure it always throws.

---------

Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
2024-05-17 16:30:55 +08:00

41 lines
1.2 KiB
TypeScript

import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import { handleError, post } from 'data/fetchers'
import type { ResponseError } from 'types'
import { BannedIPKeys } from './keys'
export type BannedIPVariables = {
projectRef?: string
}
export type BannedIPsData = BannedIPVariables[]
export type BannedIPsError = ResponseError
export async function getBannedIPs({ projectRef }: BannedIPVariables, signal?: AbortSignal) {
if (!projectRef) throw new Error('projectRef is required')
const { data, error } = await post(`/v1/projects/{ref}/network-bans/retrieve`, {
params: { path: { ref: projectRef } },
signal,
})
if (error) handleError(error)
return data
}
export type IPData = Awaited<ReturnType<typeof getBannedIPs>>
export type IPError = ResponseError
export const useBannedIPsQuery = <TData = IPData>(
{ projectRef }: BannedIPVariables,
{ enabled = true, ...options }: UseQueryOptions<IPData, IPError, TData> = {}
) =>
useQuery<IPData, IPError, TData>(
BannedIPKeys.list(projectRef),
({ signal }) => getBannedIPs({ projectRef }, signal),
{
enabled: enabled && typeof projectRef !== 'undefined',
...options,
}
)