@everestate/serverless-router
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -14,2 +14,3 @@ module.exports = { | ||
'no-plusplus': ['error', { allowForLoopAfterthoughts: true }], | ||
'implicit-arrow-linebreak': 0, | ||
}, | ||
@@ -16,0 +17,0 @@ overrides: [{ |
@@ -13,10 +13,13 @@ const BasePlugin = require('../../BasePlugin'); | ||
static match(reminder, callback) { | ||
static match(reminder) { | ||
return (number) => { | ||
if (!Number.isInteger(number)) { return null; } | ||
if (number % 2 !== reminder) { return null; } | ||
return { callback, context: { reminder } }; | ||
if (!Number.isInteger(number)) { return false; } | ||
return number % 2 === reminder; | ||
}; | ||
} | ||
static ctx(reminder) { | ||
return { reminder }; | ||
} | ||
static get pluginName() { return 'evenodd'; } | ||
@@ -23,0 +26,0 @@ } |
@@ -37,3 +37,3 @@ const BasePlugin = require('../../BasePlugin'); | ||
static match(subject, callback) { | ||
static match(subject) { | ||
let matcher = () => null; | ||
@@ -48,6 +48,3 @@ const subjectType = getType(subject); | ||
return (target) => { | ||
if (!matcher(target)) { return null; } | ||
return { callback, context: {} }; | ||
}; | ||
return target => matcher(target); | ||
} | ||
@@ -54,0 +51,0 @@ |
@@ -33,7 +33,4 @@ const BasePlugin = require('../../BasePlugin'); | ||
static match(dayIndex, callback) { | ||
return (date) => { | ||
if (date.getDay() !== dayIndex) { return null; } | ||
return { callback, context: {} }; | ||
}; | ||
static match(dayIndex) { | ||
return date => date.getDay() === dayIndex; | ||
} | ||
@@ -40,0 +37,0 @@ |
@@ -20,5 +20,9 @@ const BasePlugin = require('../BasePlugin'); | ||
plugin.appendRoute('a', 'b', 'c', 'd', 'e', 'f'); | ||
expect(plugin.routes).toEqual([42]); | ||
expect(plugin.routes).toEqual([{ | ||
callback: 'f', | ||
match: 42, | ||
pluginName: 'baseplugin', | ||
}]); | ||
expect(BasePlugin.match).toHaveBeenCalledTimes(1); | ||
expect(BasePlugin.match).toHaveBeenCalledWith('a', 'b', 'c', 'd', 'e', 'f'); | ||
expect(BasePlugin.match).toHaveBeenCalledWith('a', 'b', 'c', 'd', 'e'); | ||
}); | ||
@@ -25,0 +29,0 @@ |
@@ -14,9 +14,2 @@ const ServerlessRouter = require('../ServerlessRouter'); | ||
test('initializes .registeredPlugins property', () => | ||
expect(subj().registeredPlugins).toMatchObject({ | ||
evenodd: EvenOddPlugin, | ||
type: TypePlugin, | ||
weekday: WeekdayPlugin, | ||
})); | ||
test('initializes plugin instances', () => | ||
@@ -38,3 +31,3 @@ expect(subj()).toMatchObject({ | ||
router.registerPlugin(WeekdayPlugin); | ||
expect(router.registeredPlugins).toMatchObject({ weekday: WeekdayPlugin }); | ||
expect(router.weekday).toBeInstanceOf(WeekdayPlugin); | ||
expect(router.weekday.routes).toEqual(['foo', 'bar']); | ||
@@ -41,0 +34,0 @@ }); |
@@ -7,3 +7,7 @@ class BasePlugin { | ||
appendRoute(...args) { | ||
this.routes.push(this.constructor.match(...args)); | ||
this.routes.push({ | ||
callback: args.pop(), | ||
match: this.constructor.match(...args), | ||
pluginName: this.constructor.pluginName, | ||
}); | ||
return this; | ||
@@ -19,4 +23,8 @@ } | ||
} | ||
static ctx() { | ||
return {}; | ||
} | ||
} | ||
module.exports = BasePlugin; |
class ServerlessRouter { | ||
constructor(plugins) { | ||
this.routes = []; | ||
this.registeredPlugins = {}; | ||
this.mismatchHandler = null; | ||
@@ -23,6 +22,5 @@ | ||
if (this.registeredPlugins[pluginName]) { | ||
if (this[pluginName]) { | ||
throw new Error(`Plugin "${pluginName}" already registered`); | ||
} | ||
this.registeredPlugins[pluginName] = Plugin; | ||
this[pluginName] = new Plugin(this.routes); | ||
@@ -36,9 +34,7 @@ } | ||
dispatch(...args) { | ||
const numOfRoutes = this.routes.length; | ||
let route; | ||
for (let i = 0; i < numOfRoutes; i++) { | ||
route = this.routes[i](...args); | ||
if (route) { | ||
return route.callback.apply(null, [route.context || {}, ...args]); | ||
} | ||
const matchingRoute = this.routes.find(r => r.match(...args)); | ||
if (matchingRoute) { | ||
const ctx = this[matchingRoute.pluginName].constructor.ctx(...args); | ||
return matchingRoute.callback.apply(null, [ctx, ...args]); | ||
} | ||
@@ -45,0 +41,0 @@ |
{ | ||
"name": "@everestate/serverless-router", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Fast, minimalist, pluggable, universal router.", | ||
@@ -31,8 +31,8 @@ "keywords": [ | ||
"devDependencies": { | ||
"eslint": "^4.18.1", | ||
"eslint-config-airbnb-base": "^12.1.0", | ||
"eslint-plugin-import": "^2.10.0", | ||
"eslint": "^4.19.1", | ||
"eslint-config-airbnb-base": "^13.0.0", | ||
"eslint-plugin-import": "^2.13.0", | ||
"eslint-plugin-jest": "^21.15.2", | ||
"jest": "^22.4.2" | ||
"jest": "^23.2.0" | ||
} | ||
} |
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
133914
307