mirror of
https://github.com/vercel/commerce.git
synced 2025-04-29 22:37:51 +00:00
Moved normalizer to BC function
This commit is contained in:
parent
4499f33f13
commit
27dd4bfb69
@ -8,6 +8,43 @@ 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 '..'
|
||||||
|
|
||||||
|
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),
|
||||||
|
prices: [
|
||||||
|
{
|
||||||
|
value: prices.price.value,
|
||||||
|
currencyCode: prices.price.currencyCode,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
...rest,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
export const getAllProductsQuery = /* GraphQL */ `
|
export const getAllProductsQuery = /* GraphQL */ `
|
||||||
query getAllProducts(
|
query getAllProducts(
|
||||||
$hasLocale: Boolean = false
|
$hasLocale: Boolean = false
|
||||||
@ -72,7 +109,7 @@ async function getAllProducts(opts?: {
|
|||||||
variables?: ProductVariables
|
variables?: ProductVariables
|
||||||
config?: BigcommerceConfig
|
config?: BigcommerceConfig
|
||||||
preview?: boolean
|
preview?: boolean
|
||||||
}): Promise<GetAllProductsResult>
|
}): Promise<{ products: Product[] }>
|
||||||
|
|
||||||
async function getAllProducts<
|
async function getAllProducts<
|
||||||
T extends Record<keyof GetAllProductsResult, any[]>,
|
T extends Record<keyof GetAllProductsResult, any[]>,
|
||||||
@ -93,7 +130,7 @@ async function getAllProducts({
|
|||||||
variables?: ProductVariables
|
variables?: ProductVariables
|
||||||
config?: BigcommerceConfig
|
config?: BigcommerceConfig
|
||||||
preview?: boolean
|
preview?: boolean
|
||||||
} = {}): Promise<GetAllProductsResult> {
|
} = {}): Promise<{ products: Product[] }> {
|
||||||
config = getConfig(config)
|
config = getConfig(config)
|
||||||
|
|
||||||
const locale = vars.locale || config.locale
|
const locale = vars.locale || config.locale
|
||||||
@ -126,7 +163,7 @@ async function getAllProducts({
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return { products }
|
return { products: productsNormalizer(products) }
|
||||||
}
|
}
|
||||||
|
|
||||||
export default getAllProducts
|
export default getAllProducts
|
||||||
|
@ -11,44 +11,6 @@ import getSiteInfo from '@framework/api/operations/get-site-info'
|
|||||||
import getAllPages from '@framework/api/operations/get-all-pages'
|
import getAllPages from '@framework/api/operations/get-all-pages'
|
||||||
|
|
||||||
// Outputs from providers should already be normalized
|
// Outputs from providers should already be normalized
|
||||||
// TODO (bc) move this to the provider
|
|
||||||
|
|
||||||
function productsNormalizer(arr: any[]) {
|
|
||||||
// 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),
|
|
||||||
prices: [
|
|
||||||
{
|
|
||||||
value: prices.price.value,
|
|
||||||
currencyCode: prices.price.currencyCode,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
...rest,
|
|
||||||
})
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getStaticProps({
|
export async function getStaticProps({
|
||||||
preview,
|
preview,
|
||||||
@ -56,14 +18,12 @@ export async function getStaticProps({
|
|||||||
}: GetStaticPropsContext) {
|
}: GetStaticPropsContext) {
|
||||||
const config = getConfig({ locale })
|
const config = getConfig({ locale })
|
||||||
|
|
||||||
const { products: rawProducts } = await getAllProducts({
|
const { products } = await getAllProducts({
|
||||||
variables: { first: 12 },
|
variables: { first: 12 },
|
||||||
config,
|
config,
|
||||||
preview,
|
preview,
|
||||||
})
|
})
|
||||||
|
|
||||||
// Remove normalizer and send to framework provider.
|
|
||||||
const products = productsNormalizer(rawProducts)
|
|
||||||
const { categories, brands } = await getSiteInfo({ config, preview })
|
const { categories, brands } = await getSiteInfo({ config, preview })
|
||||||
const { pages } = await getAllPages({ config, preview })
|
const { pages } = await getAllPages({ config, preview })
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user