Files
supabase/apps/studio/data/vault/vault-secret-update-mutation.ts
T
Andrew Valleteau 31aad403de fix(studio): early fail query when x-connection-encrypted is invalid (#35331)
* fix(studio): early fail query when x-connection-encrypted is invalid

* fix(studio): uniformize readDatabase and projectDetails connString handling

* chore: update api types

* chore: add connectionString null option

* fix: only enforce x-connection-encrypted on platform

* chore: refactor connString check in a single point

* chore: fix guard logic

* chore: fix pgMetaGuard

* chore: fix types
2025-05-08 12:11:03 +02:00

69 lines
2.0 KiB
TypeScript

import { useMutation, UseMutationOptions, 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, 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<
UseMutationOptions<VaultSecretUpdateData, ResponseError, VaultSecretUpdateVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<VaultSecretUpdateData, ResponseError, VaultSecretUpdateVariables>(
(vars) => updateVaultSecret(vars),
{
async onSuccess(data, variables, context) {
const { id, projectRef } = variables
await Promise.all([
queryClient.removeQueries(vaultSecretsKeys.getDecryptedValue(projectRef, id)),
queryClient.invalidateQueries(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,
}
)
}