ali-api-gateway
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -1,4 +0,4 @@ | ||
const Api = require('./src/api.js'); | ||
const http = require('./src/http'); | ||
const Router = require('./src/router.js'); | ||
const Api = require('./es5/api.js'); | ||
const http = require('./es5/http'); | ||
const Router = require('./es5/router.js'); | ||
@@ -5,0 +5,0 @@ module.exports = { |
{ | ||
"name": "ali-api-gateway", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "proxy ali api gate way", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "ava ./test" | ||
"publish": "npm run build && npm publish", | ||
"build": "babel src -d es5 --source-maps --copy-files", | ||
"test": "npm run build && ava ./test" | ||
}, | ||
@@ -21,3 +23,7 @@ "keywords": [ | ||
"devDependencies": { | ||
"ava": "^0.19.1" | ||
"ava": "^0.19.1", | ||
"babel-cli": "^6.24.1", | ||
"babel-plugin-transform-runtime": "^6.23.0", | ||
"babel-preset-env": "^1.5.2", | ||
"source-map-support": "^0.4.15" | ||
}, | ||
@@ -24,0 +30,0 @@ "dependencies": { |
@@ -5,2 +5,3 @@ 'use strict'; | ||
const methods = ['get', 'head', 'post', 'put', 'delete', 'options', 'patch']; | ||
const signature = function(method, path) { | ||
@@ -12,2 +13,15 @@ assert(typeof method === 'string', 'signature mehotd must string'); | ||
const wrapFunc = function(funcs) { | ||
return function *(ctx, next) { | ||
var index = funcs.length; | ||
var prev = next; | ||
while (index--) { | ||
const func = funcs[index]; | ||
assert(typeof func === 'function', `router add route func: ${typeof func} is not function`); | ||
prev = func.bind(null, ctx, prev); | ||
} | ||
yield prev(); | ||
}; | ||
}; | ||
class Router { | ||
@@ -17,7 +31,8 @@ constructor() { | ||
methods.map(method => { | ||
this[method] = (funcName, func) => { | ||
this[method] = (funcName, ...funcs) => { | ||
assert(typeof funcName === 'string', `router add route funcName: ${typeof funcName} is not string`); | ||
assert(typeof func === 'function', `router add route func: ${typeof func} is not function`); | ||
const func = wrapFunc(funcs); | ||
const k = signature(method.toUpperCase(), funcName); | ||
this.map[k] = func; | ||
return this; | ||
}; | ||
@@ -27,2 +42,15 @@ }); | ||
use(funcName, ...funcs) { | ||
assert(typeof funcName === 'string', `router add route funcName: ${typeof funcName} is not string`); | ||
methods.map(method => { | ||
if (!this._find(method, funcName)) { | ||
this[method](funcName, ...funcs); | ||
} | ||
}); | ||
} | ||
_find(method, funcName) { | ||
return this.map[signature(method, funcName)]; | ||
} | ||
routes() { | ||
@@ -33,3 +61,3 @@ const self = this; | ||
const method = ctx.req.httpMethod; | ||
const func = self.map[signature(method, funcName)]; | ||
const func = self._find(method, funcName); | ||
if (!func) { | ||
@@ -36,0 +64,0 @@ throw ctx.newError(404, `route method:${method}, funcName: ${funcName} not found`); |
@@ -38,3 +38,6 @@ 'use strict'; | ||
router.get('test', function *(ctx) { | ||
router.get('test', function *(ctx, next) { | ||
ctx.setHeaders('test', 'test'); | ||
yield next(); | ||
}, function *(ctx) { | ||
ctx.body = 'get test'; | ||
@@ -54,2 +57,27 @@ }); | ||
encoding: 'utf8', | ||
headers: { test: 'test' }, | ||
statusCode: 200}); | ||
t.end(); | ||
}); | ||
}); | ||
test.cb('route use success', t => { | ||
const api = new Api(); | ||
const router = new Router(); | ||
router.use('test', function *(ctx) { | ||
ctx.body = 'use test'; | ||
}); | ||
router.post('test', function *(ctx) { | ||
ctx.body = 'use test'; | ||
}); | ||
api.use(router.routes()); | ||
http(api.wrap(), {funcName: 'test', method: 'get'}, function(err, res) { | ||
t.deepEqual(res, { | ||
body: 'use test', | ||
cookies: {}, | ||
encoding: 'utf8', | ||
headers: {}, | ||
@@ -61,2 +89,27 @@ statusCode: 200}); | ||
test.cb('route use priority success', t => { | ||
const api = new Api(); | ||
const router = new Router(); | ||
router.use('test', function *(ctx) { | ||
ctx.body = 'use test'; | ||
}); | ||
router.get('test', function *(ctx) { | ||
ctx.body = 'get test'; | ||
}); | ||
api.use(router.routes()); | ||
http(api.wrap(), {funcName: 'test', method: 'get'}, function(err, res) { | ||
t.deepEqual(res, { | ||
body: 'get test', | ||
cookies: {}, | ||
encoding: 'utf8', | ||
headers: {}, | ||
statusCode: 200}); | ||
t.end(); | ||
}); | ||
}); | ||
test.cb('route throw error', t => { | ||
@@ -63,0 +116,0 @@ const api = new Api(); |
Sorry, the diff of this file is not supported yet
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
20842
22
659
5