mirror of
https://github.com/router-for-me/Cli-Proxy-API-Management-Center.git
synced 2026-09-24 16:04:35 +08:00
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:
@@ -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
@@ -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') {
|
||||
|
||||
Reference in New Issue
Block a user