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
Unverified
parent 93f3b6b7ab
commit cd44dca9c0
20 changed files with 38 additions and 91 deletions
+1 -6
View File
@@ -5,6 +5,7 @@ import { EmptyState } from '@/components/ui/EmptyState';
import { useHeaderRefresh } from '@/hooks/useHeaderRefresh';
import { pluginsApi } from '@/services/api';
import { useAuthStore } from '@/stores';
import { getErrorMessage, isRecord } from '@/utils/helpers';
import type { PluginListResponse } from '@/types';
import {
collectPluginResourceEntries,
@@ -12,15 +13,9 @@ import {
} from './pluginResources';
import styles from './PluginResourcePage.module.scss';
const isRecord = (value: unknown): value is Record<string, unknown> =>
value !== null && typeof value === 'object' && !Array.isArray(value);
const hasStatus = (error: unknown, status: number) =>
isRecord(error) && error.status === status;
const getErrorMessage = (error: unknown, fallback: string) =>
error instanceof Error ? error.message : typeof error === 'string' ? error : fallback;
const safeDecodeURIComponent = (value = '') => {
try {
return decodeURIComponent(value);
+1 -6
View File
@@ -16,6 +16,7 @@ import {
import { useHeaderRefresh } from '@/hooks/useHeaderRefresh';
import { pluginStoreApi } from '@/services/api';
import { useAuthStore, useConfigStore, useNotificationStore } from '@/stores';
import { getErrorMessage, isRecord } from '@/utils/helpers';
import type { PluginStoreEntry, PluginStoreResponse } from '@/types';
import { buildRepositoryURL, resolvePluginAssetURL } from './pluginResources';
import styles from './PluginStorePage.module.scss';
@@ -27,12 +28,6 @@ interface StoreLoadError {
message: string;
}
const isRecord = (value: unknown): value is Record<string, unknown> =>
value !== null && typeof value === 'object' && !Array.isArray(value);
const getErrorMessage = (error: unknown, fallback: string) =>
error instanceof Error ? error.message : typeof error === 'string' ? error : fallback;
const getErrorStatus = (error: unknown): number | undefined =>
isRecord(error) && typeof error.status === 'number' ? error.status : undefined;
+1 -6
View File
@@ -20,6 +20,7 @@ import {
import { useHeaderRefresh } from '@/hooks/useHeaderRefresh';
import { pluginsApi } from '@/services/api';
import { useAuthStore, useConfigStore, useNotificationStore } from '@/stores';
import { getErrorMessage, isRecord } from '@/utils/helpers';
import type { PluginConfigField, PluginListEntry, PluginListResponse } from '@/types';
import { getPluginTitle, resolvePluginAssetURL } from './pluginResources';
import styles from './PluginsPage.module.scss';
@@ -44,15 +45,9 @@ function PluginCardLogo({ src }: { src: string }) {
);
}
const isRecord = (value: unknown): value is Record<string, unknown> =>
value !== null && typeof value === 'object' && !Array.isArray(value);
const cloneRecord = (value: unknown): Record<string, unknown> =>
isRecord(value) ? { ...value } : {};
const getErrorMessage = (error: unknown, fallback: string) =>
error instanceof Error ? error.message : typeof error === 'string' ? error : fallback;
const hasStatus = (error: unknown, status: number) =>
isRecord(error) && error.status === status;