Skip to content

Commit 2ba4c99

Browse files
committed
.
1 parent 0f35eba commit 2ba4c99

File tree

1 file changed

+78
-27
lines changed

1 file changed

+78
-27
lines changed

README.md

Lines changed: 78 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,84 @@ exports.handler = async (event, context) => {
3434

3535
For a full tutorial see [How To: Build a Serverless API with Serverless, AWS Lambda and Lambda API](https://www.jeremydaly.com/build-serverless-api-serverless-aws-lambda-lambda-api/).
3636

37+
## TypeScript Support
38+
39+
Lambda API includes comprehensive TypeScript definitions out of the box. You can leverage type safety across your entire API:
40+
41+
```typescript
42+
import { API, Request, Response } from 'lambda-api';
43+
44+
// Define your response type
45+
interface UserResponse {
46+
id: string;
47+
name: string;
48+
email: string;
49+
}
50+
51+
// Create a typed API instance
52+
const api = new API();
53+
54+
// Routes with type-safe request/response
55+
api.get<UserResponse>('/users/:id', (req, res) => {
56+
res.json({
57+
id: req.params.id,
58+
name: 'John',
59+
60+
});
61+
});
62+
63+
// Middleware with type checking
64+
const authMiddleware: Middleware<UserResponse> = (req, res, next) => {
65+
// TypeScript will ensure type safety
66+
next();
67+
};
68+
69+
// Full type support for complex scenarios
70+
interface UserQuery { fields: string }
71+
interface UserParams { id: string }
72+
interface UserBody { name: string; email: string }
73+
74+
api.post<UserResponse, ALBContext, UserQuery, UserParams, UserBody>(
75+
'/users',
76+
(req, res) => {
77+
// Full type safety for:
78+
req.query.fields; // UserQuery
79+
req.params.id; // UserParams
80+
req.body.name; // UserBody
81+
req.requestContext; // ALBContext
82+
83+
res.json({
84+
id: '1',
85+
name: req.body.name,
86+
email: req.body.email
87+
});
88+
}
89+
);
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+
};
104+
```
105+
106+
Key TypeScript Features:
107+
- Full type inference for request and response objects
108+
- Generic type parameters for response types
109+
- Support for API Gateway and ALB contexts
110+
- Type-safe query parameters, path parameters, and request body
111+
- Middleware and error handler type definitions
112+
- Automatic type inference for all HTTP methods
113+
- Type safety for cookies, headers, and other API features
114+
37115
## Why Another Web Framework?
38116

39117
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**.
@@ -1479,33 +1557,6 @@ Simply create a `{proxy+}` route that uses the `ANY` method and all requests wil
14791557

14801558
If you are using persistent connections in your function routes (such as AWS RDS or Elasticache), be sure to set `context.callbackWaitsForEmptyEventLoop = false;` in your main handler. This will allow the freezing of connections and will prevent Lambda from hanging on open connections. See [here](https://www.jeremydaly.com/reuse-database-connections-aws-lambda/) for more information.
14811559

1482-
## TypeScript Support
1483-
1484-
An `index.d.ts` declaration file has been included for use with your TypeScript projects (thanks @hassankhan). Please feel free to make suggestions and contributions to keep this up-to-date with future releases.
1485-
1486-
**TypeScript Example**
1487-
1488-
```typescript
1489-
// import AWS Lambda types
1490-
import { APIGatewayEvent, Context } from 'aws-lambda';
1491-
// import Lambda API default function
1492-
import createAPI from 'lambda-api';
1493-
1494-
// instantiate framework
1495-
const api = createAPI();
1496-
1497-
// Define a route
1498-
api.get('/status', async (req, res) => {
1499-
return { status: 'ok' };
1500-
});
1501-
1502-
// Declare your Lambda handler
1503-
exports.run = async (event: APIGatewayEvent, context: Context) => {
1504-
// Run the request
1505-
return await api.run(event, context);
1506-
};
1507-
```
1508-
15091560
## Contributions
15101561

15111562
Contributions, ideas and bug reports are welcome and greatly appreciated. Please add [issues](https://github.com/jeremydaly/lambda-api/issues) for suggestions and bug reports or create a pull request.

0 commit comments

Comments
 (0)