You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/features/event-handler/rest.md
+76-19Lines changed: 76 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -91,6 +91,34 @@ All dynamic route parameters will be available as typed object properties in the
91
91
92
92
You can also nest dynamic paths, for example `/todos/:todoId/comments/:commentId`, where both `:todoId` and `:commentId` will be resolved at runtime.
93
93
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:
- For non-regex routes, 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, registration order determines matching precedence, therefore, always place catch-all routes `/.*/` last
121
+
94
122
### HTTP Methods
95
123
96
124
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`.
@@ -130,18 +158,6 @@ Please [check this issue](https://github.com/aws-powertools/powertools-lambda-ty
130
158
131
159
You can access request details such as headers, query parameters, and body using the `Request` object provided to your route handlers and middleware functions via `reqCtx.req`.
132
160
133
-
### Handling not found routes
134
-
135
-
By default, we return a `404 Not Found` response for any unmatched routes.
136
-
137
-
You can use the `notFound()` method as a higher-order function or class method decorator to override this behavior, and return a custom response.
You can use the `errorHandler()` method as a higher-order function or class method decorator to define a custom error handler for errors thrown in your route handlers or middleware.
@@ -158,6 +174,20 @@ Error handlers receive the error object and the request context as arguments, an
We provide built-in error handlers for common routing errors so you don't have to specify the Error type explicitly.
180
+
181
+
You can use the `notFound()` and `methodNotAllowed()` methods as higher-order functions or class method decorators to customize error responses for unmatched routes and unsupported HTTP methods.
182
+
183
+
By default, we return a `404 Not Found` response for unmatched routes.
You can throw HTTP errors in your route handlers to stop execution and return specific HTTP status codes and messages. Event Handler provides a set of built-in HTTP error classes that you can use to throw common HTTP errors.
@@ -439,11 +469,10 @@ For convenience, these are the default CORS settings applied when you register t
|`allowHeaders`|`[Authorization, Content-Type, X-Amz-Date, X-Api-Key, X-Amz-Security-Token]`| Specifies the allowed headers that can be used in the actual request. |
444
474
|`exposeHeaders`|`[]`| Any additional header beyond the [safe listed by CORS specification](https://developer.mozilla.org/en-US/docs/Glossary/CORS-safelisted_response_header){target="_blank"}. |
445
475
|`credentials`|`false`| Only necessary when you need to expose cookies, authorization headers or TLS client certificates. |
446
-
|`maxAge`|`0`| Indicates how long the results of a preflight request can be cached. Value is in seconds. |
447
476
448
477
#### Per-route overrides
449
478
@@ -565,15 +594,43 @@ Please [check this issue](https://github.com/aws-powertools/powertools-lambda-ty
565
594
566
595
### Split routers
567
596
568
-
!!! note "Coming soon"
569
-
570
597
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.
571
598
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.
599
+
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.
600
+
601
+
!!! note "Merging with Global Middleware"
602
+
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.
603
+
604
+
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.
In the previous example, `split_route.ts` routes had a `/todos` prefix. This might grow over time and become repetitive.
573
621
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.
622
+
When necessary, you can set a prefix when including a `Router` instance. This means you can remove `/todos` prefix altogether.
575
623
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.
0 commit comments