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

View File

@ -35,7 +35,7 @@ const normalizePage = (
id: spreePage.id, id: spreePage.id,
name: spreePage.attributes.title, name: spreePage.attributes.title,
url: `/${usedCommerceLocale}/${spreePage.attributes.slug}`, 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 { locale, fetch } = config
const data = await fetch('content', 'list', ['pages']) const data = await fetch('content', 'list', ['pages'])
const pages = const pages =
data?.results?.map(({ slug, ...rest }: { slug: string }) => ({ data?.results?.map(
url: `/${locale}/${slug}`, ({ slug, body, ...rest }: { slug: string; body: string }) => ({
...rest, ...rest,
})) ?? [] url: `/${locale}/${slug}`,
body: body ?? '',
})
) ?? []
return { return {
pages, pages,
} }

View File

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

View File

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