🎉 chore(init): project initialization
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import { locales, NEXT_PUBLIC_DEFAULT_LANGUAGE } from '@/config/constants';
|
||||
import { isBrowser } from '@repo/ui/utils';
|
||||
import Cookies from 'universal-cookie';
|
||||
|
||||
const cookies = new Cookies(null, {
|
||||
path: '/',
|
||||
maxAge: 365 * 24 * 60 * 60,
|
||||
});
|
||||
|
||||
export function getLocale() {
|
||||
const browserLocale = navigator.language?.split(',')?.[0] || '';
|
||||
const defaultLocale = locales.includes(browserLocale) ? browserLocale : '';
|
||||
const cookies = new Cookies(null, { path: '/' });
|
||||
const cookieLocale = cookies.get('locale') || '';
|
||||
const locale = cookieLocale || defaultLocale || NEXT_PUBLIC_DEFAULT_LANGUAGE || locales[0];
|
||||
return locale;
|
||||
}
|
||||
|
||||
export function setLocale(value: string) {
|
||||
return cookies.set('locale', value);
|
||||
}
|
||||
|
||||
export function setAuthorization(value: string) {
|
||||
return cookies.set('Authorization', value);
|
||||
}
|
||||
|
||||
export function getAuthorization(value?: string) {
|
||||
const Authorization = isBrowser() ? cookies.get('Authorization') : value;
|
||||
if (!Authorization) return;
|
||||
return Authorization;
|
||||
}
|
||||
|
||||
export function setRedirectUrl(value?: string) {
|
||||
if (value) {
|
||||
sessionStorage.setItem('redirect-url', value);
|
||||
}
|
||||
}
|
||||
|
||||
export function getRedirectUrl() {
|
||||
return sessionStorage.getItem('redirect-url') ?? '/dashboard';
|
||||
}
|
||||
|
||||
export function Logout() {
|
||||
const cookies = new Cookies(null, { path: '/' });
|
||||
cookies.remove('Authorization');
|
||||
const pathname = location.pathname;
|
||||
if (!['', '/'].includes(pathname)) {
|
||||
setRedirectUrl(location.pathname);
|
||||
location.href = `/`;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import { NEXT_PUBLIC_API_URL, NEXT_PUBLIC_SITE_URL } from '@/config/constants';
|
||||
import { getTranslations } from '@/locales/utils';
|
||||
import { isBrowser } from '@repo/ui/utils';
|
||||
import { toast } from '@shadcn/ui/lib/sonner';
|
||||
import axios, { InternalAxiosRequestConfig } from 'axios';
|
||||
import { getAuthorization, Logout } from './common';
|
||||
|
||||
const request = axios.create({
|
||||
baseURL: NEXT_PUBLIC_API_URL || NEXT_PUBLIC_SITE_URL,
|
||||
// withCredentials: true,
|
||||
// withXSRFToken: true,
|
||||
timeout: 10000,
|
||||
});
|
||||
|
||||
async function handleError(response: any) {
|
||||
const code = response.data?.code;
|
||||
if (response?.config?.skipErrorHandler) return;
|
||||
if (!isBrowser()) return;
|
||||
if ([40002, 40003, 40004].includes(code)) return Logout();
|
||||
|
||||
const t = await getTranslations('common');
|
||||
const message =
|
||||
t(`request.${code}`) !== `request.${code}`
|
||||
? t(`request.${code}`)
|
||||
: response.data?.message || response.message;
|
||||
|
||||
toast.error(message);
|
||||
}
|
||||
|
||||
request.interceptors.request.use(
|
||||
async (
|
||||
config: InternalAxiosRequestConfig & {
|
||||
Authorization?: string;
|
||||
},
|
||||
) => {
|
||||
const Authorization = getAuthorization(config.Authorization);
|
||||
if (Authorization) config.headers.Authorization = Authorization;
|
||||
return config;
|
||||
},
|
||||
(error) => Promise.reject(error),
|
||||
);
|
||||
|
||||
request.interceptors.response.use(
|
||||
async (response) => {
|
||||
const { code } = response.data;
|
||||
if (code !== 200) throw response;
|
||||
return response;
|
||||
},
|
||||
async (error) => {
|
||||
await handleError(error);
|
||||
return Promise.reject(error);
|
||||
},
|
||||
);
|
||||
|
||||
export default request;
|
||||
Reference in New Issue
Block a user