Skip to content

Commit cab9a64

Browse files
committed
docs: Add README docs for event-handler package
1 parent e5ea83f commit cab9a64

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

packages/event-handler/README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# digital-event-handler
2+
3+
Minimalistic event handler & HTTP router for Serverless applications
4+
5+
## Simple Example
6+
7+
```typescript
8+
// Import API Gateway Event handler
9+
import { APIGatewayProxyEvent, Context } from 'aws-lambda';
10+
import { ApiGatewayResolver } from './ApiGateway';
11+
import { AsyncFunction, BaseProxyEvent, JSONData } from 'types';
12+
13+
// Initialize the event handler
14+
const app = new ApiGatewayResolver();
15+
16+
// Define a route
17+
const helloHandler =
18+
async (_event: BaseProxyEvent, _context: Context) : Promise<JSONData> => Promise.resolve({ message: 'Hello World' });
19+
20+
// Register Route
21+
app.addRoute('GET', '/v1/hello', helloHandler as AsyncFunction) ;
22+
23+
// Declare your Lambda handler
24+
// Declare your Lambda handler
25+
exports.handler = (
26+
_event: APIGatewayProxyEvent,
27+
_context: Context
28+
): Promise<JSONData> =>
29+
// Resolve routes
30+
app.resolve(_event, _context)
31+
;
32+
```
33+
34+
## Register Route with Decorators
35+
36+
```typescript
37+
import { APIGatewayProxyEvent, Context } from 'aws-lambda';
38+
import { ApiGatewayResolver } from './ApiGateway';
39+
import { BaseProxyEvent, JSONData } from 'types';
40+
41+
// Initialize the event handler
42+
const app = new ApiGatewayResolver();
43+
44+
// Define a Controller class
45+
export class HelloController{
46+
47+
// Register a route
48+
@app.get('/v1/hello')
49+
public hello (_event: BaseProxyEvent, _context: Context) : Promise<JSONData> {
50+
return Promise.resolve({ message: 'Hello World' });
51+
}
52+
53+
@app.post('/v1/hello')
54+
public postHello (_event: BaseProxyEvent, _context: Context) : Promise<JSONData> {
55+
return Promise.resolve({ message: 'Resource created' });
56+
}
57+
58+
}
59+
60+
// Declare your Lambda handler
61+
exports.handler = (
62+
_event: APIGatewayProxyEvent,
63+
_context: Context
64+
): Promise<JSONData> =>
65+
// Resolve routes
66+
app.resolve(_event, _context)
67+
;
68+
69+
```
70+
71+
## CORS Support
72+
73+
```typescript
74+
// Import API Gateway Event handler
75+
import { CORSConfig } from 'types';
76+
import { ApiGatewayResolver, ProxyEventType } from './ApiGateway';
77+
78+
// App with CORS Configurattion
79+
const app = new ApiGatewayResolver(
80+
ProxyEventType.APIGatewayProxyEvent,
81+
new CORSConfig()
82+
);
83+
84+
```
85+

0 commit comments

Comments
 (0)