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
+23-9Lines changed: 23 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -128,7 +128,7 @@ Please [check this issue](https://github.com/aws-powertools/powertools-lambda-ty
128
128
129
129
### Accessing request details
130
130
131
-
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.request`.
131
+
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
132
133
133
### Handling not found routes
134
134
@@ -148,6 +148,8 @@ You can use the `errorHandler()` method as a higher-order function or class meth
148
148
149
149
This allows you to catch and return custom error responses, or perform any other error handling logic you need.
150
150
151
+
Error handlers receive the error object and the request context as arguments, and can return a [`Response` object](#returning-response-objects) or a JavaScript object that will be auto-serialized as per the [response auto-serialization](#response-auto-serialization) section.
152
+
151
153
!!! tip "You can also pass a list of error classes to the `errorHandler()` method."
152
154
153
155
=== "index.ts"
@@ -158,15 +160,17 @@ This allows you to catch and return custom error responses, or perform any other
158
160
159
161
### Throwing HTTP errors
160
162
161
-
You can throw HTTP errors in your route handlers to 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.
163
+
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.
162
164
163
165
This ensures that your Lambda function doesn't fail but returns a well-defined HTTP error response to the client.
164
166
165
167
If you need to send custom headers or a different response structure/code, you can use the [Response](#returning-response-objects) object instead.
166
168
169
+
!!! tip "You can throw HTTP errors in your route handlers, middleware, or custom error handlers!"
@@ -194,15 +198,21 @@ All error classes accept optional parameters for custom messages and additional
194
198
195
199
### Route prefixes
196
200
197
-
!!! note "Coming soon"
198
-
199
201
When defining multiple routes related to a specific resource, it's common to have a shared prefix. For example, you might have several routes that all start with `/todos`.
200
202
201
203
For example, if you have a custom domain `api.example.com` and you want to map it to the `/v1` base path of your API. In this case, all the requests will contain `/v1/<resource>` in the path, requiring you to repeat the `/v1` prefix in all your route definitions.
202
204
203
-
At the moment, you have to manually include the prefix in each route definition, however we are planning to add support for route prefixes in a future release.
205
+
To avoid repeating the prefix in each route definition, you can use the `prefix` constructor parameter when creating a new `Router` instance, and we'll automatically strip it from the request path before matching routes. After mapping a path prefix, the new root path will automatically be mapped to the path argument of `/`.
Please [check this issue](https://github.com/aws-powertools/powertools-lambda-typescript/issues/4513) for more details and examples, and add 👍 if you would like us to prioritize it.
213
+
This is also useful when splitting routes into separate files (see [Split routers](#split-routers) section) or when using [API mappings](https://docs.aws.amazon.com/apigateway/latest/developerguide/rest-api-mappings.html){target="_blank"} to map custom domains to specific base paths.
214
+
215
+
For example, when using `prefix: '/pay'`, there is no difference between a request path of `/pay` and `/pay/`; and the path argument would be defined as `/`.
206
216
207
217
## Advanced
208
218
@@ -213,9 +223,8 @@ incoming request and your route handler. They provide a way to implement cross-c
213
223
concerns like authentication, logging, validation, and response transformation without
214
224
cluttering your route handlers.
215
225
216
-
Each middleware function receives the following arguments:
226
+
Each middleware function receives two arguments:
217
227
218
-
***params** - Route parameters extracted from the URL path
219
228
***reqCtx** - Request context containing the event, Lambda context, request, and response objects
220
229
***next** - A function to pass control to the next middleware in the chain
221
230
@@ -312,6 +321,11 @@ that no post-processing of your request will occur.
312
321
A common pattern to create reusable middleware is to implement a factory functions that
313
322
accepts configuration options and returns a middleware function.
0 commit comments