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

@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

Dependency-free, minimalist, pluggable, universal router.

  • 0.3.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

@everestate/serverless-router

Dependency-free, minimalist, pluggable, universal router.

Installation

npm install @everestate/serverless-router --save

Usage

To use serverless-router you will need at least one of its plugins.

  • serverless-router-web
  • serverless-router-aws
  • serverless-router-dynamics
  • and others
const Router = require('@everestate/serverless-router');
const Web = require('@everestate/serverless-router-web');

cosnt userService = require('../services/userService');

function dispatch(event) {
  const router = new Router([Web]);

  router.web
    .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 }) }));
}

When route is mismatched

By default serverless-router will throw error on route mismatch.

It's possible to define 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}` }),
  });
});

Plugins

Check the docs/plugins.md to find out how to implement the new plugin.

License

MIT

Keywords

FAQs

Package last updated on 19 Dec 2018

Did you know?

Socket

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.

Install

Related posts

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