Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
@everestate/serverless-router
Advanced tools
Serverless, minimalist, pluggable, universal router.
Serverless, minimalist, pluggable, universal router.
npm install @everestate/serverless-router --save
To use serverless-router
you will need at least one of its plugins.
const Router = require('@everestate/serverless-router');
const { HTTP } = require('@everestate/serverless-router-aws');
cosnt userService = require('../services/userService');
function dispatch(event) {
const router = new Router([HTTP]);
router.http
.post('/users', () =>
userService.createUser(event.body)) // returns promise
.get('/users/:id', () =>
userService.getUserById(event.pathParameters.id)) // returns promise
.patch('/users/:id', () =>
userService.updateUser(event.pathParameters.id, event.body)) // returns promise
.delete('/users/:id', () =>
userService.deleteUser(event.pathParameters.id)); // returns promise
router.mismatch(() => {
const { path, httpMethod } = event;
return Promise.reject(new Error(`Unknown route: ${httpMethod} ${path}`));
});
return router.dispatch(event);
}
function myLambdaHandler(event, context, callback) {
return dispatch(event)
.then(result =>
callback(null, { statusCode: result.code, body: JSON.stringify({ payload: result.payload }) }))
.catch(error =>
callback(null, { statusCode: '500', body: JSON.stringify({ message: error.message }) }));
}
By default serverless-router
will throw error
on route mismatch.
It's possible to define a custom mismatch handler, and it would be called with same arguments as dispatch
was called:
router.mismatch((event, context, callback) => {
const { path, httpMethod } = event;
return callback(null, {
statusCode: '404',
body: JSON.stringify({ message: `ServerlessRouter can't find the route ${httpMethod} ${path}` }),
});
});
When middleware are set, they will be called before each matched route in order they registered.
Middleware callback is expected to return Promise
and could be defined with use
:
router.use((event, context, callback) => {
const { source } = event;
if (source === 'bad source') {
return Promise.reject(new Error('Bad event source'));
}
return Promise.resolve();
})
Check the docs/plugins.md to find out how to implement the new plugin.
FAQs
Serverless, minimalist, pluggable, universal router.
The npm package @everestate/serverless-router receives a total of 15 weekly downloads. As such, @everestate/serverless-router popularity was classified as not popular.
We found that @everestate/serverless-router demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.