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 { DoesNotExistError } from "../types/internalErrors";
import { BaseAPIResponse } from "./types/api";
export function getHeaders(): Record<string, string> {
return {
@ -7,3 +9,19 @@ export function getHeaders(): Record<string, string> {
}
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 { appendAPIURL, getHeaders } from "../apiUtils";
import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils";
export async function listAuth(): Promise<AuthListType> {
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;
}

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> {
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;
}

View file

@ -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);
}

View file

@ -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<void> {
@ -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);
}

View file

@ -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<UserType> {
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;
}

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[]> {
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;
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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<string, unknown> } }).data.data;

View file

@ -1,4 +1,4 @@
import { appendAPIURL, getHeaders } from "../apiUtils";
import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils";
type SecretMetadataType = {
versions: Record<string, unknown>;
@ -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;
}

View file

@ -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;
}

View file

@ -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);
}

View file

@ -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<string[]> {
@ -12,8 +12,10 @@ export async function getCapabilitiesPath(path: string): Promise<string[]> {
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;
}

View file

@ -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<MountsType> {
headers: getHeaders(),
});
const resp = await fetch(request);
await checkResponse(resp);
const data = (await resp.json()) as { data: { secret: MountsType } };
return data.data.secret;
}

View file

@ -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<SealStatusType> {
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;
}

View file

@ -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<TokenInfo> {
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;
}

View file

@ -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<void> {
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);
}

View file

@ -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<void> {
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);
}

View file

@ -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<void> {
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);
}

View file

@ -1,10 +1,12 @@
import { appendAPIURL, getHeaders } from "../../apiUtils";
import { appendAPIURL, checkResponse, getHeaders } from "../../apiUtils";
export async function getPolicies(): Promise<string[]> {
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;
}

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> {
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;
}

View file

@ -1,4 +1,4 @@
import { appendAPIURL, getHeaders } from "../apiUtils";
import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils";
export async function renewSelf(): Promise<void> {
const request = new Request(appendAPIURL("/v1/auth/token/renew-self"), {
@ -10,8 +10,5 @@ export async function renewSelf(): Promise<void> {
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);
}

View file

@ -1,9 +1,10 @@
import { appendAPIURL, getHeaders } from "../apiUtils";
import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils";
export async function sealVault(): Promise<void> {
const request = new Request(appendAPIURL("/v1/sys/seal"), {
method: "PUT",
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> {
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);
if (!resp.ok) {
const data = (await resp.json()) as { errors?: string[] };
if ("errors" in data) {
throw new Error(data.errors[0]);
}
}
await checkResponse(resp);
}

View file

@ -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<void> {
@ -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);
}

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> {
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(),
});
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);
}

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> {
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;
}

View file

@ -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<string[]> {
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);
if (resp.status == 404) {
throw DoesNotExistError;
}
await checkResponse(resp);
const data = (await resp.json()) as { data: { keys: string[] } };
return data.data.keys;
}

View file

@ -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<TransitKeyType> {
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);
if (resp.status == 404) {
throw DoesNotExistError;
}
await checkResponse(resp);
const data = (await resp.json()) as { data: TransitKeyType };
return data.data;
}

View file

@ -1,14 +1,14 @@
import { DoesNotExistError } from "../../types/internalErrors";
import { appendAPIURL, getHeaders } from "../apiUtils";
import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils";
export async function getTransitKeys(baseMount: string): Promise<string[]> {
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;
}

View file

@ -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);
}

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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;
}

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

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