1
0
Fork 0

Move API error checking into checkResponse.

This commit is contained in:
Kitteh 2021-05-28 09:45:46 +01:00
parent 6b41cf4e75
commit 56aeb9b514
36 changed files with 143 additions and 177 deletions

View file

@ -1,4 +1,6 @@
import { pageState } from "../globalPageState"; import { pageState } from "../globalPageState";
import { DoesNotExistError } from "../types/internalErrors";
import { BaseAPIResponse } from "./types/api";
export function getHeaders(): Record<string, string> { export function getHeaders(): Record<string, string> {
return { return {
@ -7,3 +9,19 @@ export function getHeaders(): Record<string, string> {
} }
export const appendAPIURL = (url: string): string => pageState.apiURL + url; export const appendAPIURL = (url: string): string => pageState.apiURL + url;
export async function checkResponse(resp: Response): Promise<void> {
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]);
}
}

View file

@ -1,11 +1,13 @@
import { AuthListAPIType, AuthListType } from "../types/auth"; import { AuthListAPIType, AuthListType } from "../types/auth";
import { appendAPIURL, getHeaders } from "../apiUtils"; import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils";
export async function listAuth(): Promise<AuthListType> { export async function listAuth(): Promise<AuthListType> {
const request = new Request(appendAPIURL(`/v1/sys/auth`), { const request = new Request(appendAPIURL(`/v1/sys/auth`), {
headers: getHeaders(), headers: getHeaders(),
}); });
const resp = await fetch(request); const resp = await fetch(request);
await checkResponse(resp);
const data = (await resp.json()) as AuthListAPIType; const data = (await resp.json()) as AuthListAPIType;
return data.data; return data.data;
} }

View file

@ -1,4 +1,4 @@
import { appendAPIURL } from "../apiUtils"; import { appendAPIURL, checkResponse } from "../apiUtils";
export async function usernameLogin(username: string, password: string): Promise<string> { export async function usernameLogin(username: string, password: string): Promise<string> {
const request = new Request(appendAPIURL(`/v1/auth/userpass/login/${username}`), { 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); const resp = await fetch(request);
await checkResponse(resp);
const data = (await resp.json()) as { const data = (await resp.json()) as {
auth?: { client_token: string }; auth: { client_token: string };
errors?: string[];
}; };
if ("auth" in data) { return data.auth.client_token;
return data.auth.client_token;
} else if ("errors" in data) {
throw new Error(data.errors[0]);
}
} }

View file

@ -1,5 +1,5 @@
import { UserType } from "../../types/userpass/user"; import { UserType } from "../../types/userpass/user";
import { appendAPIURL, getHeaders } from "../../apiUtils"; import { appendAPIURL, checkResponse, getHeaders } from "../../apiUtils";
import { removeDoubleSlash } from "../../../utils"; import { removeDoubleSlash } from "../../../utils";
export async function createOrUpdateUserPassUser( export async function createOrUpdateUserPassUser(
@ -18,9 +18,6 @@ export async function createOrUpdateUserPassUser(
body: JSON.stringify(data, null, 0), body: JSON.stringify(data, null, 0),
}, },
); );
const response = await fetch(request); const resp = await fetch(request);
if (!response.ok) { await checkResponse(resp);
const json = (await response.json()) as { errors: string[] };
throw new Error(json.errors[0]);
}
} }

View file

@ -1,4 +1,4 @@
import { appendAPIURL, getHeaders } from "../../apiUtils"; import { appendAPIURL, checkResponse, getHeaders } from "../../apiUtils";
import { removeDoubleSlash } from "../../../utils"; import { removeDoubleSlash } from "../../../utils";
export async function deleteUserPassUser(path: string, username: string): Promise<void> { export async function deleteUserPassUser(path: string, username: string): Promise<void> {
@ -9,9 +9,6 @@ export async function deleteUserPassUser(path: string, username: string): Promis
headers: getHeaders(), headers: getHeaders(),
}, },
); );
const response = await fetch(request); const resp = await fetch(request);
if (!response.ok) { await checkResponse(resp);
const json = (await response.json()) as { errors: string[] };
throw new Error(json.errors[0]);
}
} }

