Skip to content

A typescript library to help implementing a cart with background synchronization

License

Notifications You must be signed in to change notification settings

shopinvader/shopinvader-js-cart

Repository files navigation

@shopinvader/cart

An asynchronous cart object, that works when the ERP is offline, by storing transactions that add or remove product quantities from the cart in local storage, and sending them to the ERP when it becomes offline, using the /cart/v2/sync endpoint.

Changes to the cart are notified to consumer (such as UI components) via an observer parttern.

/!\ this is WIP - TODO:

  • check that the cart uuid we have in local storage matches the cart uuid we got from the erp, to avoid applying transactions on the wrong cart in the client - I think we should discard the local storage if the cart uuid differs from the one we got from the erp

Installation

npm i shopinvader-js-cart

Usage

You first need an ErpFetch object from shopinvader-js, which encapsulate the ERP authentication mechanism.

You then create a cart object:

const cart = Cart(
  erpFetch,
  new WebStorageCartStorage(window.localStorage)
);
cart.registerObjserver(observer);
cart.addTransaction(new CartTransaction(productId, qty));
cartData = cart.getData();
// cartData.hasPendingTransactions indicates that transactions are pending synchronization
// cartData.syncError indicates that there was an error syncing the cart with the ERP

Linting and formatting

To scan the project for linting and formatting errors, run

npm run lint

To automatically fix linting and formatting errors, run

npm run format

Testing with Web Test Runner

To execute a single test run:

npm run test

To run the tests in interactive watch mode run:

npm run test:watch

Tooling configs

For most of the tools, the configuration is in the package.json to reduce the amount of files in your project.

If you customize the configuration a lot, you can consider moving them to individual files.

Local Demo with web-dev-server

npm start

To run a local development server that serves the basic demo located in demo/index.html

This works best when the demo backend server is running. To do this:

cd demo
python3 -m venv .venv
source .venv/bin/activate
python3 -m pip install requirements.txt.in
python3 demoserver.py

Authors

About

A typescript library to help implementing a cart with background synchronization

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •