refactor(ott-36): callApi changes

keep-around/af30b88d367751c9e05a735e4a0467a96238ef47
mirlan.maksitaliev 6 years ago
parent 0d93512e54
commit 26725457f5
  1. 7
      src/config/authKeys.tsx
  2. 2
      src/config/index.tsx
  3. 3
      src/config/pages.tsx
  4. 6
      src/helpers/callApi/clearUserAuthInfo.tsx
  5. 22
      src/helpers/callApi/getRequestConfig.tsx
  6. 42
      src/helpers/callApi/index.tsx
  7. 22
      src/helpers/callApi/logoutIfUnauthorized.tsx
  8. 7
      src/helpers/callApi/types.tsx

@ -0,0 +1,7 @@
export const AUTH_KEYS = {
authUser: 'AuthUser',
backLocation: 'backLocation',
cookieToken: 'token',
headerToken: 'x-auth-token',
idToken: 'id_token',
}

@ -1 +1,3 @@
export * from './routes'
export * from './pages'
export * from './authKeys'

@ -0,0 +1,3 @@
export const PAGES = {
login: '/login',
}

@ -1,4 +1,6 @@
import { AUTH_KEYS } from 'config'
export const clearUserAuthInfo = () => {
localStorage.removeItem('id_token')
localStorage.removeItem('AuthUser')
localStorage.removeItem(AUTH_KEYS.idToken)
localStorage.removeItem(AUTH_KEYS.authUser)
}

@ -1,34 +1,28 @@
import isString from 'lodash/isString'
import { AUTH_KEYS } from 'config'
import { loadIdToken } from './loadIdToken'
import { TRequestAbortController, TRequestConfig } from './types'
import { TRequestConfig } from './types'
export const getRequestConfig = (
config: TRequestConfig,
abortController?: TRequestAbortController,
signal?: AbortSignal,
) => {
const requestConfig = {
method: 'POST',
...config,
headers: new Headers(),
}
if (config.body && !isString(config.body)) {
requestConfig.body = JSON.stringify(config.body)
headers: config.headers || new Headers(),
method: config.method || 'POST',
signal,
}
if (config.body) {
requestConfig.body = JSON.stringify(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)
requestConfig.headers.set(AUTH_KEYS.headerToken, token)
}
return requestConfig

@ -1,18 +1,15 @@
import { TCallApi } from './types'
import { parseJSON } from './parseJSON'
import { checkStatus } from './checkStatus'
import { removeCookie } from './removeCookie'
import { clearUserAuthInfo } from './clearUserAuthInfo'
import { getRequestConfig } from './getRequestConfig'
import { logoutIfUnauthorized } from './logoutIfUnauthorized'
export * from './getResponseData'
export const callApi = ({
abortController,
export const callApiBase = ({
abortSignal,
config,
url,
}: TCallApi) => {
const requestConfig = getRequestConfig(config, abortController)
const requestConfig = getRequestConfig(config, abortSignal)
// eslint-disable-next-line no-console
console.log(
@ -22,21 +19,18 @@ export const callApi = ({
)
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 const callApi = ({
abortSignal,
config,
url,
}: TCallApi) => (
callApiBase({
abortSignal,
config,
url,
}).then(checkStatus)
.then(parseJSON)
.catch(logoutIfUnauthorized)
)

@ -0,0 +1,22 @@
import { AUTH_KEYS, PAGES } from 'config'
import { removeCookie } from './removeCookie'
import { clearUserAuthInfo } from './clearUserAuthInfo'
export const logoutIfUnauthorized = (error: Error) => {
if (error.message === 'Unauthorized') {
clearUserAuthInfo()
removeCookie(AUTH_KEYS.cookieToken)
const { pathname } = window.location
if (pathname !== PAGES.login) {
localStorage.setItem(AUTH_KEYS.backLocation, pathname)
window.location.pathname = PAGES.login
}
}
// eslint-disable-next-line no-console
console.error(error)
return Promise.reject(error)
}

@ -1,14 +1,11 @@
export type TRequestConfig = {
[key: string]: any,
body?: any,
headers?: Headers,
signal?: any,
method?: string,
}
export type TRequestAbortController = AbortController | null
export type TCallApi = {
abortController?: TRequestAbortController,
abortSignal?: AbortSignal,
config: TRequestConfig,
url: string,
}

Loading…
Cancel
Save