/** 店铺明文凭据解析模型(任务 93):凭证明文仅来自凭证端点且落 SensitiveString, * 与列表行(仅掩码)字段分离;纯逻辑。 */ import { unwrap } from '../../api/envelope.ts' import { toSensitiveString, type ShopCredential } from './shop-dto.ts' function text(value: unknown): string { return typeof value === 'string' ? value.trim() : '' } function numberOrNull(value: unknown): number | null { return typeof value === 'number' && Number.isFinite(value) ? Math.floor(value) : null } /** 解析单条明文凭据;缺 id 或密码为空视为无效(拒绝把空当明文)。 */ export function toShopCredential(raw: unknown): ShopCredential | null { if (!raw || typeof raw !== 'object') return null const record = raw as Record const id = numberOrNull(record.id) if (id === null) return null const password = text(record.password) if (!password) return null const credential: ShopCredential = { id, groupId: numberOrNull(record.groupId ?? record.group_id), groupName: text(record.groupName ?? record.group_name), shopName: text(record.shopName ?? record.shop_name), mallName: text(record.mallName ?? record.mall_name), znUsername: text(record.znUsername ?? record.zn_username), account: text(record.account), password: toSensitiveString(password), } return credential } /** 解包凭证端点负载(data 为 ShopManageCredentialVo)为明文凭据;无效时抛可操作错误。 */ export function parseShopCredential(payload: unknown): ShopCredential { const credential = toShopCredential(unwrap(payload)) if (!credential) { throw new Error('读取店铺凭证响应异常:未返回有效的明文凭据') } return credential }