@everestate/serverless-router
Advanced tools
Comparing version 0.3.1 to 0.4.0
@@ -5,3 +5,3 @@ The router delegates route matching to its plugins. So, routing subject could be represented with anything: object, string or even number. The plugin is responsible to provide the matching machanism. | ||
1. We need to declare new class which is extended from "`BasePlugin`": | ||
1. We need to declare a new class which is extended from "`BasePlugin`": | ||
@@ -15,3 +15,3 @@ ```javascript | ||
2. Router expects plugin to provide one or more public interfaces available to plugin users. | ||
2. Router expects the plugin to provide one or more public interfaces available to plugin users. | ||
@@ -34,5 +34,5 @@ ```javascript | ||
**`callback`** is the user callback which is called when router matches the route. The router takes cake of it, so just passing it down. | ||
**`callback`** is the user callback which is called when router matches the route. The router takes cake of it, so just pass it down. | ||
3. Router expects plugin to implement "match" function. | ||
3. Router expects the plugin to implement "match" function. | ||
@@ -49,6 +49,7 @@ ```javascript | ||
static match(reminder) { | ||
static match(remainder) { | ||
return (number) => { | ||
if (!Number.isInteger(number) || number === 0) { return false; } | ||
return number % 2 === reminder; | ||
if (!Number.isInteger(number) || number === 0) { return null; } | ||
if (number % 2 !== remainder) { return null; } | ||
return { remainder }; | ||
}; | ||
@@ -59,3 +60,5 @@ } | ||
**`match`** expects routing context and returns matching callback. Mathing callback is used during dispatch process. Router invokes matching context with the routing subject. | ||
**`match`** expects routing context and returns matching function. Matching function is invoked during dispatch process. | ||
When all conditions are met (successfull "match"), matching function returns context object. So, it could be an empty object or a class instance. | ||
When any of the conditions are not met, matching function returns null. | ||
@@ -62,0 +65,0 @@ The plugin would be accessible on router's instance by lower-cased class name as |
@@ -15,11 +15,8 @@ const BasePlugin = require('../../BasePlugin'); | ||
return (number) => { | ||
if (!Number.isInteger(number) || number === 0) { return false; } | ||
return number % 2 === reminder; | ||
if (!Number.isInteger(number) || number === 0) { return null; } | ||
if (number % 2 !== reminder) { return null; } | ||
return { reminder }; | ||
}; | ||
} | ||
static ctx(reminder) { | ||
return { reminder }; | ||
} | ||
static get pluginName() { return 'evenodd'; } | ||
@@ -26,0 +23,0 @@ } |
@@ -41,3 +41,3 @@ const BasePlugin = require('../../BasePlugin'); | ||
if (subjectType === '[object String]') { | ||
matcher = target => getType(target) === subject; | ||
matcher = target => (getType(target) === subject ? {} : null); | ||
} | ||
@@ -44,0 +44,0 @@ if (subjectType === '[object Function]') { |
@@ -34,3 +34,3 @@ const BasePlugin = require('../../BasePlugin'); | ||
static match(dayIndex) { | ||
return date => date.getDay() === dayIndex; | ||
return date => (date.getDay() === dayIndex ? {} : null); | ||
} | ||
@@ -37,0 +37,0 @@ |
@@ -22,4 +22,5 @@ const BasePlugin = require('../BasePlugin'); | ||
callback: 'f', | ||
match: 42, | ||
matcher: 42, | ||
pluginName: 'baseplugin', | ||
ctx: undefined, | ||
}]); | ||
@@ -26,0 +27,0 @@ expect(BasePlugin.match).toHaveBeenCalledTimes(1); |
@@ -0,1 +1,3 @@ | ||
const Route = require('./Route'); | ||
class BasePlugin { | ||
@@ -7,7 +9,7 @@ constructor(routes) { | ||
appendRoute(...args) { | ||
this.routes.push({ | ||
callback: args.pop(), | ||
match: this.constructor.match(...args), | ||
pluginName: this.constructor.pluginName, | ||
}); | ||
this.routes.push(new Route( | ||
args.pop(), // callback | ||
this.constructor.match(...args), // matcher | ||
this.constructor.pluginName, // plugin name | ||
)); | ||
return this; | ||
@@ -23,8 +25,4 @@ } | ||
} | ||
static ctx() { | ||
return {}; | ||
} | ||
} | ||
module.exports = BasePlugin; |
@@ -36,4 +36,3 @@ class ServerlessRouter { | ||
if (matchingRoute) { | ||
const ctx = this[matchingRoute.pluginName].constructor.ctx(...args); | ||
return matchingRoute.callback.apply(null, [ctx, ...args]); | ||
return matchingRoute.respond(...args); | ||
} | ||
@@ -40,0 +39,0 @@ |
{ | ||
"name": "@everestate/serverless-router", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "Serverless, minimalist, pluggable, universal router.", | ||
@@ -34,8 +34,8 @@ "keywords": [ | ||
"devDependencies": { | ||
"eslint": "^5.11.1", | ||
"eslint": "^5.12.1", | ||
"eslint-config-airbnb-base": "^13.1.0", | ||
"eslint-plugin-import": "^2.14.0", | ||
"eslint-plugin-jest": "^22.1.2", | ||
"eslint-plugin-import": "^2.15.0", | ||
"eslint-plugin-jest": "^22.1.3", | ||
"jest": "^23.6.0" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# @everestate/serverless-router | ||
# @everestate/serverless-router [![npm version](https://badge.fury.io/js/%40everestate%2Fserverless-router.svg)](https://www.npmjs.com/package/@everestate/serverless-router) | ||
@@ -22,3 +22,3 @@ > Serverless, minimalist, pluggable, universal router. | ||
const Router = require('@everestate/serverless-router'); | ||
const { Http } = require('@everestate/serverless-router-aws'); | ||
const { HTTP } = require('@everestate/serverless-router-aws'); | ||
@@ -28,3 +28,3 @@ cosnt userService = require('../services/userService'); | ||
function dispatch(event) { | ||
const router = new Router([Http]); | ||
const router = new Router([HTTP]); | ||
@@ -62,3 +62,3 @@ router.http | ||
It's possible to define custom mismatch handler, and it would be called with same arguments as `dispatch` was called: | ||
It's possible to define a custom mismatch handler, and it would be called with same arguments as `dispatch` was called: | ||
@@ -65,0 +65,0 @@ ```javascript |
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
184123
21
352