lore-hook-connect

Provides the connect decorator for data retrieval

blueprints

Extend or override the built-in blueprints. This is helpful if you want to change the interface for a getState method call.

Each blueprint is responsible for:

  1. Validating the parameters passed to the getState method
  2. Extracting the desired data from the Redux Store
  3. [optional] Calling the appropriate action if that data does not exist

Lore has seven built-in blueprints:

import all from 'lore-hook-connect/es/blueprints/all';
import byCid from 'lore-hook-connect/es/blueprints/byCid';
import byId from 'lore-hook-connect/es/blueprints/byId';
import find from 'lore-hook-connect/es/blueprints/find';
import findAll from 'lore-hook-connect/es/blueprints/findAll';
import first from 'lore-hook-connect/es/blueprints/first';
import singleton from 'lore-hook-connect/es/blueprints/singleton';

blueprints: {
  all,      // used for calls like getState('tweet.all')
  byCid,    // used for calls like getState('tweet.byCid')
  byId,     // used for calls like getState('tweet.byId')
  find,     // used for calls like getState('tweet.find')
  findAll,  // used for calls like getState('tweet.findAll')
  first,    // used for calls like getState('tweet.first')
  singleton // used for calls like getState('currentUser')
}

To illustrate how to override a blueprint, let's examine the default syntax to fetch a model by id, which looks like this:

getState('tweet.byId', {
  id: 1
})

Now let's say you wanted to modify the syntax to use a 'where' clause, to make it similar to the find blueprint like this:

getState('tweet.byId', {
  where: {
    id: 1
  }
})

You could achieve that by overriding the byId blueprint with the implementation below.

blueprints: {
  byId: {
    defaults: {
      where: {
        id: null
      }
    },

    verifyParams: function(params) {
      if (!params.where.id) {
        throw new Error('Missing required field: id');
      }
    },

    getPayload: function(reducerState, params) {
      const key = params.where.id;
      return reducerState[key];
    },

    callAction: function(action, params) {
      const id = params.where.id;
      return action(id).payload;
    }
  }
}