Skip to content

Commit 165e7f7

Browse files
committed
feat: use tanstack query
1 parent dd0379c commit 165e7f7

File tree

29 files changed

+571
-399
lines changed

29 files changed

+571
-399
lines changed

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@
1414
"@dnd-kit/core": "^6.3.1",
1515
"@dnd-kit/sortable": "^10.0.0",
1616
"@dnd-kit/utilities": "^3.2.2",
17-
"@laser-pro/acl": "^1.1.0",
18-
"@laser-pro/auth": "^1.1.0",
19-
"@laser-pro/http": "^1.1.0",
20-
"@laser-pro/router": "^1.1.0",
21-
"@laser-pro/storage": "^1.1.0",
17+
"@laser-pro/acl": "^2.0.0",
18+
"@laser-pro/auth": "^2.0.0",
19+
"@laser-pro/router": "^2.0.0",
20+
"@laser-pro/storage": "^2.0.0",
2221
"@laser-ui/components": "^2.0.4",
2322
"@laser-ui/hooks": "^2.0.4",
2423
"@laser-ui/themes": "^2.0.4",
2524
"@laser-ui/utils": "^2.0.4",
2625
"@material-design-icons/svg": "^0.14.13",
26+
"@tanstack/react-query": "^5.90.2",
2727
"axios": "^1.7.9",
2828
"dayjs": "^1.11.13",
2929
"echarts": "^5.6.0",

pnpm-lock.yaml

Lines changed: 38 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/@types/global.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ interface StandardFields {
88

99
declare global {
1010
declare namespace AppDocs {
11+
export interface UploadFile extends StandardFields {
12+
name: string;
13+
path: string;
14+
}
15+
16+
export interface User extends StandardFields {
17+
name: string;
18+
avatar?: AppDocs.UploadFile | null;
19+
permissions: (string | number)[];
20+
}
21+
1122
export interface DeviceModel extends StandardFields {
1223
name: string;
1324
disabled: boolean;

src/app/App.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ import type { LContextIn } from '@laser-ui/components/context';
33
import { useStorage } from '@laser-pro/storage';
44
import { ConfigProvider, Root } from '@laser-ui/components';
55
import { useIsomorphicLayoutEffect } from '@laser-ui/hooks';
6+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
67
import { useMemo } from 'react';
78

89
import { STORAGE } from './configs/storage';
910
import AppRouter from './routes/Router';
1011

12+
const queryClient = new QueryClient();
13+
1114
function App() {
1215
const languageStorage = useStorage(...STORAGE.language);
1316
const themeStorage = useStorage(...STORAGE.theme);
@@ -38,11 +41,13 @@ function App() {
3841
const rootContext = useMemo(() => ({ i18n: { lang: languageStorage.value } }), [languageStorage.value]);
3942

4043
return (
41-
<ConfigProvider context={lContext}>
42-
<Root context={rootContext}>
43-
<AppRouter />
44-
</Root>
45-
</ConfigProvider>
44+
<QueryClientProvider client={queryClient}>
45+
<ConfigProvider context={lContext}>
46+
<Root context={rootContext}>
47+
<AppRouter />
48+
</Root>
49+
</ConfigProvider>
50+
</QueryClientProvider>
4651
);
4752
}
4853

src/app/configs/http.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { HttpConfigs } from '@laser-pro/http/configs';
1+
import type { HttpConfigs } from '../core/axios/configs';
22

33
import mock from '../mock';
44

src/app/core/axios/axios.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import type { HttpRequestConfigOverrides } from './types';
2+
import type { AxiosRequestConfig, AxiosResponse } from 'axios';
3+
4+
import _axios from 'axios';
5+
6+
import { config, CONFIGS } from './configs';
7+
8+
export function axios<T = any, D = any>(
9+
config: AxiosRequestConfig,
10+
overrides?: true | HttpRequestConfigOverrides,
11+
): Promise<AxiosResponse<T, D>> {
12+
const axios = CONFIGS.mock ? CONFIGS.mock : _axios;
13+
14+
if (overrides === true) {
15+
return axios(config);
16+
}
17+
18+
const token = CONFIGS.token;
19+
20+
let headers = config.headers;
21+
if (!overrides?.authorization) {
22+
if (token) {
23+
headers = Object.assign({}, config.headers);
24+
headers.Authorization = `Bearer ${token.value}`;
25+
}
26+
}
27+
28+
return axios({
29+
...config,
30+
baseURL: overrides?.baseURL ? config.baseURL : CONFIGS.baseURL,
31+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
32+
url: overrides?.url ? config.url : CONFIGS.transformURL(config.url!),
33+
headers,
34+
});
35+
}
36+
37+
axios.config = config;
38+
axios.configs = CONFIGS;

src/app/core/axios/configs.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { Token } from '@laser-pro/auth';
2+
import type { AxiosRequestConfig, AxiosResponse } from 'axios';
3+
4+
export interface HttpConfigs {
5+
mock?: (config: AxiosRequestConfig) => Promise<AxiosResponse>;
6+
baseURL?: string;
7+
transformURL: (url: string) => string;
8+
token?: Token;
9+
}
10+
11+
export const CONFIGS: HttpConfigs = {
12+
transformURL: (url: string) => {
13+
return url;
14+
},
15+
};
16+
17+
export function config(configs: Partial<HttpConfigs>) {
18+
Object.keys(configs).forEach((key) => {
19+
(CONFIGS as any)[key] = (configs as any)[key];
20+
});
21+
}

src/app/core/axios/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { axios } from './axios';
2+
export { useAxios } from './useAxios';

src/app/core/axios/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export {};
2+
3+
export interface HttpRequestConfigOverrides {
4+
baseURL?: boolean;
5+
url?: boolean;
6+
authorization?: boolean;
7+
}

src/app/core/axios/useAxios.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import type { HttpRequestConfigOverrides } from './types';
2+
import type { AxiosRequestConfig } from 'axios';
3+
4+
import { DialogService, Toast } from '@laser-ui/components';
5+
import { useTranslation } from 'react-i18next';
6+
import { useLocation, useNavigate } from 'react-router';
7+
8+
import { axios } from './axios';
9+
import { LOGIN_PATH, PREV_ROUTE_KEY } from '../../configs/router';
10+
11+
export function useAxios() {
12+
const location = useLocation();
13+
const navigate = useNavigate();
14+
const { t } = useTranslation();
15+
16+
return <T = any>(config: AxiosRequestConfig, overrides?: true | HttpRequestConfigOverrides): Promise<T> => {
17+
return axios(config, overrides)
18+
.then((res) => res.data)
19+
.catch((error) => {
20+
if (error.response) {
21+
switch (error.response.status) {
22+
case 401:
23+
DialogService.open(Toast, {
24+
children: t('User not authorized'),
25+
type: 'error',
26+
});
27+
navigate(LOGIN_PATH, { state: { [PREV_ROUTE_KEY]: location } });
28+
break;
29+
30+
case 403:
31+
case 404:
32+
case 500:
33+
if (location.pathname !== LOGIN_PATH) {
34+
navigate(`/exception/${error.response.status}`);
35+
}
36+
break;
37+
38+
default:
39+
break;
40+
}
41+
} else if (error.request) {
42+
// The request was made but no response was received.
43+
} else {
44+
// Something happened in setting up the request that triggered an Error.
45+
}
46+
});
47+
};
48+
}

0 commit comments

Comments
 (0)