Skip to content

Instantly share code, notes, and snippets.

@fl0w
Created August 24, 2018 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fl0w/ccbf85b420b0042c6ea65ca5e44bbca3 to your computer and use it in GitHub Desktop.
Save fl0w/ccbf85b420b0042c6ea65ca5e44bbca3 to your computer and use it in GitHub Desktop.
Adds Router.expand which accepts and expands Router instances for Router.use.
'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