hotfix(medusa): adds variant retrieve/list endpoints
This commit is contained in:
@@ -11,6 +11,7 @@ import customerRoutes from "./customers"
|
||||
import shippingOptionRoutes from "./shipping-options"
|
||||
import regionRoutes from "./regions"
|
||||
import swapRoutes from "./swaps"
|
||||
import variantRoutes from "./variants"
|
||||
|
||||
const route = Router()
|
||||
|
||||
@@ -35,6 +36,7 @@ export default (app, container, config) => {
|
||||
shippingOptionRoutes(route)
|
||||
regionRoutes(route)
|
||||
swapRoutes(route)
|
||||
variantRoutes(route)
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
31
packages/medusa/src/api/routes/store/variants/get-variant.js
Normal file
31
packages/medusa/src/api/routes/store/variants/get-variant.js
Normal file
@@ -0,0 +1,31 @@
|
||||
import { Validator } from "medusa-core-utils"
|
||||
|
||||
export default async (req, res) => {
|
||||
const { variant_id } = req.params
|
||||
|
||||
const schema = Validator.objectId()
|
||||
const { value, error } = schema.validate(variant_id)
|
||||
|
||||
if (error) {
|
||||
throw error
|
||||
}
|
||||
|
||||
const variantService = req.scope.resolve("productVariantService")
|
||||
let variant = await variantService.retrieve(value)
|
||||
|
||||
let includeFields = [
|
||||
"title",
|
||||
"prices",
|
||||
"sku",
|
||||
"ean",
|
||||
"image",
|
||||
"inventory_quantity",
|
||||
"allow_backorder",
|
||||
"manage_inventory",
|
||||
]
|
||||
if ("fields" in req.query) {
|
||||
includeFields = req.query.fields.split(",")
|
||||
}
|
||||
variant = await variantService.decorate(variant, includeFields)
|
||||
res.json({ variant })
|
||||
}
|
||||
13
packages/medusa/src/api/routes/store/variants/index.js
Normal file
13
packages/medusa/src/api/routes/store/variants/index.js
Normal file
@@ -0,0 +1,13 @@
|
||||
import { Router } from "express"
|
||||
import middlewares from "../../../middlewares"
|
||||
|
||||
const route = Router()
|
||||
|
||||
export default app => {
|
||||
app.use("/variants", route)
|
||||
|
||||
route.get("/", middlewares.wrap(require("./list-variants").default))
|
||||
route.get("/:variant_id", middlewares.wrap(require("./get-variant").default))
|
||||
|
||||
return app
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import { Validator } from "medusa-core-utils"
|
||||
|
||||
export default async (req, res) => {
|
||||
const selector = {}
|
||||
|
||||
if ("ids" in req.query) {
|
||||
selector["_id"] = { $in: req.query.ids.split(",") }
|
||||
}
|
||||
|
||||
const variantService = req.scope.resolve("productVariantService")
|
||||
const variants = await variantService.list(selector)
|
||||
|
||||
let includeFields = [
|
||||
"title",
|
||||
"prices",
|
||||
"sku",
|
||||
"ean",
|
||||
"image",
|
||||
"inventory_quantity",
|
||||
"allow_backorder",
|
||||
"manage_inventory",
|
||||
]
|
||||
if ("fields" in req.query) {
|
||||
includeFields = req.query.fields.split(",")
|
||||
}
|
||||
|
||||
const data = await Promise.all(
|
||||
variants.map(v => variantService.decorate(v, includeFields))
|
||||
)
|
||||
res.json({ variants: data })
|
||||
}
|
||||
Reference in New Issue
Block a user