refactor(api): unify Tauri command app param as app_type with backward-compatible app/appType; update front-end invocations accordingly

This commit is contained in:
Jason
2025-08-31 16:43:33 +08:00
parent 30fe800ebe
commit 74babf9730
2 changed files with 49 additions and 14 deletions

View File

@@ -12,9 +12,14 @@ use crate::store::AppState;
#[tauri::command]
pub async fn get_providers(
state: State<'_, AppState>,
app_type: Option<AppType>,
app: Option<String>,
appType: Option<String>,
) -> Result<HashMap<String, Provider>, String> {
let app_type = app.as_deref().map(|s| s.into()).unwrap_or(AppType::Claude);
let app_type = app_type
.or_else(|| app.as_deref().map(|s| s.into()))
.or_else(|| appType.as_deref().map(|s| s.into()))
.unwrap_or(AppType::Claude);
let config = state
.config
@@ -32,9 +37,14 @@ pub async fn get_providers(
#[tauri::command]
pub async fn get_current_provider(
state: State<'_, AppState>,
app_type: Option<AppType>,
app: Option<String>,
appType: Option<String>,
) -> Result<String, String> {
let app_type = app.as_deref().map(|s| s.into()).unwrap_or(AppType::Claude);
let app_type = app_type
.or_else(|| app.as_deref().map(|s| s.into()))
.or_else(|| appType.as_deref().map(|s| s.into()))
.unwrap_or(AppType::Claude);
let config = state
.config
@@ -52,10 +62,15 @@ pub async fn get_current_provider(
#[tauri::command]
pub async fn add_provider(
state: State<'_, AppState>,
app_type: Option<AppType>,
app: Option<String>,
appType: Option<String>,
provider: Provider,
) -> Result<bool, String> {
let app_type = app.as_deref().map(|s| s.into()).unwrap_or(AppType::Claude);
let app_type = app_type
.or_else(|| app.as_deref().map(|s| s.into()))
.or_else(|| appType.as_deref().map(|s| s.into()))
.unwrap_or(AppType::Claude);
let mut config = state
.config
@@ -97,10 +112,15 @@ pub async fn add_provider(
#[tauri::command]
pub async fn update_provider(
state: State<'_, AppState>,
app_type: Option<AppType>,
app: Option<String>,
appType: Option<String>,
provider: Provider,
) -> Result<bool, String> {
let app_type = app.as_deref().map(|s| s.into()).unwrap_or(AppType::Claude);
let app_type = app_type
.or_else(|| app.as_deref().map(|s| s.into()))
.or_else(|| appType.as_deref().map(|s| s.into()))
.unwrap_or(AppType::Claude);
let mut config = state
.config
@@ -164,10 +184,15 @@ pub async fn update_provider(
#[tauri::command]
pub async fn delete_provider(
state: State<'_, AppState>,
app_type: Option<AppType>,
app: Option<String>,
appType: Option<String>,
id: String,
) -> Result<bool, String> {
let app_type = app.as_deref().map(|s| s.into()).unwrap_or(AppType::Claude);
let app_type = app_type
.or_else(|| app.as_deref().map(|s| s.into()))
.or_else(|| appType.as_deref().map(|s| s.into()))
.unwrap_or(AppType::Claude);
let mut config = state
.config
@@ -216,10 +241,15 @@ pub async fn delete_provider(
#[tauri::command]
pub async fn switch_provider(
state: State<'_, AppState>,
app_type: Option<AppType>,
app: Option<String>,
appType: Option<String>,
id: String,
) -> Result<bool, String> {
let app_type = app.as_deref().map(|s| s.into()).unwrap_or(AppType::Claude);
let app_type = app_type
.or_else(|| app.as_deref().map(|s| s.into()))
.or_else(|| appType.as_deref().map(|s| s.into()))
.unwrap_or(AppType::Claude);
let mut config = state
.config
@@ -304,9 +334,14 @@ pub async fn switch_provider(
#[tauri::command]
pub async fn import_default_config(
state: State<'_, AppState>,
app_type: Option<AppType>,
app: Option<String>,
appType: Option<String>,
) -> Result<bool, String> {
let app_type = app.as_deref().map(|s| s.into()).unwrap_or(AppType::Claude);
let app_type = app_type
.or_else(|| app.as_deref().map(|s| s.into()))
.or_else(|| appType.as_deref().map(|s| s.into()))
.unwrap_or(AppType::Claude);
// 若已存在 default 供应商,则直接返回,避免重复导入
{

View File

@@ -22,7 +22,7 @@ export const tauriAPI = {
// 获取所有供应商
getProviders: async (app?: AppType): Promise<Record<string, Provider>> => {
try {
return await invoke("get_providers", { app });
return await invoke("get_providers", { app_type: app, app });
} catch (error) {
console.error("获取供应商列表失败:", error);
return {};
@@ -32,7 +32,7 @@ export const tauriAPI = {
// 获取当前供应商ID
getCurrentProvider: async (app?: AppType): Promise<string> => {
try {
return await invoke("get_current_provider", { app });
return await invoke("get_current_provider", { app_type: app, app });
} catch (error) {
console.error("获取当前供应商失败:", error);
return "";
@@ -42,7 +42,7 @@ export const tauriAPI = {
// 添加供应商
addProvider: async (provider: Provider, app?: AppType): Promise<boolean> => {
try {
return await invoke("add_provider", { provider, app });
return await invoke("add_provider", { provider, app_type: app, app });
} catch (error) {
console.error("添加供应商失败:", error);
throw error;
@@ -55,7 +55,7 @@ export const tauriAPI = {
app?: AppType,
): Promise<boolean> => {
try {
return await invoke("update_provider", { provider, app });
return await invoke("update_provider", { provider, app_type: app, app });
} catch (error) {
console.error("更新供应商失败:", error);
throw error;
@@ -65,7 +65,7 @@ export const tauriAPI = {
// 删除供应商
deleteProvider: async (id: string, app?: AppType): Promise<boolean> => {
try {
return await invoke("delete_provider", { id, app });
return await invoke("delete_provider", { id, app_type: app, app });
} catch (error) {
console.error("删除供应商失败:", error);
throw error;
@@ -78,7 +78,7 @@ export const tauriAPI = {
app?: AppType,
): Promise<boolean> => {
try {
return await invoke("switch_provider", { id: providerId, app });
return await invoke("switch_provider", { id: providerId, app_type: app, app });
} catch (error) {
console.error("切换供应商失败:", error);
return false;
@@ -90,7 +90,7 @@ export const tauriAPI = {
app?: AppType,
): Promise<ImportResult> => {
try {
const success = await invoke<boolean>("import_default_config", { app });
const success = await invoke<boolean>("import_default_config", { app_type: app, app });
return {
success,
message: success ? "成功导入默认配置" : "导入失败",