import { safeSql } from '@supabase/pg-meta' import { Query } from '@supabase/pg-meta/src/query' import { useQuery } from '@tanstack/react-query' import { vaultSecretsKeys } from './keys' import { executeSql } from '@/data/sql/execute-sql-mutation' import type { ResponseError, UseCustomQueryOptions, VaultSecret } from '@/types' export const getVaultSecretsSql = () => { const sql = new Query() .from('secrets', 'vault') .select(safeSql`id,name,description,secret,created_at,updated_at`) .toSql() return sql } export type VaultSecretsVariables = { projectRef?: string connectionString?: string | null } export async function getVaultSecrets( { projectRef, connectionString }: VaultSecretsVariables, signal?: AbortSignal ) { const sql = getVaultSecretsSql() const { result } = await executeSql( { projectRef, connectionString, sql, queryKey: ['vault-secrets'] }, signal ) return result as VaultSecret[] } export type VaultSecretsData = Awaited> export type VaultSecretsError = ResponseError export const useVaultSecretsQuery = ( { projectRef, connectionString }: VaultSecretsVariables, { enabled = true, ...options }: UseCustomQueryOptions = {} ) => useQuery({ queryKey: vaultSecretsKeys.list(projectRef), queryFn: ({ signal }) => getVaultSecrets({ projectRef, connectionString }, signal), enabled: enabled && typeof projectRef !== 'undefined', ...options, })