Socket
Socket
Sign inDemoInstall

@everestate/serverless-router

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@everestate/serverless-router - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

.github/PULL_REQUEST_TEMPLATE.md

2

lib/__tests__/__fixtures__/EvenOddPlugin.js

@@ -20,4 +20,6 @@ const BasePlugin = require('../../BasePlugin');

}
static get pluginName() { return 'evenodd'; }
}
module.exports = EvenOddPlugin;

@@ -52,4 +52,6 @@ const BasePlugin = require('../../BasePlugin');

}
static get pluginName() { return 'type'; }
}
module.exports = TypePlugin;

@@ -39,4 +39,6 @@ const BasePlugin = require('../../BasePlugin');

}
static get pluginName() { return 'weekday'; }
}
module.exports = WeekdayPlugin;

75

lib/__tests__/ServerlessRouter.js

@@ -16,5 +16,5 @@ const ServerlessRouter = require('../ServerlessRouter');

expect(subj().registeredPlugins).toMatchObject({
evenoddplugin: EvenOddPlugin,
typeplugin: TypePlugin,
weekdayplugin: WeekdayPlugin,
evenodd: EvenOddPlugin,
type: TypePlugin,
weekday: WeekdayPlugin,
}));

@@ -24,5 +24,5 @@

expect(subj()).toMatchObject({
evenoddplugin: expect.objectContaining({ routes: [] }),
typeplugin: expect.objectContaining({ routes: [] }),
weekdayplugin: expect.objectContaining({ routes: [] }),
evenodd: expect.objectContaining({ routes: [] }),
type: expect.objectContaining({ routes: [] }),
weekday: expect.objectContaining({ routes: [] }),
}));

@@ -39,4 +39,4 @@

router.registerPlugin(WeekdayPlugin);
expect(router.registeredPlugins).toMatchObject({ weekdayplugin: WeekdayPlugin });
expect(router.weekdayplugin.routes).toEqual(['foo', 'bar']);
expect(router.registeredPlugins).toMatchObject({ weekday: WeekdayPlugin });
expect(router.weekday.routes).toEqual(['foo', 'bar']);
});

@@ -49,46 +49,33 @@

describe('dispatch', () => {
test('respects the order of the routes #1', (done) => {
test('returns callback value', () => {
const router = subj();
router.evenoddplugin.even((event, context, callback) => callback(null, { even: true }));
router.typeplugin.number((event, context, callback) => callback(null, { type: 'number' }));
router.dispatch(42, {}, (error, payload) => {
expect(error).toEqual(null);
expect(payload).toEqual({ even: true });
done();
});
router.weekday.saturday(() => ({ saturday: true }));
router.mismatch(() => ({ mismatched: true }));
expect(router.dispatch(new Date('2014-08-16'))).toEqual({ saturday: true });
expect(router.dispatch(new Date('2017-09-05'))).toEqual({ mismatched: true });
});
test('respects the order of the routes #2', (done) => {
test('respects the order of the routes #1', () => {
const router = subj();
router.typeplugin.number((event, context, callback) => callback(null, { type: 'number' }));
router.evenoddplugin.even((event, context, callback) => callback(null, { even: true }));
router.dispatch(42, {}, (error, payload) => {
expect(error).toEqual(null);
expect(payload).toEqual({ type: 'number' });
done();
});
router.evenodd.even(() => ({ even: true }));
router.type.number(() => ({ type: 'number' }));
expect(router.dispatch(42)).toEqual({ even: true });
});
test('invokes mismatching callback when there are no routes', done =>
subj().dispatch('42', {}, (error, payload) => {
expect(error).toEqual(null);
expect(payload).toEqual({
statusCode: '404',
body: '{"message":"ServerlessRouter can\'t find this route"}',
});
done();
}));
test('respects the order of the routes #2', () => {
const router = subj();
router.type.date(() => ({ type: 'date' }));
router.type.number(() => ({ type: 'number' }));
router.evenodd.even(() => ({ even: true }));
expect(router.dispatch(42)).toEqual({ type: 'number' });
});
test('invokes mismatching callback when route is not found', (done) => {
test('throws error when there are no routes', () =>
expect(() => subj().dispatch('42')).toThrowErrorMatchingSnapshot());
test('throws error when route is not found', () => {
const router = subj();
router.evenoddplugin.even((event, context, callback) => callback(null, { even: true }));
router.typeplugin.number((event, context, callback) => callback(null, { type: 'number' }));
router.dispatch('42', {}, (error, payload) => {
expect(error).toEqual(null);
expect(payload).toEqual({
statusCode: '404',
body: '{"message":"ServerlessRouter can\'t find this route"}',
});
done();
});
router.evenodd.even((event, context, callback) => callback(null, { even: true }));
router.type.number((event, context, callback) => callback(null, { type: 'number' }));
expect(() => subj().dispatch('42')).toThrowErrorMatchingSnapshot();
});

@@ -95,0 +82,0 @@ });

@@ -34,9 +34,9 @@ class ServerlessRouter {

dispatch(event, context, callback) {
dispatch(...args) {
const numOfRoutes = this.routes.length;
let route;
for (let i = 0; i < numOfRoutes; i++) {
route = this.routes[i](event);
route = this.routes[i](...args);
if (route) {
return route.callback(event, context, callback, route.context || {});
return route.callback.apply(null, [...args, route.context || {}]);
}

@@ -46,15 +46,8 @@ }

if (this.mismatchHandler) {
return this.mismatchHandler(event, context, callback);
return this.mismatchHandler(...args);
}
return this.constructor.defaultMismatchHandler(event, context, callback);
throw new Error('ServerlessRouter can\'t find the route');
}
static defaultMismatchHandler(_event, _context, callback) {
return callback(null, {
statusCode: '404',
body: JSON.stringify({ message: 'ServerlessRouter can\'t find this route' }),
});
}
}
module.exports = ServerlessRouter;
{
"name": "@everestate/serverless-router",
"version": "0.0.1",
"description": "Serverless Router",
"main": "index.js",
"version": "0.0.2",
"description": "Fast, minimalist, pluggable, universal router.",
"keywords": [

@@ -11,8 +10,8 @@ "serverless",

"router",
"route",
"restful"
"route"
],
"main": "index.js",
"scripts": {
"lint": "eslint .",
"test": "jest",
"test": "jest --verbose",
"coverage": "jest --coverage --no-cache"

@@ -19,0 +18,0 @@ },

# @everestate/serverless-router
> Serverless Router
> Fast, minimalist, pluggable, universal router.

@@ -54,8 +54,7 @@ ## Installation

### When route is not found
### When route is mismatched
`serverless-router` has default handler to catch mismatching request.
It responds with `status` `404` and `{ message: "ServerlessRouter can't find this route" }` in the body.
By default `serverless-router` will throw `error` on route mismatch.
Of course it's possible to define your custom mismatch handler:
It's possible to define custom mismatch handler, and it would be called with same arguments `dispatch` was called:

@@ -65,6 +64,5 @@ ```javascript

const { path, httpMethod } = event;
console.log(`ServerlessRouter mismatch: ${httpMethod} ${path}`);
return callback(null, {
statusCode: '500',
body: '',
statusCode: '404',
body: JSON.stringify({ message: `ServerlessRouter can't find the route ${httpMethod} ${path}` }),
});

@@ -85,2 +83,2 @@ });

MIT
[MIT](./LICENSE)

Sorry, the diff of this file is not supported yet

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