mirror of
https://github.com/router-for-me/Cli-Proxy-API-Management-Center.git
synced 2026-02-18 10:40:50 +08:00
refactor(core): harden API parsing and improve type safety
This commit is contained in:
@@ -243,7 +243,7 @@ export function AiProvidersOpenAIEditLayout() {
|
||||
setTestStatus('idle');
|
||||
setTestMessage('');
|
||||
}
|
||||
}, [availableModels, loading, testModel]);
|
||||
}, [availableModels, loading, setTestMessage, setTestModel, setTestStatus, testModel]);
|
||||
|
||||
const mergeDiscoveredModels = useCallback(
|
||||
(selectedModels: ModelInfo[]) => {
|
||||
|
||||
@@ -3,7 +3,7 @@ import { Trans, useTranslation } from 'react-i18next';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { useInterval } from '@/hooks/useInterval';
|
||||
import { useHeaderRefresh } from '@/hooks/useHeaderRefresh';
|
||||
import { usePageTransitionLayer } from '@/components/common/PageTransition';
|
||||
import { usePageTransitionLayer } from '@/components/common/PageTransitionLayer';
|
||||
import { Card } from '@/components/ui/Card';
|
||||
import { Button } from '@/components/ui/Button';
|
||||
import { LoadingSpinner } from '@/components/ui/LoadingSpinner';
|
||||
@@ -1475,11 +1475,14 @@ export function AuthFilesPage() {
|
||||
return GEMINI_CLI_CONFIG;
|
||||
};
|
||||
|
||||
const getQuotaState = (type: QuotaProviderType, fileName: string) => {
|
||||
if (type === 'antigravity') return antigravityQuota[fileName];
|
||||
if (type === 'codex') return codexQuota[fileName];
|
||||
return geminiCliQuota[fileName];
|
||||
};
|
||||
const getQuotaState = useCallback(
|
||||
(type: QuotaProviderType, fileName: string) => {
|
||||
if (type === 'antigravity') return antigravityQuota[fileName];
|
||||
if (type === 'codex') return codexQuota[fileName];
|
||||
return geminiCliQuota[fileName];
|
||||
},
|
||||
[antigravityQuota, codexQuota, geminiCliQuota]
|
||||
);
|
||||
|
||||
const updateQuotaState = useCallback(
|
||||
(
|
||||
|
||||
@@ -62,14 +62,23 @@ export function DashboardPage() {
|
||||
apiKeysCache.current = [];
|
||||
}, [apiBase, config?.apiKeys]);
|
||||
|
||||
const normalizeApiKeyList = (input: any): string[] => {
|
||||
const normalizeApiKeyList = (input: unknown): string[] => {
|
||||
if (!Array.isArray(input)) return [];
|
||||
const seen = new Set<string>();
|
||||
const keys: string[] = [];
|
||||
|
||||
input.forEach((item) => {
|
||||
const value = typeof item === 'string' ? item : item?.['api-key'] ?? item?.apiKey ?? '';
|
||||
const trimmed = String(value || '').trim();
|
||||
const record =
|
||||
item !== null && typeof item === 'object' && !Array.isArray(item)
|
||||
? (item as Record<string, unknown>)
|
||||
: null;
|
||||
const value =
|
||||
typeof item === 'string'
|
||||
? item
|
||||
: record
|
||||
? (record['api-key'] ?? record['apiKey'] ?? record.key ?? record.Key)
|
||||
: '';
|
||||
const trimmed = String(value ?? '').trim();
|
||||
if (!trimmed || seen.has(trimmed)) return;
|
||||
seen.add(trimmed);
|
||||
keys.push(trimmed);
|
||||
|
||||
@@ -15,11 +15,20 @@ import styles from './LoginPage.module.scss';
|
||||
/**
|
||||
* 将 API 错误转换为本地化的用户友好消息
|
||||
*/
|
||||
function getLocalizedErrorMessage(error: any, t: (key: string) => string): string {
|
||||
const apiError = error as ApiError;
|
||||
const status = apiError?.status;
|
||||
const code = apiError?.code;
|
||||
const message = apiError?.message || '';
|
||||
type RedirectState = { from?: { pathname?: string } };
|
||||
|
||||
function getLocalizedErrorMessage(error: unknown, t: (key: string) => string): string {
|
||||
const apiError = error as Partial<ApiError>;
|
||||
const status = typeof apiError.status === 'number' ? apiError.status : undefined;
|
||||
const code = typeof apiError.code === 'string' ? apiError.code : undefined;
|
||||
const message =
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: typeof apiError.message === 'string'
|
||||
? apiError.message
|
||||
: typeof error === 'string'
|
||||
? error
|
||||
: '';
|
||||
|
||||
// 根据 HTTP 状态码判断
|
||||
if (status === 401) {
|
||||
@@ -99,7 +108,7 @@ export function LoginPage() {
|
||||
setAutoLoginSuccess(true);
|
||||
// 延迟跳转,让用户看到成功动画
|
||||
setTimeout(() => {
|
||||
const redirect = (location.state as any)?.from?.pathname || '/';
|
||||
const redirect = (location.state as RedirectState | null)?.from?.pathname || '/';
|
||||
navigate(redirect, { replace: true });
|
||||
}, 1500);
|
||||
} else {
|
||||
@@ -135,7 +144,7 @@ export function LoginPage() {
|
||||
});
|
||||
showNotification(t('common.connected_status'), 'success');
|
||||
navigate('/', { replace: true });
|
||||
} catch (err: any) {
|
||||
} catch (err: unknown) {
|
||||
const message = getLocalizedErrorMessage(err, t);
|
||||
setError(message);
|
||||
showNotification(`${t('notification.login_failed')}: ${message}`, 'error');
|
||||
@@ -155,7 +164,7 @@ export function LoginPage() {
|
||||
);
|
||||
|
||||
if (isAuthenticated && !autoLoading && !autoLoginSuccess) {
|
||||
const redirect = (location.state as any)?.from?.pathname || '/';
|
||||
const redirect = (location.state as RedirectState | null)?.from?.pathname || '/';
|
||||
return <Navigate to={redirect} replace />;
|
||||
}
|
||||
|
||||
|
||||
@@ -56,6 +56,21 @@ interface VertexImportState {
|
||||
result?: VertexImportResult;
|
||||
}
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return value !== null && typeof value === 'object';
|
||||
}
|
||||
|
||||
function getErrorMessage(error: unknown): string {
|
||||
if (error instanceof Error) return error.message;
|
||||
if (isRecord(error) && typeof error.message === 'string') return error.message;
|
||||
return typeof error === 'string' ? error : '';
|
||||
}
|
||||
|
||||
function getErrorStatus(error: unknown): number | undefined {
|
||||
if (!isRecord(error)) return undefined;
|
||||
return typeof error.status === 'number' ? error.status : undefined;
|
||||
}
|
||||
|
||||
const PROVIDERS: { id: OAuthProvider; titleKey: string; hintKey: string; urlLabelKey: string; icon: string | { light: string; dark: string } }[] = [
|
||||
{ id: 'codex', titleKey: 'auth_login.codex_oauth_title', hintKey: 'auth_login.codex_oauth_hint', urlLabelKey: 'auth_login.codex_oauth_url_label', icon: { light: iconCodexLight, dark: iconCodexDark } },
|
||||
{ id: 'anthropic', titleKey: 'auth_login.anthropic_oauth_title', hintKey: 'auth_login.anthropic_oauth_hint', urlLabelKey: 'auth_login.anthropic_oauth_url_label', icon: iconClaude },
|
||||
@@ -127,8 +142,8 @@ export function OAuthPage() {
|
||||
window.clearInterval(timer);
|
||||
delete timers.current[provider];
|
||||
}
|
||||
} catch (err: any) {
|
||||
updateProviderState(provider, { status: 'error', error: err?.message, polling: false });
|
||||
} catch (err: unknown) {
|
||||
updateProviderState(provider, { status: 'error', error: getErrorMessage(err), polling: false });
|
||||
window.clearInterval(timer);
|
||||
delete timers.current[provider];
|
||||
}
|
||||
@@ -159,9 +174,13 @@ export function OAuthPage() {
|
||||
if (res.state) {
|
||||
startPolling(provider, res.state);
|
||||
}
|
||||
} catch (err: any) {
|
||||
updateProviderState(provider, { status: 'error', error: err?.message, polling: false });
|
||||
showNotification(`${t(getAuthKey(provider, 'oauth_start_error'))} ${err?.message || ''}`, 'error');
|
||||
} catch (err: unknown) {
|
||||
const message = getErrorMessage(err);
|
||||
updateProviderState(provider, { status: 'error', error: message, polling: false });
|
||||
showNotification(
|
||||
`${t(getAuthKey(provider, 'oauth_start_error'))}${message ? ` ${message}` : ''}`,
|
||||
'error'
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -190,13 +209,15 @@ export function OAuthPage() {
|
||||
await oauthApi.submitCallback(provider, redirectUrl);
|
||||
updateProviderState(provider, { callbackSubmitting: false, callbackStatus: 'success' });
|
||||
showNotification(t('auth_login.oauth_callback_success'), 'success');
|
||||
} catch (err: any) {
|
||||
} catch (err: unknown) {
|
||||
const status = getErrorStatus(err);
|
||||
const message = getErrorMessage(err);
|
||||
const errorMessage =
|
||||
err?.status === 404
|
||||
status === 404
|
||||
? t('auth_login.oauth_callback_upgrade_hint', {
|
||||
defaultValue: 'Please update CLI Proxy API or check the connection.'
|
||||
})
|
||||
: err?.message;
|
||||
: message || undefined;
|
||||
updateProviderState(provider, {
|
||||
callbackSubmitting: false,
|
||||
callbackStatus: 'error',
|
||||
@@ -236,15 +257,19 @@ export function OAuthPage() {
|
||||
}));
|
||||
showNotification(`${t('auth_login.iflow_cookie_status_error')} ${res.error || ''}`, 'error');
|
||||
}
|
||||
} catch (err: any) {
|
||||
if (err?.status === 409) {
|
||||
} catch (err: unknown) {
|
||||
if (getErrorStatus(err) === 409) {
|
||||
const message = t('auth_login.iflow_cookie_config_duplicate');
|
||||
setIflowCookie((prev) => ({ ...prev, loading: false, error: message, errorType: 'warning' }));
|
||||
showNotification(message, 'warning');
|
||||
return;
|
||||
}
|
||||
setIflowCookie((prev) => ({ ...prev, loading: false, error: err?.message, errorType: 'error' }));
|
||||
showNotification(`${t('auth_login.iflow_cookie_start_error')} ${err?.message || ''}`, 'error');
|
||||
const message = getErrorMessage(err);
|
||||
setIflowCookie((prev) => ({ ...prev, loading: false, error: message, errorType: 'error' }));
|
||||
showNotification(
|
||||
`${t('auth_login.iflow_cookie_start_error')}${message ? ` ${message}` : ''}`,
|
||||
'error'
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -292,8 +317,8 @@ export function OAuthPage() {
|
||||
};
|
||||
setVertexState((prev) => ({ ...prev, loading: false, result }));
|
||||
showNotification(t('vertex_import.success'), 'success');
|
||||
} catch (err: any) {
|
||||
const message = err?.message || '';
|
||||
} catch (err: unknown) {
|
||||
const message = getErrorMessage(err);
|
||||
setVertexState((prev) => ({
|
||||
...prev,
|
||||
loading: false,
|
||||
|
||||
@@ -83,14 +83,23 @@ export function SystemPage() {
|
||||
return resolvedTheme === 'dark' ? iconEntry.dark : iconEntry.light;
|
||||
};
|
||||
|
||||
const normalizeApiKeyList = (input: any): string[] => {
|
||||
const normalizeApiKeyList = (input: unknown): string[] => {
|
||||
if (!Array.isArray(input)) return [];
|
||||
const seen = new Set<string>();
|
||||
const keys: string[] = [];
|
||||
|
||||
input.forEach((item) => {
|
||||
const value = typeof item === 'string' ? item : item?.['api-key'] ?? item?.apiKey ?? '';
|
||||
const trimmed = String(value || '').trim();
|
||||
const record =
|
||||
item !== null && typeof item === 'object' && !Array.isArray(item)
|
||||
? (item as Record<string, unknown>)
|
||||
: null;
|
||||
const value =
|
||||
typeof item === 'string'
|
||||
? item
|
||||
: record
|
||||
? (record['api-key'] ?? record['apiKey'] ?? record.key ?? record.Key)
|
||||
: '';
|
||||
const trimmed = String(value ?? '').trim();
|
||||
if (!trimmed || seen.has(trimmed)) return;
|
||||
seen.add(trimmed);
|
||||
keys.push(trimmed);
|
||||
@@ -151,9 +160,12 @@ export function SystemPage() {
|
||||
type: hasModels ? 'success' : 'warning',
|
||||
message: hasModels ? t('system_info.models_count', { count: list.length }) : t('system_info.models_empty')
|
||||
});
|
||||
} catch (err: any) {
|
||||
const message = `${t('system_info.models_error')}: ${err?.message || ''}`;
|
||||
setModelStatus({ type: 'error', message });
|
||||
} catch (err: unknown) {
|
||||
const message =
|
||||
err instanceof Error ? err.message : typeof err === 'string' ? err : '';
|
||||
const suffix = message ? `: ${message}` : '';
|
||||
const text = `${t('system_info.models_error')}${suffix}`;
|
||||
setModelStatus({ type: 'error', message: text });
|
||||
}
|
||||
};
|
||||
|
||||
@@ -219,9 +231,14 @@ export function SystemPage() {
|
||||
clearCache('request-log');
|
||||
showNotification(t('notification.request_log_updated'), 'success');
|
||||
setRequestLogModalOpen(false);
|
||||
} catch (error: any) {
|
||||
} catch (error: unknown) {
|
||||
const message =
|
||||
error instanceof Error ? error.message : typeof error === 'string' ? error : '';
|
||||
updateConfigValue('request-log', previous);
|
||||
showNotification(`${t('notification.update_failed')}: ${error?.message || ''}`, 'error');
|
||||
showNotification(
|
||||
`${t('notification.update_failed')}${message ? `: ${message}` : ''}`,
|
||||
'error'
|
||||
);
|
||||
} finally {
|
||||
setRequestLogSaving(false);
|
||||
}
|
||||
@@ -282,11 +299,11 @@ export function SystemPage() {
|
||||
<div className={styles.tileValue}>{buildTime}</div>
|
||||
</div>
|
||||
|
||||
<div className={styles.infoTile}>
|
||||
<div className={styles.tileLabel}>{t('connection.status')}</div>
|
||||
<div className={styles.tileValue}>{t(`common.${auth.connectionStatus}_status` as any)}</div>
|
||||
<div className={styles.tileSub}>{auth.apiBase || '-'}</div>
|
||||
</div>
|
||||
<div className={styles.infoTile}>
|
||||
<div className={styles.tileLabel}>{t('connection.status')}</div>
|
||||
<div className={styles.tileValue}>{t(`common.${auth.connectionStatus}_status`)}</div>
|
||||
<div className={styles.tileSub}>{auth.apiBase || '-'}</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className={styles.aboutActions}>
|
||||
|
||||
Reference in New Issue
Block a user