Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
lambda-api-factory
Advanced tools
[![CircleCI](https://circleci.com/gh/acambas/lambda-api-factory.svg?style=svg)](https://circleci.com/gh/acambas/lambda-api-factory)
This is a small library that helps in reducing boilerplate and creating conventions with creating aws lambda HTTP API:
Most of the time your code for aws lambda will be something like:
return async (event, context, callback) => {
try {
// code for VALIDATING inputs that are in event or context object
if (event.foo === 'bar') {
return callback({ //Boilerplate
statusCode: 404, //Boilerplate
headers: { //Boilerplate
'Content-Type': 'application/json', //Boilerplate
}, //Boilerplate
body: JSON.stringify({
message: 'foo cant be bar',
}),//Boilerplate
})
}
// Code that does SERVICE work
const dbStuff = await getStuffFromDB(event.bar)
const serviceStuff = await getStuffFromSomeService(context)
return callback(null, { //Boilerplate
statusCode: 200, //Boilerplate
headers: { //Boilerplate
'Content-Type': 'application/json', //Boilerplate
}, //Boilerplate
body: JSON.stringify({ dbStuff, serviceStuff }), //Boilerplate
})
} catch (error) {
callback(null, { //Boilerplate
statusCode: 500, //Boilerplate
headers: {
'Content-Type': 'application/json', //Boilerplate
}, //Boilerplate
body: JSON.stringify(error), //Boilerplate
})
}
}
As you can see most of the time code can be split into 2 categories:
This library helps reducing the boilerplate part of the code by passing service function to it(and validation if it exists) and it will return a lambda handler function that will output result as HTTP API response.
Example with service part only:
// handler.js
const lambdaFactory = require('lambda-api-factory')
const service = async (event, context) => {
const stuffFromDB = await getStuffFromDb(event.foo, context)
return stuffFromDB
}
module.exports.basicApi = lambdaFactory(service)
In case there is validation for the API you can pass a second parameter which is validation function and if the validation fails the API handler will return 400 Bad Request error with details of the validation. In order for validation to pass the function MUST not return any result (undefined or null is fine :) )
Example with validation that throws
const lambdaFactory = require('lambda-api-factory')
const validate = async (event, context) => {
if (event.foo === 'bar') {
throw new Error(`foo can't be bar`)
}
}
const service = async (event, context) => {
const stuffFromDB = await getStuffFromDb(event.foo, context)
return {
stuffFromDB,
}
}
module.exports.basicApi = lambdaFactory(service, validate)
Example with validation that return validation result
const lambdaFactory = require('lambda-api-factory')
const validate = async (event, context) => {
if (event.foo === 'bar') {
return { message: `foo can't be bar`}
}
}
const service = async (event, context) => {
const stuffFromDB = await getStuffFromDb(event.foo, context)
return {
stuffFromDB,
}
}
module.exports.basicApi = lambdaFactory(service, validate)
Some of the benefits of using this library
It was built using:
FAQs
[![CircleCI](https://circleci.com/gh/acambas/lambda-api-factory.svg?style=svg)](https://circleci.com/gh/acambas/lambda-api-factory)
The npm package lambda-api-factory receives a total of 1 weekly downloads. As such, lambda-api-factory popularity was classified as not popular.
We found that lambda-api-factory demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.