From 56aeb9b51434f7bf1cc9623c1bace223df7bbe5c Mon Sep 17 00:00:00 2001 From: Kitteh Date: Fri, 28 May 2021 09:45:46 +0100 Subject: [PATCH] Move API error checking into checkResponse. --- src/api/apiUtils.ts | 18 ++++++++++++++++++ src/api/auth/listAuth.ts | 4 +++- src/api/auth/usernameLogin.ts | 13 +++++-------- .../userpass/createOrUpdateUserPassUser.ts | 9 +++------ src/api/auth/userpass/deleteUserPassUser.ts | 9 +++------ src/api/auth/userpass/getUserPassUser.ts | 4 +++- src/api/auth/userpass/listUserPassUsers.ts | 4 +++- src/api/kv/createOrUpdateSecret.ts | 9 +++------ src/api/kv/deleteSecret.ts | 9 +++------ src/api/kv/getSecret.ts | 5 ++++- src/api/kv/getSecretMetadata.ts | 4 +++- src/api/kv/getSecrets.ts | 8 ++++---- src/api/kv/undeleteSecret.ts | 9 +++------ src/api/sys/getCapabilities.ts | 8 +++++--- src/api/sys/getMounts.ts | 4 +++- src/api/sys/getSealStatus.ts | 4 +++- src/api/sys/lookupSelf.ts | 12 +++++------- src/api/sys/newMount.ts | 9 ++------- src/api/sys/policies/createOrUpdatePolicy.ts | 18 +++--------------- src/api/sys/policies/deletePolicy.ts | 17 +++-------------- src/api/sys/policies/getPolicies.ts | 8 +++++--- src/api/sys/policies/getPolicy.ts | 8 +++++--- src/api/sys/renewSelf.ts | 7 ++----- src/api/sys/sealVault.ts | 5 +++-- src/api/sys/submitUnsealKey.ts | 9 ++------- src/api/totp/addNewTOTP.ts | 9 ++------- src/api/totp/deleteTOTP.ts | 9 ++------- src/api/totp/getTOTPCode.ts | 5 ++++- src/api/totp/getTOTPKeys.ts | 7 +++---- src/api/transit/getTransitKey.ts | 8 +++----- src/api/transit/getTransitKeys.ts | 8 ++++---- src/api/transit/newTransitKey.ts | 9 ++------- src/api/transit/transitDecrypt.ts | 16 +++++++--------- src/api/transit/transitEncrypt.ts | 16 +++++++--------- src/api/transit/transitRewrap.ts | 16 +++++++--------- src/api/types/api.ts | 3 +++ 36 files changed, 143 insertions(+), 177 deletions(-) create mode 100644 src/api/types/api.ts diff --git a/src/api/apiUtils.ts b/src/api/apiUtils.ts index 824839e..4304a97 100644 --- a/src/api/apiUtils.ts +++ b/src/api/apiUtils.ts @@ -1,4 +1,6 @@ import { pageState } from "../globalPageState"; +import { DoesNotExistError } from "../types/internalErrors"; +import { BaseAPIResponse } from "./types/api"; export function getHeaders(): Record { return { @@ -7,3 +9,19 @@ export function getHeaders(): Record { } export const appendAPIURL = (url: string): string => pageState.apiURL + url; + +export async function checkResponse(resp: Response): Promise { + if (resp.ok) return; + if (resp.status == 404) throw DoesNotExistError; + + let json: BaseAPIResponse; + try { + json = (await resp.json()) as BaseAPIResponse; + } catch { + // Do Nothing + } + + if (json?.errors?.length >= 1) { + throw new Error(json.errors[0]); + } +} \ No newline at end of file diff --git a/src/api/auth/listAuth.ts b/src/api/auth/listAuth.ts index cf18c87..8267bac 100644 --- a/src/api/auth/listAuth.ts +++ b/src/api/auth/listAuth.ts @@ -1,11 +1,13 @@ import { AuthListAPIType, AuthListType } from "../types/auth"; -import { appendAPIURL, getHeaders } from "../apiUtils"; +import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; export async function listAuth(): Promise { const request = new Request(appendAPIURL(`/v1/sys/auth`), { headers: getHeaders(), }); const resp = await fetch(request); + await checkResponse(resp); + const data = (await resp.json()) as AuthListAPIType; return data.data; } diff --git a/src/api/auth/usernameLogin.ts b/src/api/auth/usernameLogin.ts index 1784cc0..5384c8a 100644 --- a/src/api/auth/usernameLogin.ts +++ b/src/api/auth/usernameLogin.ts @@ -1,4 +1,4 @@ -import { appendAPIURL } from "../apiUtils"; +import { appendAPIURL, checkResponse } from "../apiUtils"; export async function usernameLogin(username: string, password: string): Promise { const request = new Request(appendAPIURL(`/v1/auth/userpass/login/${username}`), { @@ -10,13 +10,10 @@ export async function usernameLogin(username: string, password: string): Promise }); const resp = await fetch(request); + await checkResponse(resp); + const data = (await resp.json()) as { - auth?: { client_token: string }; - errors?: string[]; + auth: { client_token: string }; }; - if ("auth" in data) { - return data.auth.client_token; - } else if ("errors" in data) { - throw new Error(data.errors[0]); - } + return data.auth.client_token; } diff --git a/src/api/auth/userpass/createOrUpdateUserPassUser.ts b/src/api/auth/userpass/createOrUpdateUserPassUser.ts index 6f49a4d..0e15e9b 100644 --- a/src/api/auth/userpass/createOrUpdateUserPassUser.ts +++ b/src/api/auth/userpass/createOrUpdateUserPassUser.ts @@ -1,5 +1,5 @@ import { UserType } from "../../types/userpass/user"; -import { appendAPIURL, getHeaders } from "../../apiUtils"; +import { appendAPIURL, checkResponse, getHeaders } from "../../apiUtils"; import { removeDoubleSlash } from "../../../utils"; export async function createOrUpdateUserPassUser( @@ -18,9 +18,6 @@ export async function createOrUpdateUserPassUser( body: JSON.stringify(data, null, 0), }, ); - const response = await fetch(request); - if (!response.ok) { - const json = (await response.json()) as { errors: string[] }; - throw new Error(json.errors[0]); - } + const resp = await fetch(request); + await checkResponse(resp); } diff --git a/src/api/auth/userpass/deleteUserPassUser.ts b/src/api/auth/userpass/deleteUserPassUser.ts index 024ced2..9278dd5 100644 --- a/src/api/auth/userpass/deleteUserPassUser.ts +++ b/src/api/auth/userpass/deleteUserPassUser.ts @@ -1,4 +1,4 @@ -import { appendAPIURL, getHeaders } from "../../apiUtils"; +import { appendAPIURL, checkResponse, getHeaders } from "../../apiUtils"; import { removeDoubleSlash } from "../../../utils"; export async function deleteUserPassUser(path: string, username: string): Promise { @@ -9,9 +9,6 @@ export async function deleteUserPassUser(path: string, username: string): Promis headers: getHeaders(), }, ); - const response = await fetch(request); - if (!response.ok) { - const json = (await response.json()) as { errors: string[] }; - throw new Error(json.errors[0]); - } + const resp = await fetch(request); + await checkResponse(resp); } diff --git a/src/api/auth/userpass/getUserPassUser.ts b/src/api/auth/userpass/getUserPassUser.ts index 47ebe87..5ce5b8e 100644 --- a/src/api/auth/userpass/getUserPassUser.ts +++ b/src/api/auth/userpass/getUserPassUser.ts @@ -1,11 +1,13 @@ import { UserType, UserTypeAPIResp } from "../../types/userpass/user"; -import { appendAPIURL, getHeaders } from "../../apiUtils"; +import { appendAPIURL, checkResponse, getHeaders } from "../../apiUtils"; export async function getUserPassUser(path: string, username: string): Promise { const request = new Request(appendAPIURL(`/v1/auth/${path}/users/${username}`), { headers: getHeaders(), }); const resp = await fetch(request); + await checkResponse(resp); + const data = (await resp.json()) as UserTypeAPIResp; return data.data; } diff --git a/src/api/auth/userpass/listUserPassUsers.ts b/src/api/auth/userpass/listUserPassUsers.ts index 12f18ee..417b2e6 100644 --- a/src/api/auth/userpass/listUserPassUsers.ts +++ b/src/api/auth/userpass/listUserPassUsers.ts @@ -1,10 +1,12 @@ -import { appendAPIURL, getHeaders } from "../../apiUtils"; +import { appendAPIURL, checkResponse, getHeaders } from "../../apiUtils"; export async function listUserPassUsers(path: string): Promise { const request = new Request(appendAPIURL(`/v1/auth/${path}/users?list=true`), { headers: getHeaders(), }); const resp = await fetch(request); + await checkResponse(resp); + const data = (await resp.json()) as { data: { keys: string[] } }; return data.data.keys; } diff --git a/src/api/kv/createOrUpdateSecret.ts b/src/api/kv/createOrUpdateSecret.ts index 9f9fd85..2d532cd 100644 --- a/src/api/kv/createOrUpdateSecret.ts +++ b/src/api/kv/createOrUpdateSecret.ts @@ -1,4 +1,4 @@ -import { appendAPIURL, getHeaders } from "../apiUtils"; +import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; import { removeDoubleSlash } from "../../utils"; export async function createOrUpdateSecret( @@ -28,9 +28,6 @@ export async function createOrUpdateSecret( }, body: JSON.stringify(APIData, null, 0), }); - const response = await fetch(request); - if (!response.ok) { - const json = (await response.json()) as { errors: string[] }; - throw new Error(json.errors[0]); - } + const resp = await fetch(request); + await checkResponse(resp); } diff --git a/src/api/kv/deleteSecret.ts b/src/api/kv/deleteSecret.ts index b9fbfb1..6435fb5 100644 --- a/src/api/kv/deleteSecret.ts +++ b/src/api/kv/deleteSecret.ts @@ -1,4 +1,4 @@ -import { appendAPIURL, getHeaders } from "../apiUtils"; +import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; import { removeDoubleSlash } from "../../utils"; export async function deleteSecret( @@ -35,9 +35,6 @@ export async function deleteSecret( headers: getHeaders(), }); } - const response = await fetch(request); - if (!response.ok) { - const json = (await response.json()) as { errors: string[] }; - throw new Error(json.errors[0]); - } + const resp = await fetch(request); + await checkResponse(resp); } diff --git a/src/api/kv/getSecret.ts b/src/api/kv/getSecret.ts index a4c10ec..33bf944 100644 --- a/src/api/kv/getSecret.ts +++ b/src/api/kv/getSecret.ts @@ -1,4 +1,4 @@ -import { appendAPIURL, getHeaders } from "../apiUtils"; +import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; export async function getSecret( baseMount: string, @@ -19,6 +19,9 @@ export async function getSecret( }); 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; diff --git a/src/api/kv/getSecretMetadata.ts b/src/api/kv/getSecretMetadata.ts index 2675c3c..657db3b 100644 --- a/src/api/kv/getSecretMetadata.ts +++ b/src/api/kv/getSecretMetadata.ts @@ -1,4 +1,4 @@ -import { appendAPIURL, getHeaders } from "../apiUtils"; +import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; type SecretMetadataType = { versions: Record; @@ -17,6 +17,8 @@ export async function getSecretMetadata( ); const resp = await fetch(request); + await checkResponse(resp); + const data = (await resp.json()) as { data: SecretMetadataType }; return data.data; } diff --git a/src/api/kv/getSecrets.ts b/src/api/kv/getSecrets.ts index 59f8b22..b02beff 100644 --- a/src/api/kv/getSecrets.ts +++ b/src/api/kv/getSecrets.ts @@ -1,5 +1,5 @@ import { DoesNotExistError } from "../../types/internalErrors"; -import { appendAPIURL, getHeaders } from "../apiUtils"; +import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; export async function getSecrets( baseMount: string, @@ -16,10 +16,10 @@ export async function getSecrets( const request = new Request(appendAPIURL(secretURL), { headers: getHeaders(), }); + const resp = await fetch(request); - if (resp.status == 404) { - throw DoesNotExistError; - } + await checkResponse(resp); + const data = (await resp.json()) as { data: { keys: string[] } }; return data.data.keys; } diff --git a/src/api/kv/undeleteSecret.ts b/src/api/kv/undeleteSecret.ts index ba95454..1aab24e 100644 --- a/src/api/kv/undeleteSecret.ts +++ b/src/api/kv/undeleteSecret.ts @@ -1,4 +1,4 @@ -import { appendAPIURL, getHeaders } from "../apiUtils"; +import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; import { getObjectKeys, removeDoubleSlash } from "../../utils"; import { getSecretMetadata } from "./getSecretMetadata"; @@ -24,9 +24,6 @@ export async function undeleteSecret( }, body: JSON.stringify({ versions: [version] }), }); - const response = await fetch(request); - if (!response.ok) { - const json = (await response.json()) as { errors: string[] }; - throw new Error(json.errors[0]); - } + const resp = await fetch(request); + await checkResponse(resp); } diff --git a/src/api/sys/getCapabilities.ts b/src/api/sys/getCapabilities.ts index cf5605a..dc7f238 100644 --- a/src/api/sys/getCapabilities.ts +++ b/src/api/sys/getCapabilities.ts @@ -1,4 +1,4 @@ -import { appendAPIURL, getHeaders } from "../apiUtils"; +import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; import { removeDoubleSlash } from "../../utils"; export async function getCapabilitiesPath(path: string): Promise { @@ -12,8 +12,10 @@ export async function getCapabilitiesPath(path: string): Promise { paths: [removeDoubleSlash(path)], }), }); - const response = await fetch(request); - const data = (await response.json()) as { capabilities: string[] }; + const resp = await fetch(request); + await checkResponse(resp); + + const data = (await resp.json()) as { capabilities: string[] }; return data.capabilities; } diff --git a/src/api/sys/getMounts.ts b/src/api/sys/getMounts.ts index 1f4caa3..53a8277 100644 --- a/src/api/sys/getMounts.ts +++ b/src/api/sys/getMounts.ts @@ -1,4 +1,4 @@ -import { appendAPIURL, getHeaders } from "../apiUtils"; +import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; export type MountType = { type: string; @@ -16,6 +16,8 @@ export async function getMounts(): Promise { headers: getHeaders(), }); const resp = await fetch(request); + await checkResponse(resp); + const data = (await resp.json()) as { data: { secret: MountsType } }; return data.data.secret; } diff --git a/src/api/sys/getSealStatus.ts b/src/api/sys/getSealStatus.ts index edbfbab..bd43066 100644 --- a/src/api/sys/getSealStatus.ts +++ b/src/api/sys/getSealStatus.ts @@ -1,4 +1,4 @@ -import { appendAPIURL } from "../apiUtils"; +import { appendAPIURL, checkResponse } from "../apiUtils"; export type SealStatusType = { progress: number; @@ -9,6 +9,8 @@ export type SealStatusType = { export async function getSealStatus(): Promise { const request = new Request(appendAPIURL("/v1/sys/seal-status")); const resp = await fetch(request); + await checkResponse(resp); + const data = (await resp.json()) as SealStatusType; return data; } diff --git a/src/api/sys/lookupSelf.ts b/src/api/sys/lookupSelf.ts index 5b3abd6..36f1478 100644 --- a/src/api/sys/lookupSelf.ts +++ b/src/api/sys/lookupSelf.ts @@ -1,15 +1,13 @@ import { TokenInfo } from "../types/token"; -import { appendAPIURL, getHeaders } from "../apiUtils"; +import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; export async function lookupSelf(): Promise { const request = new Request(appendAPIURL("/v1/auth/token/lookup-self"), { headers: getHeaders(), }); const resp = await fetch(request); - const data = (await resp.json()) as { data?: TokenInfo; errors?: string[] }; - if ("data" in data) { - return data.data; - } else if ("errors" in data) { - throw new Error(data.errors[0]); - } + await checkResponse(resp); + + const data = (await resp.json()) as { data: TokenInfo; }; + return data.data; } diff --git a/src/api/sys/newMount.ts b/src/api/sys/newMount.ts index c91b12b..febf604 100644 --- a/src/api/sys/newMount.ts +++ b/src/api/sys/newMount.ts @@ -1,4 +1,4 @@ -import { appendAPIURL, getHeaders } from "../apiUtils"; +import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; import { removeDoubleSlash } from "../../utils"; type NewMountParams = { @@ -19,10 +19,5 @@ export async function newMount(parms: NewMountParams): Promise { body: JSON.stringify(parms), }); const resp = await fetch(request); - if (!resp.ok) { - const data = (await resp.json()) as { errors?: string[] }; - if ("errors" in data) { - throw new Error(data.errors[0]); - } - } + await checkResponse(resp); } diff --git a/src/api/sys/policies/createOrUpdatePolicy.ts b/src/api/sys/policies/createOrUpdatePolicy.ts index 906c23e..ba978bf 100644 --- a/src/api/sys/policies/createOrUpdatePolicy.ts +++ b/src/api/sys/policies/createOrUpdatePolicy.ts @@ -1,6 +1,4 @@ -import { appendAPIURL, getHeaders } from "../../apiUtils"; - -type OptionalErrors = { errors?: string[] }; +import { appendAPIURL, checkResponse, getHeaders } from "../../apiUtils"; export async function createOrUpdatePolicy(name: string, policy_data: string): Promise { const request = new Request(appendAPIURL("/v1/sys/policies/acl/" + name), { @@ -12,16 +10,6 @@ export async function createOrUpdatePolicy(name: string, policy_data: string): P body: JSON.stringify({ policy: policy_data }, null, 0), }); - const response = await fetch(request); - let data: OptionalErrors = {}; - - try { - data = (await response.json()) as OptionalErrors; - } catch { - // Do Nothing - } - - if ("errors" in data) { - throw new Error(data.errors[0]); - } + const resp = await fetch(request); + await checkResponse(resp); } diff --git a/src/api/sys/policies/deletePolicy.ts b/src/api/sys/policies/deletePolicy.ts index 99c19fb..fe187d8 100644 --- a/src/api/sys/policies/deletePolicy.ts +++ b/src/api/sys/policies/deletePolicy.ts @@ -1,21 +1,10 @@ -import { appendAPIURL, getHeaders } from "../../apiUtils"; - -type OptionalErrors = { errors?: string[] }; +import { appendAPIURL, checkResponse, getHeaders } from "../../apiUtils"; export async function deletePolicy(name: string): Promise { const request = new Request(appendAPIURL("/v1/sys/policies/acl/" + name), { method: "DELETE", headers: getHeaders(), }); - const response = await fetch(request); - let data: OptionalErrors = {}; - try { - data = (await response.json()) as OptionalErrors; - } catch { - // Do Nothing - } - - if ("errors" in data) { - throw new Error(data.errors[0]); - } + const resp = await fetch(request); + await checkResponse(resp); } diff --git a/src/api/sys/policies/getPolicies.ts b/src/api/sys/policies/getPolicies.ts index 4825b13..2c927da 100644 --- a/src/api/sys/policies/getPolicies.ts +++ b/src/api/sys/policies/getPolicies.ts @@ -1,10 +1,12 @@ -import { appendAPIURL, getHeaders } from "../../apiUtils"; +import { appendAPIURL, checkResponse, getHeaders } from "../../apiUtils"; export async function getPolicies(): Promise { const request = new Request(appendAPIURL("/v1/sys/policies/acl?list=true"), { headers: getHeaders(), }); - const response = await fetch(request); - const data = (await response.json()) as { data: { keys: string[] } }; + const resp = await fetch(request); + await checkResponse(resp); + + const data = (await resp.json()) as { data: { keys: string[] } }; return data.data.keys; } diff --git a/src/api/sys/policies/getPolicy.ts b/src/api/sys/policies/getPolicy.ts index 04f8a3f..e9d7746 100644 --- a/src/api/sys/policies/getPolicy.ts +++ b/src/api/sys/policies/getPolicy.ts @@ -1,10 +1,12 @@ -import { appendAPIURL, getHeaders } from "../../apiUtils"; +import { appendAPIURL, checkResponse, getHeaders } from "../../apiUtils"; export async function getPolicy(name: string): Promise { const request = new Request(appendAPIURL("/v1/sys/policies/acl/" + name), { headers: getHeaders(), }); - const response = await fetch(request); - const data = (await response.json()) as { data: { policy: string } }; + const resp = await fetch(request); + await checkResponse(resp); + + const data = (await resp.json()) as { data: { policy: string } }; return data.data.policy; } diff --git a/src/api/sys/renewSelf.ts b/src/api/sys/renewSelf.ts index 2738ac8..a13a14e 100644 --- a/src/api/sys/renewSelf.ts +++ b/src/api/sys/renewSelf.ts @@ -1,4 +1,4 @@ -import { appendAPIURL, getHeaders } from "../apiUtils"; +import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; export async function renewSelf(): Promise { const request = new Request(appendAPIURL("/v1/auth/token/renew-self"), { @@ -10,8 +10,5 @@ export async function renewSelf(): Promise { body: JSON.stringify({}), }); const resp = await fetch(request); - const data = (await resp.json()) as { errors?: string[] }; - if ("errors" in data) { - throw new Error(data.errors[0]); - } + await checkResponse(resp); } diff --git a/src/api/sys/sealVault.ts b/src/api/sys/sealVault.ts index be24431..a9140ba 100644 --- a/src/api/sys/sealVault.ts +++ b/src/api/sys/sealVault.ts @@ -1,9 +1,10 @@ -import { appendAPIURL, getHeaders } from "../apiUtils"; +import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; export async function sealVault(): Promise { const request = new Request(appendAPIURL("/v1/sys/seal"), { method: "PUT", headers: getHeaders(), }); - await fetch(request); + const resp = await fetch(request); + await checkResponse(resp); } diff --git a/src/api/sys/submitUnsealKey.ts b/src/api/sys/submitUnsealKey.ts index b1090a7..c2a1913 100644 --- a/src/api/sys/submitUnsealKey.ts +++ b/src/api/sys/submitUnsealKey.ts @@ -1,4 +1,4 @@ -import { appendAPIURL } from "../apiUtils"; +import { appendAPIURL, checkResponse } from "../apiUtils"; export async function submitUnsealKey(key: string): Promise { const request = new Request(appendAPIURL("/v1/sys/unseal"), { @@ -11,10 +11,5 @@ export async function submitUnsealKey(key: string): Promise { }), }); const resp = await fetch(request); - if (!resp.ok) { - const data = (await resp.json()) as { errors?: string[] }; - if ("errors" in data) { - throw new Error(data.errors[0]); - } - } + await checkResponse(resp); } diff --git a/src/api/totp/addNewTOTP.ts b/src/api/totp/addNewTOTP.ts index 5005eb1..018fe26 100644 --- a/src/api/totp/addNewTOTP.ts +++ b/src/api/totp/addNewTOTP.ts @@ -1,4 +1,4 @@ -import { appendAPIURL, getHeaders } from "../apiUtils"; +import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; import { removeDoubleSlash } from "../../utils"; export async function addNewTOTP(baseMount: string, parms: { name: string }): Promise { @@ -14,10 +14,5 @@ export async function addNewTOTP(baseMount: string, parms: { name: string }): Pr }, ); const resp = await fetch(request); - if (!resp.ok) { - const data = (await resp.json()) as { errors?: string[] }; - if ("errors" in data) { - throw new Error(data.errors[0]); - } - } + await checkResponse(resp); } diff --git a/src/api/totp/deleteTOTP.ts b/src/api/totp/deleteTOTP.ts index ddfb685..3ac643f 100644 --- a/src/api/totp/deleteTOTP.ts +++ b/src/api/totp/deleteTOTP.ts @@ -1,4 +1,4 @@ -import { appendAPIURL, getHeaders } from "../apiUtils"; +import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; export async function deleteTOTP(baseMount: string, name: string): Promise { const request = new Request(appendAPIURL(`/v1/${baseMount}/keys/${name}`), { @@ -6,10 +6,5 @@ export async function deleteTOTP(baseMount: string, name: string): Promise headers: getHeaders(), }); const resp = await fetch(request); - if (!resp.ok) { - const data = (await resp.json()) as { errors?: string[] }; - if ("errors" in data) { - throw new Error(data.errors[0]); - } - } + await checkResponse(resp); } diff --git a/src/api/totp/getTOTPCode.ts b/src/api/totp/getTOTPCode.ts index 371b70c..cbacd80 100644 --- a/src/api/totp/getTOTPCode.ts +++ b/src/api/totp/getTOTPCode.ts @@ -1,10 +1,13 @@ -import { appendAPIURL, getHeaders } from "../apiUtils"; +import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; export async function getTOTPCode(baseMount: string, name: string): Promise { const request = new Request(appendAPIURL(`/v1/${baseMount}/code/${name}`), { headers: getHeaders(), }); + const resp = await fetch(request); + await checkResponse(resp); + const data = (await resp.json()) as { data: { code: string } }; return data.data.code; } diff --git a/src/api/totp/getTOTPKeys.ts b/src/api/totp/getTOTPKeys.ts index a175b86..2fce44c 100644 --- a/src/api/totp/getTOTPKeys.ts +++ b/src/api/totp/getTOTPKeys.ts @@ -1,5 +1,5 @@ import { DoesNotExistError } from "../../types/internalErrors"; -import { appendAPIURL, getHeaders } from "../apiUtils"; +import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; export async function getTOTPKeys(baseMount: string): Promise { const request = new Request(appendAPIURL(`/v1/${baseMount}/keys?list=true`), { @@ -7,9 +7,8 @@ export async function getTOTPKeys(baseMount: string): Promise { }); const resp = await fetch(request); - if (resp.status == 404) { - throw DoesNotExistError; - } + await checkResponse(resp); + const data = (await resp.json()) as { data: { keys: string[] } }; return data.data.keys; } diff --git a/src/api/transit/getTransitKey.ts b/src/api/transit/getTransitKey.ts index 9a197b5..0221e24 100644 --- a/src/api/transit/getTransitKey.ts +++ b/src/api/transit/getTransitKey.ts @@ -1,6 +1,5 @@ -import { DoesNotExistError } from "../../types/internalErrors"; import { TransitKeyType } from "../types/transit"; -import { appendAPIURL, getHeaders } from "../apiUtils"; +import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; export async function getTransitKey(baseMount: string, name: string): Promise { const request = new Request(appendAPIURL(`/v1/${baseMount}/keys/${name}`), { @@ -8,9 +7,8 @@ export async function getTransitKey(baseMount: string, name: string): Promise { const request = new Request(appendAPIURL(`/v1/${baseMount}/keys?list=true`), { headers: getHeaders(), }); + const resp = await fetch(request); - if (resp.status == 404) { - throw DoesNotExistError; - } + await checkResponse(resp); + const data = (await resp.json()) as { data: { keys: string[] } }; return data.data.keys; } diff --git a/src/api/transit/newTransitKey.ts b/src/api/transit/newTransitKey.ts index ff8d3f4..92f99b3 100644 --- a/src/api/transit/newTransitKey.ts +++ b/src/api/transit/newTransitKey.ts @@ -1,4 +1,4 @@ -import { appendAPIURL, getHeaders } from "../apiUtils"; +import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; import { removeDoubleSlash } from "../../utils"; export async function newTransitKey( @@ -17,10 +17,5 @@ export async function newTransitKey( }, ); const resp = await fetch(request); - if (!resp.ok) { - const data = (await resp.json()) as { errors?: string[] }; - if ("errors" in data) { - throw new Error(data.errors[0]); - } - } + await checkResponse(resp); } diff --git a/src/api/transit/transitDecrypt.ts b/src/api/transit/transitDecrypt.ts index 7e09b76..6cf52aa 100644 --- a/src/api/transit/transitDecrypt.ts +++ b/src/api/transit/transitDecrypt.ts @@ -1,4 +1,4 @@ -import { appendAPIURL, getHeaders } from "../apiUtils"; +import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; import { removeDoubleSlash } from "../../utils"; type DecryptionResult = { @@ -23,14 +23,12 @@ export async function transitDecrypt( body: JSON.stringify(payload), }); - const response = await fetch(request); - const data = (await response.json()) as { - errors?: string[]; + const resp = await fetch(request); + await checkResponse(resp); + + const data = (await resp.json()) as { data?: DecryptionResult; }; - if (!response.ok) { - throw new Error(data.errors[0]); - } else { - return data.data; - } + + return data.data; } diff --git a/src/api/transit/transitEncrypt.ts b/src/api/transit/transitEncrypt.ts index e3ee181..faebab2 100644 --- a/src/api/transit/transitEncrypt.ts +++ b/src/api/transit/transitEncrypt.ts @@ -1,4 +1,4 @@ -import { appendAPIURL, getHeaders } from "../apiUtils"; +import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; import { removeDoubleSlash } from "../../utils"; type EncryptionResult = { @@ -23,14 +23,12 @@ export async function transitEncrypt( body: JSON.stringify(payload), }); - const response = await fetch(request); - const data = (await response.json()) as { - errors?: string[]; + const resp = await fetch(request); + await checkResponse(resp); + + const data = (await resp.json()) as { data?: EncryptionResult; }; - if (!response.ok) { - throw new Error(data.errors[0]); - } else { - return data.data; - } + + return data.data; } diff --git a/src/api/transit/transitRewrap.ts b/src/api/transit/transitRewrap.ts index 8924069..81ed4e8 100644 --- a/src/api/transit/transitRewrap.ts +++ b/src/api/transit/transitRewrap.ts @@ -1,4 +1,4 @@ -import { appendAPIURL, getHeaders } from "../apiUtils"; +import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; import { removeDoubleSlash } from "../../utils"; type RewrapResult = { @@ -24,14 +24,12 @@ export async function transitRewrap( body: JSON.stringify(payload), }); - const response = await fetch(request); - const data = (await response.json()) as { - errors?: string[]; + const resp = await fetch(request); + await checkResponse(resp); + + const data = (await resp.json()) as { data?: RewrapResult; }; - if (!response.ok) { - throw new Error(data.errors[0]); - } else { - return data.data; - } + + return data.data; } diff --git a/src/api/types/api.ts b/src/api/types/api.ts new file mode 100644 index 0000000..70b0185 --- /dev/null +++ b/src/api/types/api.ts @@ -0,0 +1,3 @@ +export type BaseAPIResponse = { + errors?: string[]; +} \ No newline at end of file