Files
supabase/apps/studio/data/analytics/functions-resource-usage-query.ts
Alaister Young 9d534c9f5a fix: response error codes (#30581)
* fix: response error codes

* upgrade docs

* remove request url modification middleware

* move api routes for self-hosted to platform folder

* remove some lib/common/fetch usage

* docs: use middleware for openapi-fetch (#30600)

Get rid of the unauthedAllowedPost function (I don't think there's any harm in letting any requests that require authentication to just 403, they should be disabled at the React Query level and if not they will fail gracefully enough...)

* fix local count query

* add default values for clone mutation

* fix ts and codegen

* add missing lodash dep to playwright tests

* Fix the playwright tests to match the new folder structure for selfhosted variant.

* remove unused import

* Remove unused state

* remove unused sql debug mutation

* remove unused export

* fix notifications query

* fix jwt updating status

* fix typescript

* save sql snippet after renaming

* update codegen & fix ts error

* override array querySerializer

---------

Co-authored-by: Charis <26616127+charislam@users.noreply.github.com>
Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
2025-01-20 11:27:38 +08:00

71 lines
2.0 KiB
TypeScript

import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import { operations } from 'api-types'
import { get, handleError } from 'data/fetchers'
import { analyticsKeys } from './keys'
export type FunctionsResourceUsageVariables = {
projectRef?: string
functionId?: string
interval?: operations['FunctionResourceLogsController_getStatus']['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
}: UseQueryOptions<FunctionsResourceUsageData, FunctionsResourceUsageError, TData> = {}
) =>
useQuery<FunctionsResourceUsageData, FunctionsResourceUsageError, TData>(
analyticsKeys.functionsResourceUsage(projectRef, { functionId, interval }),
({ signal }) => getFunctionsResourceUsage({ projectRef, functionId, interval }, signal),
{
enabled:
enabled &&
typeof projectRef !== 'undefined' &&
typeof functionId !== 'undefined' &&
typeof interval !== 'undefined',
...options,
}
)