mirror of
https://github.com/vercel/commerce.git
synced 2025-07-03 19:51:22 +00:00
Add options and variants
This commit is contained in:
parent
4151865fa2
commit
0c2855a323
@ -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),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,3 +1,27 @@
|
||||
interface RawVariantSpec {
|
||||
SpecID: string
|
||||
Name: string
|
||||
OptionID: string
|
||||
Value: string
|
||||
}
|
||||
|
||||
export interface RawSpec {
|
||||
ID: string
|
||||
Name: string
|
||||
Options: {
|
||||
ID: string
|
||||
Value: string
|
||||
xp: {
|
||||
hexColor?: string
|
||||
}
|
||||
}[]
|
||||
}
|
||||
|
||||
export interface RawVariant {
|
||||
ID: string
|
||||
Specs: RawVariantSpec[]
|
||||
}
|
||||
|
||||
export interface RawProduct {
|
||||
OwnerID: string
|
||||
DefaultPriceScheduleID: string | null
|
||||
@ -23,6 +47,7 @@ export interface RawProduct {
|
||||
Images: {
|
||||
url: string
|
||||
}[]
|
||||
Facets: Record<string, string[]>
|
||||
Variants?: RawVariant[]
|
||||
Specs?: RawSpec[]
|
||||
}
|
||||
}
|
||||
|
@ -13,30 +13,33 @@ export function normalize(product: RawProduct): Product {
|
||||
value: product.xp.Price,
|
||||
currencyCode: product.xp.PriceCurrency,
|
||||
},
|
||||
// Variants are not always present, in case they are not, return a single unique variant
|
||||
variants:
|
||||
product.VariantCount === 0
|
||||
? [
|
||||
variants: product.xp.Variants?.length
|
||||
? product.xp.Variants.map((variant) => ({
|
||||
id: variant.ID,
|
||||
options: variant.Specs.map((spec) => ({
|
||||
id: spec.SpecID,
|
||||
__typename: 'MultipleChoiceOption',
|
||||
displayName: spec.Name,
|
||||
values: [
|
||||
{
|
||||
id: 'unique',
|
||||
options: [
|
||||
{
|
||||
id: 'unique',
|
||||
displayName: 'Unique',
|
||||
values: [{ label: 'Unique' }],
|
||||
label: spec.Value,
|
||||
},
|
||||
],
|
||||
})),
|
||||
}))
|
||||
: [
|
||||
{
|
||||
id: product.ID,
|
||||
options: [],
|
||||
},
|
||||
]
|
||||
: [],
|
||||
// Facets are not always present, just iterate them if they are
|
||||
options: product.xp.Facets
|
||||
? Object.entries(product.xp.Facets).map(([key, values]) => ({
|
||||
id: key,
|
||||
displayName: key,
|
||||
__typename: 'MultipleChoiceOption',
|
||||
values: values.map((value) => ({
|
||||
label: value,
|
||||
],
|
||||
options: product.xp.Specs?.length
|
||||
? product.xp.Specs.map((spec) => ({
|
||||
id: spec.ID,
|
||||
displayName: spec.Name,
|
||||
values: spec.Options.map((option) => ({
|
||||
label: option.Value,
|
||||
...(option.xp?.hexColor && { hexColors: [option.xp.hexColor] }),
|
||||
})),
|
||||
}))
|
||||
: [],
|
||||
|
Loading…
x
Reference in New Issue
Block a user