Skip to content

Commit

Permalink
fix: removed NotFound pageType filter and added comprehensive error m…
Browse files Browse the repository at this point in the history
…essage
  • Loading branch information
icazevedo committed Feb 17, 2022
1 parent 8c1f17b commit f613efc
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 6 deletions.
58 changes: 57 additions & 1 deletion packages/api/src/platforms/vtex/loaders/pagetype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,67 @@ import pLimit from 'p-limit'

import type { Options } from '..'
import type { Clients } from '../clients'
import type { PortalPagetype } from '../clients/commerce/types/Portal'
import type {
PortalPagetype,
ValidPortalPagetype,
} from '../clients/commerce/types/Portal'
import { isValidPortalPageType } from '../resolvers/collection'

// Limits concurrent requests to 20 so that they don't timeout
const CONCURRENT_REQUESTS_MAX = 20

export function throwIfPageTypeNotFound(
pageTypes: PromiseSettledResult<PortalPagetype>[],
slugs: readonly string[]
): pageTypes is PromiseFulfilledResult<ValidPortalPagetype>[] {
const notFoundIndexes: number[] = []
const rejectedIndexes: number[] = []

pageTypes.forEach((pageType, index) => {
// If the request fails
if (pageType.status === 'rejected') {
rejectedIndexes.push(index)

return
}

// If the catalog returns NotFound pageType
if (!isValidPortalPageType(pageType.value)) {
notFoundIndexes.push(index)
}
})

if (notFoundIndexes.length + rejectedIndexes.length > 0) {
let errorMessage = ''

if (notFoundIndexes.length > 0) {
errorMessage +=
`Catalog returned NotFound pageType for the following slug(s): ${notFoundIndexes
.map((i) => slugs[i])
.join(', ')}.` +
' This usually happens when there is more than one category with' +
' the same name in the same category tree level.'

errorMessage += '\n'
}

if (rejectedIndexes.length > 0) {
errorMessage += `pageType request failed for the following slug(s): ${rejectedIndexes
.map((i) => slugs[i])
.join(', ')}.`

errorMessage += '\n'
errorMessage += rejectedIndexes
.map((i) => (pageTypes[i] as PromiseRejectedResult).reason)
.join(`\n`)
}

throw new Error(errorMessage)
}

return true
}

export const getPagetypeLoader = (_: Options, clients: Clients) => {
const limit = pLimit(CONCURRENT_REQUESTS_MAX)

Expand Down
17 changes: 12 additions & 5 deletions packages/api/src/platforms/vtex/resolvers/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import type { Resolver } from '..'
import type { Brand } from '../clients/commerce/types/Brand'
import type { CategoryTree } from '../clients/commerce/types/CategoryTree'
import type { ValidPortalPagetype } from '../clients/commerce/types/Portal'
import { throwIfPageTypeNotFound } from '../loaders/pagetype'

type Root = Brand | (CategoryTree & { level: number }) | ValidPortalPagetype

const isBrand = (x: any): x is Brand => x.type === 'brand'

const isValidPortalPageType = (x: any): x is ValidPortalPagetype =>
export const isValidPortalPageType = (x: any): x is ValidPortalPagetype =>
typeof x.pageType === 'string' && x.pageType !== 'NotFound'

const slugify = (root: Root) => {
Expand Down Expand Up @@ -78,17 +79,23 @@ export const StoreCollection: Record<string, Resolver<Root>> = {
segments.slice(0, index + 1).join('/')
)

const validPageTypes = (await pagetypeLoader.loadMany(slugs)).filter(
isValidPortalPageType
const pageTypePromises = await Promise.allSettled(
slugs.map((slugSegment) => pagetypeLoader.load(slugSegment))
)

throwIfPageTypeNotFound(pageTypePromises, slugs)

const pageTypes = (pageTypePromises as Array<
PromiseFulfilledResult<ValidPortalPagetype>
>).map((pageType) => pageType.value)

return {
itemListElement: validPageTypes.map((pageType, index) => ({
itemListElement: pageTypes.map((pageType, index) => ({
item: new URL(`https://${pageType.url}`).pathname.toLowerCase(),
name: pageType.name,
position: index + 1,
})),
numberOfItems: validPageTypes.length,
numberOfItems: pageTypes.length,
}
},
}

0 comments on commit f613efc

Please sign in to comment.