Skip to content

Commit 54bba57

Browse files
committed
refactor: improve query array handling
1 parent d593c8d commit 54bba57

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/app/collections/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export async function GET(request: NextRequest) {
3232

3333
async function fetchCollection(handle: string) {
3434
return await medusaRequest("GET", "/collections", {
35-
query: { "handle[]": handle },
35+
query: { handle: [handle] },
3636
}).then(({ body }) => ({
3737
id: body.collections[0].id,
3838
handle: body.collections[0].handle,
@@ -53,7 +53,7 @@ async function fetchCollectionProducts({
5353
}) {
5454
const { products, count, offset } = await medusaRequest("GET", "/products", {
5555
query: {
56-
"collection_id[]": id,
56+
collection_id: [id],
5757
cart_id,
5858
limit,
5959
offset: pageParam,

src/lib/medusa-fetch/index.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const MEDUSA_API_KEY = process.env.NEXT_PUBLIC_MEDUSA_API_KEY || ""
2-
const REVALIDATE_WINDOW = process.env.REVALIDATE_WINDOW || "60*60"
2+
const REVALIDATE_WINDOW = process.env.REVALIDATE_WINDOW || "60*1000*10" // 10 minutes
33
const ENDPOINT =
44
process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL || "http://localhost:9000"
55

@@ -22,7 +22,8 @@ export default async function medusaRequest(
2222
options.body = JSON.stringify(payload.body)
2323
}
2424
if ("query" in payload) {
25-
path = `${path}?${new URLSearchParams(payload.query).toString()}`
25+
const queries = parseArrays(payload.query)
26+
path = `${path}?${new URLSearchParams(queries).toString()}`
2627
}
2728
}
2829

@@ -44,3 +45,15 @@ export default async function medusaRequest(
4445
}
4546
}
4647
}
48+
49+
const parseArrays = (obj: Record<string, any>) => {
50+
const result = {} as Record<string, any>
51+
Object.entries(obj).forEach(([key, value]) => {
52+
if (Array.isArray(value)) {
53+
result[`${key}[]`] = value.join(",")
54+
return
55+
}
56+
result[key] = value
57+
})
58+
return result
59+
}

0 commit comments

Comments
 (0)