Skip to content

Commit 491fe9f

Browse files
committed
fix(event-handler): pass event, context and request objects into handlers
1 parent 17845d0 commit 491fe9f

File tree

5 files changed

+255
-99
lines changed

5 files changed

+255
-99
lines changed

packages/event-handler/src/rest/BaseRouter.ts

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { ResolveOptions } from '../types/index.js';
88
import type {
99
ErrorConstructor,
1010
ErrorHandler,
11+
ErrorResolveOptions,
1112
HttpMethod,
1213
Path,
1314
RouteHandler,
@@ -157,12 +158,19 @@ abstract class BaseRouter {
157158
*/
158159
protected async handleError(
159160
error: Error,
160-
options?: ResolveOptions
161+
options: ErrorResolveOptions
161162
): Promise<Response> {
162163
const handler = this.errorHandlerRegistry.resolve(error);
163164
if (handler !== null) {
164165
try {
165-
const body = await handler.apply(options?.scope ?? this, [error]);
166+
const body = await handler.apply(options.scope ?? this, [
167+
error,
168+
{
169+
request: options.request,
170+
event: options.event,
171+
context: options.context,
172+
},
173+
]);
166174
return new Response(JSON.stringify(body), {
167175
status: body.statusCode,
168176
headers: { 'Content-Type': 'application/json' },
@@ -273,24 +281,6 @@ abstract class BaseRouter {
273281
): MethodDecorator | undefined {
274282
return this.#handleHttpMethod(HttpVerbs.OPTIONS, path, handler);
275283
}
276-
277-
public connect(path: Path, handler: RouteHandler): void;
278-
public connect(path: Path): MethodDecorator;
279-
public connect(
280-
path: Path,
281-
handler?: RouteHandler
282-
): MethodDecorator | undefined {
283-
return this.#handleHttpMethod(HttpVerbs.CONNECT, path, handler);
284-
}
285-
286-
public trace(path: Path, handler: RouteHandler): void;
287-
public trace(path: Path): MethodDecorator;
288-
public trace(
289-
path: Path,
290-
handler?: RouteHandler
291-
): MethodDecorator | undefined {
292-
return this.#handleHttpMethod(HttpVerbs.TRACE, path, handler);
293-
}
294284
}
295285

296286
export { BaseRouter };

packages/event-handler/src/rest/constants.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
export const HttpVerbs = {
2-
CONNECT: 'CONNECT',
3-
TRACE: 'TRACE',
42
GET: 'GET',
53
POST: 'POST',
64
PUT: 'PUT',

packages/event-handler/src/types/rest.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
1-
import type { GenericLogger } from '@aws-lambda-powertools/commons/types';
1+
import type {
2+
GenericLogger,
3+
JSONObject,
4+
} from '@aws-lambda-powertools/commons/types';
5+
import type { APIGatewayProxyEvent, Context } from 'aws-lambda';
26
import type { BaseRouter } from '../rest/BaseRouter.js';
37
import type { HttpErrorCodes, HttpVerbs } from '../rest/constants.js';
48
import type { Route } from '../rest/Route.js';
9+
import type { ResolveOptions } from './common.js';
510

611
type ErrorResponse = {
712
statusCode: HttpStatusCode;
813
error: string;
914
message: string;
1015
};
1116

12-
interface ErrorContext {
13-
path: string;
14-
method: string;
15-
headers: Record<string, string>;
16-
timestamp: string;
17-
requestId?: string;
18-
}
17+
type RequestOptions = {
18+
request: Request;
19+
event: APIGatewayProxyEvent;
20+
context: Context;
21+
};
22+
23+
type ErrorResolveOptions = RequestOptions & ResolveOptions;
1924

2025
type ErrorHandler<T extends Error = Error> = (
2126
error: T,
22-
context?: ErrorContext
27+
options?: RequestOptions
2328
) => Promise<ErrorResponse>;
2429

2530
interface ErrorConstructor<T extends Error = Error> {
@@ -49,7 +54,10 @@ interface CompiledRoute {
4954
type DynamicRoute = Route & CompiledRoute;
5055

5156
// biome-ignore lint/suspicious/noExplicitAny: we want to keep arguments and return types as any to accept any type of function
52-
type RouteHandler<T = any, R = any> = (...args: T[]) => R;
57+
type RouteHandler<
58+
TParams = Record<string, unknown>,
59+
TReturn = Response | JSONObject,
60+
> = (args: TParams, options?: RequestOptions) => Promise<TReturn>;
5361

5462
type HttpMethod = keyof typeof HttpVerbs;
5563

@@ -98,9 +106,11 @@ export type {
98106
ErrorConstructor,
99107
ErrorHandlerRegistryOptions,
100108
ErrorHandler,
109+
ErrorResolveOptions,
101110
HttpStatusCode,
102111
HttpMethod,
103112
Path,
113+
RequestOptions,
104114
RouterOptions,
105115
RouteHandler,
106116
RouteOptions,

0 commit comments

Comments
 (0)