mirror of
https://github.com/vercel/commerce.git
synced 2025-07-22 20:26:49 +00:00
Fetch product images, standardize API fetch using Spree SDK
This commit is contained in:
30
framework/spree/utils/getMediaGallery.ts
Normal file
30
framework/spree/utils/getMediaGallery.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
// Based on https://github.com/spark-solutions/spree2vuestorefront/blob/d88d85ae1bcd2ec99b13b81cd2e3c25600a0216e/src/utils/index.ts
|
||||
|
||||
import type { ProductImage } from '@commerce/types/product'
|
||||
import type { SpreeProductImage } from '../types'
|
||||
|
||||
const getMediaGallery = (
|
||||
images: SpreeProductImage[],
|
||||
getImageUrl: (
|
||||
image: SpreeProductImage,
|
||||
minWidth: number,
|
||||
minHeight: number
|
||||
) => string | null
|
||||
) => {
|
||||
return images.reduce<ProductImage[]>((productImages, _, imageIndex) => {
|
||||
const imageUrl = getImageUrl(images[imageIndex], 9001, 9001)
|
||||
|
||||
if (imageUrl) {
|
||||
return [
|
||||
...productImages,
|
||||
{
|
||||
url: imageUrl,
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
return productImages
|
||||
}, [])
|
||||
}
|
||||
|
||||
export default getMediaGallery
|
Reference in New Issue
Block a user