Skip to content

Commit 364e02c

Browse files
committed
first commit
0 parents  commit 364e02c

File tree

7 files changed

+1657
-0
lines changed

7 files changed

+1657
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
dist/

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
```

index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as aws_lambda from 'aws-lambda'
2+
export * from 'aws-lambda'
3+
4+
export type SyncHandler<T extends aws_lambda.Handler> = (
5+
event: Parameters<T>[0],
6+
context: Parameters<T>[1],
7+
callback: Parameters<T>[2],
8+
) => void;
9+
10+
export type AsyncHandler<T extends aws_lambda.Handler> = (
11+
event: Parameters<T>[0],
12+
context: Parameters<T>[1],
13+
) => Promise<NonNullable<Parameters<Parameters<T>[2]>[1]>>;

0 commit comments

Comments
 (0)