Created
August 24, 2018 07:59
-
-
Save fl0w/ccbf85b420b0042c6ea65ca5e44bbca3 to your computer and use it in GitHub Desktop.
Adds Router.expand which accepts and expands Router instances for Router.use.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const { flatten } = require('lodash') | |
const Router = require('koa-router') | |
/** | |
* Adds Router.expand which accepts and expands Router instances for Router.use. | |
* @returns this | |
*/ | |
Router.prototype.expand = function (...args) { | |
return this.use( | |
...flatten( | |
args.map( | |
mw => mw instanceof Router | |
? [mw.routes(), mw.allowedMethods()] | |
: mw | |
) | |
) | |
) | |
} | |
/** | |
* @example | |
* const Router = require('koa-router') | |
* const router = new Router() | |
* const subRouter = new Router() | |
* subRouter.get(...) | |
* subRouter.post(...) | |
* router.use(subRouter) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment