diff --git a/framework/bigcommerce/lib/normalize.ts b/framework/bigcommerce/lib/normalize.ts index 950177e73..d9e0274c7 100644 --- a/framework/bigcommerce/lib/normalize.ts +++ b/framework/bigcommerce/lib/normalize.ts @@ -1,13 +1,16 @@ import { Cart, CartItem, Product } from '../../types' import { Product as BigCommerceProduct } from '@framework/schema' +import update from "immutability-helper" + +function normalizeProductOption(productOption:any) { + const { + node: { + entityId, + values: { edges }, + ...rest + }, + } = productOption; -function normalizeProductOption({ - node: { - entityId, - values: { edges }, - ...rest - }, -}: any) { return { id: entityId, values: edges?.map(({ node }: any) => node), @@ -15,57 +18,55 @@ function normalizeProductOption({ } } -export function normalizeProduct(productNode: BigCommerceProduct): Product { +export function normalizeProduct(productNode: any): Product { const { entityId: id, - images, - variants, productOptions, prices, path, id: _, options: _0, - brand, - ...rest } = productNode - return { - id, - path, - slug: path?.replace(/^\/+|\/+$/g, ''), - images: images.edges - ? images.edges.map( - ({ node: { urlOriginal, altText, ...rest } }: any) => ({ - url: urlOriginal, - alt: altText, - ...rest, - }) - ) - : [], - variants: variants.edges - ? variants.edges.map( - ({ node: { entityId, productOptions, ...rest } }: any) => ({ - id: entityId, - options: productOptions?.edges - ? productOptions.edges.map(normalizeProductOption) - : [], - ...rest, - }) - ) - : [], - options: productOptions.edges - ? productOptions?.edges.map(normalizeProductOption) - : [], + return update(productNode, { + id: { $set: String(id) }, + images: { + $apply: ({edges} : any) => edges?.map( + ({ node: { urlOriginal, altText, ...rest } }: any) => ({ + url: urlOriginal, + alt: altText, + ...rest, + }) + ) + }, + variants: { + $apply: ({edges} : any) => edges?.map( + ({ node: { entityId, productOptions, ...rest } }: any) => ({ + id: entityId, + options: productOptions?.edges + ? productOptions.edges.map(normalizeProductOption) + : [], + ...rest, + }) + ) + }, + options: { + $set: productOptions.edges ? productOptions?.edges.map(normalizeProductOption) : [] + }, brand: { - id: brand?.entityId ? brand?.entityId : null, - ...brand, + $apply:(brand : any) => brand?.entityId ? brand?.entityId : null, + }, + slug: { + $set: path?.replace(/^\/+|\/+$/g, '') }, price: { - value: prices?.price.value, - currencyCode: prices?.price.currencyCode, - }, - ...rest, - } + $set: { + value: prices?.price.value, + currencyCode: prices?.price.currencyCode, + } + }, + $unset: ['entityId'] + }) } export function normalizeCart({ data, ...rest }: any): Cart { diff --git a/framework/types.d.ts b/framework/types.d.ts index 7a4c45359..e68b6fd17 100644 --- a/framework/types.d.ts +++ b/framework/types.d.ts @@ -8,7 +8,7 @@ interface Entity { interface Product extends Entity { name: string description: string - slug: string + slug?: string path?: string images: ProductImage[] variants: ProductVariant[] diff --git a/package.json b/package.json index aba8b4b5d..d8229071d 100644 --- a/package.json +++ b/package.json @@ -53,6 +53,7 @@ "cookie": "^0.4.1", "dot-object": "^2.1.4", "email-validator": "^2.0.4", + "immutability-helper": "^3.1.1", "js-cookie": "^2.2.1", "keen-slider": "^5.2.4", "lodash.debounce": "^4.0.8", diff --git a/yarn.lock b/yarn.lock index 441f924fc..bc77775f6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3958,6 +3958,11 @@ ignore@^5.1.4: resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.1.8.tgz#f150a8b50a34289b33e22f5889abd4d8016f0e57" integrity sha512-BMpfD7PpiETpBl/A6S498BaIJ6Y/ABT93ETbby2fP00v4EbvPBXWEoaR1UBPKs3iR53pJY7EtZk5KACI57i1Uw== +immutability-helper@^3.1.1: + version "3.1.1" + resolved "https://registry.yarnpkg.com/immutability-helper/-/immutability-helper-3.1.1.tgz#2b86b2286ed3b1241c9e23b7b21e0444f52f77b7" + integrity sha512-Q0QaXjPjwIju/28TsugCHNEASwoCcJSyJV3uO1sOIQGI0jKgm9f41Lvz0DZj3n46cNCyAZTsEYoY4C2bVRUzyQ== + immutable@~3.7.6: version "3.7.6" resolved "https://registry.yarnpkg.com/immutable/-/immutable-3.7.6.tgz#13b4d3cb12befa15482a26fe1b2ebae640071e4b"