|
| 1 | +# aws-lambda-config |
| 2 | + |
| 3 | +Utility types that convert the handler functions found in [@types/aws-lambda](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/aws-lambda) into the asynchronous and synchronous signatures that are intended to be implemented. |
| 4 | + |
| 5 | +## The Problem |
| 6 | + |
| 7 | +The AWS services that use the handler functions we write expect us to either use a provided callback method and return nothing, or ignore the callback and return a Promise. Therefore, [@types/aws-lambda](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/aws-lambda) defines the Handler type as |
| 8 | +```typescript |
| 9 | +export type Handler<TEvent = any, TResult = any> = ( |
| 10 | + event: TEvent, |
| 11 | + context: Context, |
| 12 | + callback: Callback<TResult>, |
| 13 | +) => void | Promise<TResult>; |
| 14 | +``` |
| 15 | + |
| 16 | +This is technically correct, but as users of the library, it doesn't represent the functions we are supposed to write: |
| 17 | + |
| 18 | +* If we use the callback approach, we should never return a Promise, and |
| 19 | +* If we do want to write our handler as an async method then |
| 20 | + * The callback parameter shouldn't even exist and |
| 21 | + * The return type should only be a Promise, and not void | Promise |
| 22 | + |
| 23 | +As an example, let's write a lambda handler function. We will declare its type, so future maintainers will know what we intended, and so that the IDE can help us out: |
| 24 | +```typescript |
| 25 | +// main.ts |
| 26 | +export const myHandler: Handler<MyEvent, MyResult> = async ... |
| 27 | +``` |
| 28 | + |
| 29 | +Writing tests is now difficult. We get the following types of errors: |
| 30 | +```typescript |
| 31 | +// main.test.ts |
| 32 | +it.('should work', async (done) => { |
| 33 | + |
| 34 | + let actual: MyResult = await myHandler(myEvent, myContext); // Expected 3 arguments, but got 2. |
| 35 | + // ^^^ Type 'MyResult | void' is not assignable to type 'MyResult'. |
| 36 | +}); |
| 37 | +``` |
| 38 | + |
| 39 | +## The Solution |
| 40 | + |
| 41 | +This module exports 2 utility types, `SyncHandler` and `AsyncHandler`, that transform the handlers from [@types/aws-lambda](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/aws-lambda) into the types that are intended to be implemented. It also reexports everything from [@types/aws-lambda](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/aws-lambda) for convenience. |
| 42 | + |
| 43 | +### Examples |
| 44 | +```Typescript |
| 45 | +import {AsyncHandler, SyncHandler, APIGatewayProxyHandler} from "aws-lambda-consumer" |
| 46 | + |
| 47 | +// (event: APIGatewayProxyEvent, context: Context, callback: Callback<APIGatewayProxyResult>) => void |
| 48 | +type SyncAPIGatewayProxyHandler = SyncHandler<APIGatewayProxyHandler> |
| 49 | + |
| 50 | +// (event: APIGatewayProxyEvent, context: Context) => Promise<APIGatewayProxyResult> |
| 51 | +type AsyncAPIGatewayProxyHandler = AsyncHandler<APIGatewayProxyHandler> |
| 52 | +``` |
0 commit comments