ali-api-gateway
Advanced tools
Comparing version 0.0.2 to 0.0.3
55
index.js
'use strict'; | ||
const GateWay = require('./src/gate-way.js'); | ||
const getSSOInfo = require('./src/get-sso-info.js'); | ||
const co = require('co'); | ||
const parseParams = require('./src/parse-params.js'); | ||
const allowedCrossOrigin = require('./src/allowed-cross-origin.js'); | ||
const middlewares = { | ||
getSSOInfo, | ||
parseParams, | ||
allowedCrossOrigin | ||
const defaultOptions = { | ||
parseParams: true | ||
}; | ||
module.exports = { | ||
GateWay, | ||
middlewares | ||
}; | ||
class ApiGateWay { | ||
constructor(options) { | ||
options = options || {}; | ||
this.options = Object.assign(defaultOptions, options); | ||
this.middlewares = []; | ||
if (this.options.parseParams) { | ||
this.use(parseParams); | ||
} | ||
} | ||
use(middleware) { | ||
if (typeof middleware !== 'function') { | ||
throw new Error('middleware must be function'); | ||
} | ||
this.middlewares.push(middleware); | ||
} | ||
wrap() { | ||
var middlewares = this.middlewares; | ||
return function(event, context, callback) { | ||
const ctx = { | ||
event: event, | ||
context: context, | ||
result: {} | ||
}; | ||
co(function *() { | ||
var index = middlewares.length; | ||
var prev = function *() {}; | ||
while (index--) { | ||
prev = middlewares[index].bind(ctx, prev); | ||
} | ||
yield prev(); | ||
return callback(null, ctx.result); | ||
}).catch(err => { | ||
return callback(err, {}); | ||
}); | ||
}; | ||
} | ||
} | ||
module.exports = ApiGateWay; |
{ | ||
"name": "ali-api-gateway", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "proxy ali api gate way", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
README | ||
====== | ||
为了更方便 |
'use strict'; | ||
const test = require('ava'); | ||
const apiGateWay = require('../'); | ||
const GateWay = apiGateWay.GateWay; | ||
const ApiGateWay = require('../'); | ||
test.cb('middlewares run right', t => { | ||
const apiGateWay = new GateWay(); | ||
const apiGateWay = new ApiGateWay({parseParams: false}); | ||
apiGateWay.use(function *(next) { | ||
@@ -32,3 +31,3 @@ this.result = []; | ||
test.cb('raw params in context and return result', t => { | ||
const apiGateWay = new GateWay(); | ||
const apiGateWay = new ApiGateWay({parseParams: false}); | ||
apiGateWay.use(function *() { | ||
@@ -51,3 +50,3 @@ this.result = { | ||
test.cb('throw error', t => { | ||
const apiGateWay = new GateWay(); | ||
const apiGateWay = new ApiGateWay({parseParams: false}); | ||
apiGateWay.use(function *() { | ||
@@ -54,0 +53,0 @@ throw new Error('test'); |
'use strict'; | ||
const test = require('ava'); | ||
const apiGateWay = require('../'); | ||
const GateWay = apiGateWay.GateWay; | ||
const middlewares = apiGateWay.middlewares; | ||
const ApiGateWay = require('../'); | ||
test.cb('parse params success', t => { | ||
const apiGateWay = new GateWay(); | ||
apiGateWay.use(middlewares.parseParams); | ||
const apiGateWay = new ApiGateWay(); | ||
apiGateWay.use(function *() { | ||
@@ -24,4 +21,3 @@ this.body = 'test'; | ||
test.cb('parse params success', t => { | ||
const apiGateWay = new GateWay(); | ||
apiGateWay.use(middlewares.parseParams); | ||
const apiGateWay = new ApiGateWay(); | ||
apiGateWay.use(function *() { | ||
@@ -28,0 +24,0 @@ throw this.newError(400, 'error params'); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5
4731
8
155