mirror of
https://github.com/supabase/supabase.git
synced 2026-05-07 23:19:23 +08:00
* 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.
71 lines
2.0 KiB
TypeScript
71 lines
2.0 KiB
TypeScript
import { useQuery } from '@tanstack/react-query'
|
|
import { operations } from 'api-types'
|
|
import { get, handleError } from 'data/fetchers'
|
|
import { analyticsKeys } from './keys'
|
|
import { UseCustomQueryOptions } from 'types'
|
|
|
|
export type FunctionsResourceUsageVariables = {
|
|
projectRef?: string
|
|
functionId?: string
|
|
interval?: operations['FunctionsLogsController_getRequestStats']['parameters']['query']['interval']
|
|
}
|
|
|
|
export type FunctionsResourceUsageResponse = any
|
|
|
|
export async function getFunctionsResourceUsage(
|
|
{ projectRef, functionId, interval }: FunctionsResourceUsageVariables,
|
|
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.resource-usage',
|
|
{
|
|
params: {
|
|
path: {
|
|
ref: projectRef,
|
|
},
|
|
query: {
|
|
function_id: functionId,
|
|
interval,
|
|
},
|
|
},
|
|
signal,
|
|
}
|
|
)
|
|
|
|
if (error) handleError(error)
|
|
|
|
return data
|
|
}
|
|
|
|
export type FunctionsResourceUsageData = Awaited<ReturnType<typeof getFunctionsResourceUsage>>
|
|
export type FunctionsResourceUsageError = unknown
|
|
|
|
export const useFunctionsResourceUsageQuery = <TData = FunctionsResourceUsageData>(
|
|
{ projectRef, functionId, interval }: FunctionsResourceUsageVariables,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: UseCustomQueryOptions<FunctionsResourceUsageData, FunctionsResourceUsageError, TData> = {}
|
|
) =>
|
|
useQuery<FunctionsResourceUsageData, FunctionsResourceUsageError, TData>({
|
|
queryKey: analyticsKeys.functionsResourceUsage(projectRef, { functionId, interval }),
|
|
queryFn: ({ signal }) =>
|
|
getFunctionsResourceUsage({ projectRef, functionId, interval }, signal),
|
|
enabled:
|
|
enabled &&
|
|
typeof projectRef !== 'undefined' &&
|
|
typeof functionId !== 'undefined' &&
|
|
typeof interval !== 'undefined',
|
|
...options,
|
|
})
|