View file

@ -1,11 +1,13 @@
import { UserType, UserTypeAPIResp } from "../../types/userpass/user"; 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<UserType> { export async function getUserPassUser(path: string, username: string): Promise<UserType> {
const request = new Request(appendAPIURL(`/v1/auth/${path}/users/${username}`), { const request = new Request(appendAPIURL(`/v1/auth/${path}/users/${username}`), {
headers: getHeaders(), headers: getHeaders(),
}); });
const resp = await fetch(request); const resp = await fetch(request);
await checkResponse(resp);
const data = (await resp.json()) as UserTypeAPIResp; const data = (await resp.json()) as UserTypeAPIResp;
return data.data; return data.data;
} }

View file

@ -1,10 +1,12 @@
import { appendAPIURL, getHeaders } from "../../apiUtils"; import { appendAPIURL, checkResponse, getHeaders } from "../../apiUtils";
export async function listUserPassUsers(path: string): Promise<string[]> { export async function listUserPassUsers(path: string): Promise<string[]> {
const request = new Request(appendAPIURL(`/v1/auth/${path}/users?list=true`), { const request = new Request(appendAPIURL(`/v1/auth/${path}/users?list=true`), {
headers: getHeaders(), headers: getHeaders(),
}); });
const resp = await fetch(request); const resp = await fetch(request);
await checkResponse(resp);
const data = (await resp.json()) as { data: { keys: string[] } }; const data = (await resp.json()) as { data: { keys: string[] } };
return data.data.keys; return data.data.keys;
} }

View file

@ -1,4 +1,4 @@
import { appendAPIURL, getHeaders } from "../apiUtils"; import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils";
import { removeDoubleSlash } from "../../utils"; import { removeDoubleSlash } from "../../utils";
export async function createOrUpdateSecret( export async function createOrUpdateSecret(
@ -28,9 +28,6 @@ export async function createOrUpdateSecret(
}, },
body: JSON.stringify(APIData, null, 0), body: JSON.stringify(APIData, null, 0),
}); });
const response = await fetch(request); const resp = await fetch(request);
if (!response.ok) { await checkResponse(resp);
const json = (await response.json()) as { errors: string[] };
throw new Error(json.errors[0]);
}
} }

View file

@ -1,4 +1,4 @@
import { appendAPIURL, getHeaders } from "../apiUtils"; import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils";
import { removeDoubleSlash } from "../../utils"; import { removeDoubleSlash } from "../../utils";
export async function deleteSecret( export async function deleteSecret(
@ -35,9 +35,6 @@ export async function deleteSecret(
headers: getHeaders(), headers: getHeaders(),
}); });
} }
const response = await fetch(request); const resp = await fetch(request);
if (!response.ok) { await checkResponse(resp);
const json = (await response.json()) as { errors: string[] };
throw new Error(json.errors[0]);
}
} }

View file

