1.7.2 • Published 4 years ago

lambda-expressless v1.7.2

Weekly downloads
19
License
MIT
Repository
github
Last release
4 years ago

lambda-expressless

Build Status npm version codecov semantic-release

Wrap AWS Lambda functions with Express-like functions to simplify your code

So instead of writing this:

exports.handler = (event, context, callback) => {
  const requestBody = JSON.parse(event.body);
  const responseBody = {
    success: false,
    data: requestBody.id
  };

  callback(null, {
    statusCode: 201,
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(responseBody)
  });
}

you'll have this:

const { use } = require('lambda-expressless');
const bodyParser = require('body-parser');

exports.handler = use(bodyParser.json(), (req, res) => {
  res.status(201).json({
    success: false,
    data: req.body.id
  })
});

You can also use multiple middlewares for a single handler:

const { use } = require('lambda-expressless');

const checkUser = (req, res, next) => {
  if (req.get('Authorization') === 'someToken') {
    next()
  } else {
    req.status(403).end('Forbidden');
  }
};

const getUser = (req, res) => {
  res.json({
    id: '12',
    name: 'Murat'
  });
};

exports.handler = use(checkUser, getUser);

TypeScript example:

import { use, Request, Response } from 'lambda-expressless';
import * as bodyParser from "body-parser";

const addUser = (req: Request, res: Response, next: Function) => {
  UserService.add(req.body);

  // add user
  res.json({success: true});
};

export const handler = use(
  bodyParser.json(),
  addUser
);

You can use many popular Express Middlewares. Some tested examples are:

Installation

npm i lambda-expressless

Supported Features and Limitations

This project aims to implement functionalities of ExpressJS middlewares as much as possible. Request and Response objects have properties and methods listed below.

Request Object

Properties:

PropertyNotes
bodyYou need to use body-parser
hostname-
host-
xhr-
ip-
ips-
path-
protocol-
secure-
method-
query-
params-
headers-

Methods:

MethodNotes
accepts()-
acceptsEncodings()-
acceptsCharsets()-
acceptsLanguages()-
get()-
header()-
is()-

Response Object

Methods:

MethodNotes
get()-
format()Doesn't support shorthand mime-types
set()Only supports key, value parameters
send()Only supports string values
status()-
end()-
json()-
type()-

Contribution

Every contribution is very welcome. Keep these in your mind when you want to make a contribution:

  1. Because of we use semantic-release you need to use Angular Commit Message Conventions in your commit messages.
  2. Keep code coverage 100% with your updated tests.
  3. Check your changes with a Lambda environment. You can use SAM-CLI to test on your local.
  4. Don't forget to update documentation(this readme file) about your changes.
1.7.2

4 years ago

1.7.1

4 years ago

1.7.0

4 years ago

1.6.2

4 years ago

1.6.1

4 years ago

1.6.0

4 years ago

1.5.1

5 years ago

1.5.0

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago