Skip to content

Latest commit

 

History

History
111 lines (84 loc) · 3.35 KB

interfaces.md

File metadata and controls

111 lines (84 loc) · 3.35 KB

Interfaces

Item

type Item = { id: string }

The simplest representation of the ordered item. Used only one string property to aggregate/distinguish order items with each other. Potentially could be any Setoid.

Price

type Price = { price: number }

This interface used to work with the prices. Use functions addPrices(), minusPrice(), etc to work with prices. The main idea is to calculate prices (costs) without rounding issue.

Quantity

type Qty = { qty: number }

The interface supports aggregation of several order items in one item (by id), accumulating the quantity of the aggregated items.

This value considered to be integer: no rounding treatments applied.

Usually used with the Item interface (see ItemQty), but some functions do not need id in context.

Shipping

type Shipping = { shipping: number }

Represents the costs of the shipping (in different contexts). So, all calculations should be done again through the functions like addPrices(), minusPrice(), etc.

Total

type Total = { total: number }

Represents the "total costs" (in different contexts). So, all calculations should be done again through the functions like addPrices(), minusPrice(), etc.

Items

type Items<T extends Item> = { items: T[] }

Represents the list of items. Here the Item interface used to aggregate/distinguish items in the list.

Order

type Order<T extends Total | Shipping | Items<Item>> = T & {
    invoiced: T[],
    refunded: T[],
    canceled: T[],
}

This interface represents the main model of the library - the Order model. The order could be defined in several scopes (or all of them at the same time):

  • Total
  • Shipping
  • Items (could be also more specific, than just Items<Item>)

Also, there are 3 required fields with the array of elements should have the same scope (type) as the order itself: invoiced, refunded, and canceled.

Helper types (compositions)

Should be clear from the definition - just intersections (more specific types)

type ItemQty = Item & Qty

type ItemTotal = Item & Total

type CartItem = ItemQty & Price & Partial<Total>

type Cart<T extends CartItem> = Items<T> & Shipping

type CartTotals<T extends CartItem & Total> = Cart<T> & Total

Immutability

The library does not use the above interfaces but uses read-only (immutable) variants for each of them.

For example, instead of type Item = { id: string }, library uses type ItemRO = { readonly id: string }.

The full list of immutable types could be found in readonly interfaces. This proves the immutability of types used in the library.

Tests do not use read-only types. This proves that functions could be used with mutable variables as well. The immutability in tests is proved by deepFreeze (which is based on Object.freeze)

Read more