Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.