import * as v from 'valibot' const text = (max: number) => v.pipe(v.string(), v.trim(), v.maxLength(max)) export const userInputSchema = v.object({ username: v.pipe(text(32), v.minLength(2), v.regex(/^\S+$/u)), name: v.pipe(text(60), v.minLength(1)), email: v.pipe(text(120), v.email()), phone: v.pipe(text(24), v.regex(/^[+\d ()-]*$/u)), departmentId: v.pipe(text(80), v.minLength(1)), role: v.pipe(text(80), v.minLength(1)), status: v.picklist(['enabled', 'disabled']), remark: text(300), }) export type UserInput = v.InferOutput export const userSchema = v.object({ ...userInputSchema.entries, id: v.pipe(v.string(), v.minLength(1), v.maxLength(80)), createdAt: v.pipe(v.string(), v.isoTimestamp()), }) export type AdminUser = v.InferOutput export type UserStatus = AdminUser['status'] export const departmentSchema = v.object({ id: v.pipe(v.string(), v.minLength(1)), parentId: v.nullable(v.string()), name: v.string(), labelKey: v.optional(v.string()), }) export type Department = v.InferOutput export const roleOptionSchema = v.object({ value: v.string(), label: v.string(), labelKey: v.optional(v.string()), disabled: v.optional(v.boolean()), }) export const userOptionsSchema = v.pipe( v.object({ departments: v.array(departmentSchema), roles: v.array(roleOptionSchema), }), v.check(({ departments, roles }) => { const byId = new Map(departments.map((item) => [item.id, item])) if ( byId.size !== departments.length || new Set(roles.map((item) => item.value)).size !== roles.length ) return false return departments.every((item) => { const seen = new Set() let current: Department | undefined = item while (current) { if (seen.has(current.id)) return false seen.add(current.id) if (current.parentId === null) return true current = byId.get(current.parentId) } return false }) }, 'Departments must form a tree with unique identifiers.'), ) export type UserOptions = v.InferOutput export const profileSchema = v.object({ id: v.string(), username: v.string(), name: v.string(), role: v.string(), avatar: v.optional(v.string()), }) export const sessionSchema = v.object({ user: profileSchema, permissions: v.array(v.string()), expiresAt: v.optional(v.pipe(v.string(), v.isoTimestamp())), }) export type SessionInfo = v.InferOutput export const loginInputSchema = v.object({ username: v.pipe(text(80), v.minLength(1)), password: v.pipe(v.string(), v.minLength(1), v.maxLength(128)), }) export type LoginInput = v.InferInput export const browserLoginResultSchema = v.object({ ...sessionSchema.entries, expiresAt: v.pipe(v.string(), v.isoTimestamp()), }) export const loginResultSchema = v.object({ ...browserLoginResultSchema.entries, accessToken: v.pipe(v.string(), v.minLength(1)), }) export type LoginResult = v.InferOutput export const profileInputSchema = v.strictObject({ name: userInputSchema.entries.name, phone: userInputSchema.entries.phone, }) export const accountProfileSchema = v.object({ ...profileSchema.entries, ...profileInputSchema.entries, email: userInputSchema.entries.email, department: departmentSchema, }) export type ProfileInput = v.InferInput export type AccountProfile = v.InferOutput const newPasswordSchema = v.pipe(v.string(), v.minLength(12), v.maxLength(128)) export const credentialInputSchema = v.strictObject({ newPassword: newPasswordSchema, }) export const changePasswordSchema = v.strictObject({ currentPassword: loginInputSchema.entries.password, newPassword: newPasswordSchema, }) export type CredentialInput = v.InferInput export type ChangePasswordInput = v.InferInput export interface ApiResponse { code: number msg: string data: T revision?: string error?: { key: string; fields?: Record } } export const envelopeSchema = v.object({ code: v.number(), msg: v.string(), data: v.unknown(), revision: v.optional(v.string()), error: v.optional( v.object({ key: v.string(), fields: v.optional(v.record(v.string(), v.string())), }), ), }) /** * * @param value 编辑接口返回的资源快照 * * @returns 快照内容的 SHA-256 摘要,供条件写入核对版本。 */ export async function editRevision(value: unknown): Promise { const bytes = new TextEncoder().encode(JSON.stringify(value)) const digest = await crypto.subtle.digest('SHA-256', bytes) return Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, '0'), ).join('') } export const userListSchema = v.object({ items: v.array(userSchema), total: v.pipe(v.number(), v.integer(), v.minValue(0)), }) export type UserList = v.InferOutput export const userListQuerySchema = v.object({ page: v.optional(v.pipe(v.number(), v.integer(), v.minValue(1))), pageSize: v.optional( v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(100)), ), name: v.optional(text(60)), username: v.optional(text(32)), email: v.optional(text(120)), departmentId: v.optional(text(80)), status: v.optional(v.picklist(['enabled', 'disabled'])), role: v.optional(text(80)), }) export type UserListParams = v.InferOutput export const idsSchema = v.object({ ids: v.pipe( v.array(v.pipe(v.string(), v.minLength(1))), v.minLength(1), v.maxLength(500), ), }) export type IdsInput = v.InferInput export const statusInputSchema = v.object({ ...idsSchema.entries, status: v.picklist(['enabled', 'disabled']), }) export type StatusInput = v.InferInput export const changedSchema = v.object({ affected: v.pipe(v.number(), v.integer(), v.minValue(0)), }) export type Changed = v.InferOutput export const permissionCodes = [ 'users:read', 'users:create', 'users:update', 'users:delete', 'users:status', 'users:credentials', 'users:reset', 'routes:read', 'routes:write', ] as const export type PermissionCode = (typeof permissionCodes)[number]