@ -1,4 +1,4 @@
import { appendAPIURL, getHeaders } from "../apiUtils"; import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils";
export async function getSecret( export async function getSecret(
baseMount: string, baseMount: string,
@ -19,6 +19,9 @@ export async function getSecret(
}); });
const resp = await fetch(request); const resp = await fetch(request);
await checkResponse(resp);
const data = (await resp.json()) as unknown; const data = (await resp.json()) as unknown;
if (secretMountType == "kv-v2") { if (secretMountType == "kv-v2") {
return (data as { data: { data: Record<string, unknown> } }).data.data; return (data as { data: { data: Record<string, unknown> } }).data.data;

View file

@ -1,4 +1,4 @@
import { appendAPIURL, getHeaders } from "../apiUtils"; import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils";
type SecretMetadataType = { type SecretMetadataType = {
versions: Record<string, unknown>; versions: Record<string, unknown>;
@ -17,6 +17,8 @@ export async function getSecretMetadata(
); );
const resp = await fetch(request); const resp = await fetch(request);
await checkResponse(resp);
const data = (await resp.json()) as { data: SecretMetadataType }; const data = (await resp.json()) as { data: SecretMetadataType };
return data.data; return data.data;
} }

View file

@ -1,5 +1,5 @@
import { DoesNotExistError } from "../../types/internalErrors"; import { DoesNotExistError } from "../../types/internalErrors";
import { appendAPIURL, getHeaders } from "../apiUtils"; import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils";
export async function getSecrets( export async function getSecrets(
baseMount: string, baseMount: string,
@ -16,10 +16,10 @@ export async function getSecrets(
const request = new Request(appendAPIURL(secretURL), { const request = new Request(appendAPIURL(secretURL), {
headers: getHeaders(), headers: getHeaders(),
}); });
const resp = await fetch(request); const resp = await fetch(request);
if (resp.status == 404) { await checkResponse(resp);
throw DoesNotExistError;
}
const data = (await resp.json()) as { data: { keys: string[] } }; const data = (await resp.json()) as { data: { keys: string[] } };
return data.data.keys; return data.data.keys;
} }

View file

@ -1,4 +1,4 @@
import { appendAPIURL, getHeaders } from "../apiUtils"; import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils";
import { getObjectKeys, removeDoubleSlash } from "../../utils"; import { getObjectKeys, removeDoubleSlash } from "../../utils";
import { getSecretMetadata } from "./getSecretMetadata"; import { getSecretMetadata } from "./getSecretMetadata";
@ -24,9 +24,6 @@ export async function undeleteSecret(
}, },
body: JSON.stringify({ versions: [version] }), body: JSON.stringify({ versions: [version] }),
}); });
const response = await fetch(request); const resp = await fetch(request);
if (!response.ok) { await checkResponse(resp);
const json = (await response.json()) as { errors: string[] };
throw new Error(json.errors[0]);
}
} }

View file

@ -1,4 +1,4 @@
import { appendAPIURL, getHeaders } from "../apiUtils"; import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils";
import { removeDoubleSlash } from "../../utils"; import { removeDoubleSlash } from "../../utils";
export async function getCapabilitiesPath(path: string): Promise<string[]> { export async function getCapabilitiesPath(path: string): Promise<string[]> {
@ -12,8 +12,10 @@ export async function getCapabilitiesPath(path: string): Promise<string[]> {
paths: [removeDoubleSlash(path)], paths: [removeDoubleSlash(path)],
}), }),
}); });
const response = await fetch(request); const resp = await fetch(request);
const data = (await response.json()) as { capabilities: string[] }; await checkResponse(resp);
const data = (await resp.json()) as { capabilities: string[] };
return data.capabilities; return data.capabilities;
} }

View file

@ -1,4 +1,4 @@
import { appendAPIURL, getHeaders } from "../apiUtils"; import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils";
export type MountType = { export type MountType = {
type: string; type: string;
@ -16,6 +16,8 @@ export async function getMounts(): Promise<MountsType> {
headers: getHeaders(), headers: getHeaders(),
}); });
const resp = await fetch(request); const resp = await fetch(request);
await checkResponse(resp);
const data = (await resp.json()) as { data: { secret: MountsType } }; const data = (await resp.json()) as { data: { secret: MountsType } };
return data.data.secret; return data.data.secret;
} }

View file

