49 lines
1.7 KiB
TypeScript
49 lines
1.7 KiB
TypeScript
import type {
|
||
AuthMeResponse,
|
||
AuthRefreshResponse,
|
||
AuthSessionResponse,
|
||
AuthUser,
|
||
ChangePasswordRequest,
|
||
LoginRequest,
|
||
RegisterRequest,
|
||
UpdateCurrentUserRequest,
|
||
} from '@atlas/contracts';
|
||
import { api } from '@/services/api';
|
||
|
||
export interface UpdateProfileOptions {
|
||
/** 提交时的乐观锁版本,取自当前会话 user.revision */
|
||
revision: number;
|
||
/** 幂等键,重试安全;成功响应携带新 revision */
|
||
idempotencyKey: string;
|
||
}
|
||
|
||
/** 账户与会话接口,契约见 docs/api/auth.md;类型全部来自 @atlas/contracts */
|
||
export const authApi = {
|
||
register: (payload: RegisterRequest) =>
|
||
api.post<AuthSessionResponse>('/auth/register', payload),
|
||
|
||
login: (payload: LoginRequest) => api.post<AuthSessionResponse>('/auth/login', payload),
|
||
|
||
refresh: () => api.post<AuthRefreshResponse>('/auth/refresh'),
|
||
|
||
logout: () => api.post<void>('/auth/logout'),
|
||
|
||
me: () => api.get<AuthMeResponse>('/auth/me'),
|
||
|
||
/**
|
||
* 修改资料:If-Match 乐观锁 + Idempotency-Key 幂等(docs/api/conventions.md §5)。
|
||
* 版本过期返回 409 VERSION_CONFLICT(details.current_revision 为服务端当前版本)。
|
||
*/
|
||
updateProfile: (payload: UpdateCurrentUserRequest, options: UpdateProfileOptions) =>
|
||
api.patch<AuthUser>('/auth/me', payload, {
|
||
headers: {
|
||
'If-Match': `"${options.revision}"`,
|
||
'Idempotency-Key': options.idempotencyKey,
|
||
},
|
||
}),
|
||
|
||
/** 修改密码:成功后所有旧 session 失效;响应携带新 access token,必须立即替换内存值 */
|
||
changePassword: (payload: ChangePasswordRequest) =>
|
||
api.post<AuthRefreshResponse>('/auth/change-password', payload),
|
||
};
|