mirror of
https://github.com/supabase/supabase.git
synced 2026-10-15 22:29:11 +03:00
* 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
63 lines
1.9 KiB
TypeScript
63 lines
1.9 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 VaultSecretCreateVariables = {
|
|
projectRef: string
|
|
connectionString?: string | null
|
|
} & Partial<VaultSecret>
|
|
|
|
export async function createVaultSecret({
|
|
projectRef,
|
|
connectionString,
|
|
...newSecret
|
|
}: VaultSecretCreateVariables) {
|
|
const { name, description, secret } = newSecret
|
|
const sql = /* SQL */ `
|
|
select vault.create_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 VaultSecretCreateData = Awaited<ReturnType<typeof createVaultSecret>>
|
|
|
|
export const useVaultSecretCreateMutation = ({
|
|
onError,
|
|
onSuccess,
|
|
...options
|
|
}: Omit<
|
|
UseMutationOptions<VaultSecretCreateData, ResponseError, VaultSecretCreateVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<VaultSecretCreateData, ResponseError, VaultSecretCreateVariables>(
|
|
(vars) => createVaultSecret(vars),
|
|
{
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
await queryClient.invalidateQueries(vaultSecretsKeys.list(projectRef))
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to create secret: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
}
|
|
)
|
|
}
|