import type { AuthenticationType } from '../types/project'; import { apiFetch } from './apiClient'; export type CredentialType = Exclude; export type SecretMetadata = { id: string; name: string; authenticationType: CredentialType; createdAt: string; updatedAt: string; }; export type SecretValue = | { username: string; password: string } | { token: string } | { parameterName: string; value: string }; const BASE = '/api/secrets'; async function response(res: Response): Promise { if (!res.ok) { const body = await res.json().catch(() => ({})) as { error?: string }; throw new Error(body.error ?? `HTTP ${res.status}`); } return res.status === 204 ? undefined as T : res.json() as Promise; } export const listSecrets = async (): Promise => response(await apiFetch(BASE)); export const createSecret = async (name: string, authenticationType: CredentialType, value: SecretValue): Promise => response(await apiFetch(BASE, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name, authenticationType, value }) })); export const replaceSecret = async (id: string, name: string, authenticationType: CredentialType, value: SecretValue): Promise => response(await apiFetch(`${BASE}/${id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name, authenticationType, value }) })); export const deleteSecret = async (id: string): Promise => response(await apiFetch(`${BASE}/${id}`, { method: 'DELETE' }));