1.0.12 • Published 8 years ago

@urbane/serverless-http-helper v1.0.12

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
8 years ago

npm.io Serverless HTTP Helper npm.io CircleCI license npm version

by David Urbane

Wraps serverless/lambda handlers to support a connect-like request/response handler. Allows for leveraging connect middleware functions in serverless/lambda environments. Different to serverless-http as it is more opionated towards connect and is a lighter implementation. Designed to be minimal and focused on the API Gateway <-> Lambda workflow. It also supports Promises in a simplified request format where no .send() or context.succeed is required. JSON bodys are automatically parsed.

  • Small & light with no dependencies.
  • Express.js-like Request/Response objects with (req,res,next)=>... handlers.
  • Promise & auto-send support
  • Supports global and local middleware
  • Automatic error-handling

Contents

  1. Why
  2. Usage
    1. Installation
    2. Methods
  3. Related Projects
  4. License
  5. Contributions
  6. Other Projects

Why

I wanted a small and lightweight library for simplifying using Lambda HTTP deployments with automatic error handling and Promise support. I didn't want to require other libraries (serverless-http's method) in order to keep deployments as small as possible.

Usage

Installation

Using NPM:

npm i --save @urbane/serverless-http-helper

Using Yarn:

yarn add @urbane/serverless-http-helper

No other setup is required to use the methods, just reference handler.method normally. It has no production dependencies and is a very small install.

Did you know?

NPM is pretty cool package manager, one of the best on the market, but you should check out Yarn as well. Yarn is doing some cool things with caching and parallel installs. It proxies the NPM registry, so you will still get all your packages without any trouble.

yarn add @urbane/serverless-http-helper

Methods

View API Docs

createEndpoints

This is a quick way to create a full micro-service in one call. It will wrap all the functions found in the passed object. NOTE: This is not always preferrable, since each handler will represent its own Lambda function that must be loaded. If your endpoints require different dependencies, you should split up your functions to reduce the package size of each individual one.

...other requires...

const {createEndpoints} = require('@urbane/serverless-http-helper')

module.exports = createEndpoints({
	someValue: 'this is a value that will be accessible form this.someValue in the endpoints',
    myEndpoint(req, res, next){
        //traditional style
        res.status(201).send({name: "value"})
    },
    anotherEndpoint(req, res, next){
        //You may return a value to automatically trigger res.send(...)
        return {
            name: "value"
        }
    },
    anAsyncEndpoint(req, res, next){
        //You may return a promise
        return new Promise(...)
    },
    anErrorEndpoint(req, res, next){
        //Errors are caught and returned in a JSON output, ex. {"error":true,"message":...}
        throw new Error('Some error.')
    },
    //Just use an array for middleware
    middlewareEndpoint: [someMiddlewareFunction,(req,res,next)=>({myResponse: 'is this'})]
})

wrapHandler

Use this to wrap a single function. createEndpoints just calls this for every matching value in the passed function.

...other requires...

const {wrapHandler} = require('@urbane/serverless-http-helper')

module.exports.handler = wrapHandler(function(req, res, next){
       return {name:'value'}
})

module.exports.middlewareHandler = wrapHandler([middlewareFunction,function(req, res, next){
       return {name:'value'}
}])

useGlobalMiddleware

This registers a global middleware that is called prior to the specific handler. Middleware just follows the (req, res, next)=>... format and is treated identically to your handler.

...other requires...

const {wrapHandler,useGlobalMiddleware} = require('@urbane/serverless-http-helper')

useGlobalMiddleware(middlewareFunction,...otherMiddleware)

module.exports.handler = wrapHandler(function(req, res, next){
       return {name:'value'}
})

Related Projects

serverless - Convient manager for deploying and maintaining serverless projects.

serverless-http - Similar and allows you to select your own middleware manager.

License

Apache 2.0 (view) - TL; DR: Use and modify as you want, however you are required to distribute notices of changes and there are patent restrictions. It is not "copyleft" like GPL, so you may feel safe using in proprietary applications. Note some @urbane projects are released under GPL, so double check before using libraries in your proprietary software. As long as those projects don't rely on other's GPL code, you may contact me if you need a different license.

Contributions

Feel free to create issues or pull requests. 1. Hit the Fork button on https://github.com/urbane/serverless-http-helper (you'll need to sign in to GitHub). Note that this adds a copy of this repo to your account, which will be public. 2. Clone the repository from YOUR fork in your terminal/command line (you'll need Git installed, instructions here).

git clone https://github.com/YOUR_USERNAME/serverless-http-helper
  1. Install Dependices (you'll need Node.js installed, download here).
cd serverless-http-helper && npm install
  1. Add UPSTREAM to your repo, this will allow you to pull in the latest changes from the main repo.
git remote add upstream https://github.com/urbane/serverless-http-helper.git
git fetch upstream
git checkout master
  1. Make changes to make this project AWESOME!
  2. Update to make sure you are working with the latest version. This will update your repository to the latest in the main repo without overwriting your edits.
git merge upstream/master
  1. Test your changes, make sure that all tests pass. If tests don't pass, you're changes won't be accepted into the next version. If you added a feature, you should also add a test. Unit testing is very important when multiple parties are working on the code, so other people don't break your code accidentally and vice-versa. This allows people to add features without having to understand all the code in the project.
npm test
  1. Prepare for commit. Run the lint script to "lint" (check & clean code style) to make sure your awesome code is using the same standards as the project and doesn't contain obvious errors like undefined variables.
npm run lint
  1. Commit to your fork.
git add .
git commit -m 'WHAT DID YOU CHANGE, THIS IS YOUR COMMIT MESSAGE'
git push origin master
  1. Go to https://github.com/YOUR_USERNAME/serverless-http-helper and click "Pull Request" and follow the directions on screen. You will need to make a comment on why your code should be added to the main project.
  2. Your pull request will be reviewed by a core contributer of the project. You may be asked to make changes to your code, so just repeat steps 4-8 as your commit will automatically update the pull request (PR). Afterwards your PR will be either accepted or rejected.

Other Projects by David Urbane (@urbane)

Unlike many other projects, I strive for continued maintainance of all packages with fixing bugs and updating dependencies. Projects are tested against latest dependecies and updated as needed, although all projects are AS IS and subject to the included license. If you want enchanced support or to license code differently, contact me at david@davidurbane.com or learn more at davidurbane.com.

  • config-manager - Handles the reading and writing of various config file types, such as YAML, JSON, INI, etc. with a unified API.

Twitter: @DavidUrbane
Facebook: David.Urbane
GitHub: @urbane
Website: davidurbane.com

So what's with @urbane in front of the packages?

Because I'm so vain (just ask Carly) and so I can brand in order to promote a certain level of quality and consistency across projects. I contribute to many other projects, I just want to make sure the ones I own are held to the highest standard. This also alleviates any issues with conflicting projects. If you want to adopt this project and pull it into the broader community, contact me and we maybe able to work it out and save it from possible deprecation.

:us: Coded proudly near beautiful Salt Lake City, Utah in the USA, however contributions are welcome from anywhere in the universe (except Pluto).

1.0.12

8 years ago

1.0.11

8 years ago

1.0.10

8 years ago

1.0.9

8 years ago

1.0.8

8 years ago

1.0.7

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago