The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
Change credentials to use react-query
yuneng-jiang7519a3e30b64
diff --git a/ui/litellm-dashboard/src/app/(dashboard)/hooks/credentials/useCredentials.ts b/ui/litellm-dashboard/src/app/(dashboard)/hooks/credentials/useCredentials.tsnew file mode 100644index 00000000000..aa0a6c2c9fb--- /dev/null+++ b/ui/litellm-dashboard/src/app/(dashboard)/hooks/credentials/useCredentials.ts@@ -0,0 +1,13 @@+import { credentialListCall, CredentialsResponse } from "@/components/networking";+import { useQuery } from "@tanstack/react-query";+import { createQueryKeys } from "../common/queryKeysFactory";++const credentialsKeys = createQueryKeys("credentials");++export const useCredentials = (accessToken: string | null) => {+ return useQuery<CredentialsResponse>({+ queryKey: credentialsKeys.list({}),+ queryFn: async () => await credentialListCall(accessToken!),+ enabled: Boolean(accessToken),+ });+};diff --git a/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/ModelsAndEndpointsView.tsx b/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/ModelsAndEndpointsView.tsxindex 4f33ba6585d..4cd1f549e69 100644--- a/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/ModelsAndEndpointsView.tsx+++ b/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/ModelsAndEndpointsView.tsx@@ -1,7 +1,7 @@import React, { useState, useEffect, useRef } from "react";import { Text, Grid, Col } from "@tremor/react";import { useQueryClient } from "@tanstack/react-query";-import { CredentialItem, credentialListCall, CredentialsResponse } from "@/components/networking";+import { CredentialItem } from "@/components/networking";import { handleAddModelSubmit } from "@/components/add_model/handle_add_model_submit";@@ -23,6 +23,7 @@ import {allEndUsersCall,} from "@/components/networking";import { useModelsInfo } from "@/app/(dashboard)/hooks/models/useModels";+import { useCredentials } from "@/app/(dashboard)/hooks/credentials/useCredentials";import { Form } from "antd";import { Typography } from "antd";import { RefreshIcon } from "@heroicons/react/outline";@@ -134,8 +135,6 @@ const ModelsAndEndpointsView: React.FC<ModelDashboardProps> = ({const [allEndUsers, setAllEndUsers] = useState<any[]>([]);- const [credentialsList, setCredentialsList] = useState<CredentialItem[]>([]);-// Model Group Alias stateconst [modelGroupAlias, setModelGroupAlias] = useState<{ [key: string]: string }>({});@@ -160,21 +159,14 @@ const ModelsAndEndpointsView: React.FC<ModelDashboardProps> = ({isLoading: isLoadingModels,refetch: refetchModels,} = useModelsInfo(accessToken, userID, userRole);+ const { data: credentialsResponse } = useCredentials(accessToken);+ const credentialsList = credentialsResponse?.credentials || [];const setProviderModelsFn = (provider: Providers) => {const _providerModels = getProviderModels(provider, modelMap);setProviderModels(_providerModels);};- const fetchCredentials = async (accessToken: string) => {- try {- const response: CredentialsResponse = await credentialListCall(accessToken);- setCredentialsList(response.credentials);- } catch (error) {- console.error("Error fetching credentials:", error);- }- };-useEffect(() => {const handleClickOutside = (event: MouseEvent) => {if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) {@@ -686,12 +678,7 @@ const ModelsAndEndpointsView: React.FC<ModelDashboardProps> = ({/></TabPanel><TabPanel>- <CredentialsPanel- accessToken={accessToken}- uploadProps={uploadProps}- credentialList={credentialsList}- fetchCredentials={fetchCredentials}- />+ <CredentialsPanel accessToken={accessToken} uploadProps={uploadProps} /></TabPanel><TabPanel><PassThroughSettingsdiff --git a/ui/litellm-dashboard/src/components/model_add/credentials.test.tsx b/ui/litellm-dashboard/src/components/model_add/credentials.test.tsxindex fab74a39d2e..2504b2a9789 100644--- a/ui/litellm-dashboard/src/components/model_add/credentials.test.tsx+++ b/ui/litellm-dashboard/src/components/model_add/credentials.test.tsx@@ -1,46 +1,51 @@import { CredentialItem } from "@/components/networking";-import { render, screen, waitFor } from "@testing-library/react";+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";+import { act, fireEvent, render, screen, waitFor } from "@testing-library/react";import { UploadProps } from "antd/es/upload";import { describe, expect, it, vi } from "vitest";import CredentialsPanel from "./credentials";const DEFAULT_UPLOAD_PROPS = {} as UploadProps;-describe("CredentialsPanel", () => {- it("should render", () => {- const fetchCredentials = vi.fn(() => Promise.resolve());+const mockUseAuthorized = vi.fn();+const mockUseCredentials = vi.fn();- render(- <CredentialsPanel- accessToken="test-token"- uploadProps={DEFAULT_UPLOAD_PROPS}- credentialList={[]}- fetchCredentials={fetchCredentials}- />,- );+vi.mock("@/app/(dashboard)/hooks/useAuthorized", () => ({+ default: () => mockUseAuthorized(),+}));- expect(screen.getByRole("button", { name: /add credential/i })).toBeInTheDocument();+vi.mock("@/app/(dashboard)/hooks/credentials/useCredentials", () => ({+ useCredentials: () => mockUseCredentials(),+}));++const createQueryClient = () =>+ new QueryClient({+ defaultOptions: {+ queries: {+ retry: false,+ gcTime: 0,+ },+ },});- it("should call fetchCredentials when accessToken exists", async () => {- const fetchCredentials = vi.fn(() => Promise.resolve());+describe("CredentialsPanel", () => {+ it("should render", () => {+ mockUseAuthorized.mockReturnValue({ accessToken: "test-token" });+ mockUseCredentials.mockReturnValue({+ data: { credentials: [] },+ refetch: vi.fn(),+ });render(- <CredentialsPanel- accessToken="test-token"- uploadProps={DEFAULT_UPLOAD_PROPS}- credentialList={[]}- fetchCredentials={fetchCredentials}- />,+ <QueryClientProvider client={createQueryClient()}>+ <CredentialsPanel uploadProps={DEFAULT_UPLOAD_PROPS} />+ </QueryClientProvider>,);- await waitFor(() => {- expect(fetchCredentials).toHaveBeenCalledWith("test-token");- });+ expect(screen.getByRole("button", { name: /add credential/i })).toBeInTheDocument();});it("should display provided credentials", () => {- const fetchCredentials = vi.fn(() => Promise.resolve());const credentials: CredentialItem[] = [{credential_name: "openai-key",@@ -49,30 +54,58 @@ describe("CredentialsPanel", () => {},];+ mockUseAuthorized.mockReturnValue({ accessToken: "test-token" });+ mockUseCredentials.mockReturnValue({+ data: { credentials },+ refetch: vi.fn(),+ });+render(- <CredentialsPanel- accessToken="another-token"- uploadProps={DEFAULT_UPLOAD_PROPS}- credentialList={credentials}- fetchCredentials={fetchCredentials}- />,+ <QueryClientProvider client={createQueryClient()}>+ <CredentialsPanel uploadProps={DEFAULT_UPLOAD_PROPS} />+ </QueryClientProvider>,);expect(screen.getByText("openai-key")).toBeInTheDocument();});it("should display empty state when no credentials are provided", () => {- const fetchCredentials = vi.fn(() => Promise.resolve());+ mockUseAuthorized.mockReturnValue({ accessToken: "test-token" });+ mockUseCredentials.mockReturnValue({+ data: { credentials: [] },+ refetch: vi.fn(),+ });render(- <CredentialsPanel- accessToken="test-token"- uploadProps={DEFAULT_UPLOAD_PROPS}- credentialList={[]}- fetchCredentials={fetchCredentials}- />,+ <QueryClientProvider client={createQueryClient()}>+ <CredentialsPanel uploadProps={DEFAULT_UPLOAD_PROPS} />+ </QueryClientProvider>,);expect(screen.getByText("No credentials configured")).toBeInTheDocument();});++ it("should open add modal when add button is clicked", async () => {+ mockUseAuthorized.mockReturnValue({ accessToken: "test-token" });+ mockUseCredentials.mockReturnValue({+ data: { credentials: [] },+ refetch: vi.fn(),+ });++ render(+ <QueryClientProvider client={createQueryClient()}>+ <CredentialsPanel uploadProps={DEFAULT_UPLOAD_PROPS} />+ </QueryClientProvider>,+ );++ const addButton = screen.getByRole("button", { name: /add credential/i });++ act(() => {+ fireEvent.click(addButton);+ });++ await waitFor(() => {+ expect(screen.getByText("Add New Credential")).toBeInTheDocument();+ });+ });});diff --git a/ui/litellm-dashboard/src/components/model_add/credentials.tsx b/ui/litellm-dashboard/src/components/model_add/credentials.tsxindex 1bba14bef97..3887e340daa 100644--- a/ui/litellm-dashboard/src/components/model_add/credentials.tsx+++ b/ui/litellm-dashboard/src/components/model_add/credentials.tsx@@ -19,24 +19,22 @@ import {} from "@tremor/react";import { Form } from "antd";import { UploadProps } from "antd/es/upload";-import { useEffect, useState } from "react";+import { useState } from "react";import DeleteResourceModal from "../common_components/DeleteResourceModal";import NotificationsManager from "../molecules/notifications_manager";import AddCredentialsTab from "./AddCredentialModal";import EditCredentialsModal from "./EditCredentialModal";+import { useCredentials } from "@/app/(dashboard)/hooks/credentials/useCredentials";+import useAuthorized from "@/app/(dashboard)/hooks/useAuthorized";interface CredentialsPanelProps {- accessToken: string | null;uploadProps: UploadProps;- credentialList: CredentialItem[];- fetchCredentials: (accessToken: string) => Promise<void>;}-const CredentialsPanel: React.FC<CredentialsPanelProps> = ({- accessToken,- uploadProps,- credentialList,- fetchCredentials,-}) => {+const CredentialsPanel: React.FC<CredentialsPanelProps> = ({ uploadProps }) => {+ const { accessToken } = useAuthorized();+ const { data: credentialsResponse, refetch: refetchCredentials } = useCredentials(accessToken);+ const credentialList = credentialsResponse?.credentials || [];+const [isAddModalOpen, setIsAddModalOpen] = useState(false);const [isUpdateModalOpen, setIsUpdateModalOpen] = useState(false);const [selectedCredential, setSelectedCredential] = useState<CredentialItem | null>(null);@@ -66,7 +64,7 @@ const CredentialsPanel: React.FC<CredentialsPanelProps> = ({await credentialUpdateCall(accessToken, values.credential_name, newCredential);NotificationsManager.success("Credential updated successfully");setIsUpdateModalOpen(false);- await fetchCredentials(accessToken);+ await refetchCredentials();};const handleAddCredential = async (values: any) => {@@ -90,16 +88,9 @@ const CredentialsPanel: React.FC<CredentialsPanelProps> = ({await credentialCreateCall(accessToken, newCredential);NotificationsManager.success("Credential added successfully");setIsAddModalOpen(false);- await fetchCredentials(accessToken);+ await refetchCredentials();};- useEffect(() => {- if (!accessToken) {- return;- }- fetchCredentials(accessToken);- }, [accessToken]);-const renderProviderBadge = (provider: string) => {const providerColors: Record<string, string> = {openai: "blue",@@ -124,7 +115,7 @@ const CredentialsPanel: React.FC<CredentialsPanelProps> = ({try {await credentialDeleteCall(accessToken, credentialToDelete.credential_name);NotificationsManager.success("Credential deleted successfully");- await fetchCredentials(accessToken);+ await refetchCredentials();} catch (error) {NotificationsManager.error("Failed to delete credential");} finally {