@ -1,4 +1,4 @@
import { appendAPIURL } from "../apiUtils"; import { appendAPIURL, checkResponse } from "../apiUtils";
export type SealStatusType = { export type SealStatusType = {
progress: number; progress: number;
@ -9,6 +9,8 @@ export type SealStatusType = {
export async function getSealStatus(): Promise<SealStatusType> { export async function getSealStatus(): Promise<SealStatusType> {
const request = new Request(appendAPIURL("/v1/sys/seal-status")); const request = new Request(appendAPIURL("/v1/sys/seal-status"));
const resp = await fetch(request); const resp = await fetch(request);
await checkResponse(resp);
const data = (await resp.json()) as SealStatusType; const data = (await resp.json()) as SealStatusType;
return data; return data;
} }

View file

@ -1,15 +1,13 @@
import { TokenInfo } from "../types/token"; import { TokenInfo } from "../types/token";
import { appendAPIURL, getHeaders } from "../apiUtils"; import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils";
export async function lookupSelf(): Promise<TokenInfo> { export async function lookupSelf(): Promise<TokenInfo> {
const request = new Request(appendAPIURL("/v1/auth/token/lookup-self"), { const request = new Request(appendAPIURL("/v1/auth/token/lookup-self"), {
headers: getHeaders(), headers: getHeaders(),
}); });
const resp = await fetch(request); const resp = await fetch(request);
const data = (await resp.json()) as { data?: TokenInfo; errors?: string[] }; await checkResponse(resp);
if ("data" in data) {
return data.data; const data = (await resp.json()) as { data: TokenInfo; };
} else if ("errors" in data) { return data.data;
throw new Error(data.errors[0]);
}
} }

View file

@ -1,4 +1,4 @@
import { appendAPIURL, getHeaders } from "../apiUtils"; import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils";
import { removeDoubleSlash } from "../../utils"; import { removeDoubleSlash } from "../../utils";
type NewMountParams = { type NewMountParams = {
@ -19,10 +19,5 @@ export async function newMount(parms: NewMountParams): Promise<void> {
body: JSON.stringify(parms), body: JSON.stringify(parms),
}); });
const resp = await fetch(request); const resp = await fetch(request);
if (!resp.ok) { await checkResponse(resp);
const data = (await resp.json()) as { errors?: string[] };
if ("errors" in data) {
throw new Error(data.errors[0]);
}
}
} }

View file

@ -1,6 +1,4 @@
import { appendAPIURL, getHeaders } from "../../apiUtils"; import { appendAPIURL, checkResponse, getHeaders } from "../../apiUtils";
type OptionalErrors = { errors?: string[] };
export async function createOrUpdatePolicy(name: string, policy_data: string): Promise<void> { export async function createOrUpdatePolicy(name: string, policy_data: string): Promise<void> {
const request = new Request(appendAPIURL("/v1/sys/policies/acl/" + name), { 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), body: JSON.stringify({ policy: policy_data }, null, 0),
}); });
const response = await fetch(request); const resp = await fetch(request);
let data: OptionalErrors = {}; await checkResponse(resp);
try {
data = (await response.json()) as OptionalErrors;
} catch {
// Do Nothing
}
if ("errors" in data) {
throw new Error(data.errors[0]);
}
} }

View file

@ -1,21 +1,10 @@
import { appendAPIURL, getHeaders } from "../../apiUtils"; import { appendAPIURL, checkResponse, getHeaders } from "../../apiUtils";
type OptionalErrors = { errors?: string[] };
export async function deletePolicy(name: string): Promise<void> { export async function deletePolicy(name: string): Promise<void> {
const request = new Request(appendAPIURL("/v1/sys/policies/acl/" + name), { const request = new Request(appendAPIURL("/v1/sys/policies/acl/" + name), {
method: "DELETE", method: "DELETE",
headers: getHeaders(), headers: getHeaders(),
}); });
const response = await fetch(request); const resp = await fetch(request);
let data: OptionalErrors = {}; await checkResponse(resp);
try {
data = (await response.json()) as OptionalErrors;
} catch {
// Do Nothing
}
if ("errors" in data) {
throw new Error(data.errors[0]);
}
} }

View file

