diff --git a/packages/common/src/services/audius-backend/eagerLoadUtils.ts b/packages/common/src/services/audius-backend/eagerLoadUtils.ts index 412978f6412..7ee4a523e48 100644 --- a/packages/common/src/services/audius-backend/eagerLoadUtils.ts +++ b/packages/common/src/services/audius-backend/eagerLoadUtils.ts @@ -59,20 +59,14 @@ export const makeEagerRequest = async ( let res: any if (req?.method?.toLowerCase() === 'post') { headers['Content-Type'] = 'application/json' - const url = `${baseUrl}?${paramsToQueryString( - req.queryParams, - discprovEndpoint === eagerDiscprov - )}` + const url = `${baseUrl}?${paramsToQueryString(req.queryParams)}` res = await fetch(url, { method: 'POST', headers, body: JSON.stringify(req.data) }) } else { - const url = `${baseUrl}?${paramsToQueryString( - req.queryParams, - discprovEndpoint === eagerDiscprov - )}` + const url = `${baseUrl}?${paramsToQueryString(req.queryParams)}` res = await fetch(url, { headers }) diff --git a/packages/common/src/utils/stringUtils.ts b/packages/common/src/utils/stringUtils.ts index 46ca47dbd53..6a26fe5de84 100644 --- a/packages/common/src/utils/stringUtils.ts +++ b/packages/common/src/utils/stringUtils.ts @@ -1,25 +1,19 @@ /** * Converts a provided URL params object to a query string */ -export const paramsToQueryString = ( - params: { [key: string]: string | string[] }, - formatWithoutArray = false -) => { +export const paramsToQueryString = (params: { + [key: string]: string | string[] +}) => { if (!params) return '' - return Object.keys(params) - .map((k) => { - if (Array.isArray(params[k])) { - return (params[k] as string[]) - .map((val: string) => - formatWithoutArray - ? `${encodeURIComponent(k)}=${encodeURIComponent(val)}` - : `${encodeURIComponent(k)}[]=${encodeURIComponent(val)}` - ) - .join('&') + return Object.entries(params) + .filter((p) => p[1] !== undefined && p[1] !== null) + .map((p) => { + if (Array.isArray(p[1])) { + // Otherwise, join in the form of + // ?key=val1&key=val2&key=val3... + return p[1].map((val) => `${p[0]}=${encodeURIComponent(val)}`).join('&') } - return ( - encodeURIComponent(k) + '=' + encodeURIComponent(params[k] as string) - ) + return `${p[0]}=${encodeURIComponent(p[1]!)}` }) .join('&') }