4
0
forked from crowetic/commerce

Normalizer

This commit is contained in:
Belen Curcio 2021-01-10 16:16:49 -03:00
parent 0a05182f3b
commit ac58e4a351
5 changed files with 52 additions and 51 deletions

View File

@ -7,41 +7,7 @@ import filterEdges from '../utils/filter-edges'
import setProductLocaleMeta from '../utils/set-product-locale-meta' import setProductLocaleMeta from '../utils/set-product-locale-meta'
import { productConnectionFragment } from '../fragments/product' import { productConnectionFragment } from '../fragments/product'
import { BigcommerceConfig, getConfig } from '..' import { BigcommerceConfig, getConfig } from '..'
import { normalizeProduct } from '../../lib/normalize'
function productsNormalizer(arr: any[]): Product[] {
// Normalizes products arr response and flattens node edges
return arr.map(
({
node: {
entityId: id,
images,
variants,
productOptions,
prices,
path,
...rest
},
}) => ({
id,
path,
slug: path.slice(1, -1),
images: images.edges.map(
({ node: { urlOriginal, altText, ...rest } }: any) => ({
url: urlOriginal,
alt: altText,
...rest,
})
),
variants: variants.edges.map(({ node }: any) => node),
productOptions: productOptions.edges.map(({ node }: any) => node),
price: {
value: prices.price.value,
currencyCode: prices.price.currencyCode,
},
...rest,
})
)
}
export const getAllProductsQuery = /* GraphQL */ ` export const getAllProductsQuery = /* GraphQL */ `
query getAllProducts( query getAllProducts(
@ -161,7 +127,7 @@ async function getAllProducts({
}) })
} }
return { products: productsNormalizer(products) } return { products: products.map(normalizeProduct) }
} }
export default getAllProducts export default getAllProducts

View File

@ -2,6 +2,7 @@ import type { GetProductQuery, GetProductQueryVariables } from '../../schema'
import setProductLocaleMeta from '../utils/set-product-locale-meta' import setProductLocaleMeta from '../utils/set-product-locale-meta'
import { productInfoFragment } from '../fragments/product' import { productInfoFragment } from '../fragments/product'
import { BigcommerceConfig, getConfig } from '..' import { BigcommerceConfig, getConfig } from '..'
import { normalizeProduct } from '@framework/lib/normalize'
export const getProductQuery = /* GraphQL */ ` export const getProductQuery = /* GraphQL */ `
query getProduct( query getProduct(
@ -92,7 +93,7 @@ async function getProduct({
variables: ProductVariables variables: ProductVariables
config?: BigcommerceConfig config?: BigcommerceConfig
preview?: boolean preview?: boolean
}): Promise<GetProductResult> { }): Promise<Product | {}> {
config = getConfig(config) config = getConfig(config)
const locale = vars.locale || config.locale const locale = vars.locale || config.locale
@ -109,7 +110,8 @@ async function getProduct({
if (locale && config.applyLocale) { if (locale && config.applyLocale) {
setProductLocaleMeta(product) setProductLocaleMeta(product)
} }
return { product }
return { product: normalizeProduct(product as any) }
} }
return {} return {}

View File

@ -0,0 +1,33 @@
import { ProductNode } from '@framework/api/operations/get-all-products'
export function normalizeProduct(productNode: ProductNode | any): Product {
const {
entityId: id,
images,
variants,
productOptions,
prices,
path,
...rest
} = productNode
return {
id,
path,
slug: path.slice(1, -1),
images: images.edges?.map(
({ node: { urlOriginal, altText, ...rest } }: any) => ({
url: urlOriginal,
alt: altText,
...rest,
})
),
variants: variants?.edges?.map(({ node }: any) => node),
productOptions: productOptions?.edges?.map(({ node }: any) => node),
price: {
value: prices?.price.value,
currencyCode: prices?.price.currencyCode,
},
...rest,
}
}

24
framework/types.d.ts vendored
View File

@ -1,11 +1,15 @@
interface Product { interface Entity {
id: string | number id: string | number
[prop: string]: any
}
interface Product extends Entity {
name: string name: string
description: string description: string
slug: string slug: string
path?: string path?: string
images: ProductImage[] images: ProductImage[] | any[] | undefined
variants: ProductVariant[] variants: ProductVariant[] | any[] | undefined
price: ProductPrice price: ProductPrice
} }
interface ProductImage { interface ProductImage {
@ -19,25 +23,23 @@ interface ProductVariant {
interface ProductPrice { interface ProductPrice {
value: number value: number
currencyCode: 'USD' | 'ARS' currencyCode: 'USD' | 'ARS' | string | undefined
retailValue?: number retailValue?: number
saleValue?: number saleValue?: number
} }
interface Cart { interface Cart extends Entity {
id: string id: string
products: Pick<Product, 'id' | 'name' | 'prices'>[] products: Pick<Product, 'id' | 'name' | 'prices'>[]
} }
interface Wishlist { interface Wishlist extends Entity {
id: string
products: Pick<Product, 'id' | 'name' | 'prices'>[] products: Pick<Product, 'id' | 'name' | 'prices'>[]
} }
interface Order {} interface Order {}
interface Customer { interface Customer extends Entity {
id: string | number | undefined
[prop: string]: any [prop: string]: any
} }
@ -45,12 +47,12 @@ type UseCustomerResponse = {
customer: Customer customer: Customer
} | null } | null
interface Category { interface Category extends Entity {
id: string id: string
name: string name: string
} }
interface Brand { interface Brand extends Entity {
id: string id: string
name: string name: string
} }

View File

@ -7,8 +7,6 @@ import { useRouter } from 'next/router'
import { Layout } from '@components/common' import { Layout } from '@components/common'
import { ProductView } from '@components/product' import { ProductView } from '@components/product'
// Data
import { getConfig } from '@framework/api' import { getConfig } from '@framework/api'
import getProduct from '@framework/api/operations/get-product' import getProduct from '@framework/api/operations/get-product'
import getAllPages from '@framework/api/operations/get-all-pages' import getAllPages from '@framework/api/operations/get-all-pages'