forked from crowetic/commerce
Switch away from global types
This commit is contained in:
parent
2ffe1ab71b
commit
1de3e76513
@ -5,6 +5,7 @@ import Link from 'next/link'
|
|||||||
import s from './CartItem.module.css'
|
import s from './CartItem.module.css'
|
||||||
import { Trash, Plus, Minus } from '@components/icons'
|
import { Trash, Plus, Minus } from '@components/icons'
|
||||||
import { useUI } from '@components/ui/context'
|
import { useUI } from '@components/ui/context'
|
||||||
|
import type { LineItem } from '@framework/types'
|
||||||
import usePrice from '@framework/product/use-price'
|
import usePrice from '@framework/product/use-price'
|
||||||
import useUpdateItem from '@framework/cart/use-update-item'
|
import useUpdateItem from '@framework/cart/use-update-item'
|
||||||
import useRemoveItem from '@framework/cart/use-remove-item'
|
import useRemoveItem from '@framework/cart/use-remove-item'
|
||||||
|
@ -53,10 +53,7 @@ export type Cart = {
|
|||||||
export type CartHandlers = {
|
export type CartHandlers = {
|
||||||
getCart: BigcommerceHandler<Cart, { cartId?: string }>
|
getCart: BigcommerceHandler<Cart, { cartId?: string }>
|
||||||
addItem: BigcommerceHandler<Cart, { cartId?: string } & Partial<AddItemBody>>
|
addItem: BigcommerceHandler<Cart, { cartId?: string } & Partial<AddItemBody>>
|
||||||
updateItem: BigcommerceHandler<
|
updateItem: BigcommerceHandler<Cart, UpdateItemBody>
|
||||||
Cart,
|
|
||||||
{ cartId?: string } & Partial<UpdateItemBody>
|
|
||||||
>
|
|
||||||
removeItem: BigcommerceHandler<
|
removeItem: BigcommerceHandler<
|
||||||
Cart,
|
Cart,
|
||||||
{ cartId?: string } & Partial<RemoveItemBody>
|
{ cartId?: string } & Partial<RemoveItemBody>
|
||||||
|
5
framework/bigcommerce/types.d.ts
vendored
5
framework/bigcommerce/types.d.ts
vendored
@ -1,5 +0,0 @@
|
|||||||
interface Cart extends BaseCart {
|
|
||||||
lineItems: LineItem[]
|
|
||||||
}
|
|
||||||
|
|
||||||
interface LineItem extends BaseLineItem {}
|
|
11
framework/bigcommerce/types.ts
Normal file
11
framework/bigcommerce/types.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import * as Core from '@commerce/types'
|
||||||
|
|
||||||
|
export interface Cart extends Core.Cart {
|
||||||
|
lineItems: LineItem[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface LineItem extends Core.LineItem {}
|
||||||
|
|
||||||
|
export interface UpdateLineItemBody extends Core.UpdateLineItemBody {}
|
||||||
|
|
||||||
|
export interface UpdateLineItem extends Core.UpdateItemBody {}
|
@ -1,15 +1,16 @@
|
|||||||
import Cookies from 'js-cookie'
|
import Cookies from 'js-cookie'
|
||||||
import type { HookInput, HookFetcher, HookFetcherOptions } from '../utils/types'
|
import type { HookInput, HookFetcher, HookFetcherOptions } from '../utils/types'
|
||||||
import useData, { ResponseState, SwrOptions } from '../utils/use-data'
|
import useData, { ResponseState, SwrOptions } from '../utils/use-data'
|
||||||
|
import type { Cart } from '../types'
|
||||||
import { useCommerce } from '..'
|
import { useCommerce } from '..'
|
||||||
|
|
||||||
export type CartResponse<Data> = ResponseState<Data> & { isEmpty?: boolean }
|
export type CartResponse<Data> = ResponseState<Data> & { isEmpty?: boolean }
|
||||||
|
|
||||||
export type CartInput = {
|
export type CartInput = {
|
||||||
cartId?: BaseCart['id']
|
cartId?: Cart['id']
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function useCart<Data extends BaseCart | null>(
|
export default function useCart<Data extends Cart | null>(
|
||||||
options: HookFetcherOptions,
|
options: HookFetcherOptions,
|
||||||
input: HookInput,
|
input: HookInput,
|
||||||
fetcherFn: HookFetcher<Data, CartInput>,
|
fetcherFn: HookFetcher<Data, CartInput>,
|
||||||
|
99
framework/commerce/types.d.ts
vendored
99
framework/commerce/types.d.ts
vendored
@ -45,105 +45,6 @@ interface ProductPrice {
|
|||||||
extendedListPrice?: number
|
extendedListPrice?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
interface DiscountBase {
|
|
||||||
// The value of the discount, can be an amount or percentage
|
|
||||||
value: number
|
|
||||||
}
|
|
||||||
|
|
||||||
interface BaseLineItem {
|
|
||||||
id: string
|
|
||||||
variantId: string
|
|
||||||
name: string
|
|
||||||
quantity: number
|
|
||||||
discounts: DiscountBase[]
|
|
||||||
// A human-friendly unique string automatically generated from the product’s name
|
|
||||||
path: string
|
|
||||||
variant: BaseProductVariant
|
|
||||||
}
|
|
||||||
|
|
||||||
interface Measurement {
|
|
||||||
value: number
|
|
||||||
unit: 'KILOGRAMS' | 'GRAMS' | 'POUNDS' | 'OUNCES'
|
|
||||||
}
|
|
||||||
|
|
||||||
interface Image {
|
|
||||||
url: string
|
|
||||||
altText?: string
|
|
||||||
width?: number
|
|
||||||
height?: number
|
|
||||||
}
|
|
||||||
|
|
||||||
interface BaseProductVariant {
|
|
||||||
id: string
|
|
||||||
// The SKU (stock keeping unit) associated with the product variant.
|
|
||||||
sku: string
|
|
||||||
// The product variant’s title, or the product's name.
|
|
||||||
name: string
|
|
||||||
// Whether a customer needs to provide a shipping address when placing
|
|
||||||
// an order for the product variant.
|
|
||||||
requiresShipping: boolean
|
|
||||||
// The product variant’s price after all discounts are applied.
|
|
||||||
price: number
|
|
||||||
// Product variant’s price, as quoted by the manufacturer/distributor.
|
|
||||||
listPrice: number
|
|
||||||
// Image associated with the product variant. Falls back to the product image
|
|
||||||
// if no image is available.
|
|
||||||
image?: Image
|
|
||||||
// Indicates whether this product variant is in stock.
|
|
||||||
isInStock?: boolean
|
|
||||||
// Indicates if the product variant is available for sale.
|
|
||||||
availableForSale?: boolean
|
|
||||||
// The variant's weight. If a weight was not explicitly specified on the
|
|
||||||
// variant this will be the product's weight.
|
|
||||||
weight?: Measurement
|
|
||||||
// The variant's height. If a height was not explicitly specified on the
|
|
||||||
// variant, this will be the product's height.
|
|
||||||
height?: Measurement
|
|
||||||
// The variant's width. If a width was not explicitly specified on the
|
|
||||||
// variant, this will be the product's width.
|
|
||||||
width?: Measurement
|
|
||||||
// The variant's depth. If a depth was not explicitly specified on the
|
|
||||||
// variant, this will be the product's depth.
|
|
||||||
depth?: Measurement
|
|
||||||
}
|
|
||||||
|
|
||||||
// Shopping cart, a.k.a Checkout
|
|
||||||
interface BaseCart {
|
|
||||||
id: string
|
|
||||||
// ID of the customer to which the cart belongs.
|
|
||||||
customerId?: string
|
|
||||||
// The email assigned to this cart
|
|
||||||
email?: string
|
|
||||||
// The date and time when the cart was created.
|
|
||||||
createdAt: string
|
|
||||||
// The currency used for this cart
|
|
||||||
currency: { code: string }
|
|
||||||
// Specifies if taxes are included in the line items.
|
|
||||||
taxesIncluded: boolean
|
|
||||||
lineItems: BaseLineItem[]
|
|
||||||
// The sum of all the prices of all the items in the cart.
|
|
||||||
// Duties, taxes, shipping and discounts excluded.
|
|
||||||
lineItemsSubtotalPrice: number
|
|
||||||
// Price of the cart before duties, shipping and taxes.
|
|
||||||
subtotalPrice: number
|
|
||||||
// The sum of all the prices of all the items in the cart.
|
|
||||||
// Duties, taxes and discounts included.
|
|
||||||
totalPrice: number
|
|
||||||
// Discounts that have been applied on the cart.
|
|
||||||
discounts?: DiscountBase[]
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Remove this type in favor of BaseCart
|
|
||||||
interface Cart2 extends Entity {
|
|
||||||
id: string | undefined
|
|
||||||
currency: { code: string }
|
|
||||||
taxIncluded?: boolean
|
|
||||||
items: Pick<Product, 'id' | 'name' | 'prices'> & CartItem[]
|
|
||||||
subTotal: number | string
|
|
||||||
total: number | string
|
|
||||||
customerId: Customer['id']
|
|
||||||
}
|
|
||||||
|
|
||||||
interface CartItem extends Entity {
|
interface CartItem extends Entity {
|
||||||
quantity: number
|
quantity: number
|
||||||
productId: Product['id']
|
productId: Product['id']
|
||||||
|
108
framework/commerce/types.ts
Normal file
108
framework/commerce/types.ts
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
export interface Discount {
|
||||||
|
// The value of the discount, can be an amount or percentage
|
||||||
|
value: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface LineItem {
|
||||||
|
id: string
|
||||||
|
variantId: string
|
||||||
|
name: string
|
||||||
|
quantity: number
|
||||||
|
discounts: Discount[]
|
||||||
|
// A human-friendly unique string automatically generated from the product’s name
|
||||||
|
path: string
|
||||||
|
variant: ProductVariant
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Measurement {
|
||||||
|
value: number
|
||||||
|
unit: 'KILOGRAMS' | 'GRAMS' | 'POUNDS' | 'OUNCES'
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Image {
|
||||||
|
url: string
|
||||||
|
altText?: string
|
||||||
|
width?: number
|
||||||
|
height?: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ProductVariant {
|
||||||
|
id: string
|
||||||
|
// The SKU (stock keeping unit) associated with the product variant.
|
||||||
|
sku: string
|
||||||
|
// The product variant’s title, or the product's name.
|
||||||
|
name: string
|
||||||
|
// Whether a customer needs to provide a shipping address when placing
|
||||||
|
// an order for the product variant.
|
||||||
|
requiresShipping: boolean
|
||||||
|
// The product variant’s price after all discounts are applied.
|
||||||
|
price: number
|
||||||
|
// Product variant’s price, as quoted by the manufacturer/distributor.
|
||||||
|
listPrice: number
|
||||||
|
// Image associated with the product variant. Falls back to the product image
|
||||||
|
// if no image is available.
|
||||||
|
image?: Image
|
||||||
|
// Indicates whether this product variant is in stock.
|
||||||
|
isInStock?: boolean
|
||||||
|
// Indicates if the product variant is available for sale.
|
||||||
|
availableForSale?: boolean
|
||||||
|
// The variant's weight. If a weight was not explicitly specified on the
|
||||||
|
// variant this will be the product's weight.
|
||||||
|
weight?: Measurement
|
||||||
|
// The variant's height. If a height was not explicitly specified on the
|
||||||
|
// variant, this will be the product's height.
|
||||||
|
height?: Measurement
|
||||||
|
// The variant's width. If a width was not explicitly specified on the
|
||||||
|
// variant, this will be the product's width.
|
||||||
|
width?: Measurement
|
||||||
|
// The variant's depth. If a depth was not explicitly specified on the
|
||||||
|
// variant, this will be the product's depth.
|
||||||
|
depth?: Measurement
|
||||||
|
}
|
||||||
|
|
||||||
|
// Shopping cart, a.k.a Checkout
|
||||||
|
export interface Cart {
|
||||||
|
id: string
|
||||||
|
// ID of the customer to which the cart belongs.
|
||||||
|
customerId?: string
|
||||||
|
// The email assigned to this cart
|
||||||
|
email?: string
|
||||||
|
// The date and time when the cart was created.
|
||||||
|
createdAt: string
|
||||||
|
// The currency used for this cart
|
||||||
|
currency: { code: string }
|
||||||
|
// Specifies if taxes are included in the line items.
|
||||||
|
taxesIncluded: boolean
|
||||||
|
lineItems: LineItem[]
|
||||||
|
// The sum of all the prices of all the items in the cart.
|
||||||
|
// Duties, taxes, shipping and discounts excluded.
|
||||||
|
lineItemsSubtotalPrice: number
|
||||||
|
// Price of the cart before duties, shipping and taxes.
|
||||||
|
subtotalPrice: number
|
||||||
|
// The sum of all the prices of all the items in the cart.
|
||||||
|
// Duties, taxes and discounts included.
|
||||||
|
totalPrice: number
|
||||||
|
// Discounts that have been applied on the cart.
|
||||||
|
discounts?: Discount[]
|
||||||
|
}
|
||||||
|
|
||||||
|
// interface OptionSelections {
|
||||||
|
// option_id: number
|
||||||
|
// option_value: number | string
|
||||||
|
// }
|
||||||
|
|
||||||
|
export interface LineItemBody {
|
||||||
|
productId: string
|
||||||
|
variantId: string
|
||||||
|
quantity?: number
|
||||||
|
// optionSelections?: OptionSelections
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface UpdateLineItemBody {
|
||||||
|
itemId: string
|
||||||
|
item: LineItemBody
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface UpdateItemBody extends Partial<UpdateLineItemBody> {
|
||||||
|
cartId?: string
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user