Fix providers after schema parse errors

This commit is contained in:
Catalin Pinte 2022-09-12 12:11:59 +03:00
parent ba0e7beb73
commit a44a6c0291
6 changed files with 19 additions and 11 deletions

View File

@ -2,10 +2,13 @@ import type {
OperationContext,
OperationOptions,
} from '@vercel/commerce/api/operations'
import type { Page, GetAllPagesOperation } from '../../types/page'
import type { GetAllPagesOperation } from '../../types/page'
import type { RecursivePartial, RecursiveRequired } from '../utils/types'
import { BigcommerceConfig, Provider } from '..'
import { definitions } from '../definitions/store-content'
import { normalizePage } from '../../lib/normalize'
export default function getAllPagesOperation({
commerce,
}: OperationContext<Provider>) {
@ -33,12 +36,14 @@ export default function getAllPagesOperation({
// RecursivePartial forces the method to check for every prop in the data, which is
// required in case there's a custom `url`
const { data } = await cfg.storeApiFetch<
RecursivePartial<{ data: Page[] }>
RecursivePartial<{ data: definitions['page_Full'][] }>
>('/v3/content/pages')
const pages = (data as RecursiveRequired<typeof data>) ?? []
return {
pages: preview ? pages : pages.filter((p) => p.is_visible),
pages: preview
? pages.map(normalizePage)
: pages.filter((p) => p.is_visible).map(normalizePage),
}
}

View File

@ -75,7 +75,7 @@ export function normalizePage(page: definitions['page_Full']): Page {
name: page.name,
is_visible: page.is_visible,
sort_order: page.sort_order,
body: page.body,
body: page.body ?? '',
}
}

View File

@ -35,7 +35,7 @@ const normalizePage = (
id: spreePage.id,
name: spreePage.attributes.title,
url: `/${usedCommerceLocale}/${spreePage.attributes.slug}`,
body: spreePage.attributes.content,
body: spreePage.attributes.content ?? '',
}
}

View File

@ -31,10 +31,13 @@ export default function getAllPagesOperation({
const { locale, fetch } = config
const data = await fetch('content', 'list', ['pages'])
const pages =
data?.results?.map(({ slug, ...rest }: { slug: string }) => ({
url: `/${locale}/${slug}`,
data?.results?.map(
({ slug, body, ...rest }: { slug: string; body: string }) => ({
...rest,
})) ?? []
url: `/${locale}/${slug}`,
body: body ?? '',
})
) ?? []
return {
pages,
}

View File

@ -210,7 +210,7 @@ function normalizeLineItem({
price: price,
listPrice: price,
},
path: '',
path: `/${product.slug}`,
discounts: [],
options: [
{

View File

@ -45,7 +45,7 @@ export function normalizeCart(order: CartFragment): Cart {
productId: l.productVariant.productId,
images: [{ url: l.featuredAsset?.preview + '?preset=thumb' || '' }],
discounts: l.discounts.map((d) => ({ value: d.amount / 100 })),
path: '',
path: `/${l.productVariant.product.slug}`,
variant: {
id: l.productVariant.id,
name: l.productVariant.name,