Skip to content

Commit 53645e8

Browse files
committed
feat: use tanstack query
1 parent dd0379c commit 53645e8

File tree

29 files changed

+585
-397
lines changed

29 files changed

+585
-397
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/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: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import type { HttpRequestConfigOverrides } from './types';
2+
import type { AxiosRequestConfig } from 'axios';
3+
4+
import { DialogService, Toast } from '@laser-ui/components';
5+
import { useUnmount } from '@laser-ui/hooks';
6+
import { useRef } from 'react';
7+
import { useTranslation } from 'react-i18next';
8+
import { useLocation, useNavigate } from 'react-router';
9+
10+
import { axios } from './axios';
11+
import { LOGIN_PATH, PREV_ROUTE_KEY } from '../../configs/router';
12+
13+
export function useAxios() {
14+
const location = useLocation();
15+
const navigate = useNavigate();
16+
const { t } = useTranslation();
17+
18+
const controllers = useRef<Set<AbortController>>(new Set());
19+
20+
useUnmount(() => {
21+
for (const controller of controllers.current) {
22+
controller.abort();
23+
}
24+
controllers.current.clear();
25+
});
26+
27+
return <T = any>(config: AxiosRequestConfig, overrides?: true | HttpRequestConfigOverrides): Promise<T> => {
28+
const controller = new AbortController();
29+
controllers.current.add(controller);
30+
return axios({ signal: controller.signal, ...config }, overrides)
31+
.then((res) => res.data)
32+
.catch((error) => {
33+
if (error.response) {
34+
switch (error.response.status) {
35+
case 401:
36+
DialogService.open(Toast, {
37+
children: t('User not authorized'),
38+
type: 'error',
39+
});
40+
navigate(LOGIN_PATH, { state: { [PREV_ROUTE_KEY]: location } });
41+
break;
42+
43+
case 403:
44+
case 404:
45+
case 500:
46+
if (location.pathname !== LOGIN_PATH) {
47+
navigate(`/exception/${error.response.status}`);
48+
}
49+
break;
50+
51+
default:
52+
break;
53+
}
54+
} else if (error.request) {
55+
// The request was made but no response was received.
56+
} else {
57+
// Something happened in setting up the request that triggered an Error.
58+
}
59+
})
60+
.finally(() => {
61+
controllers.current.delete(controller);
62+
});
63+
};
64+
}

src/app/core/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
export { axios, useAxios } from './axios';
12
export { initUser } from './initUser';
23
export { GlobalStore } from './store';
34
export { TOKEN } from './token';
4-
export { useHttp } from './useHttp';
55
export { useMenu } from './useMenu';

0 commit comments

Comments
 (0)