@@ -1,6 +1,4 @@ -import axios from 'axios' - -import { getApiUrl } from '#/shared/lib/constants' +import { api } from '#/shared/api' import { IUserSetting } from '../model/types' @@ -9,12 +7,9 @@ export const addUserSettings = async ( setting: Omit ): Promise => { try { - const { data } = await axios.post(getApiUrl() + '/api/users/settings/', setting, { - headers: { - Authorization: `Bearer ${token}`, - }, + const { data } = await api.post('/api/users/settings/', setting, { + headers: { Authorization: `Bearer ${token}` }, }) - return data } catch (err) { return null @@ -1,17 +1,12 @@ -import axios from 'axios' - -import { getApiUrl } from '#/shared/lib/constants' +import { api } from '#/shared/api' import { IUserSetting } from '../model/types' export const getUserSettings = async (token: string): Promise => { try { - const { data } = await axios.get(getApiUrl() + '/api/users/settings/', { - headers: { - Authorization: `Bearer ${token}`, - }, + const { data } = await api.get('/api/users/settings/', { + headers: { Authorization: `Bearer ${token}` }, }) - return data } catch (err) { return [] @@ -1,6 +1,4 @@ -import axios from 'axios' - -import { getApiUrl } from '#/shared/lib/constants' +import { api } from '#/shared/api' import { getUpdatedSettingsLocal } from '../lib/helpers/update-setting-local' import { IUserSetting, SettingValueType } from '../model/types' @@ -12,15 +10,9 @@ export const updateUserSettings = async ( settings: IUserSetting[] ) => { try { - const { data } = await axios.put( - getApiUrl() + `/api/users/settings/${id}`, - { value: value }, - { - headers: { - Authorization: `Bearer ${token}`, - }, - } - ) + await api.put(`/api/users/settings/${id}`, { value }, { + headers: { Authorization: `Bearer ${token}` }, + }) return getUpdatedSettingsLocal({ settings, id, value }) } catch (err) { return null @@ -2,8 +2,6 @@ import axios from 'axios' import * as https from 'https' import { getSession, signOut } from 'next-auth/react' -const BLACKLISTED_TOKEN_MESSAGE = 'Токен занесен в черный список' - export function setupAxios() { axios.defaults.httpsAgent = new https.Agent({ rejectUnauthorized: false, @@ -35,7 +33,7 @@ export function setupAxios() { (response) => response, (error) => { if (!error.response && typeof window !== 'undefined') { - window.location.href = '/network-error' + return error } if (error?.response?.status === 401 || error?.status === 401) { @@ -4,20 +4,18 @@ import { getApiUrl } from '#/shared/lib/constants' export const api = axios.create({ validateStatus: () => true, + baseURL: process.env.NEXT_PUBLIC_API_HOST ?? '', }) api.interceptors.request.use((config) => { const baseURL = getApiUrl() - if (baseURL) config.baseURL = baseURL + config.baseURL = baseURL || process.env.NEXT_PUBLIC_API_HOST || '' return config }) api.interceptors.response.use( (response) => response, (error) => { - if (!error.response && typeof window !== 'undefined') { - window.location.href = '/network-error' - } return error } ) @@ -1,10 +1,10 @@ import { getEnv } from '#/shared/lib/env-store' export const getApiHost = (): string => { - return getEnv()?.NEXT_PUBLIC_MAIN_URL ?? process.env.NEXT_PUBLIC_MAIN_URL ?? '' + return (getEnv()?.NEXT_PUBLIC_MAIN_URL || process.env.NEXT_PUBLIC_MAIN_URL) ?? '' } export const getApiUrl = (): string => { - return getEnv()?.NEXT_PUBLIC_API_HOST ?? process.env.NEXT_PUBLIC_API_HOST ?? '' + return (getEnv()?.NEXT_PUBLIC_API_HOST || process.env.NEXT_PUBLIC_API_HOST) ?? '' } export enum ModelPagesList {