@ -1,10 +1,12 @@
import { appendAPIURL, getHeaders } from "../../apiUtils"; import { appendAPIURL, checkResponse, getHeaders } from "../../apiUtils";
export async function getPolicies(): Promise<string[]> { export async function getPolicies(): Promise<string[]> {
const request = new Request(appendAPIURL("/v1/sys/policies/acl?list=true"), { const request = new Request(appendAPIURL("/v1/sys/policies/acl?list=true"), {
headers: getHeaders(), headers: getHeaders(),
}); });
const response = await fetch(request); const resp = await fetch(request);
const data = (await response.json()) as { data: { keys: string[] } }; await checkResponse(resp);
const data = (await resp.json()) as { data: { keys: string[] } };
return data.data.keys; return data.data.keys;
} }

View file

@ -1,10 +1,12 @@
import { appendAPIURL, getHeaders } from "../../apiUtils"; import { appendAPIURL, checkResponse, getHeaders } from "../../apiUtils";
export async function getPolicy(name: string): Promise<string> { export async function getPolicy(name: string): Promise<string> {
const request = new Request(appendAPIURL("/v1/sys/policies/acl/" + name), { const request = new Request(appendAPIURL("/v1/sys/policies/acl/" + name), {
headers: getHeaders(), headers: getHeaders(),
}); });
const response = await fetch(request); const resp = await fetch(request);
const data = (await response.json()) as { data: { policy: string } }; await checkResponse(resp);
const data = (await resp.json()) as { data: { policy: string } };
return data.data.policy; return data.data.policy;
} }

View file

@ -1,4 +1,4 @@
import { appendAPIURL, getHeaders } from "../apiUtils"; import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils";
export async function renewSelf(): Promise<void> { export async function renewSelf(): Promise<void> {
const request = new Request(appendAPIURL("/v1/auth/token/renew-self"), { const request = new Request(appendAPIURL("/v1/auth/token/renew-self"), {
@ -10,8 +10,5 @@ export async function renewSelf(): Promise<void> {
body: JSON.stringify({}), body: JSON.stringify({}),
}); });
const resp = await fetch(request); const resp = await fetch(request);
const data = (await resp.json()) as { errors?: string[] }; await checkResponse(resp);
if ("errors" in data) {
throw new Error(data.errors[0]);
}
} }

View file

@ -1,9 +1,10 @@
import { appendAPIURL, getHeaders } from "../apiUtils"; import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils";
export async function sealVault(): Promise<void> { export async function sealVault(): Promise<void> {
const request = new Request(appendAPIURL("/v1/sys/seal"), { const request = new Request(appendAPIURL("/v1/sys/seal"), {
method: "PUT", method: "PUT",
headers: getHeaders(), headers: getHeaders(),
}); });
await fetch(request); const resp = await fetch(request);
await checkResponse(resp);
} }

View file

@ -1,4 +1,4 @@
import { appendAPIURL } from "../apiUtils"; import { appendAPIURL, checkResponse } from "../apiUtils";
export async function submitUnsealKey(key: string): Promise<void> { export async function submitUnsealKey(key: string): Promise<void> {
const request = new Request(appendAPIURL("/v1/sys/unseal"), { const request = new Request(appendAPIURL("/v1/sys/unseal"), {
@ -11,10 +11,5 @@ export async function submitUnsealKey(key: string): Promise<void> {
}), }),
}); });
const resp = await fetch(request); const resp = await fetch(request);
if (!resp.ok) { await checkResponse(resp);
const data = (await resp.json()) as { errors?: string[] };
if ("errors" in data) {
throw new Error(data.errors[0]);
}
}
} }

View file

@ -1,4 +1,4 @@
import { appendAPIURL, getHeaders } from "../apiUtils"; import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils";
import { removeDoubleSlash } from "../../utils"; import { removeDoubleSlash } from "../../utils";
export async function addNewTOTP(baseMount: string, parms: { name: string }): Promise<void> { export async function addNewTOTP(baseMount: string, parms: { name: string }): Promise<void> {
@ -14,10 +14,5 @@ export async function addNewTOTP(baseMount: string, parms: { name: string }): Pr
}, },
); );
const resp = await fetch(request); const resp = await fetch(request);
if (!resp.ok) { await checkResponse(resp);
const data = (await resp.json()) as { errors?: string[] };
if ("errors" in data) {
throw new Error(data.errors[0]);
}
}
} }

