extract koa-mock-response and lws stack
This commit is contained in:
@ -7,9 +7,27 @@ const mw = require('./middleware')
|
||||
const t = require('typical')
|
||||
const compose = require('koa-compose')
|
||||
const flatten = require('reduce-flatten')
|
||||
const MiddlewareStack = require('./middleware-stack')
|
||||
const MiddlewareStack = require('local-web-server-stack')
|
||||
const mockResponses = require('koa-mock-response')
|
||||
|
||||
class DefaultStack extends MiddlewareStack {
|
||||
addAll () {
|
||||
this
|
||||
.addCors()
|
||||
.addJson()
|
||||
.addRewrite()
|
||||
.addBodyParser()
|
||||
.addBlacklist()
|
||||
.addCache()
|
||||
.addMimeOverride()
|
||||
.addCompression()
|
||||
.addLogging()
|
||||
.addMockResponses()
|
||||
.addSpa()
|
||||
.addStatic()
|
||||
.addIndex()
|
||||
return this
|
||||
}
|
||||
/**
|
||||
* allow from any origin
|
||||
*/
|
||||
@ -194,13 +212,13 @@ class DefaultStack extends MiddlewareStack {
|
||||
}
|
||||
|
||||
if (mock.responses) {
|
||||
return mw.mockResponses(mock.route, mock.responses)
|
||||
return mockResponses(mock.route, mock.responses)
|
||||
} else if (mock.response) {
|
||||
mock.target = {
|
||||
request: mock.request,
|
||||
response: mock.response
|
||||
}
|
||||
return mw.mockResponses(mock.route, mock.target)
|
||||
return mockResponses(mock.route, mock.target)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -18,15 +18,22 @@ const tool = new CommandLineTool()
|
||||
* @alias module:local-web-server
|
||||
* @extends module:middleware-stack
|
||||
*/
|
||||
class LocalWebServer extends DefaultStack {
|
||||
class LocalWebServer {
|
||||
constructor (stack) {
|
||||
this.stack = stack || new DefaultStack()
|
||||
this.stack.addAll()
|
||||
}
|
||||
_init (options) {
|
||||
this.options = this.options || Object.assign(options || {}, collectUserOptions(this.getOptionDefinitions()))
|
||||
this.options = this.options || Object.assign(options || {}, collectUserOptions(this.stack.getOptionDefinitions()))
|
||||
}
|
||||
addStack (stack) {
|
||||
this.stack = stack
|
||||
}
|
||||
getApplication (options) {
|
||||
this._init(options)
|
||||
const Koa = require('koa')
|
||||
const app = new Koa()
|
||||
app.use(this.compose(this.options))
|
||||
app.use(this.stack.compose(this.options))
|
||||
return app
|
||||
}
|
||||
|
||||
|
@ -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}
|
||||
*/
|
@ -10,7 +10,6 @@ const debug = require('./debug')
|
||||
* @module middleware
|
||||
*/
|
||||
exports.proxyRequest = proxyRequest
|
||||
exports.mockResponses = mockResponses
|
||||
exports.mime = mime
|
||||
|
||||
function proxyRequest (route) {
|
||||
@ -58,45 +57,3 @@ function mime (mimeTypes) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function mockResponses (route, targets) {
|
||||
targets = arrayify(targets)
|
||||
const pathRe = pathToRegexp(route)
|
||||
debug('mock route: %s, targets: %s', route, targets.length)
|
||||
|
||||
return function mockResponse (ctx, next) {
|
||||
if (pathRe.test(ctx.path)) {
|
||||
const testValue = require('test-value')
|
||||
|
||||
/* find a mock with compatible method and accepts */
|
||||
let target = targets.find(target => {
|
||||
return testValue(target, {
|
||||
request: {
|
||||
method: [ ctx.method, undefined ],
|
||||
accepts: type => ctx.accepts(type)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
/* else take the first target without a request (no request means 'all requests') */
|
||||
if (!target) {
|
||||
target = targets.find(target => !target.request)
|
||||
}
|
||||
|
||||
debug(`mock path: ${ctx.path} target: ${target && target.name || 'unnamed'}`)
|
||||
|
||||
if (target) {
|
||||
if (t.isFunction(target.response)) {
|
||||
const pathMatches = ctx.path.match(pathRe).slice(1)
|
||||
return target.response.apply(null, [ctx].concat(pathMatches))
|
||||
} else if (t.isPlainObject(target.response)) {
|
||||
Object.assign(ctx.response, target.response)
|
||||
} else {
|
||||
throw new Error(`Invalid response: ${JSON.stringify(target.response)}`)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return next()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user