From 446c41a37a5f9446a19d7e0832d59662283163ac Mon Sep 17 00:00:00 2001 From: "mirlan.maksitaliev" Date: Tue, 2 Jun 2020 19:55:14 +0600 Subject: [PATCH] refactor: removed callApi --- src/helpers/callApi/checkStatus.tsx | 7 ---- src/helpers/callApi/clearUserAuthInfo.tsx | 4 --- src/helpers/callApi/getRequestConfig.tsx | 35 ------------------- src/helpers/callApi/getResponseData.tsx | 3 -- src/helpers/callApi/index.tsx | 42 ----------------------- src/helpers/callApi/loadIdToken.tsx | 1 - src/helpers/callApi/parseJSON.tsx | 1 - src/helpers/callApi/removeCookie.tsx | 11 ------ src/helpers/callApi/types.tsx | 14 -------- src/helpers/index.tsx | 2 +- 10 files changed, 1 insertion(+), 119 deletions(-) delete mode 100644 src/helpers/callApi/checkStatus.tsx delete mode 100644 src/helpers/callApi/clearUserAuthInfo.tsx delete mode 100644 src/helpers/callApi/getRequestConfig.tsx delete mode 100644 src/helpers/callApi/getResponseData.tsx delete mode 100644 src/helpers/callApi/index.tsx delete mode 100644 src/helpers/callApi/loadIdToken.tsx delete mode 100644 src/helpers/callApi/parseJSON.tsx delete mode 100644 src/helpers/callApi/removeCookie.tsx delete mode 100644 src/helpers/callApi/types.tsx diff --git a/src/helpers/callApi/checkStatus.tsx b/src/helpers/callApi/checkStatus.tsx deleted file mode 100644 index 4b3b206a..00000000 --- a/src/helpers/callApi/checkStatus.tsx +++ /dev/null @@ -1,7 +0,0 @@ -export const checkStatus = (response: Response) => { - if (!response.ok) { - return Promise.reject(new Error(response.statusText)) - } - - return Promise.resolve(response) -} diff --git a/src/helpers/callApi/clearUserAuthInfo.tsx b/src/helpers/callApi/clearUserAuthInfo.tsx deleted file mode 100644 index c48925f4..00000000 --- a/src/helpers/callApi/clearUserAuthInfo.tsx +++ /dev/null @@ -1,4 +0,0 @@ -export const clearUserAuthInfo = () => { - localStorage.removeItem('id_token') - localStorage.removeItem('AuthUser') -} diff --git a/src/helpers/callApi/getRequestConfig.tsx b/src/helpers/callApi/getRequestConfig.tsx deleted file mode 100644 index 8bbf113e..00000000 --- a/src/helpers/callApi/getRequestConfig.tsx +++ /dev/null @@ -1,35 +0,0 @@ -import isString from 'lodash/isString' - -import { loadIdToken } from './loadIdToken' -import { TRequestAbortController, TRequestConfig } from './types' - -export const getRequestConfig = ( - config: TRequestConfig, - abortController?: TRequestAbortController, -) => { - const requestConfig = { - method: 'POST', - ...config, - headers: new Headers(), - } - - if (config.body && !isString(config.body)) { - requestConfig.body = JSON.stringify(config.body) - } - - if (config.body) { - requestConfig.headers.set('Content-Type', 'application/json') - } - - if (abortController) { - requestConfig.signal = abortController.signal - } - - const token = loadIdToken() - - if (token) { - requestConfig.headers.set('x-auth-token', token) - } - - return requestConfig -} diff --git a/src/helpers/callApi/getResponseData.tsx b/src/helpers/callApi/getResponseData.tsx deleted file mode 100644 index 149ace17..00000000 --- a/src/helpers/callApi/getResponseData.tsx +++ /dev/null @@ -1,3 +0,0 @@ -export const getResponseData = (proc: string) => (response: any) => ( - response?.data?.[0]?.[proc] -) diff --git a/src/helpers/callApi/index.tsx b/src/helpers/callApi/index.tsx deleted file mode 100644 index 00a38273..00000000 --- a/src/helpers/callApi/index.tsx +++ /dev/null @@ -1,42 +0,0 @@ -import { TCallApi } from './types' -import { parseJSON } from './parseJSON' -import { checkStatus } from './checkStatus' -import { removeCookie } from './removeCookie' -import { clearUserAuthInfo } from './clearUserAuthInfo' -import { getRequestConfig } from './getRequestConfig' - -export const callApi = ({ - abortController, - config, - url, -}: TCallApi) => { - const requestConfig = getRequestConfig(config, abortController) - - // eslint-disable-next-line no-console - console.log( - '%c callApi from module - config ', - 'color: white; background-color: #95B46A', - config, - ) - - return fetch(url, requestConfig) - .then(checkStatus) - .then(parseJSON) - .catch((error) => { - if (error.message === 'Unauthorized') { - clearUserAuthInfo() - removeCookie('token') - - if (window.location.pathname !== '/login') { - localStorage.setItem('backLocation', window.location.pathname) - window.location.pathname = '/login' - } - } - - // eslint-disable-next-line no-console - console.error(error) - return Promise.reject(error) - }) -} - -export { getResponseData } from './getResponseData' diff --git a/src/helpers/callApi/loadIdToken.tsx b/src/helpers/callApi/loadIdToken.tsx deleted file mode 100644 index c1f70feb..00000000 --- a/src/helpers/callApi/loadIdToken.tsx +++ /dev/null @@ -1 +0,0 @@ -export const loadIdToken = () => localStorage.getItem('id_token') diff --git a/src/helpers/callApi/parseJSON.tsx b/src/helpers/callApi/parseJSON.tsx deleted file mode 100644 index 12709130..00000000 --- a/src/helpers/callApi/parseJSON.tsx +++ /dev/null @@ -1 +0,0 @@ -export const parseJSON = (response: Response) => response.json() diff --git a/src/helpers/callApi/removeCookie.tsx b/src/helpers/callApi/removeCookie.tsx deleted file mode 100644 index 4118169c..00000000 --- a/src/helpers/callApi/removeCookie.tsx +++ /dev/null @@ -1,11 +0,0 @@ -export const removeCookie = ( - name: string, - domain: string = '.instatscout.com', -) => { - document.cookie = ` - ${name}=; - expires='Thu, 01 Jan 1970 00:00:00 UTC'; - path=/; - domain=${domain} - ` -} diff --git a/src/helpers/callApi/types.tsx b/src/helpers/callApi/types.tsx deleted file mode 100644 index cb2b105a..00000000 --- a/src/helpers/callApi/types.tsx +++ /dev/null @@ -1,14 +0,0 @@ -export type TRequestConfig = { - [key: string]: any, - body?: any, - headers?: Headers, - signal?: any, -} - -export type TRequestAbortController = AbortController | null - -export type TCallApi = { - abortController?: TRequestAbortController, - config: TRequestConfig, - url: string, -} diff --git a/src/helpers/index.tsx b/src/helpers/index.tsx index 8842e651..336ce12b 100644 --- a/src/helpers/index.tsx +++ b/src/helpers/index.tsx @@ -1 +1 @@ -export * from './callApi' +export {}