Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/event-handler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@
"types": "./lib/esm/types/index.d.ts",
"default": "./lib/esm/types/index.js"
}
},
"./experimental-rest": {
"require": {
"types": "./lib/cjs/rest/index.d.ts",
"default": "./lib/cjs/rest/index.js"
},
"import": {
"types": "./lib/esm/rest/index.d.ts",
"default": "./lib/esm/rest/index.js"
}
}
},
"typesVersions": {
Expand All @@ -86,6 +96,10 @@
"types": [
"./lib/cjs/types/index.d.ts",
"./lib/esm/types/index.d.ts"
],
"experimental-rest": [
"./lib/cjs/rest/index.d.ts",
"./lib/esm/rest/index.d.ts"
]
}
},
Expand Down
7 changes: 5 additions & 2 deletions packages/event-handler/src/rest/RouteHandlerRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {
DynamicRoute,
HttpMethod,
Path,
RouteHandlerOptions,
RestRouteHandlerOptions,
RouteRegistryOptions,
ValidationResult,
} from '../types/rest.js';
Expand Down Expand Up @@ -147,7 +147,10 @@ class RouteHandlerRegistry {
* @param path - The path to match
* @returns Route handler options or null if no match found
*/
public resolve(method: HttpMethod, path: Path): RouteHandlerOptions | null {
public resolve(
method: HttpMethod,
path: Path
): RestRouteHandlerOptions | null {
if (this.#shouldSort) {
this.#dynamicRoutes.sort(this.#compareRouteSpecificity);
this.#shouldSort = false;
Expand Down
16 changes: 8 additions & 8 deletions packages/event-handler/src/rest/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ import type {
Middleware,
Path,
RequestContext,
RestRouteOptions,
RestRouterOptions,
RouteHandler,
RouteOptions,
RouterOptions,
} from '../types/rest.js';
import { HttpErrorCodes, HttpVerbs } from './constants.js';
import {
handlerResultToProxyResult,
handlerResultToResponse,
handlerResultToWebResponse,
proxyEventToWebRequest,
responseToProxyResult,
webResponseToProxyResult,
} from './converters.js';
import { ErrorHandlerRegistry } from './ErrorHandlerRegistry.js';
import {
Expand Down Expand Up @@ -57,7 +57,7 @@ class Router {
*/
protected readonly isDev: boolean = false;

public constructor(options?: RouterOptions) {
public constructor(options?: RestRouterOptions) {
this.context = {};
const alcLogLevel = getStringFromEnv({
key: 'AWS_LAMBDA_LOG_LEVEL',
Expand Down Expand Up @@ -236,7 +236,7 @@ class Router {

const handlerMiddleware: Middleware = async (params, options, next) => {
const handlerResult = await handler(params, options);
options.res = handlerResultToResponse(
options.res = handlerResultToWebResponse(
handlerResult,
options.res.headers
);
Expand Down Expand Up @@ -266,11 +266,11 @@ class Router {
...handlerOptions,
scope: options?.scope,
});
return await responseToProxyResult(result);
return await webResponseToProxyResult(result);
}
}

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

Expand Down
6 changes: 3 additions & 3 deletions packages/event-handler/src/rest/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const proxyEventToWebRequest = (
* @param response - The Web API Response object
* @returns An API Gateway proxy result
*/
export const responseToProxyResult = async (
export const webResponseToProxyResult = async (
response: Response
): Promise<APIGatewayProxyResult> => {
const headers: Record<string, string> = {};
Expand Down Expand Up @@ -111,7 +111,7 @@ export const responseToProxyResult = async (
* @param headers - Optional headers to be included in the response
* @returns A Web API Response object
*/
export const handlerResultToResponse = (
export const handlerResultToWebResponse = (
response: HandlerResponse,
resHeaders?: Headers
): Response => {
Expand Down Expand Up @@ -159,7 +159,7 @@ export const handlerResultToProxyResult = async (
return response;
}
if (response instanceof Response) {
return await responseToProxyResult(response);
return await webResponseToProxyResult(response);
}
return {
statusCode: 200,
Expand Down
5 changes: 5 additions & 0 deletions packages/event-handler/src/rest/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export { HttpErrorCodes, HttpVerbs } from './constants.js';
export * from './converters.js';
export * from './errors.js';
export { Router } from './Router.js';
export * from './utils.js';
16 changes: 16 additions & 0 deletions packages/event-handler/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,19 @@ export type {
Anything,
ResolveOptions,
} from './common.js';

export type {
ErrorHandler,
ErrorResolveOptions,
ErrorResponse,
HandlerResponse,
HttpMethod,
HttpStatusCode,
Middleware,
Path,
RequestContext,
RestRouteHandlerOptions,
RestRouteOptions,
RestRouterOptions,
RouteHandler,
} from './rest.js';
12 changes: 6 additions & 6 deletions packages/event-handler/src/types/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ interface ErrorConstructor<T extends Error = Error> {
/**
* Options for the {@link Router} class
*/
type RouterOptions = {
type RestRouterOptions = {
/**
* A logger instance to be used for logging debug, warning, and error messages.
*
Expand Down Expand Up @@ -67,14 +67,14 @@ type HttpStatusCode = (typeof HttpErrorCodes)[keyof typeof HttpErrorCodes];

type Path = `/${string}`;

type RouteHandlerOptions = {
type RestRouteHandlerOptions = {
handler: RouteHandler;
params: Record<string, string>;
rawParams: Record<string, string>;
middleware: Middleware[];
};

type RouteOptions = {
type RestRouteOptions = {
method: HttpMethod | HttpMethod[];
path: Path;
middleware?: Middleware[];
Expand Down Expand Up @@ -125,10 +125,10 @@ export type {
Middleware,
Path,
RequestContext,
RouterOptions,
RestRouterOptions,
RouteHandler,
RouteOptions,
RouteHandlerOptions,
RestRouteOptions,
RestRouteHandlerOptions,
RouteRegistryOptions,
ValidationResult,
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';
import { HttpErrorCodes } from '../../../src/rest/constants.js';
import { ErrorHandlerRegistry } from '../../../src/rest/ErrorHandlerRegistry.js';
import { HttpErrorCodes } from '../../../src/rest/index.js';
import type {
HttpStatusCode,
RequestContext,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
import { HttpVerbs } from '../../../src/rest/constants.js';
import { HttpVerbs } from '../../../src/rest/index.js';
import { Route } from '../../../src/rest/Route.js';
import { RouteHandlerRegistry } from '../../../src/rest/RouteHandlerRegistry.js';
import type { Path } from '../../../src/types/rest.js';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import context from '@aws-lambda-powertools/testing-utils/context';
import { describe, expect, it } from 'vitest';
import { HttpErrorCodes, HttpVerbs } from '../../../../src/rest/constants.js';
import { InternalServerError } from '../../../../src/rest/errors.js';
import { Router } from '../../../../src/rest/Router.js';
import {
HttpErrorCodes,
HttpVerbs,
InternalServerError,
Router,
} from '../../../../src/rest/index.js';
import type { HttpMethod, RouteHandler } from '../../../../src/types/rest.js';
import { createTestEvent } from '../helpers.js';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import context from '@aws-lambda-powertools/testing-utils/context';
import type { Context } from 'aws-lambda';
import { describe, expect, it } from 'vitest';
import { HttpErrorCodes } from '../../../../src/rest/constants.js';
import {
BadRequestError,
HttpErrorCodes,
MethodNotAllowedError,
type NotFoundError,
} from '../../../../src/rest/errors.js';
import { Router } from '../../../../src/rest/Router.js';
Router,
} from '../../../../src/rest/index.js';
import { createTestEvent, createTrackingMiddleware } from '../helpers.js';

describe('Class: Router - Decorators', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import context from '@aws-lambda-powertools/testing-utils/context';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { HttpErrorCodes } from '../../../../src/rest/constants.js';
import {
BadRequestError,
HttpErrorCodes,
InternalServerError,
MethodNotAllowedError,
} from '../../../../src/rest/errors.js';
import { Router } from '../../../../src/rest/Router.js';
Router,
} from '../../../../src/rest/index.js';
import { createTestEvent } from '../helpers.js';

describe('Class: Router - Error Handling', () => {
Expand Down
7 changes: 3 additions & 4 deletions packages/event-handler/tests/unit/rest/Router/logging.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { HttpVerbs } from '../../../../src/rest/constants.js';
import { Router } from '../../../../src/rest/Router.js';
import type { RouterOptions } from '../../../../src/types/rest.js';
import { HttpVerbs, Router } from '../../../../src/rest/index.js';
import type { RestRouterOptions } from '../../../../src/types/rest.js';

describe('Class: Router - Logging', () => {
class TestResolver extends Router {
constructor(options?: RouterOptions) {
constructor(options?: RestRouterOptions) {
super(options);
this.logger.debug('test debug');
this.logger.warn('test warn');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import context from '@aws-lambda-powertools/testing-utils/context';
import type { Context } from 'aws-lambda';
import { describe, expect, it, vi } from 'vitest';
import { HttpErrorCodes } from '../../../../src/rest/constants.js';
import { Router } from '../../../../src/rest/Router.js';
import { HttpErrorCodes, Router } from '../../../../src/rest/index.js';
import type {
Middleware,
Path,
Expand Down
Loading
Loading