View file

@ -1,4 +1,4 @@
import { appendAPIURL, getHeaders } from "../apiUtils"; import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils";
export async function deleteTOTP(baseMount: string, name: string): Promise<void> { export async function deleteTOTP(baseMount: string, name: string): Promise<void> {
const request = new Request(appendAPIURL(`/v1/${baseMount}/keys/${name}`), { const request = new Request(appendAPIURL(`/v1/${baseMount}/keys/${name}`), {
@ -6,10 +6,5 @@ export async function deleteTOTP(baseMount: string, name: string): Promise<void>
headers: getHeaders(), headers: getHeaders(),
}); });
const resp = await fetch(request); const resp = await fetch(request);
if (!resp.ok) { await checkResponse(resp);
const data = (await resp.json()) as { errors?: string[] };
if ("errors" in data) {
throw new Error(data.errors[0]);
}
}
} }

View file

@ -1,10 +1,13 @@
import { appendAPIURL, getHeaders } from "../apiUtils"; import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils";
export async function getTOTPCode(baseMount: string, name: string): Promise<string> { export async function getTOTPCode(baseMount: string, name: string): Promise<string> {
const request = new Request(appendAPIURL(`/v1/${baseMount}/code/${name}`), { const request = new Request(appendAPIURL(`/v1/${baseMount}/code/${name}`), {
headers: getHeaders(), headers: getHeaders(),
}); });
const resp = await fetch(request); const resp = await fetch(request);
await checkResponse(resp);
const data = (await resp.json()) as { data: { code: string } }; const data = (await resp.json()) as { data: { code: string } };
return data.data.code; return data.data.code;
} }

View file

@ -1,5 +1,5 @@
import { DoesNotExistError } from "../../types/internalErrors"; import { DoesNotExistError } from "../../types/internalErrors";
import { appendAPIURL, getHeaders } from "../apiUtils"; import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils";
export async function getTOTPKeys(baseMount: string): Promise<string[]> { export async function getTOTPKeys(baseMount: string): Promise<string[]> {
const request = new Request(appendAPIURL(`/v1/${baseMount}/keys?list=true`), { const request = new Request(appendAPIURL(`/v1/${baseMount}/keys?list=true`), {
@ -7,9 +7,8 @@ export async function getTOTPKeys(baseMount: string): Promise<string[]> {
}); });
const resp = await fetch(request); const resp = await fetch(request);
if (resp.status == 404) { await checkResponse(resp);
throw DoesNotExistError;
}
const data = (await resp.json()) as { data: { keys: string[] } }; const data = (await resp.json()) as { data: { keys: string[] } };
return data.data.keys; return data.data.keys;
} }

View file

@ -1,6 +1,5 @@
import { DoesNotExistError } from "../../types/internalErrors";
import { TransitKeyType } from "../types/transit"; 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<TransitKeyType> { export async function getTransitKey(baseMount: string, name: string): Promise<TransitKeyType> {
const request = new Request(appendAPIURL(`/v1/${baseMount}/keys/${name}`), { const request = new Request(appendAPIURL(`/v1/${baseMount}/keys/${name}`), {
@ -8,9 +7,8 @@ export async function getTransitKey(baseMount: string, name: string): Promise<Tr
}); });
const resp = await fetch(request); const resp = await fetch(request);
if (resp.status == 404) { await checkResponse(resp);
throw DoesNotExistError;
}
const data = (await resp.json()) as { data: TransitKeyType }; const data = (await resp.json()) as { data: TransitKeyType };
return data.data; return data.data;
} }

View file

@ -1,14 +1,14 @@
import { DoesNotExistError } from "../../types/internalErrors"; import { DoesNotExistError } from "../../types/internalErrors";
import { appendAPIURL, getHeaders } from "../apiUtils"; import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils";
export async function getTransitKeys(baseMount: string): Promise<string[]> { export async function getTransitKeys(baseMount: string): Promise<string[]> {
const request = new Request(appendAPIURL(`/v1/${baseMount}/keys?list=true`), { const request = new Request(appendAPIURL(`/v1/${baseMount}/keys?list=true`), {
headers: getHeaders(), headers: getHeaders(),
}); });
const resp = await fetch(request); const resp = await fetch(request);
if (resp.status == 404) { await checkResponse(resp);
throw DoesNotExistError;
}
const data = (await resp.json()) as { data: { keys: string[] } }; const data = (await resp.json()) as { data: { keys: string[] } };
return data.data.keys; return data.data.keys;
} }

View file

@ -1,4 +1,4 @@
import { appendAPIURL, getHeaders } from "../apiUtils"; import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils";
import { removeDoubleSlash } from "../../utils"; import { removeDoubleSlash } from "../../utils";
export async function newTransitKey( export async function newTransitKey(
@ -17,10 +17,5 @@ export async function newTransitKey(
}, },
); );
const resp = await fetch(request); const resp = await fetch(request);
if (!resp.ok) { await checkResponse(resp);
const data = (await resp.json()) as { errors?: string[] };
if ("errors" in data) {
throw new Error(data.errors[0]);
}
}
} }

