Skip to content

Commit d7af922

Browse files
committed
refactor: use auto-generated API client (maa-copilot-client)
1 parent ebadb4f commit d7af922

36 files changed

+793
-726
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@
4040
"eslint-plugin-react": "^7.33.2",
4141
"eslint-plugin-react-hooks": "^4.6.0",
4242
"fuse.js": "^6.6.2",
43-
"jotai": "^1.7.3",
43+
"jotai": "^2.7.0",
4444
"linkify-react": "^3.0.4",
4545
"linkifyjs": "^3.0.5",
4646
"lodash-es": "^4.17.21",
47+
"maa-copilot-client": "https://github.com/MaaAssistantArknights/maa-copilot-client-ts.git#0.1.0-SNAPSHOT.726.3d19287",
4748
"normalize.css": "^8.0.1",
4849
"prettier": "^3.2.5",
4950
"react": "^18.0.0",
@@ -58,7 +59,7 @@
5859
"remark-breaks": "^3.0.2",
5960
"remark-gfm": "^3.0.1",
6061
"snakecase-keys": "^5.4.4",
61-
"swr": "^2.0.0-rc.3",
62+
"swr": "^2.2.5",
6263
"type-fest": "^4.10.2",
6364
"unfetch": "^4.2.0",
6465
"vite-tsconfig-paths": "^3.5.0"

src/App.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1+
import { getDefaultStore } from 'jotai/vanilla'
12
import { BrowserRouter } from 'react-router-dom'
23
import { SWRConfig } from 'swr'
34

4-
import { Effects } from 'components/Effects'
5+
import { authAtom } from 'store/auth'
6+
import { TokenManager } from 'utils/token-manager'
57

68
import { GlobalErrorBoundary } from './components/GlobalErrorBoundary'
79
import { FCC } from './types'
810
import { request } from './utils/fetcher'
911

