1
0
Fork 0

Add typing to API/*.ts.

This commit is contained in:
Kitteh 2021-05-08 00:21:58 +01:00
parent 8351b81b82
commit 1b1a7a1d2d
29 changed files with 182 additions and 120 deletions

View file

@ -1,8 +1,8 @@
import { appendAPIURL, getHeaders } from "./apiUtils.js";
import { appendAPIURL, getHeaders } from "./apiUtils";
import { removeDoubleSlash } from "../utils";
export async function addNewTOTP(baseMount, parms) {
export async function addNewTOTP(baseMount: string, parms: {name: string}): Promise<void> {
const request = new Request(appendAPIURL(removeDoubleSlash(`/v1/${baseMount}/keys/${parms.name}`)), {
method: 'POST',
headers: {
@ -11,9 +11,9 @@ export async function addNewTOTP(baseMount, parms) {
},
body: JSON.stringify(parms)
});
let response = await fetch(request);
const response = await fetch(request);
if (!response.ok) {
let json = await response.json();
const json = await response.json();
throw new Error(json.errors[0]);
}
}

View file

@ -1,9 +0,0 @@
import { pageState } from "../globalPageState.ts";
export function getHeaders() {
return {
"X-Vault-Token": pageState.token,
}
}
export const appendAPIURL = (url) => pageState.apiURL + url;

9
src/api/apiUtils.ts Normal file
View file

@ -0,0 +1,9 @@
import { pageState } from "../globalPageState";
export function getHeaders(): any {
return {
"X-Vault-Token": pageState.token,
}
}
export const appendAPIURL = (url: string): string => pageState.apiURL + url;

View file

@ -1,8 +1,14 @@
import { appendAPIURL, getHeaders } from "./apiUtils.js";
import { appendAPIURL, getHeaders } from "./apiUtils";
import { removeDoubleSlash } from "../utils";
export async function createOrUpdateSecret(baseMount, mountType, secretPath, name, data) {
export async function createOrUpdateSecret(
baseMount: string,
mountType: string,
secretPath: string[],
name: string,
data: Record<string, unknown>
): Promise<void> {
let secretURL = "";
let APIData = {};
@ -23,9 +29,9 @@ export async function createOrUpdateSecret(baseMount, mountType, secretPath, nam
},
body: JSON.stringify(APIData, null, 0)
});
let response = await fetch(request);
const response = await fetch(request);
if (!response.ok) {
let json = await response.json();
const json = await response.json();
throw new Error(json.errors[0]);
}
}

View file

@ -1,8 +1,14 @@
import { appendAPIURL, getHeaders } from "./apiUtils.js";
import { appendAPIURL, getHeaders } from "./apiUtils";
import { removeDoubleSlash } from "../utils";
export async function deleteSecret(baseMount, mountType, secretPath, name, version = null) {
export async function deleteSecret(
baseMount: string,
mountType: string,
secretPath: string[],
name: string,
version: string | null = null
): Promise<void> {
let secretURL = "";
let request;
@ -27,12 +33,12 @@ export async function deleteSecret(baseMount, mountType, secretPath, name, versi
secretURL = removeDoubleSlash(secretURL).replace(/\/$/, "");
request = new Request(appendAPIURL(secretURL), {
method: "DELETE",
headers: getHeaders(),
headers: (getHeaders() as any),
});
}
let response = await fetch(request);
const response = await fetch(request);
if (!response.ok) {
let json = await response.json();
const json = await response.json();
throw new Error(json.errors[0]);
}
}

View file

@ -1,8 +1,8 @@
import { appendAPIURL, getHeaders } from "./apiUtils.js";
import { appendAPIURL, getHeaders } from "./apiUtils";
import { removeDoubleSlash } from "../utils";
export async function getCapabilitiesPath(path) {
export async function getCapabilitiesPath(path: string): Promise<string[]> {
const request = new Request(appendAPIURL("/v1/sys/capabilities-self"), {
method: "POST",
headers: {
@ -22,6 +22,10 @@ export async function getCapabilitiesPath(path) {
});
}
export async function getCapabilities(baseMount, secretPath, name) {
export async function getCapabilities(
baseMount: string,
secretPath: string[],
name: string
): Promise<string[]> {
return await getCapabilitiesPath(removeDoubleSlash(baseMount + secretPath.join("/") + "/" + name));
}

View file

@ -1,13 +0,0 @@
import { appendAPIURL, getHeaders } from "./apiUtils.js";
export async function getMounts() {
const request = new Request(appendAPIURL("/v1/sys/internal/ui/mounts"), {
headers: getHeaders(),
});
return fetch(request).then(response => {
return response.json();
}).then(data => {
return data.data.secret;
});
}

21
src/api/getMounts.ts Normal file
View file

@ -0,0 +1,21 @@
import { appendAPIURL, getHeaders } from "./apiUtils";
type MountsType = {
[key: string]: {
type: string
options: {
version: string
}
}
}
export async function getMounts(): Promise<MountsType> {
const request = new Request(appendAPIURL("/v1/sys/internal/ui/mounts"), {
headers: (getHeaders() as any),
});
return fetch(request).then(response => {
return response.json();
}).then(data => {
return data.data.secret;
});
}

View file

@ -1,7 +1,12 @@
import { appendAPIURL } from "./apiUtils.js";
import { appendAPIURL } from "./apiUtils";
type SealStatusType = {
progress: number;
t: number;
sealed: boolean;
}
export async function getSealStatus() {
export async function getSealStatus(): Promise<SealStatusType> {
const request = new Request(appendAPIURL("/v1/sys/seal-status"));
return fetch(request).then(response => {
return response.json();

View file

@ -1,7 +1,13 @@
import { appendAPIURL, getHeaders } from "./apiUtils.js";
import { appendAPIURL, getHeaders } from "./apiUtils";
export async function getSecret(baseMount, mountType, secretPath, name, version = null) {
export async function getSecret(
baseMount: string,
mountType: string,
secretPath: string[],
name: string,
version: string|null = null
): Promise<Record<any, any>> {
let secretURL = "";
if (mountType == "kv-v2") {
secretURL = `/v1/${baseMount}/data/${secretPath.join("")}/${name}`;
@ -11,7 +17,7 @@ export async function getSecret(baseMount, mountType, secretPath, name, version
secretURL = `/v1/${baseMount}/${secretPath.join("")}/${name}`;
}
const request = new Request(appendAPIURL(secretURL), {
headers: getHeaders(),
headers: (getHeaders() as any),
});
return fetch(request).then(response => {

View file

@ -1,14 +0,0 @@
import { appendAPIURL, getHeaders } from "./apiUtils.js";
export async function getSecretMetadata(baseMount, secretPath, name) {
const request = new Request(appendAPIURL(`/v1/${baseMount}/metadata/${secretPath.join("")}/${name}`), {
headers: getHeaders(),
});
return fetch(request).then(response => {
return response.json();
}).then(data => {
return data.data;
});
}

View file

@ -0,0 +1,21 @@
import { appendAPIURL, getHeaders } from "./apiUtils";
type SecretMetadataType = {
versions: Record<string, Record<any, any>>
}
export async function getSecretMetadata(
baseMount: string,
secretPath: string[],
name: string
): Promise<SecretMetadataType> {
const request = new Request(appendAPIURL(`/v1/${baseMount}/metadata/${secretPath.join("")}/${name}`), {
headers: (getHeaders() as any),
});
return fetch(request).then(response => {
return response.json();
}).then(data => {
return data.data;
});
}

View file

@ -1,8 +1,11 @@
import { DoesNotExistError } from "../types/internalErrors";
import { appendAPIURL, getHeaders } from "./apiUtils.js";
import { appendAPIURL, getHeaders } from "./apiUtils";
export async function getSecrets(baseMount, mountType, secretPath) {
export async function getSecrets(
baseMount: string,
mountType: string,
secretPath: string[]
): Promise<string[]> {
let secretURL = "";
if (mountType == "kv-v2") {
secretURL = `/v1/${baseMount}/metadata/${secretPath.join("")}?list=true`;
@ -11,7 +14,7 @@ export async function getSecrets(baseMount, mountType, secretPath) {
secretURL = `/v1/${baseMount}/${secretPath.join("")}?list=true`;
}
const request = new Request(appendAPIURL(secretURL), {
headers: getHeaders(),
headers: (getHeaders() as any),
});
return fetch(request).then(response => {
if (response.status == 404) {

View file

@ -1,13 +0,0 @@
import { appendAPIURL, getHeaders } from "./apiUtils.js";
export async function getTOTPCode(baseMount, name) {
const request = new Request(appendAPIURL(`/v1/${baseMount}/code/${name}`), {
headers: getHeaders(),
});
return fetch(request).then(response => {
return response.json();
}).then(data => {
return data.data.code;
});
}

13
src/api/getTOTPCode.ts Normal file
View file

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

View file

@ -1,8 +1,8 @@
import { DoesNotExistError } from "../types/internalErrors";
import { appendAPIURL, getHeaders } from "./apiUtils.js";
import { appendAPIURL, getHeaders } from "./apiUtils";
export async function getTOTPKeys(baseMount) {
export async function getTOTPKeys(baseMount: string): Promise<string[]> {
const request = new Request(appendAPIURL(`/v1/${baseMount}/keys?list=true`), {
headers: getHeaders(),
});

View file

@ -1,8 +1,7 @@
import { DoesNotExistError } from "../types/internalErrors";
import { appendAPIURL, getHeaders } from "./apiUtils.js";
import { appendAPIURL, getHeaders } from "./apiUtils";
export async function getTransitKeys(baseMount) {
export async function getTransitKeys(baseMount: string): Promise<string[]> {
const request = new Request(appendAPIURL(`/v1/${baseMount}/keys?list=true`), {
headers: getHeaders(),
});

View file

@ -1,7 +1,10 @@
import { appendAPIURL, getHeaders } from "./apiUtils.js";
import { appendAPIURL, getHeaders } from "./apiUtils";
type TokenInfo = {
expire_time: string;
}
export async function lookupSelf() {
export async function lookupSelf(): Promise<TokenInfo> {
const request = new Request(appendAPIURL("/v1/auth/token/lookup-self"), {
headers: getHeaders(),
});

View file

@ -1,7 +1,6 @@
import { appendAPIURL, getHeaders } from "./apiUtils.js";
import { appendAPIURL, getHeaders } from "./apiUtils";
export async function renewSelf() {
export async function renewSelf(): Promise<void> {
const request = new Request(appendAPIURL("/v1/auth/token/renew-self"), {
method: 'POST',
headers: {

View file

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

View file

@ -1,7 +1,6 @@
import { appendAPIURL } from "./apiUtils.js";
import { appendAPIURL } from "./apiUtils";
export async function submitUnsealKey(key) {
export async function submitUnsealKey(key: string): Promise<void> {
const request = new Request(appendAPIURL("/v1/sys/unseal"), {
method: "POST",
headers: {
@ -11,9 +10,9 @@ export async function submitUnsealKey(key) {
"key": key
})
});
let response = await fetch(request);
const response = await fetch(request);
if (!response.ok) {
let json = await response.json();
const json = await response.json();
throw new Error(json.errors[0]);
}
}

View file

@ -1,8 +1,15 @@
import { appendAPIURL, getHeaders } from "./apiUtils.js";
import { appendAPIURL, getHeaders } from "./apiUtils";
import { removeDoubleSlash } from "../utils";
type DecryptionResult = {
plaintext: string;
}
export async function transitDecrypt(baseMount, name, data) {
export async function transitDecrypt(
baseMount: string,
name: string,
data: string
): Promise<DecryptionResult> {
const request = new Request(appendAPIURL(removeDoubleSlash(`/v1/${baseMount}/decrypt/${name}`)), {
method: 'POST',
headers: {
@ -11,12 +18,12 @@ export async function transitDecrypt(baseMount, name, data) {
},
body: JSON.stringify({ ciphertext: data })
});
let response = await fetch(request);
const response = await fetch(request);
if (!response.ok) {
let json = await response.json();
const json = await response.json();
throw new Error(json.errors[0]);
} else {
let json = await response.json();
const json = await response.json();
return json.data;
}
}

View file

@ -1,8 +1,15 @@
import { appendAPIURL, getHeaders } from "./apiUtils.js";
import { appendAPIURL, getHeaders } from "./apiUtils";
import { removeDoubleSlash } from "../utils";
type EncryptionResult = {
ciphertext: string;
}
export async function transitEncrypt(baseMount, name, data) {
export async function transitEncrypt(
baseMount: string,
name: string,
data: string
): Promise<EncryptionResult> {
const request = new Request(appendAPIURL(removeDoubleSlash(`/v1/${baseMount}/encrypt/${name}`)), {
method: 'POST',
headers: {
@ -11,12 +18,12 @@ export async function transitEncrypt(baseMount, name, data) {
},
body: JSON.stringify({ plaintext: data })
});
let response = await fetch(request);
const response = await fetch(request);
if (!response.ok) {
let json = await response.json();
const json = await response.json();
throw new Error(json.errors[0]);
} else {
let json = await response.json();
const json = await response.json();
return json.data;
}
}

View file

@ -1,22 +1,26 @@
import { appendAPIURL, getHeaders } from "./apiUtils.js";
import { appendAPIURL, getHeaders } from "./apiUtils";
import { getObjectKeys, removeDoubleSlash } from "../utils";
import { getSecretMetadata } from "./getSecretMetadata";
export async function undeleteSecret(baseMount, secretPath, name, version = null) {
export async function undeleteSecret(
baseMount: string,
secretPath: string[],
name: string,
version: string|null = null
): Promise<void> {
let secretURL = `/v1/${baseMount}/undelete/${secretPath.join("/")}/${name}`;
secretURL = removeDoubleSlash(secretURL).replace(/\/$/, "");
if (version == null) {
let meta = await getSecretMetadata(
const meta = await getSecretMetadata(
baseMount,
secretPath,
name
);
let versions = getObjectKeys(meta.versions);
const versions = getObjectKeys(meta.versions);
version = String(versions[versions.length - 1]);
}
let request = new Request(appendAPIURL(secretURL), {
const request = new Request(appendAPIURL(secretURL), {
method: "POST",
headers: {
...getHeaders(),
@ -24,9 +28,9 @@ export async function undeleteSecret(baseMount, secretPath, name, version = null
},
body: JSON.stringify({ "versions": [version] })
});
let response = await fetch(request);
const response = await fetch(request);
if (!response.ok) {
let json = await response.json();
const json = await response.json();
throw new Error(json.errors[0]);
}
}

View file

@ -1,7 +1,6 @@
import { appendAPIURL } from "./apiUtils.js";
import { appendAPIURL } from "./apiUtils";
export async function usernameLogin(username, password) {
export async function usernameLogin(username: string, password: string): Promise<string> {
const request = new Request(appendAPIURL(`/v1/auth/userpass/login/${username}`), {
method: 'POST',
headers: {

View file

@ -1,8 +1,8 @@
import { CodeJar } from "codejar";
import { Page } from "../../types/Page";
import { changePage, setErrorText, setPageContent, setTitleElement } from "../../pageUtils";
import { createOrUpdateSecret } from "../../api/createOrUpdateSecret.js";
import { getSecret } from "../../api/getSecret.js";
import { createOrUpdateSecret } from "../../api/createOrUpdateSecret";
import { getSecret } from "../../api/getSecret";
import { makeElement } from "../../htmlUtils";
import { pageState } from "../../globalPageState.ts";
import { verifyJSONString } from "../../utils";

View file

@ -1,6 +1,6 @@
import { Page } from "../../types/Page";
import { changePage, setPageContent, setTitleElement } from "../../pageUtils";
import { getSecretMetadata } from "../../api/getSecretMetadata.js";
import { getSecretMetadata } from "../../api/getSecretMetadata";
import { makeElement } from "../../htmlUtils";
import { objectToMap } from "../../utils";
import { pageState } from "../../globalPageState.ts";

View file

@ -1,10 +1,10 @@
import { Page } from "../types/Page";
import { addClipboardNotifications, changePage, prePageChecks, setErrorText, setPageContent } from "../pageUtils";
import { getCapabilitiesPath } from "../api/getCapabilities.js";
import { getCapabilitiesPath } from "../api/getCapabilities";
import { makeElement } from "../htmlUtils";
import { pageState } from "../globalPageState.ts";
import { renewSelf } from "../api/renewSelf.js";
import { sealVault } from "../api/sealVault.js";
import { renewSelf } from "../api/renewSelf";
import { sealVault } from "../api/sealVault";
import ClipboardJS from "clipboard";
import i18next from 'i18next';

View file

@ -2,9 +2,9 @@ import { MarginInline } from "../elements/MarginInline.js";
import { Page } from "../types/Page";
import { QRScanner } from "../elements/QRScanner.js";
import { changePage, setErrorText, setPageContent } from "../pageUtils";
import { getSealStatus } from "../api/getSealStatus.js";
import { getSealStatus } from "../api/getSealStatus";
import { makeElement } from "../htmlUtils";
import { submitUnsealKey } from "../api/submitUnsealKey.js";
import { submitUnsealKey } from "../api/submitUnsealKey";
import i18next from 'i18next';