mirror of
https://github.com/supabase/supabase.git
synced 2026-05-07 09:20:21 -04:00
71 lines
1.9 KiB
TypeScript
71 lines
1.9 KiB
TypeScript
import { useQuery } from '@tanstack/react-query'
|
|
import { operations } from 'api-types'
|
|
|
|
import { analyticsKeys } from './keys'
|
|
import { get, handleError } from '@/data/fetchers'
|
|
import { UseCustomQueryOptions } from '@/types'
|
|
|
|
export type FunctionsReqStatsVariables = {
|
|
projectRef?: string
|
|
functionId?: string
|
|
interval?: operations['FunctionsLogsController_getRequestStats']['parameters']['query']['interval']
|
|
}
|
|
|
|
export type FunctionsReqStatsResponse = any
|
|
|
|
export async function getFunctionsReqStats(
|
|
{ projectRef, functionId, interval }: FunctionsReqStatsVariables,
|
|
signal?: AbortSignal
|
|
) {
|
|
if (!projectRef) {
|
|
throw new Error('projectRef is required')
|
|
}
|
|
if (!functionId) {
|
|
throw new Error('functionId is required')
|
|
}
|
|
if (!interval) {
|
|
throw new Error('interval is required')
|
|
}
|
|
|
|
const { data, error } = await get(
|
|
'/platform/projects/{ref}/analytics/endpoints/functions.req-stats',
|
|
{
|
|
params: {
|
|
path: {
|
|
ref: projectRef,
|
|
},
|
|
query: {
|
|
function_id: functionId,
|
|
interval,
|
|
},
|
|
},
|
|
signal,
|
|
}
|
|
)
|
|
|
|
if (error) handleError(error)
|
|
|
|
return data
|
|
}
|
|
|
|
export type FunctionsReqStatsData = Awaited<ReturnType<typeof getFunctionsReqStats>>
|
|
export type FunctionsReqStatsError = unknown
|
|
|
|
export const useFunctionsReqStatsQuery = <TData = FunctionsReqStatsData>(
|
|
{ projectRef, functionId, interval }: FunctionsReqStatsVariables,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: UseCustomQueryOptions<FunctionsReqStatsData, FunctionsReqStatsError, TData> = {}
|
|
) =>
|
|
useQuery<FunctionsReqStatsData, FunctionsReqStatsError, TData>({
|
|
queryKey: analyticsKeys.functionsReqStats(projectRef, { functionId, interval }),
|
|
queryFn: ({ signal }) => getFunctionsReqStats({ projectRef, functionId, interval }, signal),
|
|
enabled:
|
|
enabled &&
|
|
typeof projectRef !== 'undefined' &&
|
|
typeof functionId !== 'undefined' &&
|
|
typeof interval !== 'undefined',
|
|
...options,
|
|
})
|