conductor/frontend/src/api/secretsApi.ts

28 lines
1.5 KiB
TypeScript

import type { AuthenticationType } from '../types/project';
export type CredentialType = Exclude<AuthenticationType, 'anonymous'>;
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<T>(res: Response): Promise<T> {
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<T>;
}
export const listSecrets = async (): Promise<SecretMetadata[]> => response(await fetch(BASE));
export const createSecret = async (name: string, authenticationType: CredentialType, value: SecretValue): Promise<SecretMetadata> => response(await fetch(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<SecretMetadata> => response(await fetch(`${BASE}/${id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name, authenticationType, value }) }));
export const deleteSecret = async (id: string): Promise<void> => response(await fetch(`${BASE}/${id}`, { method: 'DELETE' }));