mirror of
https://github.com/docmost/docmost.git
synced 2025-11-20 12:01:08 +10:00
switch to nx monorepo
This commit is contained in:
53
apps/client/src/lib/api-client.ts
Normal file
53
apps/client/src/lib/api-client.ts
Normal file
@ -0,0 +1,53 @@
|
||||
import axios, { AxiosInstance } from "axios";
|
||||
import Cookies from "js-cookie";
|
||||
import Routes from "@/lib/routes";
|
||||
|
||||
const api: AxiosInstance = axios.create({
|
||||
baseURL: import.meta.env.VITE_BACKEND_API_URL + '/api'
|
||||
});
|
||||
|
||||
api.interceptors.request.use(config => {
|
||||
const tokenData = Cookies.get("authTokens");
|
||||
const accessToken = tokenData && JSON.parse(tokenData)?.accessToken;
|
||||
|
||||
if (accessToken) {
|
||||
config.headers.Authorization = `Bearer ${accessToken}`;
|
||||
}
|
||||
return config;
|
||||
},
|
||||
error => {
|
||||
return Promise.reject(error);
|
||||
},
|
||||
);
|
||||
|
||||
api.interceptors.response.use(
|
||||
response => {
|
||||
return response.data;
|
||||
},
|
||||
error => {
|
||||
if (error.response) {
|
||||
switch (error.response.status) {
|
||||
case 401:
|
||||
// Handle unauthorized error
|
||||
if (window.location.pathname != Routes.AUTH.LOGIN){
|
||||
window.location.href = Routes.AUTH.LOGIN;
|
||||
}
|
||||
break;
|
||||
case 403:
|
||||
// Handle forbidden error
|
||||
break;
|
||||
case 404:
|
||||
// Handle not found error
|
||||
break;
|
||||
case 500:
|
||||
// Handle internal server error
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return Promise.reject(error);
|
||||
},
|
||||
);
|
||||
|
||||
export default api;
|
||||
17
apps/client/src/lib/jotai-helper.ts
Normal file
17
apps/client/src/lib/jotai-helper.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import { atom } from "jotai";
|
||||
|
||||
export function atomWithWebStorage<Value>(key: string, initialValue: Value, storage = localStorage) {
|
||||
const storedValue = localStorage.getItem(key);
|
||||
const isString = typeof initialValue === "string";
|
||||
|
||||
const storageValue = storedValue ? isString ? storedValue : storedValue === "true" : undefined;
|
||||
|
||||
const baseAtom = atom(storageValue ?? initialValue);
|
||||
return atom(
|
||||
get => get(baseAtom) as Value,
|
||||
(_get, set, nextValue: Value) => {
|
||||
set(baseAtom, nextValue);
|
||||
storage.setItem(key, nextValue!.toString());
|
||||
},
|
||||
);
|
||||
}
|
||||
9
apps/client/src/lib/routes.ts
Normal file
9
apps/client/src/lib/routes.ts
Normal file
@ -0,0 +1,9 @@
|
||||
const ROUTES = {
|
||||
HOME: '/home',
|
||||
AUTH: {
|
||||
LOGIN: '/login',
|
||||
SIGNUP: '/signup',
|
||||
},
|
||||
};
|
||||
|
||||
export default ROUTES;
|
||||
16
apps/client/src/lib/time.ts
Normal file
16
apps/client/src/lib/time.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import { formatDistanceStrict } from 'date-fns';
|
||||
import { format, isToday, isYesterday } from 'date-fns';
|
||||
|
||||
export function timeAgo(date: Date) {
|
||||
return formatDistanceStrict(new Date(date), new Date(), { addSuffix: true });
|
||||
}
|
||||
|
||||
export function formatDate(date: Date) {
|
||||
if (isToday(date)) {
|
||||
return `Today, ${format(date, 'h:mma')}`;
|
||||
} else if (isYesterday(date)) {
|
||||
return `Yesterday, ${format(date, 'h:mma')}`;
|
||||
} else {
|
||||
return format(date, 'MMM dd, yyyy, h:mma');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user