From da83989baea88003457ac3bac8c9ff016bec8c80 Mon Sep 17 00:00:00 2001 From: elysee15 Date: Mon, 6 Sep 2021 16:41:59 +0000 Subject: [PATCH] refact: modify service file structure --- web/src/__tests__/FetchSecret.spec.tsx | 4 +-- web/src/components/CreateSecretModal.tsx | 2 +- web/src/components/ShowSecret.tsx | 2 +- web/src/pages/CreateSecret.tsx | 2 +- web/src/pages/FetchSecret.tsx | 2 +- web/src/services/ShowSecret.ts | 19 ------------ web/src/services/createSecret.ts | 7 ----- web/src/services/deleteSecret.ts | 18 ------------ web/src/services/index.ts | 2 ++ web/src/services/secret.ts | 37 ++++++++++++++++++++++++ 10 files changed, 45 insertions(+), 50 deletions(-) delete mode 100644 web/src/services/ShowSecret.ts delete mode 100644 web/src/services/createSecret.ts delete mode 100644 web/src/services/deleteSecret.ts create mode 100644 web/src/services/index.ts create mode 100644 web/src/services/secret.ts diff --git a/web/src/__tests__/FetchSecret.spec.tsx b/web/src/__tests__/FetchSecret.spec.tsx index 2f68d63..2d9db14 100644 --- a/web/src/__tests__/FetchSecret.spec.tsx +++ b/web/src/__tests__/FetchSecret.spec.tsx @@ -1,9 +1,9 @@ import FetchSecret from "pages/FetchSecret"; import { renderWithRouterMatch, waitFor } from "utils/test-utils"; import { SecretMock } from "__mocks__/SecretMock"; -import getSecret from "../services/ShowSecret"; +import { getSecret } from "../services"; -jest.mock("../services/ShowSecret"); +jest.mock("../services"); const mockedGetSecret = getSecret as jest.MockedFunction; diff --git a/web/src/components/CreateSecretModal.tsx b/web/src/components/CreateSecretModal.tsx index 0350a1f..25c9da0 100644 --- a/web/src/components/CreateSecretModal.tsx +++ b/web/src/components/CreateSecretModal.tsx @@ -5,7 +5,7 @@ import dayjs from "dayjs"; import relativeTime from "dayjs/plugin/relativeTime"; import React, { useState } from "react"; import CopyToClipboard from "react-copy-to-clipboard"; -import deleteSecret from "services/deleteSecret"; +import { deleteSecret } from "services"; import LinkIcon from "@material-ui/icons/Link"; import { ModalType } from "utils/enums/modal"; import Button from "./Button"; diff --git a/web/src/components/ShowSecret.tsx b/web/src/components/ShowSecret.tsx index 3b2e100..60a60d2 100644 --- a/web/src/components/ShowSecret.tsx +++ b/web/src/components/ShowSecret.tsx @@ -6,7 +6,7 @@ import dayjs from "dayjs"; import relativeTime from "dayjs/plugin/relativeTime"; import { dataURLtoFile, selectOnFocus } from "utils/utils"; import { Link, useHistory } from "react-router-dom"; -import deleteSecret from "services/deleteSecret"; +import { deleteSecret } from "services"; import clsx from "clsx"; import Button from "./Button"; import ShowFile from "./ShowFile"; diff --git a/web/src/pages/CreateSecret.tsx b/web/src/pages/CreateSecret.tsx index 16b6dab..6eb2c2c 100644 --- a/web/src/pages/CreateSecret.tsx +++ b/web/src/pages/CreateSecret.tsx @@ -1,6 +1,6 @@ import React from "react"; import { Grid, Snackbar } from "@material-ui/core"; -import createSecret from "services/createSecret"; +import { createSecret } from "services"; import { Secret } from "utils/interfaces/Secret"; import { FormikValues } from "formik"; import { AxiosError, AxiosResponse } from "axios"; diff --git a/web/src/pages/FetchSecret.tsx b/web/src/pages/FetchSecret.tsx index 67295b8..d0aa288 100644 --- a/web/src/pages/FetchSecret.tsx +++ b/web/src/pages/FetchSecret.tsx @@ -3,7 +3,7 @@ import SecretNotFound from "components/SecretNotFound"; import { FormikHelpers, FormikValues } from "formik"; import React from "react"; import { useParams } from "react-router-dom"; -import getSecret from "services/ShowSecret"; +import { getSecret } from "services"; import { Secret } from "utils/interfaces/Secret"; import SecretPassword from "../components/SecretPassword"; import dayjs from "dayjs"; diff --git a/web/src/services/ShowSecret.ts b/web/src/services/ShowSecret.ts deleted file mode 100644 index 755fb8c..0000000 --- a/web/src/services/ShowSecret.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { AxiosPromise, AxiosRequestConfig, AxiosResponse } from "axios"; -import client from "config"; -import { stringToBase64 } from "utils/utils"; - -export default function getSecret(token: string, password?: string, config: AxiosRequestConfig = {}): AxiosPromise { - const encodedPassword = password ? stringToBase64(password) : ""; - if (password) { - return client - .get(`/secrets/${token}`, { - headers: { Authorization: `Bearer ${encodedPassword}`, "Access-Control-Request-Headers": "Authorization" } - }) - .then((response: AxiosResponse) => { - window.sessionStorage.setItem("__KEY__", encodedPassword); - return response; - }); - } - - return client.get(`/secrets/${token}`, config); -} diff --git a/web/src/services/createSecret.ts b/web/src/services/createSecret.ts deleted file mode 100644 index 7dc49e2..0000000 --- a/web/src/services/createSecret.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { AxiosPromise, AxiosRequestConfig } from "axios"; -import client from "config"; -import { Secret } from "utils/interfaces/Secret"; - -export default function createSecret(data: Secret, config: AxiosRequestConfig = {}): AxiosPromise { - return client.post("/secrets", data, config); -} diff --git a/web/src/services/deleteSecret.ts b/web/src/services/deleteSecret.ts deleted file mode 100644 index e92e8c5..0000000 --- a/web/src/services/deleteSecret.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { AxiosPromise, AxiosRequestConfig } from "axios"; -import client from "config"; - -export default function deleteSecret( - token: string, - password?: string | null, - config: AxiosRequestConfig = {} -): AxiosPromise { - if (password) { - return client.delete(`/secrets/${token}`, { - headers: { - Authorization: `Bearer ${password}`, - "Access-Control-Request-Headers": "Authorization" - } - }); - } - return client.delete(`/secrets/${token}`, config); -} diff --git a/web/src/services/index.ts b/web/src/services/index.ts new file mode 100644 index 0000000..3abff81 --- /dev/null +++ b/web/src/services/index.ts @@ -0,0 +1,2 @@ +export * from "./secret"; +export * from "./status"; diff --git a/web/src/services/secret.ts b/web/src/services/secret.ts new file mode 100644 index 0000000..8aa189e --- /dev/null +++ b/web/src/services/secret.ts @@ -0,0 +1,37 @@ +import { AxiosPromise, AxiosRequestConfig, AxiosResponse } from "axios"; +import client from "config"; +import { Secret } from "utils/interfaces/Secret"; +import { stringToBase64 } from "utils/utils"; + +function createSecret(data: Secret, config: AxiosRequestConfig = {}): AxiosPromise { + return client.post("/secrets", data, config); +} + +function deleteSecret(token: string, password?: string | null, config: AxiosRequestConfig = {}): AxiosPromise { + if (password) { + return client.delete(`/secrets/${token}`, { + headers: { + Authorization: `Bearer ${password}`, + "Access-Control-Request-Headers": "Authorization" + } + }); + } + return client.delete(`/secrets/${token}`, config); +} +function getSecret(token: string, password?: string, config: AxiosRequestConfig = {}): AxiosPromise { + const encodedPassword = password ? stringToBase64(password) : ""; + if (password) { + return client + .get(`/secrets/${token}`, { + headers: { Authorization: `Bearer ${encodedPassword}`, "Access-Control-Request-Headers": "Authorization" } + }) + .then((response: AxiosResponse) => { + window.sessionStorage.setItem("__KEY__", encodedPassword); + return response; + }); + } + + return client.get(`/secrets/${token}`, config); +} + +export { getSecret, deleteSecret, createSecret };