Browse Source

extract koa-mock-response and lws stack

master
Lloyd Brookes 9 years ago
parent
commit
fec62b7253
  1. 15
      bin/cli.js
  2. 24
      lib/default-stack.js
  3. 13
      lib/local-web-server.js
  4. 58
      lib/middleware-stack.js
  5. 43
      lib/middleware.js
  6. 7
      package.json

15
bin/cli.js

@ -3,17 +3,4 @@
const LocalWebServer = require('../') const LocalWebServer = require('../')
const ws = new LocalWebServer() const ws = new LocalWebServer()
ws.addCors()
.addJson()
.addRewrite()
.addBodyParser()
.addBlacklist()
.addCache()
.addMimeOverride()
.addCompression()
.addLogging()
.addMockResponses()
.addSpa()
.addStatic()
.addIndex()
.listen()
ws.listen()

24
lib/default-stack.js

@ -7,9 +7,27 @@ const mw = require('./middleware')
const t = require('typical') const t = require('typical')
const compose = require('koa-compose') const compose = require('koa-compose')
const flatten = require('reduce-flatten') 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 { 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 * allow from any origin
*/ */
@ -194,13 +212,13 @@ class DefaultStack extends MiddlewareStack {
} }
if (mock.responses) { if (mock.responses) {
return mw.mockResponses(mock.route, mock.responses)
return mockResponses(mock.route, mock.responses)
} else if (mock.response) { } else if (mock.response) {
mock.target = { mock.target = {
request: mock.request, request: mock.request,
response: mock.response response: mock.response
} }
return mw.mockResponses(mock.route, mock.target)
return mockResponses(mock.route, mock.target)
} }
}) })
} }

13
lib/local-web-server.js

@ -18,15 +18,22 @@ const tool = new CommandLineTool()
* @alias module:local-web-server * @alias module:local-web-server
* @extends module:middleware-stack * @extends module:middleware-stack
*/ */
class LocalWebServer extends DefaultStack {
class LocalWebServer {
constructor (stack) {
this.stack = stack || new DefaultStack()
this.stack.addAll()
}
_init (options) { _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) { getApplication (options) {
this._init(options) this._init(options)
const Koa = require('koa') const Koa = require('koa')
const app = new Koa() const app = new Koa()
app.use(this.compose(this.options))
app.use(this.stack.compose(this.options))
return app return app
} }

58
lib/middleware-stack.js

@ -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}
*/

43
lib/middleware.js

@ -10,7 +10,6 @@ const debug = require('./debug')
* @module middleware * @module middleware
*/ */
exports.proxyRequest = proxyRequest exports.proxyRequest = proxyRequest
exports.mockResponses = mockResponses
exports.mime = mime exports.mime = mime
function proxyRequest (route) { 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()
}
}
}

7
package.json

@ -16,7 +16,10 @@
"development", "development",
"cors", "cors",
"mime", "mime",
"rest"
"rest",
"mock",
"api",
"proxy"
], ],
"engines": { "engines": {
"node": ">=4.0.0" "node": ">=4.0.0"
@ -43,12 +46,14 @@
"koa-convert": "^1.2.0", "koa-convert": "^1.2.0",
"koa-etag": "^2.1.1", "koa-etag": "^2.1.1",
"koa-json": "^1.1.3", "koa-json": "^1.1.3",
"koa-mock-response": "0.0.2",
"koa-morgan": "^1.0.1", "koa-morgan": "^1.0.1",
"koa-rewrite": "^2.1.0", "koa-rewrite": "^2.1.0",
"koa-route": "^3.0.0", "koa-route": "^3.0.0",
"koa-send": "^3.2.0", "koa-send": "^3.2.0",
"koa-serve-index": "^1.1.1", "koa-serve-index": "^1.1.1",
"koa-static": "^2.0.0", "koa-static": "^2.0.0",
"local-web-server-stack": "github:75lb/local-web-server-stack",
"path-to-regexp": "^1.5.0", "path-to-regexp": "^1.5.0",
"reduce-flatten": "^1.0.0", "reduce-flatten": "^1.0.0",
"stream-log-stats": "^1.1.3", "stream-log-stats": "^1.1.3",

Loading…
Cancel
Save