mirror of
https://github.com/supabase/supabase.git
synced 2026-06-20 11:22:25 +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.
51 lines
1.5 KiB
TypeScript
51 lines
1.5 KiB
TypeScript
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { handleError, post } from 'data/fetchers'
|
|
import type { ResponseError, UseCustomMutationOptions } from 'types'
|
|
import { branchKeys } from './keys'
|
|
|
|
export type BranchPushVariables = {
|
|
branchRef: string
|
|
projectRef: string
|
|
}
|
|
|
|
export async function pushBranch({ branchRef }: Pick<BranchPushVariables, 'branchRef'>) {
|
|
const { data, error } = await post('/v1/branches/{branch_id_or_ref}/push', {
|
|
params: { path: { branch_id_or_ref: branchRef } },
|
|
body: {},
|
|
})
|
|
|
|
if (error) handleError(error)
|
|
return data
|
|
}
|
|
|
|
type BranchPushData = Awaited<ReturnType<typeof pushBranch>>
|
|
|
|
export const useBranchPushMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<BranchPushData, ResponseError, BranchPushVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
return useMutation<BranchPushData, ResponseError, BranchPushVariables>({
|
|
mutationFn: (vars) => pushBranch(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
await queryClient.invalidateQueries({ queryKey: branchKeys.list(projectRef) })
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to push branch: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|