Lloyd Brookes
9 years ago
6 changed files with 38 additions and 122 deletions
-
15bin/cli.js
-
24lib/default-stack.js
-
13lib/local-web-server.js
-
58lib/middleware-stack.js
-
43lib/middleware.js
-
7package.json
@ -1,58 +0,0 @@ |
|||||
'use strict' |
|
||||
const arrayify = require('array-back') |
|
||||
const path = require('path') |
|
||||
const url = require('url') |
|
||||
const debug = require('./debug') |
|
||||
const mw = require('./middleware') |
|
||||
const t = require('typical') |
|
||||
const compose = require('koa-compose') |
|
||||
const flatten = require('reduce-flatten') |
|
||||
|
|
||||
/** |
|
||||
* @module middleware-stack |
|
||||
*/ |
|
||||
|
|
||||
/** |
|
||||
* @extends Array |
|
||||
* @alias module:middleware-stack |
|
||||
*/ |
|
||||
class MiddlewareStack extends Array { |
|
||||
/** |
|
||||
* @param {module:middleware-stack~middleware} |
|
||||
* @chainable |
|
||||
*/ |
|
||||
add (middleware) { |
|
||||
this.push(middleware) |
|
||||
return this |
|
||||
} |
|
||||
|
|
||||
getOptionDefinitions () { |
|
||||
return this |
|
||||
.filter(mw => mw.optionDefinitions) |
|
||||
.map(mw => mw.optionDefinitions) |
|
||||
.reduce(flatten, []) |
|
||||
.map(def => { |
|
||||
def.group = 'middleware' |
|
||||
return def |
|
||||
}) |
|
||||
} |
|
||||
compose (options) { |
|
||||
const convert = require('koa-convert') |
|
||||
const middlewareStack = this |
|
||||
.filter(mw => mw.middleware) |
|
||||
.map(mw => mw.middleware) |
|
||||
.map(middleware => middleware(options)) |
|
||||
.filter(middleware => middleware) |
|
||||
.reduce(flatten, []) |
|
||||
.map(convert) |
|
||||
return compose(middlewareStack) |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
module.exports = MiddlewareStack |
|
||||
|
|
||||
/** |
|
||||
* @typedef middleware |
|
||||
* @property optionDefinitions {object|object[]} |
|
||||
* @property middleware {function} |
|
||||
*/ |
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue