mirror of
https://github.com/vercel/commerce.git
synced 2025-05-17 15:06:59 +00:00
More provider fixes
This commit is contained in:
parent
8eedf41879
commit
261442d49d
@ -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)
|
||||
: [],
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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) => {
|
||||
|
@ -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,
|
||||
|
@ -11,7 +11,9 @@ export type GetSiteInfoResult<
|
||||
}
|
||||
> = T
|
||||
|
||||
export default function getSiteInfoOperation({commerce}: OperationContext<any>) {
|
||||
export default function getSiteInfoOperation({
|
||||
commerce,
|
||||
}: OperationContext<any>) {
|
||||
async function getSiteInfo({
|
||||
query = categoryTreeQuery,
|
||||
variables,
|
||||
@ -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
|
||||
|
@ -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: '',
|
||||
})),
|
||||
})) || [],
|
||||
}
|
||||
|
@ -48,6 +48,7 @@ export default function getPageOperation({
|
||||
? {
|
||||
...page,
|
||||
url: `/${locale}/${page.slug}`,
|
||||
body: page.body ?? '',
|
||||
}
|
||||
: null,
|
||||
}
|
||||
|
@ -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' }],
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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}` : ''
|
||||
}`
|
||||
}
|
||||
|
@ -28,6 +28,7 @@ export async function getStaticProps({
|
||||
config,
|
||||
preview,
|
||||
})
|
||||
|
||||
const { pages } = await pagesPromise
|
||||
const { categories } = await siteInfoPromise
|
||||
const { product } = await productPromise
|
||||
|
@ -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"],
|
||||
|
Loading…
x
Reference in New Issue
Block a user