More provider fixes

This commit is contained in:
Catalin Pinte 2022-09-13 10:24:26 +03:00
parent 8eedf41879
commit 261442d49d
13 changed files with 33 additions and 30 deletions

View File

@ -12,7 +12,7 @@ function normalizeProductOption(productOption: any) {
} = productOption
return {
id: entityId,
id: String(entityId),
values: edges?.map(({ node }: any) => node),
...rest,
}
@ -41,7 +41,7 @@ export function normalizeProduct(productNode: any): Product {
variants: {
$apply: ({ edges }: any) =>
edges?.map(({ node: { entityId, productOptions, ...rest } }: any) => ({
id: entityId,
id: String(entityId),
options: productOptions?.edges
? productOptions.edges.map(normalizeProductOption)
: [],

View File

@ -32,7 +32,7 @@ export const getErrorMessage = (error: unknown) => {
export const getOperationError = (operation: string, error: unknown) => {
if (error instanceof ZodError) {
error = new CommerceError({
return new CommerceError({
code: 'SCHEMA_VALIDATION_ERROR',
message:
`The ${operation} operation returned invalid data and has ${
@ -49,12 +49,5 @@ export const getOperationError = (operation: string, error: unknown) => {
})
}
if (error instanceof Error) {
return new CommerceError({
code: 'OPERATION_ERROR',
message: `An unexpected error ocurred with the ${operation} operation: ${error.message}`,
})
}
return error
}

View File

@ -1,6 +1,7 @@
import type { AllowedOperations, OperationsData } from '../operations'
import { z } from 'zod'
import { getOperationError } from './errors'
import { pageSchema } from '../../schemas/page'
import { siteInfoSchema } from '../../schemas/site'
@ -12,13 +13,15 @@ export const withSchemaParser =
fn: (...args: any[]) => Promise<OperationsData>
) =>
async (...args: any[]) => {
try {
const result = await fn(...args)
try {
parse(operation, result)
return result
} catch (error) {
return Promise.reject(getOperationError(operation, error))
throw getOperationError(operation, error)
}
return result
}
const parse = (operation: AllowedOperations, data: OperationsData) => {

View File

@ -2,7 +2,7 @@ import { Product } from '@vercel/commerce/types/product'
import { GetAllProductsOperation } from '@vercel/commerce/types/product'
import type { OperationContext } from '@vercel/commerce/api/operations'
import type { KiboCommerceConfig } from '../index'
import { getAllProductsQuery } from '../queries/get-all-products-query';
import { getAllProductsQuery } from '../queries/get-all-products-query'
import { normalizeProduct } from '../../lib/normalize'
export default function getAllProductsOperation({
@ -18,11 +18,12 @@ export default function getAllProductsOperation({
config?: Partial<KiboCommerceConfig>
preview?: boolean
} = {}): Promise<{ products: Product[] | any[] }> {
const cfg = commerce.getConfig(config)
const { data } = await cfg.fetch(query);
const { data } = await cfg.fetch(query)
let normalizedProducts = data.products.items ? data.products.items.map( (item:any) => normalizeProduct(item, cfg)) : [];
let normalizedProducts = data.products.items
? data.products.items.map((item: any) => normalizeProduct(item, cfg))
: []
return {
products: normalizedProducts,

View File

@ -1,7 +1,7 @@
import { OperationContext } from '@vercel/commerce/api/operations'
import { Category } from '@vercel/commerce/types/site'
import { KiboCommerceConfig } from '../index'
import {categoryTreeQuery} from '../queries/get-categories-tree-query'
import { categoryTreeQuery } from '../queries/get-categories-tree-query'
import { normalizeCategory } from '../../lib/normalize'
export type GetSiteInfoResult<
@ -11,9 +11,11 @@ export type GetSiteInfoResult<
}
> = T
export default function getSiteInfoOperation({commerce}: OperationContext<any>) {
export default function getSiteInfoOperation({
commerce,
}: OperationContext<any>) {
async function getSiteInfo({
query= categoryTreeQuery,
query = categoryTreeQuery,
variables,
config,
}: {
@ -23,12 +25,13 @@ export default function getSiteInfoOperation({commerce}: OperationContext<any>)
preview?: boolean
} = {}): Promise<GetSiteInfoResult> {
const cfg = commerce.getConfig(config)
const { data } = await cfg.fetch(query);
const categories= data.categories.items.map(normalizeCategory);
return Promise.resolve({
const { data } = await cfg.fetch(query)
const categories = data.categories.items.map(normalizeCategory)
return {
categories: categories ?? [],
brands: [],
})
}
}
return getSiteInfo

View File

@ -43,7 +43,6 @@ export function normalizeProduct(productNode: any, config: any): any {
displayName: o.attributeDetail.name,
values: o.values.map((v: any) => ({
label: v.value.toString(),
hexColors: '',
})),
})) || [],
}

View File

@ -48,6 +48,7 @@ export default function getPageOperation({
? {
...page,
url: `/${locale}/${page.slug}`,
body: page.body ?? '',
}
: null,
}

View File

@ -9,6 +9,7 @@ const fetchGraphqlApi: GraphQLFetcher = async (
fetchOptions
) => {
const config = getCommerceApi().getConfig()
const res = await fetch(config.commerceUrl, {
...fetchOptions,
method: 'POST',
@ -23,6 +24,7 @@ const fetchGraphqlApi: GraphQLFetcher = async (
})
const json = await res.json()
if (json.errors) {
throw new FetcherError({
errors: json.errors ?? [{ message: 'Failed to fetch Vendure API' }],

View File

@ -44,7 +44,7 @@ export const fetcher: Fetcher = async ({
if (res.ok) {
const { data, errors } = await res.json()
if (errors) {
throw await new FetcherError({ status: res.status, errors })
throw new FetcherError({ status: res.status, errors })
}
return data
}

View File

@ -8,7 +8,7 @@ export function normalizeSearchResult(item: SearchResultFragment): Product {
name: item.productName,
description: item.description,
slug: item.slug,
path: `${item.slug}`,
path: `/${item.slug}`,
images: [
{
url: item.productAsset?.preview

View File

@ -44,7 +44,7 @@ export const getCategoryPath = (path: string, brand?: string) => {
}
export const getDesignerPath = (path: string, category?: string) => {
return `/search${path ? `/designers/${path}` : ''}${
return `/search${path ? `/designers${path}` : ''}${
category ? `/${category}` : ''
}`
}

View File

@ -28,6 +28,7 @@ export async function getStaticProps({
config,
preview,
})
const { pages } = await pagesPromise
const { categories } = await siteInfoPromise
const { product } = await productPromise

View File

@ -23,8 +23,8 @@
"@components/*": ["components/*"],
"@commerce": ["../packages/commerce/src"],
"@commerce/*": ["../packages/commerce/src/*"],
"@framework": ["../packages/vendure/src"],
"@framework/*": ["../packages/vendure/src/*"]
"@framework": ["../packages/shopify/src"],
"@framework/*": ["../packages/shopify/src/*"]
}
},
"include": ["next-env.d.ts", "**/*.d.ts", "**/*.ts", "**/*.tsx", "**/*.js"],