mirror of
https://github.com/supabase/supabase.git
synced 2026-05-18 19:06:50 +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.
55 lines
1.8 KiB
TypeScript
55 lines
1.8 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 { databasePublicationsKeys } from './keys'
|
|
|
|
export type DatabasePublicationsVariables = {
|
|
projectRef?: string
|
|
connectionString?: string | null
|
|
}
|
|
|
|
export async function getDatabasePublications(
|
|
{ projectRef, connectionString }: DatabasePublicationsVariables,
|
|
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}/publications', {
|
|
params: {
|
|
header: {
|
|
'x-connection-encrypted': connectionString!,
|
|
'x-pg-application-name': DEFAULT_PLATFORM_APPLICATION_NAME,
|
|
},
|
|
path: {
|
|
ref: projectRef,
|
|
},
|
|
},
|
|
headers,
|
|
signal,
|
|
})
|
|
|
|
if (error) handleError(error)
|
|
return data
|
|
}
|
|
|
|
export type DatabasePublicationsData = Awaited<ReturnType<typeof getDatabasePublications>>
|
|
export type DatabasePublicationsError = ResponseError
|
|
|
|
export const useDatabasePublicationsQuery = <TData = DatabasePublicationsData>(
|
|
{ projectRef, connectionString }: DatabasePublicationsVariables,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: UseCustomQueryOptions<DatabasePublicationsData, DatabasePublicationsError, TData> = {}
|
|
) =>
|
|
useQuery<DatabasePublicationsData, DatabasePublicationsError, TData>({
|
|
queryKey: databasePublicationsKeys.list(projectRef),
|
|
queryFn: ({ signal }) => getDatabasePublications({ projectRef, connectionString }, signal),
|
|
enabled: enabled && typeof projectRef !== 'undefined',
|
|
...options,
|
|
})
|