Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ali-api-gateway

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ali-api-gateway - npm Package Compare versions

Comparing version 1.0.1 to 1.0.2

.babelrc

6

index.js

@@ -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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc