29 lines
1.6 KiB
TypeScript
29 lines
1.6 KiB
TypeScript
import type { AuthenticationType } from '../types/project';
|
|
import { apiFetch } from './apiClient';
|
|
|
|
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 apiFetch(BASE));
|
|
export const createSecret = async (name: string, authenticationType: CredentialType, value: SecretValue): Promise<SecretMetadata> => 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<SecretMetadata> => 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<void> => response(await apiFetch(`${BASE}/${id}`, { method: 'DELETE' }));
|