
Product
Redesigned Repositories Page: A Faster Way to Prioritize Security Risk
Our redesigned Repositories page adds alert severity, filtering, and tabs for faster triage and clearer insights across all your projects.
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 3 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.
Product
Our redesigned Repositories page adds alert severity, filtering, and tabs for faster triage and clearer insights across all your projects.
Security News
Slopsquatting is a new supply chain threat where AI-assisted code generators recommend hallucinated packages that attackers register and weaponize.
Security News
Multiple deserialization flaws in PyTorch Lightning could allow remote code execution when loading untrusted model files, affecting versions up to 2.4.0.