Skip to content

Commit

Permalink
Expose pipe method
Browse files Browse the repository at this point in the history
  • Loading branch information
ramiel committed Nov 12, 2019
1 parent 8bee371 commit 8d29a94
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,26 @@ const router = createRouter()
);
```

If you just have middlewares but not an handler, you can use pipe instead.

```js
import { createRouter, compose } from 'routerjs';

const userRoute = (req, context) => {
//...
};

const router = createRouter()
.get(
'/users',
pipe(
logMiddleware,
authMiddleware,
// any other middleware
)
);
```

### 4.7. <a name='Alwayscallbacks'></a>Always callbacks

You can define a callback that is executed always, on every route and despite the fact that the request has been stopped or not.
Expand Down
5 changes: 5 additions & 0 deletions src/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ function compose(...funcs: Function[]): Function {
return funcs.reduce((a, b) => (...args: any) => a(b(...args)));
}

const noop = () => {};
export const pipe = (...funcs: Function[]): Function => {
return compose(...funcs)(noop);
};

export default compose;
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import createRouter from './router';
import BrowserHistoryEngine from './engines/BrowserHistoryEngine';
import compose from './compose';
import compose, { pipe } from './compose';

export * from './router';
export { createRouter, BrowserHistoryEngine, compose };
export { createRouter, BrowserHistoryEngine, compose, pipe };

0 comments on commit 8d29a94

Please sign in to comment.