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:
@@ -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