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