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

59 lines
1.8 KiB
TypeScript

import { QueryClient, useQuery } from '@tanstack/react-query'
import { get, handleError } from 'data/fetchers'
import type { ResponseError, UseCustomQueryOptions } 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
}: UseCustomQueryOptions<ProjectLogRequestsCountData, ProjectLogRequestsCountError, TData> = {}
) =>
useQuery<ProjectLogRequestsCountData, ProjectLogRequestsCountError, TData>({
queryKey: analyticsKeys.usageApiRequestsCount(projectRef),
queryFn: ({ signal }) => getProjectLogRequestsCountStats({ projectRef }, signal),
enabled: enabled && typeof projectRef !== 'undefined',
...options,
})
export function prefetchProjectLogRequestsCount(
client: QueryClient,
{ projectRef }: ProjectLogRequestsCountVariables
) {
return client.fetchQuery({
queryKey: analyticsKeys.usageApiRequestsCount(projectRef),
queryFn: ({ signal }) => getProjectLogRequestsCountStats({ projectRef }, signal),
})
}