
Security News
NVD Concedes Inability to Keep Pace with Surging CVE Disclosures in 2025
Security experts warn that recent classification changes obscure the true scope of the NVD backlog as CVE volume hits all-time highs.
lambda-api-factory
Advanced tools
[](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
[](https://circleci.com/gh/acambas/lambda-api-factory)
The npm package lambda-api-factory receives a total of 22 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
Security experts warn that recent classification changes obscure the true scope of the NVD backlog as CVE volume hits all-time highs.
Security Fundamentals
Attackers use obfuscation to hide malware in open source packages. Learn how to spot these techniques across npm, PyPI, Maven, and more.
Security News
Join Socket for exclusive networking events, rooftop gatherings, and one-on-one meetings during BSidesSF and RSA 2025 in San Francisco.