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. 40
      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 './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 = () => { export const clearUserAuthInfo = () => {
localStorage.removeItem('id_token') localStorage.removeItem(AUTH_KEYS.idToken)
localStorage.removeItem('AuthUser') localStorage.removeItem(AUTH_KEYS.authUser)
} }

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

@ -1,18 +1,15 @@
import { TCallApi } from './types' import { TCallApi } from './types'
import { parseJSON } from './parseJSON' import { parseJSON } from './parseJSON'
import { checkStatus } from './checkStatus' import { checkStatus } from './checkStatus'
import { removeCookie } from './removeCookie'
import { clearUserAuthInfo } from './clearUserAuthInfo'
import { getRequestConfig } from './getRequestConfig' import { getRequestConfig } from './getRequestConfig'
import { logoutIfUnauthorized } from './logoutIfUnauthorized'
export * from './getResponseData' export const callApiBase = ({
abortSignal,
export const callApi = ({
abortController,
config, config,
url, url,
}: TCallApi) => { }: TCallApi) => {
const requestConfig = getRequestConfig(config, abortController) const requestConfig = getRequestConfig(config, abortSignal)
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.log( console.log(
@ -22,21 +19,18 @@ export const callApi = ({
) )
return fetch(url, requestConfig) 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 export const callApi = ({
console.error(error) abortSignal,
return Promise.reject(error) 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 = { export type TRequestConfig = {
[key: string]: any,
body?: any, body?: any,
headers?: Headers, headers?: Headers,
signal?: any, method?: string,
} }
export type TRequestAbortController = AbortController | null
export type TCallApi = { export type TCallApi = {
abortController?: TRequestAbortController, abortSignal?: AbortSignal,
config: TRequestConfig, config: TRequestConfig,
url: string, url: string,
} }

Loading…
Cancel
Save