Skip to content

Commit

Permalink
Move type definition back to where it was
Browse files Browse the repository at this point in the history
  • Loading branch information
gvc committed Jan 25, 2024
1 parent 75802a0 commit ff8e8e8
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions packages/api/src/platforms/vtex/resolvers/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const getPath = (link: string, id: string) => `/${getSlug(link, id)}/p`
const nonEmptyArray = <T>(array: T[] | null | undefined) =>
Array.isArray(array) && array.length > 0 ? array : null

type ActualStoreProductType = Record<string, Resolver<Root, any>> & {
export const StoreProduct: Record<string, Resolver<Root>> & {
offers: Resolver<
Root,
any,
Expand All @@ -40,10 +40,8 @@ type ActualStoreProductType = Record<string, Resolver<Root, any>> & {

isVariantOf: Resolver<Root, any, Root>

image: Resolver<Root, StoreProductImageArgs, StoreImage[]>
}

export const StoreProduct: ActualStoreProductType = {
image: Resolver<Root, any, StoreImage[]>
} = {
productID: ({ itemId }) => itemId,
name: ({ isVariantOf, name }) => name ?? isVariantOf.productName,
slug: ({ isVariantOf: { linkText }, itemId }) => getSlug(linkText, itemId),
Expand Down Expand Up @@ -80,13 +78,7 @@ export const StoreProduct: ActualStoreProductType = {
numberOfItems: categories.length,
}
},
image: ({ images }, { keywords, count }: StoreProductImageArgs) => {
const shouldFilter = keywords !== 'all'

// Normalize count to undefined as we want any negative value to always return the full list of images
count = count || -1
count = count <= -1 ? undefined : count

image: ({ images }, args) => {
const resolvedImages = (nonEmptyArray(images) ?? [DEFAULT_IMAGE]).map(
({ imageUrl, imageText, imageLabel }) => ({
alternateName: imageText ?? '',
Expand All @@ -95,13 +87,26 @@ export const StoreProduct: ActualStoreProductType = {
})
)

if(typeof args !== 'object') {
return resolvedImages;
}

let { keywords, count } = args as StoreProductImageArgs

const shouldFilter = keywords !== 'all'

// Normalize count to undefined as we want any negative value to always return the full list of images
count = count || -1
count = count <= -1 ? undefined : count

let filteredImages = shouldFilter
? resolvedImages.filter(
({ keywords: imageKeywords }) => imageKeywords === keywords
)
: resolvedImages

filteredImages = filteredImages.length === 0 ? resolvedImages : filteredImages
filteredImages =
filteredImages.length === 0 ? resolvedImages : filteredImages

return filteredImages.slice(0, count)
},
Expand Down

0 comments on commit ff8e8e8

Please sign in to comment.