mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-06-16 13:34:04 +08:00
7096957b40
Add independent credential fields for usage query to support different query endpoints and authentication methods. Changes: - Add `apiKey` and `baseUrl` fields to UsageScript struct - Remove dependency on provider config credentials in query_usage - Update test_usage_script to accept independent credential parameters - Add credential input fields in UsageScriptModal based on template: * General: apiKey + baseUrl * NewAPI: baseUrl + accessToken + userId * Custom: no additional fields (full freedom) - Auto-clear irrelevant fields when switching templates - Add i18n text for "credentialsConfig" Benefits: - Query API can use different endpoint/key than provider config - Better separation of concerns - More flexible for various usage query scenarios
66 lines
1.6 KiB
TypeScript
66 lines
1.6 KiB
TypeScript
import { invoke } from "@tauri-apps/api/core";
|
|
import type { UsageResult } from "@/types";
|
|
import type { AppId } from "./types";
|
|
import i18n from "@/i18n";
|
|
|
|
export const usageApi = {
|
|
async query(providerId: string, appId: AppId): Promise<UsageResult> {
|
|
try {
|
|
return await invoke("queryProviderUsage", {
|
|
providerId: providerId,
|
|
app: appId,
|
|
});
|
|
} catch (error: unknown) {
|
|
// 提取错误消息:优先使用后端返回的错误信息
|
|
const message =
|
|
typeof error === "string"
|
|
? error
|
|
: error instanceof Error
|
|
? error.message
|
|
: "";
|
|
|
|
// 如果没有错误消息,使用国际化的默认提示
|
|
return {
|
|
success: false,
|
|
error: message || i18n.t("errors.usage_query_failed"),
|
|
};
|
|
}
|
|
},
|
|
|
|
async testScript(
|
|
providerId: string,
|
|
appId: AppId,
|
|
scriptCode: string,
|
|
timeout?: number,
|
|
apiKey?: string,
|
|
baseUrl?: string,
|
|
accessToken?: string,
|
|
userId?: string,
|
|
): Promise<UsageResult> {
|
|
try {
|
|
return await invoke("testUsageScript", {
|
|
providerId: providerId,
|
|
app: appId,
|
|
scriptCode: scriptCode,
|
|
timeout: timeout,
|
|
apiKey: apiKey,
|
|
baseUrl: baseUrl,
|
|
accessToken: accessToken,
|
|
userId: userId,
|
|
});
|
|
} catch (error: unknown) {
|
|
const message =
|
|
typeof error === "string"
|
|
? error
|
|
: error instanceof Error
|
|
? error.message
|
|
: "";
|
|
|
|
return {
|
|
success: false,
|
|
error: message || i18n.t("errors.usage_query_failed"),
|
|
};
|
|
}
|
|
},
|
|
};
|