halux
v0.1.19
Published
Halux middleware for Redux
Downloads
14
Readme
halux
HAL middleware for Redux which uses HalCrawler under the hood. If your state matches the HAL conventions described in the above Repo, you can use this middleware to load nested HAL resources easily. The authors of both modules teamed up and discussed the API and application flow together.
Usage
To use this module, please install it's peer dependency HalCrawler.
$ npm install -S hal-crawler
Setup your HAL config and then install this module using:
$ npm install -S halux
For the following examples I use the config generated by Hal-Crawler.
To see a working example check out Hal-Crawler examples: HalCrawlerExample
create a Halux action
We provide a helper method for you to create a Halux action. Although it's not necessary, we highly recommend using it.
The interface for this action is:
(haluxObject: {
schema: Schema, // a Hal-crawler Schema
link?: string, // url to resource
identifiers?: {
[index: string]: any, // an object of identifiers to identify the different Resources
},
body?: any; // if body is set, then a post will be executed using the body
into?: Schema; // defines in which schema the result should be stored
overwriteStore?: boolean; // ignore state in store and always refetch resource, default: false
handlers?: { // handler to be called on a particular event. Can be a redux-action or a regular function
successHandler?: (reduxStore: any, haluxState: any, createdResource: Resource) => any,
pendingHandler?: (reduxStore: any) => any,
errorHandler?: (reduxStore: any, error: any) => any,
}
config?: any; // pass custom configuration object, nested calls will receive the same config object
}): HaluxActionI
Note that only identifiers or link should be set, not both.
You can use the method like this:
import { createHaluxAction } from 'halux';
const fetchRoot = () => createHaluxAction({
schema: root,
identifiers: undefined
});
const fetchAdmins = ({ clientId: id} ) => createHaluxAction({
schema: admins,
identifiers: {
id,
},
handlers: {
errorHandler: (reduxStore, error) => console.log(error.toString())
}
});
###nestHaluxActions
Because each Schema must be inside the root schema, nesting actions is need.
You can use the provided nestHaluxActions
method to do so.
It is possible to nest data as many layers deep as you need to, but only one level per method called
The interface is:
const nested = nestHaluxActions(
() => createHaluxAction({...}),
() => createHaluxAction({...})
);
nested({}, {}); // one object per method
Example using the above setup:
const nestedAdmins = (client) => nestHaluxActions(fetchRoot, fetchAdmins)({}, client);
// now you would dispatch this action like this:
dispatch(nestedAdmins({ clientId: 1 }))
###Reducer
This library provides its own reducer which you should insert into your state.
The reducer knows how to handle the actions created by the middleware and updates the store accordingly.
It is not recommended to use a custom reducer for managing those actions as it's considered an internal feature and may change.
To use the reducer import haluxReducer
from the middleware and combine it into your store.
Example:
import { combineReducers } from 'redux';
import { haluxReducer } from 'halux';
export const store = combineReducers({
app: {}, // your app state
data: combineReducers({
halux: haluxReducer,
somethingOther: {},
})
})
###Middleware
Finally you have to add the middleware.
There is a helper method to create the middleware which you must use.
It accepts the hal-crawler config as the first parameter and the location of the halux store inside the store as a second parameter.
The location must be a string and may be something like this: 'data.halux'
.
It's default value is data.halux
.
The helper function returns the middleware which can be passed to the createStore method.
import { createHalux } from 'halux';
import { createStore, applyMiddleware } from 'redux';
const haluxMiddleware = createHalux(config, 'data.halux');
const store = createStore(
reducers,
undefined,
applyMiddleware(haluxMiddleware)
)