2.1.0 • Published 3 years ago

@nxtdtc/nxt-warehouse v2.1.0

Weekly downloads
275
License
ISC
Repository
-
Last release
3 years ago

:factory:

Warehouse

How to Use

Local Development

Best Practices and Testing

Publishing

A redux-based library to handle common front end interactions with global state and consolidate how our front ends communicate and receive data from our microservices.

The approach in this library generally follows the advice of Martin Fowler

If you are using redux, the usual approach is to have a single, global, shared store for the entire application

and redux

Try reducer composition first, and only use multiple stores if it doesn't solve your problem.

Reducers are exposed as exports and can be composed in the front end using combineReducers while also allowing the consumer to use its own reducers for app-specific logic.

Example:

import {
  AccountReducer,
  CatalogReducer,
  StompReducer,
  CartReducer,
  OrderReducer,
  StaticPageReducer,
  StorefrontReducer,
  PaypalReducer,
  NotificationReducer
} from '@nxtdtc/nxt-warehouse/lib/WarehouseReducers';

import AppSpecificReducer from './AppSpecificReducer';

const rootReducer = combineReducers({
  stomp: StompReducer,
  storefront: StorefrontReducer,
  cart: CartReducer,
  account: AccountReducer,
  order: OrderReducer,
  catalog: CatalogReducer,
  utils: NotificationReducer,
  page: StaticPageReducer,
  paypal: PaypalReducer,
  localReducer: AppSpecificReducer
});

How to Use


In the consuming front end, we create a Warehouse object which stores references to the store, apiClient, and replyTo.

const apiClient = Stomp.over(wss);
const replyTo = ObjectId();

export const sharedStore = new Warehouse(apiClient, replyTo, store);

We handle ALL incoming websocket responses like so:

const onStompConnectSuccess = async () => {
  console.log('Successfully Connected');
  store.dispatch(connectStomp({ apiClient, replyTo }));

  apiClient.subscribe(
    `/queue/${replyTo}`,
    (body) => {
      sharedStore.handleResponseFromStomp(body);
    },
    {
      'auto-delete': true
    }
  );

  ReactDOM.render(<Main />, document.querySelector('#root'));
};

Any common actions that are shared across apps including cart, account, bootstrap, storefront, staticpage, catalog and paypal actions are stored in this library.

To use a common redux action in a component:

import {
  requestSubscribeEmail,
  requestCheckEmailExistence
} from '@nxtdtc/nxt-warehouse/lib/AccountActions';

You can use all redux hooks as well as mapStateToProps as you normally would and any store updates will be reflected in your redux store.

Local Development


In the warehouse directory:

  • npm install
  • npm link
  • npm start this will start webpack with live reloading enabled

In the consuming front end directory:

  • npm link @nxtdtc/nxt-warehouse

If you run into issues where you are asked for sudo permissions you may need to make sure you are using a compatible version of node. Node Version Manager is useful tool for doing exactly what it sounds like.

Best Practices and Testing


This library is leaning towards ducks architecture without fully doing so :confused:

In an effor to simplify our redux architecture, types and reducers are stored in the same .ducks file. The naming conventions here are important as our webpack configuration has some logic to look for files ending in .actions or .ducks

Any functions which only serve to handle responses from our apiClient should follow the naming convention <"Name">Handler.js and be exposed in our Warehouse class.

There is a LOT of untested code here. If you touch a file, you should increase the test coverage there until we reach a sufficient level of coverage.

To run tests: npm test

Publishing


For the most part, we should be publishing minor versions of our library. The logic behind this is that if we need to publish a hotfix but have unwanted changes from another developer who is working on v1.1.0, we find ourselves in a position where we have to pick apart a branch to get the changes we need.

By publishing minor versions, we can more easily do hotfixes.

Scenario:

An issue is discovered on v1.0.0 which is currently in production but we have published v1.2.0 which has changes we don't want or need

To begin a hotfix:

  • checkout library at the version currently in production git checkout tags/1.0.0 or use the commit hash
  • create a branch off what is in prod git checkout -b DC-1234_hotfix
  • when your work has been approved cut a new patch version npm version patch
  • update the front end with the correct version and deploy

To cut a new version

  • checkout master git checkout master
  • create a branch to do your work
  • once your branch has been approved merge to master
  • create git tags and bump the version npm version minor
  • publish your changes npm publish
  • update any front ends which depend on your new changes

Major version npm version major should be reserved for breaking changes and should include a discussion with the reviewer to determine if your change is breaking or not. Most changes should fall into minor.

Misc


Useful commands:

  • npm run analyze - creates a visualization of our bundled assets and can be used to determine any dependencies which are hogging up space
  • npm run coverage - run the test suite and get a print out of coverage by functions, lines, branches, etc per file

Improvements

There's always room for improvement :bowtie: as this codebase was mostly ported from one of our beginning projects. Side effects should be moved to used redux-sagas or some other async library instead of being called in our actions. Dispatches with no payloads should be examined and our test coverage should reach the limits of testability.

2.1.0

3 years ago

2.0.0

3 years ago

1.56.2

3 years ago

1.56.1

3 years ago

1.56.0

3 years ago

1.55.1-DC-3830.0

3 years ago

1.53.1-alpha.0

3 years ago

1.53.0

3 years ago

1.55.0

3 years ago

1.52.0

3 years ago

1.54.0

3 years ago

1.51.0

3 years ago

1.50.0

3 years ago

1.49.0

3 years ago

1.48.0

3 years ago

1.47.0

3 years ago

1.46.0

3 years ago

1.45.0

3 years ago

1.44.0

3 years ago

1.39.0

3 years ago

1.42.0

3 years ago

1.41.0

3 years ago

1.43.0

3 years ago

1.37.0

3 years ago

1.38.0

3 years ago

1.35.0

3 years ago

1.34.0

3 years ago

1.32.0

3 years ago

1.33.0

3 years ago

1.31.0

3 years ago

1.30.0

3 years ago

1.29.0

3 years ago

1.28.0

3 years ago

1.27.0

3 years ago

1.25.0

3 years ago

1.26.0

3 years ago

1.24.0

3 years ago

1.23.0

3 years ago

1.21.0

3 years ago

1.22.0

3 years ago

1.20.0

3 years ago

1.19.0

3 years ago

1.18.0

4 years ago

1.17.0

4 years ago

1.16.0

4 years ago

1.15.0

4 years ago

1.14.0

4 years ago

1.13.0

4 years ago

1.12.0

4 years ago

1.11.0

4 years ago

1.10.0

4 years ago

1.9.0

4 years ago

1.8.0

4 years ago

1.7.0

4 years ago

1.6.0

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.0

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago