mirror of
https://github.com/vercel/commerce.git
synced 2025-07-22 20:26:49 +00:00
Moved normalizer to BC function
This commit is contained in:
@@ -8,6 +8,43 @@ import setProductLocaleMeta from '../utils/set-product-locale-meta'
|
||||
import { productConnectionFragment } from '../fragments/product'
|
||||
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 */ `
|
||||
query getAllProducts(
|
||||
$hasLocale: Boolean = false
|
||||
@@ -72,7 +109,7 @@ async function getAllProducts(opts?: {
|
||||
variables?: ProductVariables
|
||||
config?: BigcommerceConfig
|
||||
preview?: boolean
|
||||
}): Promise<GetAllProductsResult>
|
||||
}): Promise<{ products: Product[] }>
|
||||
|
||||
async function getAllProducts<
|
||||
T extends Record<keyof GetAllProductsResult, any[]>,
|
||||
@@ -93,7 +130,7 @@ async function getAllProducts({
|
||||
variables?: ProductVariables
|
||||
config?: BigcommerceConfig
|
||||
preview?: boolean
|
||||
} = {}): Promise<GetAllProductsResult> {
|
||||
} = {}): Promise<{ products: Product[] }> {
|
||||
config = getConfig(config)
|
||||
|
||||
const locale = vars.locale || config.locale
|
||||
@@ -126,7 +163,7 @@ async function getAllProducts({
|
||||
})
|
||||
}
|
||||
|
||||
return { products }
|
||||
return { products: productsNormalizer(products) }
|
||||
}
|
||||
|
||||
export default getAllProducts
|
||||
|
Reference in New Issue
Block a user