• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

metalabdesign / http-middleware-metalab
96%
master: 97%

Build:
Build:
LAST BUILD BRANCH: midori
DEFAULT BRANCH: master
Repo Added 21 Jan 2016 12:34AM UTC
Files 24
Badge
Embed ▾
README BADGES
x

If you need to use a raster PNG badge, change the '.svg' to '.png' in the link

Markdown

Textile

RDoc

HTML

Rst

LAST BUILD ON BRANCH thunk
branch: thunk
CHANGE BRANCH
x
Reset
  • thunk
  • adana-tools
  • adapters
  • asset-index
  • bump-version
  • dev-server-update
  • errors
  • fix-assets
  • fix-render
  • fix-root-paths
  • header
  • host-matching
  • http-presets
  • master
  • matching
  • midori
  • more-adapter-tests
  • proxy
  • redirect
  • render
  • render-tests
  • send
  • send-body
  • send-header
  • send-promise
  • smarter-assets
  • status
  • status-buttons
  • v0.1.2

pending completion
200

push

travis-ci

izaakschroeder
Add `thunk` middleware.

This is essentially the `apply` method of an applicative.

Sometimes you want to change the processing mechanism of your application at runtime instead of construction time. For example:

```javascript
const app = compose(
  match(path('/foo'), send())
);
```

This app structure is static. That app will always match `/foo` to `send()`. What if we wanted the structure of the app to be the result of a function instead? For example, swapping between a random app per request or using routes from a database? Such a thing is now possible in relatively elegant fashion:

Swapping between two apps:

```javascript
const createApp = compose(
  thunk((app) => {
    const a = send('Hello')(app);
    const b = send('World')(app);
    return () => {
      return (Math.random() > 0.5) ? a : b;
    };
  })
);
```

Updating routes at runtime:

```javascript
const createApp = compose(
  thunk((app) => {
    let result = app;
    db.on('update', (routes) => {
      result = routes.reduce((result, route) => {
        return middlewareForRoute(route)(result);
      }, app);
    });
    return () => result;
  })
);
```

The `thunk` middleware basically defers access to the appropriate method (e.g. `request`, `error`, etc.) to the last possible moment at which point it calls the thunk to get the method at that instant.

You provide one argument to `thunk` which is your thunk creator: a function that takes, as input, an `app` and returns as output not an app but a _function_ returning an app. This function is then invoked whenever the app is actually needed (i.e. on any of the event calls like `request`).

358 of 373 relevant lines covered (95.98%)

10.33 hits per line

Relevant lines Covered
Build:
Build:
373 RELEVANT LINES 358 COVERED LINES
10.33 HITS PER LINE
Source Files on thunk
Detailed source file information is not available for this build.

Recent builds

Builds Branch Commit Type Ran Committer Via Coverage
200 thunk Add `thunk` middleware. This is essentially the `apply` method of an applicative. Sometimes you want to change the processing mechanism of your application at runtime instead of construction time. For example: ```javascript const app = compose(... push 01 Feb 2016 10:58PM UTC izaakschroeder travis-ci pending completion  
197 thunk Add example. push 01 Feb 2016 10:47PM UTC izaakschroeder travis-ci pending completion  
195 thunk Add simple spec. push 01 Feb 2016 10:18PM UTC izaakschroeder travis-ci pending completion  
193 thunk Add `thunk` middleware. Sometimes you want to change the processing mechanism of your application at runtime instead of construction time. For example: ```javascript const app = compose( match(path('/foo'), send()) ); ``` This app structure i... push 01 Feb 2016 07:46PM UTC izaakschroeder travis-ci pending completion  
See All Builds (227)
  • Repo on GitHub
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc