mirror of
https://github.com/supabase/supabase.git
synced 2026-10-12 04:45:08 +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.
67 lines
2.1 KiB
TypeScript
67 lines
2.1 KiB
TypeScript
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { executeSql } from 'data/sql/execute-sql-query'
|
|
import { quoteLiteral } from 'lib/pg-format'
|
|
import type { ResponseError, UseCustomMutationOptions, VaultSecret } from 'types'
|
|
import { vaultSecretsKeys } from './keys'
|
|
|
|
export type VaultSecretUpdateVariables = {
|
|
projectRef: string
|
|
connectionString?: string | null
|
|
id: string
|
|
} & Partial<VaultSecret>
|
|
|
|
export async function updateVaultSecret({
|
|
projectRef,
|
|
connectionString,
|
|
id,
|
|
...payload
|
|
}: VaultSecretUpdateVariables) {
|
|
const { name, description, secret } = payload
|
|
const sql = /* SQL */ `
|
|
select vault.update_secret(
|
|
secret_id := ${quoteLiteral(id)}
|
|
${secret ? `, new_secret := ${quoteLiteral(secret)}` : ''}
|
|
${name ? `, new_name := ${quoteLiteral(name)}` : ''}
|
|
${description ? `, new_description := ${quoteLiteral(description)}` : ''}
|
|
)
|
|
`
|
|
|
|
const { result } = await executeSql({ projectRef, connectionString, sql })
|
|
return result
|
|
}
|
|
|
|
type VaultSecretUpdateData = Awaited<ReturnType<typeof updateVaultSecret>>
|
|
|
|
export const useVaultSecretUpdateMutation = ({
|
|
onError,
|
|
onSuccess,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<VaultSecretUpdateData, ResponseError, VaultSecretUpdateVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<VaultSecretUpdateData, ResponseError, VaultSecretUpdateVariables>({
|
|
mutationFn: (vars) => updateVaultSecret(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { id, projectRef } = variables
|
|
await Promise.all([
|
|
queryClient.removeQueries({ queryKey: vaultSecretsKeys.getDecryptedValue(projectRef, id) }),
|
|
queryClient.invalidateQueries({ queryKey: vaultSecretsKeys.list(projectRef) }),
|
|
])
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to update key: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|