Skip to content

Commit

Permalink
refact: modify service file structure
Browse files Browse the repository at this point in the history
  • Loading branch information
elysee15 committed Sep 6, 2021
1 parent e0bf316 commit da83989
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 50 deletions.
4 changes: 2 additions & 2 deletions web/src/__tests__/FetchSecret.spec.tsx
Original file line number Diff line number Diff line change
@@ -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<typeof getSecret>;

Expand Down
2 changes: 1 addition & 1 deletion web/src/components/CreateSecretModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion web/src/components/ShowSecret.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion web/src/pages/CreateSecret.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
2 changes: 1 addition & 1 deletion web/src/pages/FetchSecret.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
19 changes: 0 additions & 19 deletions web/src/services/ShowSecret.ts

This file was deleted.

7 changes: 0 additions & 7 deletions web/src/services/createSecret.ts

This file was deleted.

18 changes: 0 additions & 18 deletions web/src/services/deleteSecret.ts

This file was deleted.

2 changes: 2 additions & 0 deletions web/src/services/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./secret";
export * from "./status";
37 changes: 37 additions & 0 deletions web/src/services/secret.ts
Original file line number Diff line number Diff line change
@@ -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 };

0 comments on commit da83989

Please sign in to comment.