Skip to content

Commit b8efac8

Browse files
committed
feat: add feature toggle - product by handle
1 parent 10cfdfd commit b8efac8

File tree

4 files changed

+52
-19
lines changed

4 files changed

+52
-19
lines changed
Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getProductByHandle } from "@lib/medusa-fetch/products"
12
import ProductTemplate from "@modules/products/templates"
23
import { Metadata } from "next"
34
import { notFound } from "next/navigation"
@@ -6,20 +7,13 @@ type Props = {
67
params: { handle: string }
78
}
89

9-
async function getProducts(handle: string) {
10-
const data = await fetch(
11-
`${process.env.NEXT_PUBLIC_BASE_URL}/api/products/${handle}`
12-
)
13-
.then((res) => res.json())
14-
.catch(() => {
15-
notFound()
16-
})
17-
18-
return data
19-
}
20-
2110
export async function generateMetadata({ params }: Props): Promise<Metadata> {
22-
const { product } = await getProducts(params.handle)
11+
const { products } = await getProductByHandle(params.handle).catch((err) => {
12+
console.error(err)
13+
notFound()
14+
})
15+
16+
const product = products[0]
2317

2418
return {
2519
title: `${product.title} | Acme Store`,
@@ -33,7 +27,12 @@ export async function generateMetadata({ params }: Props): Promise<Metadata> {
3327
}
3428

3529
export default async function CollectionPage({ params }: Props) {
36-
const { product } = await getProducts(params.handle)
30+
const { products } = await getProductByHandle(params.handle).catch((err) => {
31+
console.error(err)
32+
notFound()
33+
})
34+
35+
console.log(products)
3736

38-
return <ProductTemplate product={product} />
37+
return <ProductTemplate product={products[0]} />
3938
}

src/app/api/products/[handle]/route.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ export async function GET(
88
{ params }: { params: Record<string, any> }
99
) {
1010
const { handle } = params
11-
const { cart_id } = Object.fromEntries(request.nextUrl.searchParams)
11+
const { cart_id, region_id } = Object.fromEntries(
12+
request.nextUrl.searchParams
13+
)
1214
const productService = await initializeProductModule()
1315

1416
const data = await productService.list(
@@ -29,7 +31,7 @@ export async function GET(
2931
}
3032
)
3133

32-
const productsWithPrices = await getPrices(data, cart_id)
34+
const productsWithPrices = await getPrices(data, cart_id, region_id)
3335

34-
return NextResponse.json({ product: productsWithPrices[0] })
36+
return NextResponse.json({ products: productsWithPrices })
3537
}

src/lib/medusa-fetch/products.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import medusaRequest from "./"
2+
3+
const PRODUCT_MODULE_ENABLED = process.env.FEATURE_PRODUCTMODULE_ENABLED
4+
5+
export async function getProductByHandle(handle: string) {
6+
if (PRODUCT_MODULE_ENABLED) {
7+
console.log("PRODUCT_MODULE_ENABLED")
8+
const data = await fetch(
9+
`${process.env.NEXT_PUBLIC_BASE_URL}/api/products/${handle}`
10+
)
11+
.then((res) => res.json())
12+
.catch((err) => {
13+
throw err
14+
})
15+
16+
return data
17+
}
18+
19+
console.log("PRODUCT_MODULE_DISABLED")
20+
const data = await medusaRequest("GET", "/products", {
21+
query: {
22+
handle,
23+
},
24+
})
25+
26+
return {
27+
products: data.body.products,
28+
status: data.status,
29+
ok: data.ok,
30+
}
31+
}

store.config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"features": {
3-
"search": false
3+
"search": false,
4+
"productModule": false
45
}
56
}

0 commit comments

Comments
 (0)