AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(frontend): Filter out `undefined` query params in API requests (#11238)
Reinier van der Leer39792d517ec8
diff --git a/autogpt_platform/frontend/src/lib/autogpt-server-api/helpers.ts b/autogpt_platform/frontend/src/lib/autogpt-server-api/helpers.tsindex b56912b0e8..2e274675a7 100644--- a/autogpt_platform/frontend/src/lib/autogpt-server-api/helpers.ts+++ b/autogpt_platform/frontend/src/lib/autogpt-server-api/helpers.ts@@ -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;++ // 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(payload);- return `${url}?${queryParams.toString()}`;+ const queryParams = new URLSearchParams(filteredQuery);+ return queryParams.size > 0 ? `${url}?${queryParams.toString()}` : url;}export async function handleFetchError(response: Response): Promise<ApiError> {