From 77105dca45ae3aceffadec3805f15bb58eeae712 Mon Sep 17 00:00:00 2001 From: Kitteh Date: Fri, 28 May 2021 09:50:07 +0100 Subject: [PATCH] Seporate getSecret into v1 and v2. --- src/api/kv/getSecret.ts | 59 ++++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 18 deletions(-) diff --git a/src/api/kv/getSecret.ts b/src/api/kv/getSecret.ts index 33bf944..4ae2c0c 100644 --- a/src/api/kv/getSecret.ts +++ b/src/api/kv/getSecret.ts @@ -1,5 +1,44 @@ import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; +export async function getSecretKV1( + baseMount: string, + secretPath: string[], + name: string, +): Promise> { + const request = new Request(appendAPIURL(`/v1/${baseMount}/${secretPath.join("")}/${name}`), { + headers: getHeaders(), + }); + + const resp = await fetch(request); + await checkResponse(resp); + + const data = (await resp.json()) as unknown; + + return (data as { data: Record }).data; +} + +export async function getSecretKV2( + baseMount: string, + secretPath: string[], + name: string, + version: string | null = null, +): Promise> { + let secretURL = ""; + + secretURL = `/v1/${baseMount}/data/${secretPath.join("")}/${name}`; + if (version != null) secretURL += `?version=${version}`; + + const request = new Request(appendAPIURL(secretURL), { + headers: getHeaders(), + }); + + const resp = await fetch(request); + await checkResponse(resp); + + const data = (await resp.json()) as unknown; + return (data as { data: { data: Record } }).data.data; +} + export async function getSecret( baseMount: string, secretMountType: string, @@ -7,25 +46,9 @@ export async function getSecret( name: string, version: string | null = null, ): Promise> { - let secretURL = ""; if (secretMountType == "kv-v2") { - secretURL = `/v1/${baseMount}/data/${secretPath.join("")}/${name}`; - if (version != null) secretURL += `?version=${version}`; + return await getSecretKV2(baseMount, secretPath, name, version) } else { - secretURL = `/v1/${baseMount}/${secretPath.join("")}/${name}`; - } - const request = new Request(appendAPIURL(secretURL), { - headers: getHeaders(), - }); - - const resp = await fetch(request); - await checkResponse(resp); - - - const data = (await resp.json()) as unknown; - if (secretMountType == "kv-v2") { - return (data as { data: { data: Record } }).data.data; - } else { - return (data as { data: Record }).data; + return await getSecretKV1(baseMount, secretPath, name) } }