refactor: removed callApi

keep-around/af30b88d367751c9e05a735e4a0467a96238ef47
mirlan.maksitaliev 6 years ago
parent 71886fc333
commit 446c41a37a
  1. 7
      src/helpers/callApi/checkStatus.tsx
  2. 4
      src/helpers/callApi/clearUserAuthInfo.tsx
  3. 35
      src/helpers/callApi/getRequestConfig.tsx
  4. 3
      src/helpers/callApi/getResponseData.tsx
  5. 42
      src/helpers/callApi/index.tsx
  6. 1
      src/helpers/callApi/loadIdToken.tsx
  7. 1
      src/helpers/callApi/parseJSON.tsx
  8. 11
      src/helpers/callApi/removeCookie.tsx
  9. 14
      src/helpers/callApi/types.tsx
  10. 2
      src/helpers/index.tsx

@ -1,7 +0,0 @@
export const checkStatus = (response: Response) => {
if (!response.ok) {
return Promise.reject(new Error(response.statusText))
}
return Promise.resolve(response)
}

@ -1,4 +0,0 @@
export const clearUserAuthInfo = () => {
localStorage.removeItem('id_token')
localStorage.removeItem('AuthUser')
}

@ -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
}

@ -1,3 +0,0 @@
export const getResponseData = (proc: string) => (response: any) => (
response?.data?.[0]?.[proc]
)

@ -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'

@ -1 +0,0 @@
export const loadIdToken = () => localStorage.getItem('id_token')

@ -1 +0,0 @@
export const parseJSON = (response: Response) => response.json()

@ -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}
`
}

@ -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,
}

@ -1 +1 @@
export * from './callApi'
export {}

Loading…
Cancel
Save