4
0
forked from crowetic/commerce

Updated types

This commit is contained in:
cond0r 2021-02-18 15:49:44 +02:00
parent 89cc3d0608
commit ce5fcb6975
4 changed files with 47 additions and 46 deletions

View File

@ -3,18 +3,10 @@ import { CommerceError } from '@commerce/utils/errors'
import useAddItem, { UseAddItem } from '@commerce/cart/use-add-item' import useAddItem, { UseAddItem } from '@commerce/cart/use-add-item'
import useCart from './use-cart' import useCart from './use-cart'
import { ShopifyProvider } from '..' import { ShopifyProvider } from '..'
import { AddCartItemBody, CartItemBody } from '@commerce/types' import { Cart, AddCartItemBody, CartItemBody } from '../types'
import { Cart } from '@framework/types' import { checkoutLineItemAddMutation, getCheckoutId } from '../utils'
import {
checkoutLineItemAddMutation,
getCheckoutId,
getCheckoutQuery,
} from '@framework/utils'
import { checkoutToCart } from './utils' import { checkoutToCart } from './utils'
import { Mutation } from '../schema'
const defaultOpts = {
query: checkoutLineItemAddMutation,
}
export default useAddItem as UseAddItem<ShopifyProvider, CartItemBody> export default useAddItem as UseAddItem<ShopifyProvider, CartItemBody>
@ -22,8 +14,7 @@ export const handler: MutationHandler<Cart, {}, AddCartItemBody> = {
fetchOptions: { fetchOptions: {
query: checkoutLineItemAddMutation, query: checkoutLineItemAddMutation,
}, },
async fetcher({ input, options, fetch }) { async fetcher({ input: { item }, options, fetch }) {
const item = input.item ?? input
if ( if (
item.quantity && item.quantity &&
(!Number.isInteger(item.quantity) || item.quantity! < 1) (!Number.isInteger(item.quantity) || item.quantity! < 1)
@ -33,8 +24,7 @@ export const handler: MutationHandler<Cart, {}, AddCartItemBody> = {
}) })
} }
const data = await fetch<any, any>({ const { checkoutLineItemsAdd }: Mutation = await fetch<any, any>({
...defaultOpts,
...options, ...options,
variables: { variables: {
lineItems: [ lineItems: [
@ -47,7 +37,7 @@ export const handler: MutationHandler<Cart, {}, AddCartItemBody> = {
}, },
}) })
return checkoutToCart(data.checkoutLineItemsAdd) return checkoutToCart(checkoutLineItemsAdd)
}, },
useHook() { useHook() {
const { mutate } = useCart() const { mutate } = useCart()

View File

@ -1,14 +1,26 @@
import { Cart } from '@commerce/types' import { Cart } from '@commerce/types'
import { ValidationError } from '@commerce/utils/errors' import { CommerceError, ValidationError } from '@commerce/utils/errors'
import { normalizeCart } from '@framework/utils/normalize'
import { Checkout, UserError } from '@framework/schema'
const checkoutToCart = (checkoutResponse: { import {
checkout: Checkout CheckoutLineItemsAddPayload,
userErrors?: UserError[] CheckoutLineItemsUpdatePayload,
}): Cart => { Maybe,
const checkout = checkoutResponse?.checkout } from '@framework/schema'
const userErrors = checkoutResponse?.userErrors 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) { if (userErrors && userErrors.length) {
throw new ValidationError({ throw new ValidationError({

View File

@ -23,25 +23,24 @@ export type OptionSelections = {
option_value: number | string option_value: number | string
} }
export interface CartItemBody extends Core.CartItemBody { export type CartItemBody = Core.CartItemBody & {
productId: string // The product id is always required for BC productId: string // The product id is always required for BC
optionSelections?: OptionSelections optionSelections?: OptionSelections
} }
export interface GetCartHandlerBody extends Core.GetCartHandlerBody {} type X = Core.CartItemBody extends CartItemBody ? any : never
type Y = CartItemBody extends Core.CartItemBody ? any : never
export interface AddCartItemBody extends Core.AddCartItemBody<CartItemBody> {} export type GetCartHandlerBody = Core.GetCartHandlerBody
export interface AddCartItemHandlerBody export type AddCartItemBody = Core.AddCartItemBody<CartItemBody>
extends Core.AddCartItemHandlerBody<CartItemBody> {}
export interface UpdateCartItemBody export type AddCartItemHandlerBody = Core.AddCartItemHandlerBody<CartItemBody>
extends Core.UpdateCartItemBody<CartItemBody> {}
export interface UpdateCartItemHandlerBody export type UpdateCartItemBody = Core.UpdateCartItemBody<CartItemBody>
extends Core.UpdateCartItemHandlerBody<CartItemBody> {}
export interface RemoveCartItemBody extends Core.RemoveCartItemBody {} export type UpdateCartItemHandlerBody = Core.UpdateCartItemHandlerBody<CartItemBody>
export interface RemoveCartItemHandlerBody export type RemoveCartItemBody = Core.RemoveCartItemBody
extends Core.RemoveCartItemHandlerBody {}
export type RemoveCartItemHandlerBody = Core.RemoveCartItemHandlerBody

View File

@ -81,20 +81,20 @@ export function normalizeProduct(productNode: ShopifyProduct): any {
} }
} }
export function normalizeCart(data: Checkout): Cart { export function normalizeCart(checkout: Checkout): Cart {
return { return {
id: data.id, id: checkout.id,
customerId: '', customerId: '',
email: '', email: '',
createdAt: data.createdAt, createdAt: checkout.createdAt,
currency: { currency: {
code: data.currencyCode, code: checkout.currencyCode,
}, },
taxesIncluded: data.taxesIncluded, taxesIncluded: checkout.taxesIncluded,
lineItems: data.lineItems?.edges.map(normalizeLineItem), lineItems: checkout.lineItems?.edges.map(normalizeLineItem),
lineItemsSubtotalPrice: data.subtotalPrice, lineItemsSubtotalPrice: checkout.subtotalPrice,
subtotalPrice: data.subtotalPrice, subtotalPrice: checkout.subtotalPrice,
totalPrice: data.totalPrice, totalPrice: checkout.totalPrice,
discounts: [], discounts: [],
} }
} }