From d3fe16e1d7b9bb09a347916c2d0d77be1b52adcc Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Fri, 26 Mar 2021 15:50:31 -0600 Subject: [PATCH] Updated commerce types --- framework/bigcommerce/api/cart/index.ts | 26 +-- framework/bigcommerce/types.ts | 58 ------- framework/bigcommerce/types/cart.ts | 74 ++++++++ framework/bigcommerce/types/index.ts | 2 + framework/commerce/api/index.ts | 2 +- framework/commerce/cart/use-cart.tsx | 2 +- framework/commerce/types.ts | 216 ------------------------ framework/commerce/types/cart.ts | 3 +- framework/commerce/types/index.ts | 52 ------ 9 files changed, 82 insertions(+), 353 deletions(-) delete mode 100644 framework/bigcommerce/types.ts create mode 100644 framework/bigcommerce/types/cart.ts create mode 100644 framework/bigcommerce/types/index.ts delete mode 100644 framework/commerce/types.ts diff --git a/framework/bigcommerce/api/cart/index.ts b/framework/bigcommerce/api/cart/index.ts index 091cabf5a..3eb330643 100644 --- a/framework/bigcommerce/api/cart/index.ts +++ b/framework/bigcommerce/api/cart/index.ts @@ -1,32 +1,12 @@ import type { GetAPISchema } from '@commerce/api' -import type { AddItemOperation } from '@commerce/types' +import type { CartSchema } from '../../types/cart' +import type { CommerceAPI } from '..' import getCart from './get-cart' import addItem from './add-item' import updateItem from './update-item' import removeItem from './remove-item' -import type { - GetCartHandlerBody, - AddCartItemHandlerBody, - UpdateCartItemHandlerBody, - RemoveCartItemHandlerBody, - Cart, -} from '../../types' -import type { CommerceAPI } from '..' -export type CartAPI = GetAPISchema< - CommerceAPI, - { - endpoint: { - options: {} - operations: { - getCart: { data: Cart | null; body: GetCartHandlerBody } - addItem: { data: Cart; body: AddItemOperation['body'] } - updateItem: { data: Cart; body: UpdateCartItemHandlerBody } - removeItem: { data: Cart; body: RemoveCartItemHandlerBody } - } - } - } -> +export type CartAPI = GetAPISchema export type CartEndpoint = CartAPI['endpoint'] diff --git a/framework/bigcommerce/types.ts b/framework/bigcommerce/types.ts deleted file mode 100644 index beeab0223..000000000 --- a/framework/bigcommerce/types.ts +++ /dev/null @@ -1,58 +0,0 @@ -import * as Core from '@commerce/types' - -// TODO: this type should match: -// https://developer.bigcommerce.com/api-reference/cart-checkout/server-server-cart-api/cart/getacart#responses -export type BigcommerceCart = { - id: string - parent_id?: string - customer_id: number - email: string - currency: { code: string } - tax_included: boolean - base_amount: number - discount_amount: number - cart_amount: number - line_items: { - custom_items: any[] - digital_items: any[] - gift_certificates: any[] - physical_items: any[] - } - created_time: string - discounts?: { id: number; discounted_amount: number }[] - // TODO: add missing fields -} - -export type Cart = Core.Cart & { - lineItems: LineItem[] -} - -export type LineItem = Core.LineItem - -/** - * Cart mutations - */ - -export type OptionSelections = { - option_id: number - option_value: number | string -} - -export type CartItemBody = Core.CartItemBody & { - productId: string // The product id is always required for BC - optionSelections?: OptionSelections -} - -export type GetCartHandlerBody = Core.GetCartHandlerBody - -export type AddCartItemBody = Core.AddCartItemBody - -export type AddCartItemHandlerBody = Core.AddCartItemHandlerBody - -export type UpdateCartItemBody = Core.UpdateCartItemBody - -export type UpdateCartItemHandlerBody = Core.UpdateCartItemHandlerBody - -export type RemoveCartItemBody = Core.RemoveCartItemBody - -export type RemoveCartItemHandlerBody = Core.RemoveCartItemHandlerBody diff --git a/framework/bigcommerce/types/cart.ts b/framework/bigcommerce/types/cart.ts new file mode 100644 index 000000000..da2f13da9 --- /dev/null +++ b/framework/bigcommerce/types/cart.ts @@ -0,0 +1,74 @@ +import * as Core from '@commerce/types/cart' + +export * from '@commerce/types/cart' + +// TODO: this type should match: +// https://developer.bigcommerce.com/api-reference/cart-checkout/server-server-cart-api/cart/getacart#responses +export type BigcommerceCart = { + id: string + parent_id?: string + customer_id: number + email: string + currency: { code: string } + tax_included: boolean + base_amount: number + discount_amount: number + cart_amount: number + line_items: { + custom_items: any[] + digital_items: any[] + gift_certificates: any[] + physical_items: any[] + } + created_time: string + discounts?: { id: number; discounted_amount: number }[] + // TODO: add missing fields +} + +/** + * Extend core cart types + */ + +export type Cart = Core.Cart & { + lineItems: Core.LineItem[] +} + +export type OptionSelections = { + option_id: number + option_value: number | string +} + +export type CartItemBody = Core.CartItemBody & { + productId: string // The product id is always required for BC + optionSelections?: OptionSelections +} + +export type CartHooks = Core.CartHooks & { + getCart: { data: Cart | null } + addItem: { data: Cart; body: { item: CartItemBody } } + updateItem: { data: Cart; body: { item: CartItemBody } } + removeItem: { data: Cart | null } +} + +export type GetCartHook = CartHooks['getCart'] +export type AddItemHook = CartHooks['addItem'] +export type UpdateItemHook = CartHooks['updateItem'] +export type RemoveItemHook = CartHooks['removeItem'] + +export type CartSchema = Core.CartSchema & { + endpoint: { + operations: CartOperations + } +} + +export type CartOperations = { + getCart: GetCartHook + addItem: AddItemHook + updateItem: UpdateItemHook + removeItem: RemoveItemHook +} + +export type GetCartOperation = CartOperations['getCart'] +export type AddItemOperation = CartOperations['addItem'] +export type UpdateItemOperation = CartOperations['updateItem'] +export type RemoveItemOperation = CartOperations['removeItem'] diff --git a/framework/bigcommerce/types/index.ts b/framework/bigcommerce/types/index.ts new file mode 100644 index 000000000..a0792d798 --- /dev/null +++ b/framework/bigcommerce/types/index.ts @@ -0,0 +1,2 @@ +export * from '@commerce/types' +export * from './cart' diff --git a/framework/commerce/api/index.ts b/framework/commerce/api/index.ts index 1d200fc82..ed3e80378 100644 --- a/framework/commerce/api/index.ts +++ b/framework/commerce/api/index.ts @@ -1,7 +1,7 @@ import type { NextApiHandler } from 'next' import type { RequestInit, Response } from '@vercel/fetch' import type { APIEndpoint, APIHandler } from './utils/types' -import type { CartSchema } from '../types' +import type { CartSchema } from '../types/cart' export type APISchemas = CartSchema diff --git a/framework/commerce/cart/use-cart.tsx b/framework/commerce/cart/use-cart.tsx index fbed715c8..98ab9713d 100644 --- a/framework/commerce/cart/use-cart.tsx +++ b/framework/commerce/cart/use-cart.tsx @@ -1,7 +1,7 @@ import Cookies from 'js-cookie' import { useHook, useSWRHook } from '../utils/use-hook' import type { HookFetcherFn, SWRHook } from '../utils/types' -import type { Cart } from '../types' +import type { Cart } from '../types/cart' import { Provider, useCommerce } from '..' export type FetchCartInput = { diff --git a/framework/commerce/types.ts b/framework/commerce/types.ts deleted file mode 100644 index c48c66f2b..000000000 --- a/framework/commerce/types.ts +++ /dev/null @@ -1,216 +0,0 @@ -import type { Wishlist as BCWishlist } from '../bigcommerce/api/wishlist' -import type { Customer as BCCustomer } from '../bigcommerce/api/customers' -import type { SearchProductsData as BCSearchProductsData } from '../bigcommerce/api/catalog/products' - -export type Discount = { - // The value of the discount, can be an amount or percentage - value: number -} - -export type LineItem = { - id: string - variantId: string - productId: string - name: string - quantity: number - discounts: Discount[] - // A human-friendly unique string automatically generated from the product’s name - path: string - variant: ProductVariant -} - -export type Measurement = { - value: number - unit: 'KILOGRAMS' | 'GRAMS' | 'POUNDS' | 'OUNCES' -} - -export type Image = { - url: string - altText?: string - width?: number - height?: number -} - -export type 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 type 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[] -} - -// TODO: Properly define this type -export interface Wishlist extends BCWishlist {} - -// TODO: Properly define this type -export interface Customer extends BCCustomer {} - -// TODO: Properly define this type -export interface SearchProductsData extends BCSearchProductsData {} - -/** - * Cart mutations - */ - -// Base cart item body used for cart mutations -export type CartItemBody = { - variantId: string - productId?: string - quantity?: number -} - -// Body used by the `getCart` operation handler -export type GetCartHandlerBody = { - cartId?: string -} - -// Body used by the add item to cart operation -export type AddCartItemBody = { - item: T -} - -// Body expected by the add item to cart operation handler -export type AddCartItemHandlerBody = Partial< - AddCartItemBody -> & { - cartId?: string -} - -// Body used by the update cart item operation -export type UpdateCartItemBody = { - itemId: string - item: T -} - -// Body expected by the update cart item operation handler -export type UpdateCartItemHandlerBody = Partial< - UpdateCartItemBody -> & { - cartId?: string -} - -// Body used by the remove cart item operation -export type RemoveCartItemBody = { - itemId: string -} - -// Body expected by the remove cart item operation handler -export type RemoveCartItemHandlerBody = Partial & { - cartId?: string -} - -/** - * Temporal types - */ - -interface Entity { - id: string | number - [prop: string]: any -} - -export interface Product2 { - id: string - name: string - description: string - sku?: string - slug?: string - path?: string - images: ProductImage[] - variants: ProductVariant2[] - price: ProductPrice - options: ProductOption[] -} - -export interface Product extends Entity { - name: string - description: string - slug?: string - path?: string - images: ProductImage[] - variants: ProductVariant2[] - price: ProductPrice - options: ProductOption[] - sku?: string -} - -interface ProductOption extends Entity { - displayName: string - values: ProductOptionValues[] -} - -interface ProductOptionValues { - label: string - hexColors?: string[] -} - -interface ProductImage { - url: string - alt?: string -} - -interface ProductVariant2 { - id: string | number - options: ProductOption[] -} - -interface ProductPrice { - value: number - currencyCode: 'USD' | 'ARS' | string | undefined - retailPrice?: number - salePrice?: number - listPrice?: number - extendedSalePrice?: number - extendedListPrice?: number -} diff --git a/framework/commerce/types/cart.ts b/framework/commerce/types/cart.ts index f2873b005..82959bf78 100644 --- a/framework/commerce/types/cart.ts +++ b/framework/commerce/types/cart.ts @@ -129,8 +129,7 @@ export type CartOperations = { removeItem: RemoveItemOperation } -export type GetCartOperation = { - data: Cart | null +export type GetCartOperation = GetCartHook & { body: { cartId?: string } } diff --git a/framework/commerce/types/index.ts b/framework/commerce/types/index.ts index 057b4c420..e4affba6e 100644 --- a/framework/commerce/types/index.ts +++ b/framework/commerce/types/index.ts @@ -2,7 +2,6 @@ import type { Wishlist as BCWishlist } from '../../bigcommerce/api/wishlist' import type { Customer as BCCustomer } from '../../bigcommerce/api/customers' import type { SearchProductsData as BCSearchProductsData } from '../../bigcommerce/api/catalog/products' -export * from './cart' export * from './common' // TODO: Properly define this type @@ -14,57 +13,6 @@ export interface Customer extends BCCustomer {} // TODO: Properly define this type export interface SearchProductsData extends BCSearchProductsData {} -/** - * Cart mutations - */ - -// Base cart item body used for cart mutations -export type CartItemBody = { - variantId: string - productId?: string - quantity?: number -} - -// Body used by the `getCart` operation handler -export type GetCartHandlerBody = { - cartId?: string -} - -// Body used by the add item to cart operation -export type AddCartItemBody = { - item: T -} - -// Body expected by the add item to cart operation handler -export type AddCartItemHandlerBody = Partial< - AddCartItemBody -> & { - cartId?: string -} - -// Body used by the update cart item operation -export type UpdateCartItemBody = { - itemId: string - item: T -} - -// Body expected by the update cart item operation handler -export type UpdateCartItemHandlerBody = Partial< - UpdateCartItemBody -> & { - cartId?: string -} - -// Body used by the remove cart item operation -export type RemoveCartItemBody = { - itemId: string -} - -// Body expected by the remove cart item operation handler -export type RemoveCartItemHandlerBody = Partial & { - cartId?: string -} - /** * Temporal types */