Skip to content

Commit 1ea7144

Browse files
committed
Added documentation for Split ROutes
1 parent 0dea57b commit 1ea7144

File tree

5 files changed

+69
-5
lines changed

5 files changed

+69
-5
lines changed

docs/features/event-handler/rest.md

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -565,15 +565,43 @@ Please [check this issue](https://github.com/aws-powertools/powertools-lambda-ty
565565

566566
### Split routers
567567

568-
!!! note "Coming soon"
569-
570568
As applications grow and the number of routes a Lambda function handles increases, it becomes natural to either break it into smaller Lambda functions or split routes into separate files to ease maintenance.
571569

572-
Currently, the TypeScript event-handler's Router class doesn't provide a way to compose multiple router instances, forcing developers to define all routes in a single file or manually merge route definitions.
570+
The `Router` class provide an `includeRouter` method to compose multiple router instances allowing developers to define routes in multiple files and merge route definitions. You will be able to define routes in separate files and import them into a main router file, improving code organization and maintainability.
571+
572+
!!! note "Merging with Global Middleware"
573+
When merging two `Router` instances together, if you have a global middleware defined in one of your instances, the global middleware gets applied to the all the merged routes.
574+
575+
Let's assume you have `index.ts` as your Lambda function entrypoint and routes in `split_route.ts`. This is how you'd use the `includeRouter` feature.
576+
577+
=== "split_route.ts"
578+
579+
```typescript
580+
--8<-- "examples/snippets/event-handler/rest/split_route.ts"
581+
```
582+
583+
=== "index.ts"
584+
585+
```typescript hl_lines="8"
586+
--8<-- "examples/snippets/event-handler/rest/split_route_index.ts"
587+
```
588+
589+
#### Route Prefix
590+
591+
In the previous example, `split_route.ts` routes had a `/todos` prefix. This might grow over time and become repetitive.
592+
593+
When necessary, you can set a prefix when including a `Router` instance. This means you could remove `/todos` prefix altogether.
573594

574-
Once this feature is available, you will be able to define routes in separate files and import them into a main router file, improving code organization and maintainability.
595+
=== "split_route_prefix.ts"
575596

576-
Please [check this issue](https://github.com/aws-powertools/powertools-lambda-typescript/issues/4481) for more details and examples, and add 👍 if you would like us to prioritize it.
597+
```typescript
598+
--8<-- "examples/snippets/event-handler/rest/split_route_prefix.ts"
599+
```
600+
=== "index.ts"
601+
602+
```typescript hl_lines="8"
603+
--8<-- "examples/snippets/event-handler/rest/split_route_prefix_index.ts"
604+
```
577605

578606
### Considerations
579607

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Router } from '@aws-lambda-powertools/event-handler/experimental-rest';
2+
3+
const router = new Router();
4+
router.get('/todos', () => 'Get all todos');
5+
router.get('/todos/:id', () => 'Get a single todo item');
6+
7+
export { router };
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Router } from '@aws-lambda-powertools/event-handler/experimental-rest';
2+
import type { APIGatewayProxyEvent, Context } from 'aws-lambda';
3+
import { router } from './split_route';
4+
5+
const app = new Router();
6+
7+
// Split Routers
8+
app.includeRouter(router);
9+
10+
export const handler = async (event: APIGatewayProxyEvent, context: Context) =>
11+
app.resolve(event, context);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Router } from '@aws-lambda-powertools/event-handler/experimental-rest';
2+
3+
const router = new Router();
4+
router.get('/', () => 'Get all todos');
5+
router.get('/:id', () => 'Get a single todo item');
6+
7+
export { router };
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Router } from '@aws-lambda-powertools/event-handler/experimental-rest';
2+
import type { APIGatewayProxyEvent, Context } from 'aws-lambda';
3+
import { router } from './split_route';
4+
5+
const app = new Router();
6+
7+
// Split Routers
8+
app.includeRouter(router, { prefix: '/todos' });
9+
10+
export const handler = async (event: APIGatewayProxyEvent, context: Context) =>
11+
app.resolve(event, context);

0 commit comments

Comments
 (0)