mirror of
https://github.com/vercel/commerce.git
synced 2025-07-26 19:51:23 +00:00
Updated types
This commit is contained in:
@@ -3,18 +3,10 @@ import { CommerceError } from '@commerce/utils/errors'
|
||||
import useAddItem, { UseAddItem } from '@commerce/cart/use-add-item'
|
||||
import useCart from './use-cart'
|
||||
import { ShopifyProvider } from '..'
|
||||
import { AddCartItemBody, CartItemBody } from '@commerce/types'
|
||||
import { Cart } from '@framework/types'
|
||||
import {
|
||||
checkoutLineItemAddMutation,
|
||||
getCheckoutId,
|
||||
getCheckoutQuery,
|
||||
} from '@framework/utils'
|
||||
import { Cart, AddCartItemBody, CartItemBody } from '../types'
|
||||
import { checkoutLineItemAddMutation, getCheckoutId } from '../utils'
|
||||
import { checkoutToCart } from './utils'
|
||||
|
||||
const defaultOpts = {
|
||||
query: checkoutLineItemAddMutation,
|
||||
}
|
||||
import { Mutation } from '../schema'
|
||||
|
||||
export default useAddItem as UseAddItem<ShopifyProvider, CartItemBody>
|
||||
|
||||
@@ -22,8 +14,7 @@ export const handler: MutationHandler<Cart, {}, AddCartItemBody> = {
|
||||
fetchOptions: {
|
||||
query: checkoutLineItemAddMutation,
|
||||
},
|
||||
async fetcher({ input, options, fetch }) {
|
||||
const item = input.item ?? input
|
||||
async fetcher({ input: { item }, options, fetch }) {
|
||||
if (
|
||||
item.quantity &&
|
||||
(!Number.isInteger(item.quantity) || item.quantity! < 1)
|
||||
@@ -33,8 +24,7 @@ export const handler: MutationHandler<Cart, {}, AddCartItemBody> = {
|
||||
})
|
||||
}
|
||||
|
||||
const data = await fetch<any, any>({
|
||||
...defaultOpts,
|
||||
const { checkoutLineItemsAdd }: Mutation = await fetch<any, any>({
|
||||
...options,
|
||||
variables: {
|
||||
lineItems: [
|
||||
@@ -47,7 +37,7 @@ export const handler: MutationHandler<Cart, {}, AddCartItemBody> = {
|
||||
},
|
||||
})
|
||||
|
||||
return checkoutToCart(data.checkoutLineItemsAdd)
|
||||
return checkoutToCart(checkoutLineItemsAdd)
|
||||
},
|
||||
useHook() {
|
||||
const { mutate } = useCart()
|
||||
|
@@ -1,14 +1,26 @@
|
||||
import { Cart } from '@commerce/types'
|
||||
import { ValidationError } from '@commerce/utils/errors'
|
||||
import { normalizeCart } from '@framework/utils/normalize'
|
||||
import { Checkout, UserError } from '@framework/schema'
|
||||
import { CommerceError, ValidationError } from '@commerce/utils/errors'
|
||||
|
||||
const checkoutToCart = (checkoutResponse: {
|
||||
checkout: Checkout
|
||||
userErrors?: UserError[]
|
||||
}): Cart => {
|
||||
const checkout = checkoutResponse?.checkout
|
||||
const userErrors = checkoutResponse?.userErrors
|
||||
import {
|
||||
CheckoutLineItemsAddPayload,
|
||||
CheckoutLineItemsUpdatePayload,
|
||||
Maybe,
|
||||
} from '@framework/schema'
|
||||
import { normalizeCart } from '@framework/utils'
|
||||
|
||||
export type CheckoutPayload =
|
||||
| CheckoutLineItemsAddPayload
|
||||
| CheckoutLineItemsUpdatePayload
|
||||
|
||||
const checkoutToCart = (checkoutPayload?: Maybe<CheckoutPayload>): Cart => {
|
||||
if (!checkoutPayload || !checkoutPayload?.checkout) {
|
||||
throw new CommerceError({
|
||||
message: 'Invalid response from Shopify',
|
||||
})
|
||||
}
|
||||
|
||||
const checkout = checkoutPayload?.checkout
|
||||
const userErrors = checkoutPayload?.userErrors
|
||||
|
||||
if (userErrors && userErrors.length) {
|
||||
throw new ValidationError({
|
||||
|
Reference in New Issue
Block a user