mirror of
https://github.com/supabase/supabase.git
synced 2026-05-08 15:57:47 +08:00
## Context Enforce `noUnusedLocals` and `noUnusedParameters` in tsconfig.json + fix all related issues
70 lines
1.8 KiB
TypeScript
70 lines
1.8 KiB
TypeScript
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { apiKeysKeys } from './keys'
|
|
import { handleError, patch } from '@/data/fetchers'
|
|
import type { ResponseError, UseCustomMutationOptions } from '@/types'
|
|
|
|
export interface UpdateAPIKeybyIdVariables {
|
|
projectRef?: string
|
|
id?: string
|
|
description?: string
|
|
}
|
|
|
|
export async function updateAPIKeysById(
|
|
{ projectRef, id, description }: UpdateAPIKeybyIdVariables,
|
|
signal?: AbortSignal
|
|
) {
|
|
if (typeof projectRef === 'undefined') throw new Error('projectRef is required')
|
|
if (typeof id === 'undefined') throw new Error('API key ID is required')
|
|
|
|
const { data, error } = await patch('/v1/projects/{ref}/api-keys/{id}', {
|
|
params: {
|
|
path: { ref: projectRef, id },
|
|
query: { reveal: false },
|
|
},
|
|
body: {
|
|
description: description,
|
|
},
|
|
signal,
|
|
})
|
|
|
|
if (error) {
|
|
handleError(error)
|
|
}
|
|
|
|
return data
|
|
}
|
|
|
|
export type ResourceUpdateData = Awaited<ReturnType<typeof updateAPIKeysById>>
|
|
|
|
export const useResourceUpdateMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<ResourceUpdateData, ResponseError, UpdateAPIKeybyIdVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<ResourceUpdateData, ResponseError, UpdateAPIKeybyIdVariables>({
|
|
mutationFn: (vars) => updateAPIKeysById(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
|
|
await queryClient.invalidateQueries({ queryKey: apiKeysKeys.list(projectRef) })
|
|
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to mutate: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|