Skip to content

Commit 23eddfd

Browse files
authored
chore(event-handler): expose rest handler functionality (#4458)
1 parent 200f47b commit 23eddfd

17 files changed

+119
-58
lines changed

packages/event-handler/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@
6767
"types": "./lib/esm/types/index.d.ts",
6868
"default": "./lib/esm/types/index.js"
6969
}
70+
},
71+
"./experimental-rest": {
72+
"require": {
73+
"types": "./lib/cjs/rest/index.d.ts",
74+
"default": "./lib/cjs/rest/index.js"
75+
},
76+
"import": {
77+
"types": "./lib/esm/rest/index.d.ts",
78+
"default": "./lib/esm/rest/index.js"
79+
}
7080
}
7181
},
7282
"typesVersions": {
@@ -86,6 +96,10 @@
8696
"types": [
8797
"./lib/cjs/types/index.d.ts",
8898
"./lib/esm/types/index.d.ts"
99+
],
100+
"experimental-rest": [
101+
"./lib/cjs/rest/index.d.ts",
102+
"./lib/esm/rest/index.d.ts"
89103
]
90104
}
91105
},

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type {
33
DynamicRoute,
44
HttpMethod,
55
Path,
6-
RouteHandlerOptions,
6+
RestRouteHandlerOptions,
77
RouteRegistryOptions,
88
ValidationResult,
99
} from '../types/rest.js';
@@ -147,7 +147,10 @@ class RouteHandlerRegistry {
147147
* @param path - The path to match
148148
* @returns Route handler options or null if no match found
149149
*/
150-
public resolve(method: HttpMethod, path: Path): RouteHandlerOptions | null {
150+
public resolve(
151+
method: HttpMethod,
152+
path: Path
153+
): RestRouteHandlerOptions | null {
151154
if (this.#shouldSort) {
152155
this.#dynamicRoutes.sort(this.#compareRouteSpecificity);
153156
this.#shouldSort = false;

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ import type {
1313
Middleware,
1414
Path,
1515
RequestContext,
16+
RestRouteOptions,
17+
RestRouterOptions,
1618
RouteHandler,
17-
RouteOptions,
18-
RouterOptions,
1919
} from '../types/rest.js';
2020
import { HttpErrorCodes, HttpVerbs } from './constants.js';
2121
import {
2222
handlerResultToProxyResult,
23-
handlerResultToResponse,
23+
handlerResultToWebResponse,
2424
proxyEventToWebRequest,
25-
responseToProxyResult,
25+
webResponseToProxyResult,
2626
} from './converters.js';
2727
import { ErrorHandlerRegistry } from './ErrorHandlerRegistry.js';
2828
import {
@@ -57,7 +57,7 @@ class Router {
5757
*/
5858
protected readonly isDev: boolean = false;
5959

60-
public constructor(options?: RouterOptions) {
60+
public constructor(options?: RestRouterOptions) {
6161
this.context = {};
6262
const alcLogLevel = getStringFromEnv({
6363
key: 'AWS_LAMBDA_LOG_LEVEL',
@@ -236,7 +236,7 @@ class Router {
236236

237237
const handlerMiddleware: Middleware = async (params, options, next) => {
238238
const handlerResult = await handler(params, options);
239-
options.res = handlerResultToResponse(
239+
options.res = handlerResultToWebResponse(
240240
handlerResult,
241241
options.res.headers
242242
);
@@ -266,11 +266,11 @@ class Router {
266266
...handlerOptions,
267267
scope: options?.scope,
268268
});
269-
return await responseToProxyResult(result);
269+
return await webResponseToProxyResult(result);
270270
}
271271
}
272272

273-
public route(handler: RouteHandler, options: RouteOptions): void {
273+
public route(handler: RouteHandler, options: RestRouteOptions): void {
274274
const { method, path, middleware = [] } = options;
275275
const methods = Array.isArray(method) ? method : [method];
276276

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export const proxyEventToWebRequest = (
7474
* @param response - The Web API Response object
7575
* @returns An API Gateway proxy result
7676
*/
77-
export const responseToProxyResult = async (
77+
export const webResponseToProxyResult = async (
7878
response: Response
7979
): Promise<APIGatewayProxyResult> => {
8080
const headers: Record<string, string> = {};
@@ -111,7 +111,7 @@ export const responseToProxyResult = async (
111111
* @param headers - Optional headers to be included in the response
112112
* @returns A Web API Response object
113113
*/
114-
export const handlerResultToResponse = (
114+
export const handlerResultToWebResponse = (
115115
response: HandlerResponse,
116116
resHeaders?: Headers
117117
): Response => {
@@ -159,7 +159,7 @@ export const handlerResultToProxyResult = async (
159159
return response;
160160
}
161161
if (response instanceof Response) {
162-
return await responseToProxyResult(response);
162+
return await webResponseToProxyResult(response);
163163
}
164164
return {
165165
statusCode: 200,
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
export { HttpErrorCodes, HttpVerbs } from './constants.js';
2+
export {
3+
handlerResultToProxyResult,
4+
handlerResultToWebResponse,
5+
proxyEventToWebRequest,
6+
webResponseToProxyResult,
7+
} from './converters.js';
8+
export {
9+
BadRequestError,
10+
ForbiddenError,
11+
InternalServerError,
12+
MethodNotAllowedError,
13+
NotFoundError,
14+
ParameterValidationError,
15+
RequestEntityTooLargeError,
16+
RequestTimeoutError,
17+
RouteMatchingError,
18+
ServiceError,
19+
ServiceUnavailableError,
20+
UnauthorizedError,
21+
} from './errors.js';
22+
export { Router } from './Router.js';
23+
export {
24+
composeMiddleware,
25+
isAPIGatewayProxyEvent,
26+
isAPIGatewayProxyResult,
27+
isHttpMethod,
28+
} from './utils.js';

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,19 @@ export type {
3030
Anything,
3131
ResolveOptions,
3232
} from './common.js';
33+
34+
export type {
35+
ErrorHandler,
36+
ErrorResolveOptions,
37+
ErrorResponse,
38+
HandlerResponse,
39+
HttpMethod,
40+
HttpStatusCode,
41+
Middleware,
42+
Path,
43+
RequestContext,
44+
RestRouteHandlerOptions,
45+
RestRouteOptions,
46+
RestRouterOptions,
47+
RouteHandler,
48+
} from './rest.js';

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ interface ErrorConstructor<T extends Error = Error> {
3636
/**
3737
* Options for the {@link Router} class
3838
*/
39-
type RouterOptions = {
39+
type RestRouterOptions = {
4040
/**
4141
* A logger instance to be used for logging debug, warning, and error messages.
4242
*
@@ -67,14 +67,14 @@ type HttpStatusCode = (typeof HttpErrorCodes)[keyof typeof HttpErrorCodes];
6767

6868
type Path = `/${string}`;
6969

70-
type RouteHandlerOptions = {
70+
type RestRouteHandlerOptions = {
7171
handler: RouteHandler;
7272
params: Record<string, string>;
7373
rawParams: Record<string, string>;
7474
middleware: Middleware[];
7575
};
7676

77-
type RouteOptions = {
77+
type RestRouteOptions = {
7878
method: HttpMethod | HttpMethod[];
7979
path: Path;
8080
middleware?: Middleware[];
@@ -125,10 +125,10 @@ export type {
125125
Middleware,
126126
Path,
127127
RequestContext,
128-
RouterOptions,
128+
RestRouterOptions,
129129
RouteHandler,
130-
RouteOptions,
131-
RouteHandlerOptions,
130+
RestRouteOptions,
131+
RestRouteHandlerOptions,
132132
RouteRegistryOptions,
133133
ValidationResult,
134134
};

packages/event-handler/tests/unit/rest/ErrorHandlerRegistry.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from 'vitest';
2-
import { HttpErrorCodes } from '../../../src/rest/constants.js';
32
import { ErrorHandlerRegistry } from '../../../src/rest/ErrorHandlerRegistry.js';
3+
import { HttpErrorCodes } from '../../../src/rest/index.js';
44
import type {
55
HttpStatusCode,
66
RequestContext,

packages/event-handler/tests/unit/rest/RouteHandlerRegistry.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from 'vitest';
2-
import { HttpVerbs } from '../../../src/rest/constants.js';
2+
import { HttpVerbs } from '../../../src/rest/index.js';
33
import { Route } from '../../../src/rest/Route.js';
44
import { RouteHandlerRegistry } from '../../../src/rest/RouteHandlerRegistry.js';
55
import type { Path } from '../../../src/types/rest.js';

packages/event-handler/tests/unit/rest/Router/basic-routing.test.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import context from '@aws-lambda-powertools/testing-utils/context';
22
import { describe, expect, it } from 'vitest';
3-
import { HttpErrorCodes, HttpVerbs } from '../../../../src/rest/constants.js';
4-
import { InternalServerError } from '../../../../src/rest/errors.js';
5-
import { Router } from '../../../../src/rest/Router.js';
3+
import {
4+
HttpErrorCodes,
5+
HttpVerbs,
6+
InternalServerError,
7+
Router,
8+
} from '../../../../src/rest/index.js';
69
import type { HttpMethod, RouteHandler } from '../../../../src/types/rest.js';
710
import { createTestEvent } from '../helpers.js';
811

0 commit comments

Comments
 (0)