mirror of
https://github.com/supabase/supabase.git
synced 2026-10-08 02:45:07 +03: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.
76 lines
2.5 KiB
TypeScript
76 lines
2.5 KiB
TypeScript
import { DEFAULT_PLATFORM_APPLICATION_NAME } from '@supabase/pg-meta/src/constants'
|
|
import { useQuery } from '@tanstack/react-query'
|
|
import { get, handleError } from 'data/fetchers'
|
|
import type { ResponseError, UseCustomQueryOptions } from 'types'
|
|
import { databaseTriggerKeys } from './keys'
|
|
|
|
export type DatabaseTriggersVariables = {
|
|
projectRef?: string
|
|
connectionString?: string | null
|
|
}
|
|
|
|
export async function getDatabaseTriggers(
|
|
{ projectRef, connectionString }: DatabaseTriggersVariables,
|
|
signal?: AbortSignal
|
|
) {
|
|
if (!projectRef) throw new Error('projectRef is required')
|
|
|
|
let headers = new Headers()
|
|
if (connectionString) headers.set('x-connection-encrypted', connectionString)
|
|
|
|
const { data, error } = await get('/platform/pg-meta/{ref}/triggers', {
|
|
params: {
|
|
header: {
|
|
'x-connection-encrypted': connectionString!,
|
|
'x-pg-application-name': DEFAULT_PLATFORM_APPLICATION_NAME,
|
|
},
|
|
path: { ref: projectRef },
|
|
query: undefined as any,
|
|
},
|
|
headers,
|
|
signal,
|
|
})
|
|
|
|
if (error) handleError(error)
|
|
return data
|
|
}
|
|
|
|
export type DatabaseTriggersData = Awaited<ReturnType<typeof getDatabaseTriggers>>
|
|
export type DatabaseTriggersError = ResponseError
|
|
|
|
export const useDatabaseHooksQuery = <TData = DatabaseTriggersData>(
|
|
{ projectRef, connectionString }: DatabaseTriggersVariables,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: UseCustomQueryOptions<DatabaseTriggersData, DatabaseTriggersError, TData> = {}
|
|
) =>
|
|
useQuery<DatabaseTriggersData, DatabaseTriggersError, TData>({
|
|
queryKey: databaseTriggerKeys.list(projectRef),
|
|
queryFn: ({ signal }) => getDatabaseTriggers({ projectRef, connectionString }, signal),
|
|
select: (data) => {
|
|
return data.filter((trigger) => {
|
|
return (
|
|
trigger.function_schema === 'supabase_functions' &&
|
|
(trigger.schema !== 'net' || trigger.function_args.length === 0)
|
|
)
|
|
}) as any
|
|
},
|
|
enabled: enabled && typeof projectRef !== 'undefined',
|
|
...options,
|
|
})
|
|
|
|
export const useDatabaseTriggersQuery = <TData = DatabaseTriggersData>(
|
|
{ projectRef, connectionString }: DatabaseTriggersVariables,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: UseCustomQueryOptions<DatabaseTriggersData, DatabaseTriggersError, TData> = {}
|
|
) =>
|
|
useQuery<DatabaseTriggersData, DatabaseTriggersError, TData>({
|
|
queryKey: databaseTriggerKeys.list(projectRef),
|
|
queryFn: ({ signal }) => getDatabaseTriggers({ projectRef, connectionString }, signal),
|
|
enabled: enabled && typeof projectRef !== 'undefined',
|
|
...options,
|
|
})
|