Hyperapp Middleware
A 0.4 KB utility belt to build higher-order app
s (HOAp) for Hyperapp using higher-order actions
(HOAc) known as middleware
.
Installation
Node.js
Install with npm / Yarn.
npm i hyperapp-middleware
Then with a module bundler like rollup or webpack, use as you would anything else.
Or using require.
const enhance =
Browser
Download the minified library from the CDN.
You can find the library in window.middleware
.
API
First, a few formal definitions with the shape of some common types of interest.
- An
ActionResult
is aState
slice,Thunk
, empty object, or falsy value - An
Action
is a function that receives the currentState
,Actions
, anddata
from theAction
call. It returns anActionResult
ActionInfo
is an object that describes a call to anAction
by itsname
and whatdata
was passed to theAction
function- An
HOAc
/Middleware
is a function that takes anAction
function as its argument and returns anotherAction
function, or a falsy value to use the result of the originalAction
instead of theMiddleware
- An
HOAp
is a function that takes anApp
function and returns anotherApp
function
ActionResult = Partial<State> | Thunk | {} | falsy: ActionResultActionInfo = { name: string data: any }HOAc = : Action | falsyHOAp = function(App) : App
enhance
The most basic primitive used for applying middleware
to actions
in an app. This is a function that accepts either a singular middleware
, or an array of such functions, and returns an HOAp.
: HOAp
Usage
Bear in mind that this option comes with the most power and therefore you have the most responsibility when using it.
// To pass multiple middleware, use an arrayapp...
makeResolve
This HOHOAc (higher-order higher-order action) is a helper for creating HOAcs that are used for reacting to/modifying the results ofAction
s. The Resolver
passed to makeResolve
is used to create the Resolve
function that receives Action
results or a falsy value to skip this entirely. An HOAc is returned which can be converted to a usable HOAp with enhance
.
Resolver = function(ActionInfo) : Resolve | falsymakeResolve = function(Resolver): HOAc
Usage
Here is an example of using makeResolve
to support actions returning a Promise
for async updates.
app...
makeUpdate
This function works similiarly to makeResolve
except that it is called after a sync/async Action
completes and is requesting to update the State
. The Updater
passed to makeUpdate
is used to create the Update
function for validating/modifying state updates or a falsy value to skip this entirely. An HOAc is returned which can be converted to a usable HOAp with enhance
.
Updater = function(ActionInfo) : Update | falsymakeUpdate = function(Updater): HOAc
Usage
Here is an example of using makeUpdate
to only allow state updates that include the valid
property.
app...
License
Hyperapp Middleware is MIT licensed. See LICENSE.