mirror of
https://github.com/router-for-me/Cli-Proxy-API-Management-Center.git
synced 2026-06-16 21:03:58 +08:00
fix(authFiles): address PR review comments for custom headers
- Add missing translation keys for headers hint - Use dynamic translation key for headers textarea placeholder - Add validation and error throwing for invalid JSON headers - Prevent saving if headers JSON is invalid
This commit is contained in:
@@ -143,7 +143,7 @@ export function AuthFilesPrefixProxyEditorModal(props: AuthFilesPrefixProxyEdito
|
||||
<textarea
|
||||
className="input"
|
||||
value={editor.headersText}
|
||||
placeholder='{"X-Custom-Header": "value"}'
|
||||
placeholder={t('auth_files.headers_placeholder')}
|
||||
rows={4}
|
||||
disabled={disableControls || editor.saving || !editor.json}
|
||||
onChange={(e) => onChange('headersText', e.target.value)}
|
||||
|
||||
@@ -107,11 +107,16 @@ const buildPrefixProxyUpdatedText = (editor: PrefixProxyEditorState | null): str
|
||||
}
|
||||
|
||||
if (editor.headersText.trim()) {
|
||||
let parsedHeaders;
|
||||
try {
|
||||
next.headers = JSON.parse(editor.headersText);
|
||||
parsedHeaders = JSON.parse(editor.headersText);
|
||||
} catch {
|
||||
// ignore or handle error
|
||||
throw new Error('Invalid JSON format for Custom Headers. Must be an object.');
|
||||
}
|
||||
if (!parsedHeaders || typeof parsedHeaders !== 'object' || Array.isArray(parsedHeaders)) {
|
||||
throw new Error('Invalid JSON format for Custom Headers. Must be an object.');
|
||||
}
|
||||
next.headers = parsedHeaders;
|
||||
} else {
|
||||
delete next.headers;
|
||||
}
|
||||
@@ -130,11 +135,17 @@ export function useAuthFilesPrefixProxyEditor(
|
||||
|
||||
const [prefixProxyEditor, setPrefixProxyEditor] = useState<PrefixProxyEditorState | null>(null);
|
||||
|
||||
const prefixProxyUpdatedText = buildPrefixProxyUpdatedText(prefixProxyEditor);
|
||||
let prefixProxyUpdatedText = '';
|
||||
try {
|
||||
prefixProxyUpdatedText = buildPrefixProxyUpdatedText(prefixProxyEditor);
|
||||
} catch {
|
||||
// Catch JSON parsing errors during render so the UI doesn't crash.
|
||||
}
|
||||
|
||||
const prefixProxyDirty =
|
||||
Boolean(prefixProxyEditor?.json) &&
|
||||
Boolean(prefixProxyEditor?.originalText) &&
|
||||
prefixProxyUpdatedText !== prefixProxyEditor?.originalText;
|
||||
(prefixProxyUpdatedText === '' || prefixProxyUpdatedText !== prefixProxyEditor?.originalText);
|
||||
|
||||
const closePrefixProxyEditor = () => {
|
||||
setPrefixProxyEditor(null);
|
||||
@@ -285,7 +296,15 @@ export function useAuthFilesPrefixProxyEditor(
|
||||
if (!prefixProxyDirty) return;
|
||||
|
||||
const name = prefixProxyEditor.fileName;
|
||||
const payload = prefixProxyUpdatedText;
|
||||
let payload = '';
|
||||
try {
|
||||
payload = buildPrefixProxyUpdatedText(prefixProxyEditor);
|
||||
} catch (err: unknown) {
|
||||
const errorMessage = err instanceof Error ? err.message : 'Invalid format';
|
||||
showNotification(errorMessage, 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
const fileSize = new Blob([payload]).size;
|
||||
if (fileSize > MAX_AUTH_FILE_SIZE) {
|
||||
showNotification(
|
||||
|
||||
@@ -599,6 +599,7 @@
|
||||
"note_display": "Note",
|
||||
"headers_label": "Custom Headers (headers)",
|
||||
"headers_placeholder": "{\n \"Header-Name\": \"value\"\n}",
|
||||
"headers_hint": "Enter custom HTTP headers as a JSON object, e.g., {\"X-My-Header\": \"value\"}",
|
||||
"prefix_proxy_invalid_json": "This auth file is not a JSON object, so fields cannot be edited.",
|
||||
"prefix_proxy_saved_success": "Updated auth file \"{{name}}\" successfully",
|
||||
"quota_refresh_success": "Quota refreshed for \"{{name}}\"",
|
||||
|
||||
@@ -599,6 +599,7 @@
|
||||
"note_display": "备注",
|
||||
"headers_label": "自定义请求头(headers)",
|
||||
"headers_placeholder": "{\n \"Header-Name\": \"value\"\n}",
|
||||
"headers_hint": "以 JSON 对象格式输入自定义 HTTP 请求头,例如:{\"X-My-Header\": \"value\"}",
|
||||
"prefix_proxy_invalid_json": "该认证文件不是 JSON 对象,无法编辑字段。",
|
||||
"prefix_proxy_saved_success": "已更新认证文件 \"{{name}}\"",
|
||||
"quota_refresh_success": "已刷新 \"{{name}}\" 的额度",
|
||||
|
||||
Reference in New Issue
Block a user