Files
supabase/apps/studio/data/analytics/project-log-requests-count-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

51 lines
1.4 KiB
TypeScript

import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import { get, handleError } from 'data/fetchers'
import type { ResponseError } from 'types'
import { analyticsKeys } from './keys'
export type ProjectLogRequestsCountVariables = {
projectRef?: string
}
export async function getProjectLogRequestsCountStats(
{ projectRef }: ProjectLogRequestsCountVariables,
signal?: AbortSignal
) {
if (!projectRef) {
throw new Error('projectRef is required')
}
const { data, error } = await get(
'/platform/projects/{ref}/analytics/endpoints/usage.api-requests-count',
{
params: { path: { ref: projectRef } },
signal,
}
)
if (error) handleError(error)
return data
}
export type ProjectLogRequestsCountData = Awaited<
ReturnType<typeof getProjectLogRequestsCountStats>
>
export type ProjectLogRequestsCountError = ResponseError
export const useProjectLogRequestsCountQuery = <TData = ProjectLogRequestsCountData>(
{ projectRef }: ProjectLogRequestsCountVariables,
{
enabled = true,
...options
}: UseQueryOptions<ProjectLogRequestsCountData, ProjectLogRequestsCountError, TData> = {}
) =>
useQuery<ProjectLogRequestsCountData, ProjectLogRequestsCountError, TData>(
analyticsKeys.usageApiRequestsCount(projectRef),
({ signal }) => getProjectLogRequestsCountStats({ projectRef }, signal),
{
enabled: enabled && typeof projectRef !== 'undefined',
...options,
}
)