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

37 lines
836 B
TypeScript
Raw Normal View History

2021-05-09 11:18:18 +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
type EncryptionPayload = {
plaintext: string;
};
2021-05-08 00:21:58 +01:00
export async function transitEncrypt(
baseMount: string,
name: string,
payload: EncryptionPayload,
2021-05-08 00:21:58 +01:00
): Promise<EncryptionResult> {
2021-05-07 11:53:26 +01:00
const request = new Request(appendAPIURL(removeDoubleSlash(`/v1/${baseMount}/encrypt/${name}`)), {
method: "POST",
2021-05-07 11:53:26 +01:00
headers: {
"Content-Type": "application/json",
2021-05-07 11:53:26 +01:00
...getHeaders(),
},
body: JSON.stringify(payload),
2021-05-07 11:53:26 +01:00
});
2021-05-10 11:35:14 +01:00
2021-05-08 00:21:58 +01:00
const response = await fetch(request);
const data = (await response.json()) as {
errors?: string[];
data?: EncryptionResult;
};
2021-05-07 11:53:26 +01:00
if (!response.ok) {
2021-05-10 11:35:14 +01:00
throw new Error(data.errors[0]);
2021-05-07 11:53:26 +01:00
} else {
2021-05-10 11:35:14 +01:00
return data.data;
2021-05-07 11:53:26 +01:00
}
}