1
0
Fork 0
VaultUI/src/api/transitEncrypt.ts

30 lines
753 B
TypeScript
Raw Normal View History

2021-05-08 00:21:58 +01:00
import { appendAPIURL, getHeaders } from "./apiUtils";
import { removeDoubleSlash } from "../utils";
2021-05-07 11:53:26 +01:00
2021-05-08 00:21:58 +01:00
type EncryptionResult = {
ciphertext: string;
}
2021-05-07 11:53:26 +01:00
2021-05-08 00:21:58 +01:00
export async function transitEncrypt(
baseMount: string,
name: string,
data: string
): Promise<EncryptionResult> {
2021-05-07 11:53:26 +01:00
const request = new Request(appendAPIURL(removeDoubleSlash(`/v1/${baseMount}/encrypt/${name}`)), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...getHeaders(),
},
body: JSON.stringify({ plaintext: data })
});
2021-05-08 00:21:58 +01:00
const response = await fetch(request);
2021-05-07 11:53:26 +01:00
if (!response.ok) {
2021-05-08 00:21:58 +01:00
const json = await response.json();
2021-05-07 11:53:26 +01:00
throw new Error(json.errors[0]);
} else {
2021-05-08 00:21:58 +01:00
const json = await response.json();
2021-05-07 11:53:26 +01:00
return json.data;
}
}