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
|
|
|
|
2021-05-09 15:04:20 +01:00
|
|
|
type EncryptionPayload = {
|
|
|
|
plaintext: string;
|
|
|
|
}
|
|
|
|
|
2021-05-08 00:21:58 +01:00
|
|
|
export async function transitEncrypt(
|
|
|
|
baseMount: string,
|
|
|
|
name: string,
|
2021-05-09 15:04:20 +01:00
|
|
|
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',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
...getHeaders(),
|
|
|
|
},
|
2021-05-09 15:04:20 +01:00
|
|
|
body: JSON.stringify(payload)
|
2021-05-07 11:53:26 +01:00
|
|
|
});
|
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;
|
|
|
|
}
|
|
|
|
}
|