mirror of
https://github.com/vercel/commerce.git
synced 2025-07-27 04:01:23 +00:00
Add options and variants
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import type { OperationContext } from '@commerce/api/operations'
|
||||
import type { GetProductOperation } from '@commerce/types/product'
|
||||
|
||||
import type { RawProduct } from '../../types/product'
|
||||
import type { RawProduct, RawSpec, RawVariant } from '../../types/product'
|
||||
import type { OrdercloudConfig, Provider } from '../index'
|
||||
|
||||
import { normalize as normalizeProduct } from '../../utils/product'
|
||||
@@ -22,14 +22,37 @@ export default function getProductOperation({
|
||||
const { fetch } = commerce.getConfig(config)
|
||||
|
||||
// Get a single product
|
||||
const rawProduct: RawProduct = await fetch<RawProduct>(
|
||||
const productPromise = fetch<RawProduct>(
|
||||
'GET',
|
||||
`/me/products/${variables?.slug}`
|
||||
)
|
||||
|
||||
// Get product specs
|
||||
const specsPromise = fetch<{ Items: RawSpec[] }>(
|
||||
'GET',
|
||||
`/me/products/${variables?.slug}/specs`
|
||||
).then((res) => res.Items)
|
||||
|
||||
// Get product variants
|
||||
const variantsPromise = fetch<{ Items: RawVariant[] }>(
|
||||
'GET',
|
||||
`/me/products/${variables?.slug}/variants`
|
||||
).then((res) => res.Items)
|
||||
|
||||
// Execute all promises in parallel
|
||||
const [product, specs, variants] = await Promise.all([
|
||||
productPromise,
|
||||
specsPromise,
|
||||
variantsPromise,
|
||||
])
|
||||
|
||||
// Hydrate product
|
||||
product.xp.Specs = specs
|
||||
product.xp.Variants = variants
|
||||
|
||||
return {
|
||||
// Normalize product to commerce schema
|
||||
product: normalizeProduct(rawProduct),
|
||||
product: normalizeProduct(product),
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user