npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

halux

v0.1.19

Published

Halux middleware for Redux

Downloads

11

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)
)