Giter Club home page Giter Club logo

lambda-expressless's Introduction

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:

Property Notes
body You need to use body-parser
hostname -
host -
xhr -
ip -
ips -
path -
protocol -
secure -
method -
query -
params -
headers -

Methods:

Method Notes
accepts() -
acceptsEncodings() -
acceptsCharsets() -
acceptsLanguages() -
get() -
header() -
is() -

Response Object

Methods:

Method Notes
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.

lambda-expressless's People

Contributors

cenkingunlugu avatar dependabot[bot] avatar muratcorlu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

lambda-expressless's Issues

Handling multiValueHeaders and multiValueQueryStringParameters

Now, we read headers from headers property and querystrings from queryStringParameters property of APIGatewayProxyEvent object. But in the case of having repeating headers or querystring parameters, these properties will not give list of values. So we need to check multiValueHeaders and multiValueQueryStringParameters for parsing headers and querystring parameters.

Reporting 5XX responses as an error on Lambda

In current situation, if you do this;

response.status(500).end();

requester will get Internal server error but this will be logged as a successful response on Lambda side. I'm not completely sure what is the best practice for this but I feel that, 5XX statuses should be reported as errors on Lambda level too. Because that kind of errors should mean pointing a coding issue in Lambda function.

For now, the only place that we report an error on lambda level is in lambda-wrapper.js function(use function).

Lazy parsing of headers and query parameters

As of now, we parse headers and query parameters on every initialisation of Request object. A better approach could be doing that lazy by using some getters to not spent effort on parsing query params and headers if user don't need to read them. We'll also need to use getters for many other properties(host, ip, protocol etc) to not parse headers without really needing them.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.