Skip to content

Commit 0d8fd88

Browse files
committed
.
1 parent ca1aadf commit 0d8fd88

File tree

1 file changed

+103
-25
lines changed

1 file changed

+103
-25
lines changed

README.md

Lines changed: 103 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,15 @@ For a full tutorial see [How To: Build a Serverless API with Serverless, AWS Lam
3939
Lambda API includes comprehensive TypeScript definitions out of the box. You can leverage type safety across your entire API:
4040

4141
```typescript
42-
import { API, Request, Response } from 'lambda-api';
42+
import {
43+
API,
44+
Request,
45+
Response,
46+
SourceAgnosticHandler,
47+
SourceAgnosticMiddleware,
48+
isApiGatewayContext,
49+
isAlbContext
50+
} from 'lambda-api';
4351

4452
// Define your response type
4553
interface UserResponse {
@@ -51,34 +59,48 @@ interface UserResponse {
5159
// Create a typed API instance
5260
const api = new API();
5361

54-
// Routes with type-safe request/response
55-
api.get<UserResponse>('/users/:id', (req, res) => {
62+
// Source-agnostic handler that works with any Lambda trigger
63+
const handler: SourceAgnosticHandler<UserResponse> = (req, res) => {
64+
// Common properties are always available
65+
console.log(req.method, req.path);
66+
67+
// Type-safe access to source-specific features
68+
if (isApiGatewayContext(req.requestContext)) {
69+
console.log(req.requestContext.identity);
70+
} else if (isAlbContext(req.requestContext)) {
71+
console.log(req.requestContext.elb);
72+
}
73+
5674
res.json({
57-
id: req.params.id,
75+
id: '1',
5876
name: 'John',
5977
6078
});
61-
});
79+
};
6280

63-
// Middleware with type checking
64-
const authMiddleware: Middleware<UserResponse> = (req, res, next) => {
65-
// TypeScript will ensure type safety
81+
// Source-agnostic middleware
82+
const middleware: SourceAgnosticMiddleware = (req, res, next) => {
83+
// Works with any Lambda trigger
84+
console.log(`${req.method} ${req.path}`);
6685
next();
6786
};
6887

69-
// Full type support for complex scenarios
88+
// Use with API methods
89+
api.get('/users', middleware, handler);
90+
91+
// For source-specific handlers, you can specify the context type
7092
interface UserQuery { fields: string }
7193
interface UserParams { id: string }
7294
interface UserBody { name: string; email: string }
7395

74-
api.post<UserResponse, ALBContext, UserQuery, UserParams, UserBody>(
96+
api.post<UserResponse, APIGatewayContext, UserQuery, UserParams, UserBody>(
7597
'/users',
7698
(req, res) => {
7799
// Full type safety for:
78100
req.query.fields; // UserQuery
79101
req.params.id; // UserParams
80102
req.body.name; // UserBody
81-
req.requestContext; // ALBContext
103+
req.requestContext; // APIGatewayContext
82104

83105
res.json({
84106
id: '1',
@@ -87,23 +109,11 @@ api.post<UserResponse, ALBContext, UserQuery, UserParams, UserBody>(
87109
});
88110
}
89111
);
90-
91-
// Error handling with types
92-
const errorHandler: ErrorHandlingMiddleware<UserResponse> = (
93-
error,
94-
req,
95-
res,
96-
next
97-
) => {
98-
res.status(500).json({
99-
id: 'error',
100-
name: error.name,
101-
email: error.message
102-
});
103-
};
104112
```
105113

106114
Key TypeScript Features:
115+
- Source-agnostic types that work with any Lambda trigger
116+
- Type guards for safe context type checking
107117
- Full type inference for request and response objects
108118
- Generic type parameters for response types
109119
- Support for API Gateway and ALB contexts
@@ -112,6 +122,74 @@ Key TypeScript Features:
112122
- Automatic type inference for all HTTP methods
113123
- Type safety for cookies, headers, and other API features
114124

125+
## Type Guards
126+
127+
Lambda API provides type guards to safely work with different request sources:
128+
129+
```typescript
130+
import {
131+
isApiGatewayContext,
132+
isApiGatewayV2Context,
133+
isAlbContext,
134+
isApiGatewayRequest,
135+
isApiGatewayV2Request,
136+
isAlbRequest
137+
} from 'lambda-api';
138+
139+
// Check request context type
140+
if (isApiGatewayContext(req.requestContext)) {
141+
// TypeScript knows this is APIGatewayContext
142+
console.log(req.requestContext.identity);
143+
}
144+
145+
// Check entire request type
146+
if (isApiGatewayRequest(req)) {
147+
// TypeScript knows this is Request<APIGatewayContext>
148+
console.log(req.requestContext.identity);
149+
}
150+
```
151+
152+
## Handling Multiple Request Sources
153+
154+
Lambda API provides type-safe support for different AWS Lambda triggers. You can write source-specific handlers or use source-agnostic handlers that work with any trigger:
155+
156+
```typescript
157+
import {
158+
isApiGatewayContext,
159+
isApiGatewayV2Context,
160+
isAlbContext,
161+
SourceAgnosticHandler
162+
} from 'lambda-api';
163+
164+
// Source-specific handler
165+
api.get<Response, APIGatewayContext>('/api-gateway', (req, res) => {
166+
console.log(req.requestContext.identity);
167+
});
168+
169+
api.get<Response, ALBContext>('/alb', (req, res) => {
170+
console.log(req.requestContext.elb);
171+
});
172+
173+
// Source-agnostic handler (works with any trigger)
174+
const handler: SourceAgnosticHandler = (req, res) => {
175+
if (isApiGatewayContext(req.requestContext)) {
176+
console.log(req.requestContext.identity);
177+
} else if (isAlbContext(req.requestContext)) {
178+
console.log(req.requestContext.elb);
179+
}
180+
181+
res.json({ status: 'ok' });
182+
};
183+
184+
api.get('/any', handler);
185+
```
186+
187+
Key features for handling multiple sources:
188+
- Type guards for safe context type checking
189+
- Source-agnostic types that work with any trigger
190+
- Full type safety for source-specific properties
191+
- Automatic payload format detection
192+
115193
## Why Another Web Framework?
116194

117195
Express.js, Fastify, Koa, Restify, and Hapi are just a few of the many amazing web frameworks out there for Node.js. So why build yet another one when there are so many great options already? One word: **DEPENDENCIES**.

0 commit comments

Comments
 (0)