From 0d93512e5462daf0120694646577fcfce03fafc8 Mon Sep 17 00:00:00 2001 From: "mirlan.maksitaliev" Date: Fri, 5 Jun 2020 13:01:07 +0600 Subject: [PATCH] refactor(ott-67): copied callApi helper from hockey --- 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, 119 insertions(+), 1 deletion(-) create mode 100644 src/helpers/callApi/checkStatus.tsx create mode 100644 src/helpers/callApi/clearUserAuthInfo.tsx create mode 100644 src/helpers/callApi/getRequestConfig.tsx create mode 100644 src/helpers/callApi/getResponseData.tsx create mode 100644 src/helpers/callApi/index.tsx create mode 100644 src/helpers/callApi/loadIdToken.tsx create mode 100644 src/helpers/callApi/parseJSON.tsx create mode 100644 src/helpers/callApi/removeCookie.tsx create mode 100644 src/helpers/callApi/types.tsx diff --git a/src/helpers/callApi/checkStatus.tsx b/src/helpers/callApi/checkStatus.tsx new file mode 100644 index 00000000..4b3b206a --- /dev/null +++ b/src/helpers/callApi/checkStatus.tsx @@ -0,0 +1,7 @@ +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 new file mode 100644 index 00000000..c48925f4 --- /dev/null +++ b/src/helpers/callApi/clearUserAuthInfo.tsx @@ -0,0 +1,4 @@ +export const clearUserAuthInfo = () => { + localStorage.removeItem('id_token') + localStorage.removeItem('AuthUser') +} diff --git a/src/helpers/callApi/getRequestConfig.tsx b/src/helpers/callApi/getRequestConfig.tsx new file mode 100644 index 00000000..8bbf113e --- /dev/null +++ b/src/helpers/callApi/getRequestConfig.tsx @@ -0,0 +1,35 @@ +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 new file mode 100644 index 00000000..149ace17 --- /dev/null +++ b/src/helpers/callApi/getResponseData.tsx @@ -0,0 +1,3 @@ +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 new file mode 100644 index 00000000..fa8ef94f --- /dev/null +++ b/src/helpers/callApi/index.tsx @@ -0,0 +1,42 @@ +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 * from './getResponseData' + +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) + }) +} diff --git a/src/helpers/callApi/loadIdToken.tsx b/src/helpers/callApi/loadIdToken.tsx new file mode 100644 index 00000000..c1f70feb --- /dev/null +++ b/src/helpers/callApi/loadIdToken.tsx @@ -0,0 +1 @@ +export const loadIdToken = () => localStorage.getItem('id_token') diff --git a/src/helpers/callApi/parseJSON.tsx b/src/helpers/callApi/parseJSON.tsx new file mode 100644 index 00000000..12709130 --- /dev/null +++ b/src/helpers/callApi/parseJSON.tsx @@ -0,0 +1 @@ +export const parseJSON = (response: Response) => response.json() diff --git a/src/helpers/callApi/removeCookie.tsx b/src/helpers/callApi/removeCookie.tsx new file mode 100644 index 00000000..4118169c --- /dev/null +++ b/src/helpers/callApi/removeCookie.tsx @@ -0,0 +1,11 @@ +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 new file mode 100644 index 00000000..cb2b105a --- /dev/null +++ b/src/helpers/callApi/types.tsx @@ -0,0 +1,14 @@ +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 336ce12b..8842e651 100644 --- a/src/helpers/index.tsx +++ b/src/helpers/index.tsx @@ -1 +1 @@ -export {} +export * from './callApi'