New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

lambd

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lambd

Create & mantain easily Google Cloud Functiosn and/or AWS Lambdas.

  • 0.2.15
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6
decreased by-40%
Maintainers
1
Weekly downloads
 
Created
Source

LAMBD

Create and maintain your AWS Lambdas or Google Cloud Functions functions easily with LAMBD!

How to install

npm i -S lambd

Getting started

How does I select platform?

const Lambd = require('lambd');
const { Platforms } = Lambd;

const myLambdaFunction = ({ request, response }) => {
  // Here your lambda code
  response.json({ ok: true, userid: request.params.userid });
});

// AWS Lambdas by default:
// Lambd.create()

// Google Cloud Functions
// Lambd.createFunctions()

// Otherwise
// You can use Platform enum object and select platform
module.exports.myFunction = Lambd.platform(Platforms.GCLOUD).get('/:userid', myLambdaFunction).getHandler();

// This allows you make compatible all your lambdas between AWS and GCLOUD Functions platforms only you must change platform on code.

Simply example

const Lambd = require('lambd');

const myLambdaFunction = ({ response }) => {
  // Here your functions code
  response.json({ ok: true });
});

module.exports.handler = Lambd.createFunctions().get(myLambdaFunction).getHandler();

### API example

const Lambd = require('lambd');

const myLambda = Lambd.create();

// Middlewares
const authMiddleware = (next) => (options) => {
  const { request, response } = options;
  const { auth } = request;
  if (auth && auth === 'esto_es_una_prueba') {
    next(options);
  } else {
    response.status(403).error('not authorized');
  }
};

const mongoMiddleware = (next) => (options) => {
  const { response } = options;
  const url = 'mongodb://localhost:27017/myproject';
  MongoClient.connect(url, (err, db) => {
    if (err) {
      return response.error(err);
    }
    options.db = db;
    return next(options);
  });
};

myLambda.use(authMiddleware);
myLambda.use(mongoMiddleware);

// Route: /users/:userid
const handler = myLamba
  .get(({ request, db }) => UserService(db).findById(request.params.userid))
  .put('/:userid', ({ request, db }) => UserService(db).findByIdAndUpdate(request.params.userid, request.body))
  .delete(':userid', ({ request, db }) => UserService(db).findByIdAndDelete(request.params.userid))
  .getHandler();

module.exports.handler = handler;

Advanced example

LAMBD allows you use middlewares to add power to your lambda function.

const Lambd = require('lambd');
constMongoClient } = require('mongodb');

const myLambdaFunction = ({ response, db }) => {
  // Here your lambda code
  db.collection('users').find({}).toArray((err, users) => {
    if (err) {
      return response.error(err);
    }
    return response.json({ ok: true, users });
  });
});

const mongoMiddleware = (next) => (options) => {
  const { response } = options;
  const url = 'mongodb://localhost:27017/myproject';
  MongoClient.connect(url, (err, db) => {
    if (err) {
      return response.error(err);
    }
    options.db = db;
    return next(options);
  });
};

// Global Middleware
// Lambd.use(mongoMiddleware);

// Lambda Middleware
const myLambda = Lambd.create();
myLambda.use(mongoMiddleware);

// Set headers to all lambdas
// Lambd.set('MyFirstHeader', 'value');
// Lambd.set({ 'MySecondHeader': 'value2', 'MyThirdHeader': 'value3' });

// Lambda header
myLambda.set('MyFirstHeader', 'value');
myLambda.set({ 'MySecondHeader': 'value2', 'MyThirdHeader': 'value3' });

module.exports.handler = myLambda.get(myLambdaFunction).getHandler();

Keywords

FAQs

Package last updated on 13 Jan 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc