keep-around/af30b88d367751c9e05a735e4a0467a96238ef47
parent
9e7e96b2a2
commit
0d93512e54
@ -0,0 +1,7 @@ |
|||||||
|
export const checkStatus = (response: Response) => { |
||||||
|
if (!response.ok) { |
||||||
|
return Promise.reject(new Error(response.statusText)) |
||||||
|
} |
||||||
|
|
||||||
|
return Promise.resolve(response) |
||||||
|
} |
||||||
@ -0,0 +1,4 @@ |
|||||||
|
export const clearUserAuthInfo = () => { |
||||||
|
localStorage.removeItem('id_token') |
||||||
|
localStorage.removeItem('AuthUser') |
||||||
|
} |
||||||
@ -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 |
||||||
|
} |
||||||
@ -0,0 +1,3 @@ |
|||||||
|
export const getResponseData = (proc: string) => (response: any) => ( |
||||||
|
response?.data?.[0]?.[proc] |
||||||
|
) |
||||||
@ -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) |
||||||
|
}) |
||||||
|
} |
||||||
@ -0,0 +1 @@ |
|||||||
|
export const loadIdToken = () => localStorage.getItem('id_token') |
||||||
@ -0,0 +1 @@ |
|||||||
|
export const parseJSON = (response: Response) => response.json() |
||||||
@ -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} |
||||||
|
` |
||||||
|
} |
||||||
@ -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, |
||||||
|
} |
||||||
@ -1 +1 @@ |
|||||||
export {} |
export * from './callApi' |
||||||
|
|||||||
Loading…
Reference in new issue