Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
lambda-promise-interop
Advanced tools
Readme
Have you ever found yourself wishing you could use the async/await syntax in the unit tests of your Lambda handlers? Or wished there was a way to just write a Lambda handler as an async function that takes in an event & context and returns some result? Well now you can.
This package offers two tiny functions for transforming traditional handler functions to promisified async ones and vice versa. Type definitions for TypeScript users are included as well.
npm install --save lambda-promise-interop
handlerifyAsyncFn<E, R>
Input:
fn
: (event: E, context: Context) => Promise<R>
Output:
Handler<E, R>
Description:
Transforms simple (Event, Context) => Promise<Result>
functions into functions
that are compatible with the Handler type that AWS Lambda expects. This lets you
write a simple function signature that is easily testable and fits neatly with
the async/await model.
promisifyHandler<E, R>
Input:
handler
: Handler<E, R>
Output:
(event: E, context: Context) => Promise<R>
Description:
Transforms lambda handler functions into functions that take in an event input object E and return a promise of type R. This is useful for unit-testing of traditionally written lambda handlers, as it makes it much easier to just write tests using async/await syntax.
import { handlerifyAsyncFn, promisifyHandler } from 'lambda-promise-interop'
TypeScript users may also find it helpful to take a devDependency on @types/aws-lambda, since that includes many useful type definitions.
You have to point Lambda do a function that matches a signature of
<T, R>(T, Context, Callback<R>) => void
. It'd be preferable to just return a
Promise or write an async function instead of invoking a callback and making
sure you account for both synchronous and asynchronous errors. That would make
code more readable and easier to test.
JavaScript example:
import { handlerifyAsyncFn } from 'lambda-promise-interop'
import { someAsyncThing, ClientError } from './lib/example'
// Our easily testable and readable async function
const myApi = async (event, context) => {
let body, statusCode
try {
body = await someAsyncThing(event.body)
statusCode = 200
} catch (err) {
body = err.message
statusCode = (err instanceof ClientError) ? 400 : 500
}
return { body, statusCode }
}
// A handler function in the signature that Lambda expects
const handler = handlerifyAsyncFn(myApi)
// We export `myApi` just for ease of testing and export `handler` for Lambda to
// actually use.
export { handler, myApi }
TypeScript example:
import { APIGatewayProxyEvent, APIGatewayProxyResult, Context } from 'aws-lambda'
import { handlerifyAsyncFn } from 'lambda-promise-interop'
import { someAsyncThing, ClientError } from './lib/example'
// Our easily testable and readable async function
const myApi = async (event: APIGatewayProxyEvent, context: Context): APIGatewayProxyResult => {
let body: string
let statusCode: number
try {
body = await someAsyncThing(event.body)
statusCode = 200
} catch (err) {
body = err.message
statusCode = (err instanceof ClientError) ? 400 : 500
}
return { body, statusCode }
}
// A handler function in the signature that Lambda expects
const handler = handlerifyAsyncFn(myApi)
// We export `myApi` just for ease of testing and export `handler` for Lambda to
// actually use.
export { handler, myApi }
If you already have Lambda handler functions written, you may have noticed that it's somewhat painful to test them. JS testing frameworks like Jest support async functions, but Lambda handlers force you back into Callback Hell if you want to run any assertions before your test is done.
You're stuck writing something like this:
// Example spec
import { handler } from '../index'
import mockContext from 'aws-lambda-mock-context'
describe('My Lambda Handler', () => {
it('does something', (done) => {
handler({someInput: 'foo'}, mockContext(), (err, res) => {
expect(err).toBeFalsy()
expect(res.someValue).toEqual(something)
done(undefined, res)
})
})
})
With this library, you can instead write that same test as:
// Example spec
import { handler } from '../index'
import mockContext from 'aws-lambda-mock-context'
import { promisifyHandler } from 'lambda-promise-interop'
const asyncHandler = promisifyHandler(handler)
describe('My Lambda Handler', () => {
it('does something', async () => {
const res = await asyncHandler({someInput: 'foo'}, mockContext())
expect(res.someValue).toEqual(something)
})
})
MIT
FAQs
Small JavaScript/TypeScript library for converting between AWS Lambda Handlers and (Event, Context) => Promise<Result> functions.
The npm package lambda-promise-interop receives a total of 3 weekly downloads. As such, lambda-promise-interop popularity was classified as not popular.
We found that lambda-promise-interop 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
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.