1
1
import { NextResponse , NextRequest } from "next/server"
2
-
3
- import { MedusaApp , Modules } from "@medusajs/modules-sdk"
2
+ import { initialize as initializeProductModule } from "@medusajs/product"
4
3
5
4
/**
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.
7
6
* The modules connect directly to your Medusa database to retrieve and manipulate data, without the need for a dedicated server.
8
7
* Read more about the Product Module here: https://docs.medusajs.com/modules/products/serverless-module
9
8
*/
@@ -14,78 +13,27 @@ export async function GET(
14
13
// Extract the query parameters
15
14
const { handle } = params
16
15
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 ( )
82
18
83
19
// 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
+ )
89
37
90
38
// Return the response
91
39
return NextResponse . json ( { products } )
0 commit comments