Merge branch 'vercel:main' into main

This commit is contained in:
Iceberg verse 2022-11-28 09:52:20 +01:00 committed by GitHub
commit bd180bda2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 8 deletions

View File

@ -42,7 +42,9 @@ export class CommerceNetworkError extends Error {
} }
export const normalizeZodIssues = (issues: ZodError['issues']) => export const normalizeZodIssues = (issues: ZodError['issues']) =>
issues.map(({ path, message }) => `${message} at "${path.join('.')}" field`) issues.map(({ path, message }) =>
path.length ? `${message} at "${path.join('.')}" field` : message
)
export const getOperationError = (operation: string, error: unknown) => { export const getOperationError = (operation: string, error: unknown) => {
if (error instanceof ZodError) { if (error instanceof ZodError) {

View File

@ -21,7 +21,7 @@ export const withOperationCallback =
const parse = ({ name, data }: Operation) => { const parse = ({ name, data }: Operation) => {
switch (name) { switch (name) {
case 'getProduct': case 'getProduct':
productSchema.nullable().parse(data.product) productSchema.optional().parse(data.product)
break break
case 'getAllProducts': case 'getAllProducts':
z.array(productSchema).parse(data.products) z.array(productSchema).parse(data.products)

View File

@ -2,7 +2,6 @@ import type { GraphQLFetcher } from '@vercel/commerce/api'
import { API_URL } from '../../const' import { API_URL } from '../../const'
import { getError } from '../../utils/handle-fetch-response' import { getError } from '../../utils/handle-fetch-response'
import { getCommerceApi } from '..'
import { getToken } from '../../utils/index' import { getToken } from '../../utils/index'
const fetchGraphqlApi: GraphQLFetcher = async ( const fetchGraphqlApi: GraphQLFetcher = async (
@ -10,7 +9,6 @@ const fetchGraphqlApi: GraphQLFetcher = async (
{ variables } = {}, { variables } = {},
headers?: HeadersInit headers?: HeadersInit
) => { ) => {
const config = getCommerceApi().getConfig()
const token = getToken() const token = getToken()
const res = await fetch(API_URL!, { const res = await fetch(API_URL!, {
@ -28,10 +26,17 @@ const fetchGraphqlApi: GraphQLFetcher = async (
}), }),
}) })
const { data, errors, status } = await res.json() const { data, errors, message, type, status } = await res.json()
if (errors) { if (errors || res.status >= 400) {
throw getError(errors, status) throw getError(
errors || [
{
message: `${type ? `${type}, ` : ''}${message}`,
},
],
status || res.status
)
} }
return { data, res } return { data, res }

View File

@ -34,7 +34,9 @@ export async function getStaticProps({
const { products: relatedProducts } = await allProductsPromise const { products: relatedProducts } = await allProductsPromise
if (!product) { if (!product) {
throw new Error(`Product with slug '${params!.slug}' not found`) return {
notFound: true,
}
} }
return { return {