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

serverless-http-interface

v0.1.5

Published

Facades to consistently handle HTTP request/response regardless provider

Downloads

2

Readme

This module aimes to make an interface among all providers about the way of HTTP request/response handling.

This single way is closer to Express syntax.

This module provides a decorator for that.

Providers:

  • AWS (Supported)
  • Google (Supported)
  • Azure (Supported)
  • Kubeless (Not yet)

Install

Require Node 6.x at least.

npm install --save serverless-http-interface;

Usage

const httpInterface = require('serverless-http-interface');

console.log(process.env.SLS_PROVIDER_NAME)
// --> e.g "aws"

function handler(request, response) {
    response.status(200).send({ hello: 'World' })
}

exports.handler = httpInterface(handler);
// or pass the providerName explicitly
// exports.handler = httpInterface(handler, 'aws');

How to migrate ?

  • Before using it, you will need to maintain your code if you migrate from provider to another ( AWS)
// (((((         Request     ))))
// With AWS
event.queryStringParameters
// With Google
req.query

//... so on
// (((((         Response     ))))
// With AWS
callback(null, {statusCode: 200, body: JSON.stringify(body)})
// With Azure
context.res = {status: 200, body};
context.done()
  • Now , you have one code base (close to Express syntax) :
// Before

exports.handler = function(event, context, callback) {
    callback(null, {statusCode: 200, body: JSON.stringify({ hello: 'World' })})
}

// Now
const httpInterface = require('serverless-http-interface');

function handler(request, response) {
    response.status(200).send({ hello: 'World' })
}

exports.handler = httpInterface(handler);

Now, change the provider (aws, azure, google) and don't worry. The function should be stay the same.

Flexibility

You are still able to access to default arguments according to provider. Those default arguments are available starting for the third index (after "request" and "response" arguments).

For example, if the provider is AWS :

  • 3rd argument : event
  • 4th argument: context
  • 5th argument: callback

If provider is Azure:

  • 3rd argument: context
  • 4th argument: request

The following example is an AWS Lambda function that leverage both : The interface by using "response" API, and The default by using "event"

const httpInterface = require('serverless-http-interface');

function handler(request, response, event, context, callback) {
    response.status(200).send(event.queryStringParameters);
}

exports.handler = httpInterface(handler, 'azure');

Note

We should NOT release without writing unit-tests. But you are welcome to contribute. Nevertheless, the setup of unit-tests framework is done and you have to follow contribution section.

Contribution:

  • Tests pass with 80% coverage.
  • Test filename convention is "*.spec.js"