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

38 lines
984 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
export async function createOrUpdateSecret(
baseMount: string,
mountType: string,
secretPath: string[],
name: string,
data: Record<string, unknown>
): Promise<void> {
2021-05-07 11:53:26 +01:00
let secretURL = "";
let APIData = {};
if (mountType == "kv-v2") {
secretURL = `/v1/${baseMount}/data/${secretPath.join("/")}/${name}`;
APIData = { "data": data };
} else {
secretURL = `/v1/${baseMount}/${secretPath.join("/")}/${name}`;
APIData = data;
}
secretURL = removeDoubleSlash(secretURL).replace(/\/$/, "");
const request = new Request(appendAPIURL(secretURL), {
method: "POST",
headers: {
'Content-Type': 'application/json',
...getHeaders(),
},
body: JSON.stringify(APIData, null, 0)
});
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]);
}
}