Skip to content

Commit d9d7c86

Browse files
committed
refactor: product handle route
1 parent 06b3a0e commit d9d7c86

File tree

1 file changed

+21
-73
lines changed

1 file changed

+21
-73
lines changed

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

Lines changed: 21 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { NextResponse, NextRequest } from "next/server"
2-
3-
import { MedusaApp, Modules } from "@medusajs/modules-sdk"
2+
import { initialize as initializeProductModule } from "@medusajs/product"
43

54
/**
6-
* This endpoint uses the serverless Product and Pricing Modules to retrieve a product by handle.
5+
* This endpoint uses the serverless Product Modules to retrieve a product by handle.
76
* The modules connect directly to your Medusa database to retrieve and manipulate data, without the need for a dedicated server.
87
* Read more about the Product Module here: https://docs.medusajs.com/modules/products/serverless-module
98
*/
@@ -14,78 +13,27 @@ export async function GET(
1413
// Extract the query parameters
1514
const { handle } = params
1615

17-
// Initialize Remote Query with the Product and Pricing Modules
18-
const { query } = await MedusaApp({
19-
modulesConfig: [
20-
{
21-
module: Modules.PRODUCT,
22-
path: "@medusajs/product",
23-
},
24-
{
25-
module: Modules.PRICING,
26-
path: "@medusajs/pricing",
27-
},
28-
],
29-
sharedResourcesConfig: {
30-
database: { clientUrl: process.env.POSTGRES_URL },
31-
},
32-
})
33-
34-
// Set the filters for the query
35-
const filters = {
36-
handle,
37-
take: 1,
38-
}
39-
40-
// Set the GraphQL query
41-
const productsQuery = `#graphql
42-
query($handle: String, $take: Int) {
43-
products(handle: $handle, take: $take) {
44-
id
45-
title
46-
handle
47-
tags
48-
status
49-
collection
50-
collection_id
51-
thumbnail
52-
images {
53-
url
54-
alt_text
55-
id
56-
}
57-
options {
58-
id
59-
value
60-
title
61-
}
62-
variants {
63-
id
64-
title
65-
created_at
66-
updated_at
67-
thumbnail
68-
inventory_quantity
69-
material
70-
weight
71-
length
72-
height
73-
width
74-
options {
75-
id
76-
value
77-
title
78-
}
79-
}
80-
}
81-
}`
16+
// Initialize the Product Module
17+
const productService = await initializeProductModule()
8218

8319
// Run the query
84-
const data = await query(productsQuery, filters)
85-
86-
// Format the response
87-
// const products = formatModuleResponse(data)
88-
const products = data
20+
const products = await productService.list(
21+
{ handle },
22+
{
23+
relations: [
24+
"variants",
25+
"variants.options",
26+
"tags",
27+
"options",
28+
"options.values",
29+
"images",
30+
"description",
31+
"collection",
32+
"status",
33+
],
34+
take: 1,
35+
}
36+
)
8937

9038
// Return the response
9139
return NextResponse.json({ products })

0 commit comments

Comments
 (0)