refactor(utils): share isRecord and getErrorMessage helpers

isRecord was declared locally in 15 modules (with two divergent shapes) and getErrorMessage in 7. Move a single canonical pair into utils/helpers and import it everywhere. The shared isRecord excludes arrays; the call sites that previously allowed them only read named properties, so behavior is unchanged.
This commit is contained in:
LTbinglingfeng
2026-06-13 02:11:21 +08:00
parent 93f3b6b7ab
commit cd44dca9c0
20 changed files with 38 additions and 91 deletions
+16
View File
@@ -9,3 +9,19 @@
export function generateId(): string {
return `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
}
/**
* 判断是否为普通对象(排除 null 与数组)
*/
export const isRecord = (value: unknown): value is Record<string, unknown> =>
value !== null && typeof value === 'object' && !Array.isArray(value);
/**
* 从 unknown 错误中提取可读消息
*/
export const getErrorMessage = (error: unknown, fallback = ''): string => {
if (error instanceof Error) return error.message || fallback;
if (typeof error === 'string') return error || fallback;
if (isRecord(error) && typeof error.message === 'string') return error.message || fallback;
return fallback;
};
+2 -3
View File
@@ -3,6 +3,8 @@
* 迁移自基线 utils/models.js
*/
import { isRecord } from './helpers';
export interface ModelInfo {
name: string;
alias?: string;
@@ -30,9 +32,6 @@ const matchCategory = (text: string) => {
return null;
};
const isRecord = (value: unknown): value is Record<string, unknown> =>
value !== null && typeof value === 'object' && !Array.isArray(value);
export function normalizeModelList(payload: unknown, { dedupe = false } = {}): ModelInfo[] {
const toModel = (entry: unknown): ModelInfo | null => {
if (typeof entry === 'string') {