refactor: migrate all Tauri commands to camelCase parameters
This commit addresses parameter naming inconsistencies caused by Tauri v2's requirement for camelCase parameter names in IPC commands. Backend changes (Rust): - Updated all command parameters from snake_case to camelCase - Commands affected: * provider.rs: providerId (×4), timeoutSecs * import_export.rs: filePath (×2), defaultName * config.rs: defaultPath - Added #[allow(non_snake_case)] attributes for camelCase parameters - Removed unused QueryUsageParams struct Frontend changes (TypeScript): - Removed redundant snake_case parameters from all invoke() calls - Updated API files: * usage.ts: removed debug logs, unified to providerId * vscode.ts: updated 8 functions (providerId, timeoutSecs, filePath, defaultName) * settings.ts: updated 4 functions (defaultPath, filePath, defaultName) - Ensured all parameters now use camelCase exclusively Test updates: - Updated MSW handlers to accept both old and new parameter formats during transition - Added i18n mock compatibility for tests Root cause: The issue stemmed from Tauri v2 strictly requiring camelCase for command parameters, while the codebase was using snake_case. This caused parameters like 'provider_id' to not be recognized by the backend, resulting in "missing providerId parameter" errors. BREAKING CHANGE: All Tauri command invocations now require camelCase parameters. Any external tools or scripts calling these commands must be updated accordingly. Fixes: Usage query always failing with "missing providerId" error Fixes: Custom endpoint management not receiving provider ID Fixes: Import/export dialogs not respecting default paths
This commit is contained in:
@@ -73,9 +73,10 @@ pub async fn open_config_folder(handle: AppHandle, app: String) -> Result<bool,
|
|||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn pick_directory(
|
pub async fn pick_directory(
|
||||||
app: AppHandle,
|
app: AppHandle,
|
||||||
default_path: Option<String>,
|
#[allow(non_snake_case)]
|
||||||
|
defaultPath: Option<String>,
|
||||||
) -> Result<Option<String>, String> {
|
) -> Result<Option<String>, String> {
|
||||||
let initial = default_path
|
let initial = defaultPath
|
||||||
.map(|p| p.trim().to_string())
|
.map(|p| p.trim().to_string())
|
||||||
.filter(|p| !p.is_empty());
|
.filter(|p| !p.is_empty());
|
||||||
|
|
||||||
|
|||||||
@@ -11,14 +11,17 @@ use crate::store::AppState;
|
|||||||
|
|
||||||
/// 导出配置文件
|
/// 导出配置文件
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn export_config_to_file(file_path: String) -> Result<Value, String> {
|
pub async fn export_config_to_file(
|
||||||
|
#[allow(non_snake_case)]
|
||||||
|
filePath: String
|
||||||
|
) -> Result<Value, String> {
|
||||||
tauri::async_runtime::spawn_blocking(move || {
|
tauri::async_runtime::spawn_blocking(move || {
|
||||||
let target_path = PathBuf::from(&file_path);
|
let target_path = PathBuf::from(&filePath);
|
||||||
ConfigService::export_config_to_path(&target_path)?;
|
ConfigService::export_config_to_path(&target_path)?;
|
||||||
Ok::<_, AppError>(json!({
|
Ok::<_, AppError>(json!({
|
||||||
"success": true,
|
"success": true,
|
||||||
"message": "Configuration exported successfully",
|
"message": "Configuration exported successfully",
|
||||||
"filePath": file_path
|
"filePath": filePath
|
||||||
}))
|
}))
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
@@ -29,11 +32,12 @@ pub async fn export_config_to_file(file_path: String) -> Result<Value, String> {
|
|||||||
/// 从文件导入配置
|
/// 从文件导入配置
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn import_config_from_file(
|
pub async fn import_config_from_file(
|
||||||
file_path: String,
|
#[allow(non_snake_case)]
|
||||||
|
filePath: String,
|
||||||
state: State<'_, AppState>,
|
state: State<'_, AppState>,
|
||||||
) -> Result<Value, String> {
|
) -> Result<Value, String> {
|
||||||
let (new_config, backup_id) = tauri::async_runtime::spawn_blocking(move || {
|
let (new_config, backup_id) = tauri::async_runtime::spawn_blocking(move || {
|
||||||
let path_buf = PathBuf::from(&file_path);
|
let path_buf = PathBuf::from(&filePath);
|
||||||
ConfigService::load_config_for_import(&path_buf)
|
ConfigService::load_config_for_import(&path_buf)
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
@@ -77,13 +81,14 @@ pub async fn sync_current_providers_live(state: State<'_, AppState>) -> Result<V
|
|||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn save_file_dialog<R: tauri::Runtime>(
|
pub async fn save_file_dialog<R: tauri::Runtime>(
|
||||||
app: tauri::AppHandle<R>,
|
app: tauri::AppHandle<R>,
|
||||||
default_name: String,
|
#[allow(non_snake_case)]
|
||||||
|
defaultName: String,
|
||||||
) -> Result<Option<String>, String> {
|
) -> Result<Option<String>, String> {
|
||||||
let dialog = app.dialog();
|
let dialog = app.dialog();
|
||||||
let result = dialog
|
let result = dialog
|
||||||
.file()
|
.file()
|
||||||
.add_filter("JSON", &["json"])
|
.add_filter("JSON", &["json"])
|
||||||
.set_file_name(&default_name)
|
.set_file_name(&defaultName)
|
||||||
.blocking_save_file();
|
.blocking_save_file();
|
||||||
|
|
||||||
Ok(result.map(|p| p.to_string()))
|
Ok(result.map(|p| p.to_string()))
|
||||||
|
|||||||
@@ -116,12 +116,12 @@ pub fn import_default_config(state: State<'_, AppState>, app: String) -> Result<
|
|||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn query_provider_usage(
|
pub async fn query_provider_usage(
|
||||||
state: State<'_, AppState>,
|
state: State<'_, AppState>,
|
||||||
provider_id: Option<String>,
|
#[allow(non_snake_case)]
|
||||||
|
providerId: String, // 使用 camelCase 匹配前端
|
||||||
app: String,
|
app: String,
|
||||||
) -> Result<crate::provider::UsageResult, String> {
|
) -> Result<crate::provider::UsageResult, String> {
|
||||||
let provider_id = provider_id.ok_or_else(|| missing_param("providerId"))?;
|
|
||||||
let app_type = AppType::from_str(&app).map_err(|e| e.to_string())?;
|
let app_type = AppType::from_str(&app).map_err(|e| e.to_string())?;
|
||||||
ProviderService::query_usage(state.inner(), app_type, &provider_id)
|
ProviderService::query_usage(state.inner(), app_type, &providerId)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| e.to_string())
|
.map_err(|e| e.to_string())
|
||||||
}
|
}
|
||||||
@@ -137,9 +137,10 @@ pub fn read_live_provider_settings(app: String) -> Result<serde_json::Value, Str
|
|||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn test_api_endpoints(
|
pub async fn test_api_endpoints(
|
||||||
urls: Vec<String>,
|
urls: Vec<String>,
|
||||||
timeout_secs: Option<u64>,
|
#[allow(non_snake_case)]
|
||||||
|
timeoutSecs: Option<u64>,
|
||||||
) -> Result<Vec<EndpointLatency>, String> {
|
) -> Result<Vec<EndpointLatency>, String> {
|
||||||
SpeedtestService::test_endpoints(urls, timeout_secs)
|
SpeedtestService::test_endpoints(urls, timeoutSecs)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| e.to_string())
|
.map_err(|e| e.to_string())
|
||||||
}
|
}
|
||||||
@@ -149,11 +150,11 @@ pub async fn test_api_endpoints(
|
|||||||
pub fn get_custom_endpoints(
|
pub fn get_custom_endpoints(
|
||||||
state: State<'_, AppState>,
|
state: State<'_, AppState>,
|
||||||
app: String,
|
app: String,
|
||||||
provider_id: Option<String>,
|
#[allow(non_snake_case)]
|
||||||
|
providerId: String,
|
||||||
) -> Result<Vec<crate::settings::CustomEndpoint>, String> {
|
) -> Result<Vec<crate::settings::CustomEndpoint>, String> {
|
||||||
let app_type = AppType::from_str(&app).map_err(|e| e.to_string())?;
|
let app_type = AppType::from_str(&app).map_err(|e| e.to_string())?;
|
||||||
let provider_id = provider_id.ok_or_else(|| missing_param("providerId"))?;
|
ProviderService::get_custom_endpoints(state.inner(), app_type, &providerId)
|
||||||
ProviderService::get_custom_endpoints(state.inner(), app_type, &provider_id)
|
|
||||||
.map_err(|e| e.to_string())
|
.map_err(|e| e.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,12 +163,12 @@ pub fn get_custom_endpoints(
|
|||||||
pub fn add_custom_endpoint(
|
pub fn add_custom_endpoint(
|
||||||
state: State<'_, AppState>,
|
state: State<'_, AppState>,
|
||||||
app: String,
|
app: String,
|
||||||
provider_id: Option<String>,
|
#[allow(non_snake_case)]
|
||||||
|
providerId: String,
|
||||||
url: String,
|
url: String,
|
||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
let app_type = AppType::from_str(&app).map_err(|e| e.to_string())?;
|
let app_type = AppType::from_str(&app).map_err(|e| e.to_string())?;
|
||||||
let provider_id = provider_id.ok_or_else(|| missing_param("providerId"))?;
|
ProviderService::add_custom_endpoint(state.inner(), app_type, &providerId, url)
|
||||||
ProviderService::add_custom_endpoint(state.inner(), app_type, &provider_id, url)
|
|
||||||
.map_err(|e| e.to_string())
|
.map_err(|e| e.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -176,12 +177,12 @@ pub fn add_custom_endpoint(
|
|||||||
pub fn remove_custom_endpoint(
|
pub fn remove_custom_endpoint(
|
||||||
state: State<'_, AppState>,
|
state: State<'_, AppState>,
|
||||||
app: String,
|
app: String,
|
||||||
provider_id: Option<String>,
|
#[allow(non_snake_case)]
|
||||||
|
providerId: String,
|
||||||
url: String,
|
url: String,
|
||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
let app_type = AppType::from_str(&app).map_err(|e| e.to_string())?;
|
let app_type = AppType::from_str(&app).map_err(|e| e.to_string())?;
|
||||||
let provider_id = provider_id.ok_or_else(|| missing_param("providerId"))?;
|
ProviderService::remove_custom_endpoint(state.inner(), app_type, &providerId, url)
|
||||||
ProviderService::remove_custom_endpoint(state.inner(), app_type, &provider_id, url)
|
|
||||||
.map_err(|e| e.to_string())
|
.map_err(|e| e.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -190,12 +191,12 @@ pub fn remove_custom_endpoint(
|
|||||||
pub fn update_endpoint_last_used(
|
pub fn update_endpoint_last_used(
|
||||||
state: State<'_, AppState>,
|
state: State<'_, AppState>,
|
||||||
app: String,
|
app: String,
|
||||||
provider_id: Option<String>,
|
#[allow(non_snake_case)]
|
||||||
|
providerId: String,
|
||||||
url: String,
|
url: String,
|
||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
let app_type = AppType::from_str(&app).map_err(|e| e.to_string())?;
|
let app_type = AppType::from_str(&app).map_err(|e| e.to_string())?;
|
||||||
let provider_id = provider_id.ok_or_else(|| missing_param("providerId"))?;
|
ProviderService::update_endpoint_last_used(state.inner(), app_type, &providerId, url)
|
||||||
ProviderService::update_endpoint_last_used(state.inner(), app_type, &provider_id, url)
|
|
||||||
.map_err(|e| e.to_string())
|
.map_err(|e| e.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ export const settingsApi = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
async selectConfigDirectory(defaultPath?: string): Promise<string | null> {
|
async selectConfigDirectory(defaultPath?: string): Promise<string | null> {
|
||||||
return await invoke("pick_directory", { default_path: defaultPath });
|
return await invoke("pick_directory", { defaultPath });
|
||||||
},
|
},
|
||||||
|
|
||||||
async getClaudeCodeConfigPath(): Promise<string> {
|
async getClaudeCodeConfigPath(): Promise<string> {
|
||||||
@@ -70,10 +70,7 @@ export const settingsApi = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
async saveFileDialog(defaultName: string): Promise<string | null> {
|
async saveFileDialog(defaultName: string): Promise<string | null> {
|
||||||
return await invoke("save_file_dialog", {
|
return await invoke("save_file_dialog", { defaultName });
|
||||||
default_name: defaultName,
|
|
||||||
defaultName,
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
async openFileDialog(): Promise<string | null> {
|
async openFileDialog(): Promise<string | null> {
|
||||||
@@ -81,17 +78,11 @@ export const settingsApi = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
async exportConfigToFile(filePath: string): Promise<ConfigTransferResult> {
|
async exportConfigToFile(filePath: string): Promise<ConfigTransferResult> {
|
||||||
return await invoke("export_config_to_file", {
|
return await invoke("export_config_to_file", { filePath });
|
||||||
file_path: filePath,
|
|
||||||
filePath,
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
async importConfigFromFile(filePath: string): Promise<ConfigTransferResult> {
|
async importConfigFromFile(filePath: string): Promise<ConfigTransferResult> {
|
||||||
return await invoke("import_config_from_file", {
|
return await invoke("import_config_from_file", { filePath });
|
||||||
file_path: filePath,
|
|
||||||
filePath,
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
async syncCurrentProvidersLive(): Promise<void> {
|
async syncCurrentProvidersLive(): Promise<void> {
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ export const usageApi = {
|
|||||||
async query(providerId: string, appId: AppId): Promise<UsageResult> {
|
async query(providerId: string, appId: AppId): Promise<UsageResult> {
|
||||||
try {
|
try {
|
||||||
return await invoke("query_provider_usage", {
|
return await invoke("query_provider_usage", {
|
||||||
provider_id: providerId,
|
providerId: providerId,
|
||||||
app: appId,
|
app: appId,
|
||||||
});
|
});
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ export const vscodeApi = {
|
|||||||
): Promise<EndpointLatencyResult[]> {
|
): Promise<EndpointLatencyResult[]> {
|
||||||
return await invoke("test_api_endpoints", {
|
return await invoke("test_api_endpoints", {
|
||||||
urls,
|
urls,
|
||||||
timeout_secs: options?.timeoutSecs,
|
timeoutSecs: options?.timeoutSecs,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -30,7 +30,7 @@ export const vscodeApi = {
|
|||||||
): Promise<CustomEndpoint[]> {
|
): Promise<CustomEndpoint[]> {
|
||||||
return await invoke("get_custom_endpoints", {
|
return await invoke("get_custom_endpoints", {
|
||||||
app: appId,
|
app: appId,
|
||||||
provider_id: providerId,
|
providerId: providerId,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -41,7 +41,7 @@ export const vscodeApi = {
|
|||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
await invoke("add_custom_endpoint", {
|
await invoke("add_custom_endpoint", {
|
||||||
app: appId,
|
app: appId,
|
||||||
provider_id: providerId,
|
providerId: providerId,
|
||||||
url,
|
url,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
@@ -53,7 +53,7 @@ export const vscodeApi = {
|
|||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
await invoke("remove_custom_endpoint", {
|
await invoke("remove_custom_endpoint", {
|
||||||
app: appId,
|
app: appId,
|
||||||
provider_id: providerId,
|
providerId: providerId,
|
||||||
url,
|
url,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
@@ -65,28 +65,25 @@ export const vscodeApi = {
|
|||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
await invoke("update_endpoint_last_used", {
|
await invoke("update_endpoint_last_used", {
|
||||||
app: appId,
|
app: appId,
|
||||||
provider_id: providerId,
|
providerId: providerId,
|
||||||
url,
|
url,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
async exportConfigToFile(filePath: string) {
|
async exportConfigToFile(filePath: string) {
|
||||||
return await invoke("export_config_to_file", {
|
return await invoke("export_config_to_file", {
|
||||||
file_path: filePath,
|
|
||||||
filePath,
|
filePath,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
async importConfigFromFile(filePath: string) {
|
async importConfigFromFile(filePath: string) {
|
||||||
return await invoke("import_config_from_file", {
|
return await invoke("import_config_from_file", {
|
||||||
file_path: filePath,
|
|
||||||
filePath,
|
filePath,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
async saveFileDialog(defaultName: string): Promise<string | null> {
|
async saveFileDialog(defaultName: string): Promise<string | null> {
|
||||||
return await invoke("save_file_dialog", {
|
return await invoke("save_file_dialog", {
|
||||||
default_name: defaultName,
|
|
||||||
defaultName,
|
defaultName,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ vi.mock("react-i18next", () => ({
|
|||||||
t: (key: string, params?: Record<string, unknown>) =>
|
t: (key: string, params?: Record<string, unknown>) =>
|
||||||
params ? `${key}:${JSON.stringify(params)}` : key,
|
params ? `${key}:${JSON.stringify(params)}` : key,
|
||||||
}),
|
}),
|
||||||
|
// 提供 initReactI18next 以兼容 i18n 初始化路径
|
||||||
|
initReactI18next: { type: "3rdParty", init: () => {} },
|
||||||
}));
|
}));
|
||||||
|
|
||||||
vi.mock("@/config/mcpPresets", () => ({
|
vi.mock("@/config/mcpPresets", () => ({
|
||||||
|
|||||||
@@ -169,13 +169,21 @@ export const handlers = [
|
|||||||
http.post(`${TAURI_ENDPOINT}/is_portable_mode`, () => success(false)),
|
http.post(`${TAURI_ENDPOINT}/is_portable_mode`, () => success(false)),
|
||||||
|
|
||||||
http.post(`${TAURI_ENDPOINT}/select_config_directory`, async ({ request }) => {
|
http.post(`${TAURI_ENDPOINT}/select_config_directory`, async ({ request }) => {
|
||||||
const { default_path } = await withJson<{ default_path?: string }>(request);
|
const { defaultPath, default_path } = await withJson<{
|
||||||
return success(default_path ? `${default_path}/picked` : "/mock/selected-dir");
|
defaultPath?: string;
|
||||||
|
default_path?: string;
|
||||||
|
}>(request);
|
||||||
|
const initial = defaultPath ?? default_path;
|
||||||
|
return success(initial ? `${initial}/picked` : "/mock/selected-dir");
|
||||||
}),
|
}),
|
||||||
|
|
||||||
http.post(`${TAURI_ENDPOINT}/pick_directory`, async ({ request }) => {
|
http.post(`${TAURI_ENDPOINT}/pick_directory`, async ({ request }) => {
|
||||||
const { default_path } = await withJson<{ default_path?: string }>(request);
|
const { defaultPath, default_path } = await withJson<{
|
||||||
return success(default_path ? `${default_path}/picked` : "/mock/selected-dir");
|
defaultPath?: string;
|
||||||
|
default_path?: string;
|
||||||
|
}>(request);
|
||||||
|
const initial = defaultPath ?? default_path;
|
||||||
|
return success(initial ? `${initial}/picked` : "/mock/selected-dir");
|
||||||
}),
|
}),
|
||||||
|
|
||||||
http.post(`${TAURI_ENDPOINT}/open_file_dialog`, () =>
|
http.post(`${TAURI_ENDPOINT}/open_file_dialog`, () =>
|
||||||
|
|||||||
Reference in New Issue
Block a user