task-46(账号/权限页面): 实现新建用户请求适配
新增 user-create-adapter.ts 编排提交:先 validateCreateUserForm 校验(不通过抛首条错误不发请求)、 再 toCreateUserRequest 序列化 camelCase 请求体 POST /api/admin/user、最后解析返回新用户 id。 parseCreatedUserId 纯解析下沉 users-model.ts:解包 ApiResponse、success=false 抛后端 message、 data 非正整数抛可读错误。8 用例全过,331 单测 + build 绿。
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
/** 用户列表查询适配(任务 42):纯解析模块,无 axios 依赖,与 Java AdminUserListVo 对齐。 */
|
||||
/** 用户 API 响应解析模型:列表分页(任务 42)与新建用户(任务 46)负载归一。 */
|
||||
import { unwrap } from './envelope.ts'
|
||||
import { emptyUserPageResult, type UserPageResult } from './users-dto.ts'
|
||||
import type { AdminUser } from '../types/admin'
|
||||
@@ -52,3 +52,11 @@ export function parseUserPage(payload: unknown): UserPageResult {
|
||||
if (typeof record.current_user_username === 'string') out.currentUserUsername = record.current_user_username
|
||||
return out
|
||||
}
|
||||
|
||||
/** 解包新建用户响应并取回新用户 id(POST /api/admin/user 的 data 为 userId)。 */
|
||||
export function parseCreatedUserId(payload: unknown): number {
|
||||
const data = unwrap<unknown>(payload)
|
||||
const id = typeof data === 'number' && Number.isFinite(data) ? Math.floor(data) : null
|
||||
if (id === null || id < 1) throw new Error('创建用户响应异常:未返回有效的用户 ID')
|
||||
return id
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/** 新建用户请求适配(任务 46):表单模型 → POST /api/admin/user → 新用户 id。 */
|
||||
import { http } from '@/api/http'
|
||||
import { parseCreatedUserId } from '@/api/users-model'
|
||||
import {
|
||||
toCreateUserRequest,
|
||||
validateCreateUserForm,
|
||||
type CreateUserForm,
|
||||
} from './user-create-model'
|
||||
|
||||
/** Java AdminUserController.createUser 端点。 */
|
||||
export const CREATE_USER_ENDPOINT = '/api/admin/user'
|
||||
|
||||
/**
|
||||
* 提交新建用户:先校验表单(不通过直接抛首条错误,不发请求),再序列化请求体 POST,
|
||||
* 最后解析返回的新用户 id;http/后端失败交由调用方提示,不吞异常。
|
||||
*/
|
||||
export async function submitCreateUser(form: CreateUserForm): Promise<number> {
|
||||
const { valid, errors } = validateCreateUserForm(form)
|
||||
if (!valid) {
|
||||
throw new Error(Object.values(errors)[0] || '创建用户表单校验未通过')
|
||||
}
|
||||
const { data } = await http.post<unknown>(CREATE_USER_ENDPOINT, toCreateUserRequest(form))
|
||||
return parseCreatedUserId(data)
|
||||
}
|
||||
Reference in New Issue
Block a user