Skip to content

Commit 2a63628

Browse files
committed
Added documentation for catch all route
1 parent 5975cc8 commit 2a63628

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

docs/features/event-handler/rest.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,35 @@ All dynamic route parameters will be available as typed object properties in the
9191

9292
You can also nest dynamic paths, for example `/todos/:todoId/comments/:commentId`, where both `:todoId` and `:commentId` will be resolved at runtime.
9393

94+
#### Catch-all routes
95+
96+
For scenarios where you need to handle arbitrary or deeply nested paths, you can use regex patterns directly in your route definitions. These are particularly useful for proxy routes or when dealing with file paths.
97+
98+
**We recommend** having explicit routes whenever possible; use catch-all routes sparingly.
99+
100+
##### Using Regex Patterns
101+
102+
You can use standard [Regular Expressions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions){target="_blank" rel="nofollow"} in your route definitions, for example:
103+
104+
| Pattern | Description | Examples |
105+
|-----------|------------------------------------------|-------------------------------------------------------------|
106+
| `/.+/` | Matches one or more characters (greedy) | `/\/proxy\/.+/` matches `/proxy/any/deep/path` |
107+
| `/.*/` | Matches zero or more characters (greedy) | `/\/files\/.*/` matches `/files/` and `/files/deep/path` |
108+
| `/[^/]+/` | Matches one or more non-slash characters | `/\/api\/[^\/]+/` matches `/api/v1` but not `/api/v1/users` |
109+
| `/\w+/` | Matches one or more word characters | `/\/users\/\w+/` matches `/users/john123` |
110+
111+
=== "gettingStarted_dynamic_routes_catch_all.ts"
112+
113+
```python hl_lines="7 10 13 20"
114+
--8<-- "examples/snippets/event-handler/rest/gettingStarted_dynamic_routes_catch_all.ts"
115+
```
116+
117+
???+ warning "Route Matching Priority"
118+
- Routes are matched in **order of specificity**, not registration order
119+
- More specific routes (exact matches) take precedence over regex patterns
120+
- Among regex routes, the first registered matching route wins
121+
- Always place catch-all routes (`.*`) last
122+
94123
### HTTP Methods
95124

96125
You can use dedicated methods to specify the HTTP method that should be handled in each resolver. That is, `app.<httpMethod>()`, where the HTTP method could be `delete`, `get`, `head`, `patch`, `post`, `put`, `options`.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Router } from '@aws-lambda-powertools/event-handler/experimental-rest';
2+
import type { APIGatewayProxyEvent, Context } from 'aws-lambda';
3+
4+
const app = new Router();
5+
6+
// File path proxy
7+
app.get(/\/files\/.+/, () => 'Catch any GET method under /files');
8+
9+
// API versioning with any format
10+
app.get(/\/api\/v\d+\/.*/, () => 'Catch any GET method under /api/vX');
11+
12+
// Mixed: dynamic parameter + regex catch-all
13+
app.get(/\/users\/:userId\/files\/.+/, (reqCtx) => {
14+
return {
15+
userId: reqCtx.params.userId,
16+
};
17+
});
18+
19+
// Catch all route
20+
app.get(/.+/, () => 'Catch any GET method');
21+
22+
export const handler = async (event: APIGatewayProxyEvent, context: Context) =>
23+
app.resolve(event, context);

0 commit comments

Comments
 (0)