fix(frontend): Filter out undefined query params in API requests (#11238)

Part of our effort to eliminate preventable warnings and errors.

- Resolves #11237

### Changes 🏗️

- Exclude `undefined` query params in API requests

### Checklist 📋

#### For code changes:
- [x] I have clearly listed my changes in the PR description
- [x] I have made a test plan
- [x] I have tested my changes according to the test plan:
  - Open the Builder without a `flowVersion` URL parameter
    - [x] -> `GET /api/library/agents/by-graph/{graph_id}` succeeds
  - Open the builder with a `flowVersion` URL parameter
    - [x] -> version is correctly included in request URL parameters
This commit is contained in:
Reinier van der Leer
2025-10-22 15:25:34 +02:00
committed by GitHub
parent a6a2f71458
commit 39792d517e

View File

@@ -40,12 +40,11 @@ export function buildRequestUrl(
method: string,
payload?: Record<string, any>,
): string {
let url = baseUrl + path;
const url = baseUrl + path;
const payloadAsQuery = ["GET", "DELETE"].includes(method);
if (payloadAsQuery && payload) {
const queryParams = new URLSearchParams(payload);
url += `?${queryParams.toString()}`;
return buildUrlWithQuery(url, payload);
}
return url;
@@ -61,12 +60,23 @@ export function buildServerUrl(path: string): string {
export function buildUrlWithQuery(
url: string,
payload?: Record<string, any>,
query?: Record<string, any>,
): string {
if (!payload) return url;
if (!query) return url;
const queryParams = new URLSearchParams(payload);
return `${url}?${queryParams.toString()}`;
// Filter out undefined values to prevent them from being included as "undefined" strings
const filteredQuery = Object.entries(query).reduce(
(acc, [key, value]) => {
if (value !== undefined) {
acc[key] = value;
}
return acc;
},
{} as Record<string, any>,
);
const queryParams = new URLSearchParams(filteredQuery);
return queryParams.size > 0 ? `${url}?${queryParams.toString()}` : url;
}
export async function handleFetchError(response: Response): Promise<ApiError> {