mirror of
https://github.com/supabase/supabase.git
synced 2026-07-05 02:24:20 +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.
56 lines
1.5 KiB
TypeScript
56 lines
1.5 KiB
TypeScript
import { useQuery } from '@tanstack/react-query'
|
|
|
|
import { get, handleError } from 'data/fetchers'
|
|
import { IS_PLATFORM } from 'lib/constants'
|
|
import type { ResponseError, UseCustomQueryOptions } from 'types'
|
|
import { branchKeys } from './keys'
|
|
|
|
export type BranchDiffVariables = {
|
|
branchRef: string
|
|
projectRef: string
|
|
includedSchemas?: string
|
|
}
|
|
|
|
export async function getBranchDiff({
|
|
branchRef,
|
|
includedSchemas,
|
|
}: Pick<BranchDiffVariables, 'branchRef' | 'includedSchemas'>) {
|
|
const { data: diffData, error } = await get('/v1/branches/{branch_id_or_ref}/diff', {
|
|
params: {
|
|
path: { branch_id_or_ref: branchRef },
|
|
query: includedSchemas ? { included_schemas: includedSchemas } : undefined,
|
|
},
|
|
headers: {
|
|
Accept: 'text/plain',
|
|
},
|
|
parseAs: 'text',
|
|
})
|
|
|
|
if (error) {
|
|
handleError(error)
|
|
}
|
|
|
|
// Handle empty object responses (when no diff exists)
|
|
if (typeof diffData === 'object' && Object.keys(diffData).length === 0) {
|
|
return ''
|
|
}
|
|
|
|
return diffData || ''
|
|
}
|
|
|
|
type BranchDiffData = Awaited<ReturnType<typeof getBranchDiff>>
|
|
|
|
export const useBranchDiffQuery = (
|
|
{ branchRef, projectRef, includedSchemas }: BranchDiffVariables,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: Omit<UseCustomQueryOptions<BranchDiffData, ResponseError>, 'queryKey' | 'queryFn'> = {}
|
|
) =>
|
|
useQuery<BranchDiffData, ResponseError>({
|
|
queryKey: branchKeys.diff(projectRef, branchRef),
|
|
queryFn: () => getBranchDiff({ branchRef, includedSchemas }),
|
|
enabled: IS_PLATFORM && enabled && Boolean(branchRef),
|
|
...options,
|
|
})
|