npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@rodrigogs/serverless-router

v0.0.1

Published

Serverless, minimalist, pluggable, universal router.

Downloads

2

Readme

@rodrigogs/serverless-router npm version

Serverless, minimalist, pluggable, universal router.

Installation

npm install @rodrigogs/serverless-router --save

Usage

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

const Router = require('@rodrigogs/serverless-router');
const { HTTP } = require('@rodrigogs/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 }) }));
}

When route is mismatched

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}` }),
  });
});

Middleware

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();
})

Plugins

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

License

MIT