12+
// jotai 在没有 Provider 时会使用默认的 store
13+
TokenManager.setAuthGetter(() => getDefaultStore().get(authAtom))
14+
TokenManager.setAuthSetter((v) => getDefaultStore().set(authAtom, v))
15+
1016
export const App: FCC = ({ children }) => {
1117
return (
1218
<>
13-
<Effects />
1419
<SWRConfig
1520
value={{
1621
fetcher: request,

src/apis/auth.ts

Lines changed: 59 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,75 @@
11
import { Response } from 'models/network'
22
import { jsonRequest } from 'utils/fetcher'
3+
import { UserApi } from 'utils/maa-copilot-client'
34

4-
export interface UserCredentials {
5-
token: string
6-
validBefore: string
7-
refreshToken: string
8-
refreshTokenValidBefore: string
9-
userInfo: UserInfo
5+
export async function sendRegistrationEmail(req: { email: string }) {
6+
await new UserApi({ sendToken: 'never' }).sendRegistrationToken({
7+
sendRegistrationTokenDTO: req,
8+
})
109
}
1110

12-
export interface UserInfo {
13-
id: string
14-
userName: string
15-
role: string
16-
activated: boolean
17-
favoriteLists: Record<string, any>
18-
uploadCount: number
11+
export async function register(req: {
12+
email: string
13+
registrationToken: string
14+
username: string
15+
password: string
16+
}) {
17+
await new UserApi({ sendToken: 'never' }).register({
18+
registerDTO: {
19+
...req,
20+
userName: req.username,
21+
},
22+
})
1923
}
2024

21-
export interface LoginResponse extends UserCredentials {}
25+
export async function login(req: { email: string; password: string }) {
26+
const res = await new UserApi({
27+
sendToken: 'never',
28+
requireData: true,
29+
}).login({
30+
loginDTO: req,
31+
})
32+
return res.data
33+
}
2234

23-
export const requestLogin = (email: string, password: string) => {
24-
return jsonRequest<Response<LoginResponse>>('/user/login', {
25-
method: 'POST',
26-
json: {
27-
email,
28-
password,
35+
export async function refreshAccessToken(req: { refreshToken: string }) {
36+
const res = await new UserApi({
37+
sendToken: 'never',
38+
requireData: true,
39+
}).refresh({
40+
refreshReq: req,
41+
})
42+
return res.data
43+
}
44+
45+
export async function updateUserInfo(req: { username: string }) {
46+
await new UserApi().updateInfo({
47+
userInfoUpdateDTO: {
48+
userName: req.username,
2949
},
3050
})
3151
}
3252

33-
export interface RefreshResponse extends UserCredentials {}
53+
export async function updatePassword(req: {
54+
originalPassword: string
55+
newPassword: string
56+
}) {
57+
await new UserApi().updatePassword({ passwordUpdateDTO: req })
58+
}
3459

35-
export const requestRefresh = (token: string, refreshToken: string) => {
36-
return jsonRequest<Response<RefreshResponse>>('/user/refresh', {
37-
method: 'POST',
38-
json: {
39-
access_token: token,
40-
refresh_token: refreshToken,
41-
},
42-
noToken: true,
60+
export async function sendResetPasswordEmail(req: { email: string }) {
61+
await new UserApi({ sendToken: 'never' }).passwordResetRequest({
62+
passwordResetVCodeDTO: req,
63+
})
64+
}
65+
66+
export function resetPassword(req: {
67+
email: string
68+
activeCode: string
69+
password: string
70+
}) {
71+
return new UserApi({ sendToken: 'never' }).passwordReset({
72+
passwordResetDTO: req,
4373
})
4474
}
4575

@@ -65,104 +95,3 @@ export const requestActivationCode = () => {
6595
},
6696
)
6797
}
68-
69-
export interface RegisterResponse {}
70-
71-
export const requestRegister = (
72-
email: string,
73-
registrationToken: string,
74-
username: string,
75-
password: string,
76-
) => {
77-
return jsonRequest<Response<RegisterResponse>>('/user/register', {
78-
method: 'POST',
79-
json: {
80-
email,
81-
registration_token: registrationToken,
82-
user_name: username,
83-
password,
84-
},
85-
})
86-
}
87-
88-
export interface EmailToenResponse {}
89-
90-
export const reqeustRegistrationToken = (email: string) => {
91-
return jsonRequest<Response<EmailToenResponse>>(
92-
'/user/sendRegistrationToken',
93-
{
94-
method: 'POST',
95-
json: {
96-
email,
97-
},
98-
},
99-
)
100-
}
101-
102-
export interface UpdateInfoResponse {}
103-
104-
export const requestUpdateInfo = ({
105-
email,
106-
username,
107-
}: {
108-
email?: string
109-
username?: string
110-
}) => {
111-
return jsonRequest<Response<UpdateInfoResponse>>('/user/update/info', {
112-
method: 'POST',
113-
json: {
114-
email,
115-
user_name: username,
116-
},
117-
})
118-
}
119-
120-
export interface UpdatePasswordResponse {}
121-
122-
export const requestUpdatePassword = ({
123-
original,
124-
newPassword,
125-
}: {
126-
original?: string
127-
newPassword?: string
128-
}) => {
129-
return jsonRequest<Response<UpdatePasswordResponse>>(
130-
'/user/update/password',
131-
{
132-
method: 'POST',
133-
json: {
134-
original_password: original,
135-
new_password: newPassword,
136-
},
137-
},
138-
)
139-
}
140-
141-
export interface ResetPasswordTokenResponse {}
142-
143-
export const requestResetPasswordToken = (data: { email: string }) => {
144-
return jsonRequest<Response<ResetPasswordTokenResponse>>(
145-
'/user/password/reset_request',
146-
{
147-
method: 'POST',
148-
json: data,
149-
},
150-
)
151-
}
152-
153-
export interface ResetPasswordResponse {}
154-
155-
export const requestResetPassword = (data: {
156-
email: string
157-
token: string
158-
password: string
159-
}) => {
160-
return jsonRequest<Response<ResetPasswordResponse>>('/user/password/reset', {
161-
method: 'POST',
162-
json: {
163-
email: data.email,
164-
active_code: data.token,
165-
password: data.password,
166-
},
167-
})
168-
}

0 commit comments

Comments
 (0)