Skip to content

Commit

Permalink
Fix eager fetch (#3302)
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondjacobson authored May 2, 2023
1 parent 4b639ba commit f1366ac
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 25 deletions.
10 changes: 2 additions & 8 deletions packages/common/src/services/audius-backend/eagerLoadUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down
28 changes: 11 additions & 17 deletions packages/common/src/utils/stringUtils.ts
Original file line number Diff line number Diff line change
@@ -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('&')
}
Expand Down

0 comments on commit f1366ac

Please sign in to comment.