View file

@ -1,4 +1,4 @@
import { appendAPIURL, getHeaders } from "../apiUtils"; import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils";
import { removeDoubleSlash } from "../../utils"; import { removeDoubleSlash } from "../../utils";
type DecryptionResult = { type DecryptionResult = {
@ -23,14 +23,12 @@ export async function transitDecrypt(
body: JSON.stringify(payload), body: JSON.stringify(payload),
}); });
const response = await fetch(request); const resp = await fetch(request);
const data = (await response.json()) as { await checkResponse(resp);
errors?: string[];
const data = (await resp.json()) as {
data?: DecryptionResult; data?: DecryptionResult;
}; };
if (!response.ok) {
throw new Error(data.errors[0]); return data.data;
} else {
return data.data;
}
} }

View file

@ -1,4 +1,4 @@
import { appendAPIURL, getHeaders } from "../apiUtils"; import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils";
import { removeDoubleSlash } from "../../utils"; import { removeDoubleSlash } from "../../utils";
type EncryptionResult = { type EncryptionResult = {
@ -23,14 +23,12 @@ export async function transitEncrypt(
body: JSON.stringify(payload), body: JSON.stringify(payload),
}); });
const response = await fetch(request); const resp = await fetch(request);
const data = (await response.json()) as { await checkResponse(resp);
errors?: string[];
const data = (await resp.json()) as {
data?: EncryptionResult; data?: EncryptionResult;
}; };
if (!response.ok) {
throw new Error(data.errors[0]); return data.data;
} else {
return data.data;
}
} }

View file

@ -1,4 +1,4 @@
import { appendAPIURL, getHeaders } from "../apiUtils"; import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils";
import { removeDoubleSlash } from "../../utils"; import { removeDoubleSlash } from "../../utils";
type RewrapResult = { type RewrapResult = {
@ -24,14 +24,12 @@ export async function transitRewrap(
body: JSON.stringify(payload), body: JSON.stringify(payload),
}); });
const response = await fetch(request); const resp = await fetch(request);
const data = (await response.json()) as { await checkResponse(resp);
errors?: string[];
const data = (await resp.json()) as {
data?: RewrapResult; data?: RewrapResult;
}; };
if (!response.ok) {
throw new Error(data.errors[0]); return data.data;
} else {
return data.data;
}
} }

3
src/api/types/api.ts Normal file
View file

@ -0,0 +1,3 @@
export type BaseAPIResponse = {
errors?: string[];
}