mirror of
https://github.com/vercel/commerce.git
synced 2025-07-22 20:26:49 +00:00
handle empty cart, variants, options, errors
This commit is contained in:
@@ -13,7 +13,7 @@ export type Brands = BrandEdge[]
|
||||
|
||||
const getVendors = async (config: SwellConfig) => {
|
||||
const vendors: [string] =
|
||||
(await config.fetchSwell('attributes', 'get', ['brand'])).values ?? []
|
||||
(await config.fetchSwell('attributes', 'get', ['brand']))?.values ?? []
|
||||
|
||||
return [...new Set(vendors)].map((v) => ({
|
||||
node: {
|
||||
|
@@ -1,28 +1,19 @@
|
||||
import { FetcherError } from '@commerce/utils/errors'
|
||||
import { CommerceError } from '@commerce/utils/errors'
|
||||
|
||||
export function getError(errors: any[], status: number) {
|
||||
errors = errors ?? [{ message: 'Failed to fetch Swell API' }]
|
||||
return new FetcherError({ errors, status })
|
||||
type SwellFetchResponse = {
|
||||
error: {
|
||||
message: string
|
||||
code?: string
|
||||
}
|
||||
}
|
||||
|
||||
export async function getAsyncError(res: Response) {
|
||||
const data = await res.json()
|
||||
return getError(data.errors, res.status)
|
||||
}
|
||||
|
||||
const handleFetchResponse = async (res: Response) => {
|
||||
// if (res.ok) {
|
||||
// const { data, errors } = await res.json()
|
||||
|
||||
// if (errors && errors.length) {
|
||||
// throw getError(errors, res.status)
|
||||
// }
|
||||
|
||||
// return data
|
||||
// }
|
||||
if (res) return res
|
||||
|
||||
throw await getAsyncError(res)
|
||||
const handleFetchResponse = async (res: SwellFetchResponse) => {
|
||||
if (res) {
|
||||
if (res.error) {
|
||||
throw new CommerceError(res.error)
|
||||
}
|
||||
return res
|
||||
}
|
||||
}
|
||||
|
||||
export default handleFetchResponse
|
||||
|
@@ -76,7 +76,7 @@ const normalizeProductVariants = (
|
||||
productOptions: normalizedProductOption[]
|
||||
) => {
|
||||
return variants?.map(
|
||||
({ id, name, price, option_value_ids: optionValueIds }) => {
|
||||
({ id, name, price, option_value_ids: optionValueIds = [] }) => {
|
||||
const values = name
|
||||
.split(',')
|
||||
.map((i) => ({ name: i.trim(), label: i.trim() }))
|
||||
@@ -167,7 +167,7 @@ export function normalizeCart({
|
||||
createdAt: date_created,
|
||||
currency: { code: currency },
|
||||
taxesIncluded: tax_included_total > 0,
|
||||
lineItems: items?.map(normalizeLineItem),
|
||||
lineItems: items?.map(normalizeLineItem) ?? [],
|
||||
lineItemsSubtotalPrice: +sub_total,
|
||||
subtotalPrice: +sub_total,
|
||||
totalPrice: grand_total,
|
||||
|
Reference in New Issue
Block a user