API Reference
    Preparing search index...
    Index

    Constructors

    Properties

    context: Record<string, unknown>
    errorHandlerRegistry: ErrorHandlerRegistry
    isDev: boolean = false

    Whether the router is running in development mode.

    logger: Pick<GenericLogger, "debug" | "warn" | "error">

    A logger instance to be used for logging debug, warning, and error messages.

    When no logger is provided, we'll only log warnings and errors using the global console object.

    middleware: Middleware[] = []
    prefix?: Path

    The base prefix to be used for all routes registered using this Router.

    routeRegistry: RouteHandlerRegistry

    Methods

    • Merges the routes, context and middleware from the passed router instance into this router instance.

      Override Behaviors:

      • Context: Properties from the included router override existing properties with the same key in the current router. A warning is logged when conflicts occur.
      • Routes: Routes from the included router are added to the current router's registry. If a route with the same method and path already exists, the included router's route takes precedence.
      • Error Handlers: Error handlers from the included router are merged with existing handlers. If handlers for the same error type exist in both routers, the included router's handler takes precedence.
      • Middleware: Middleware from the included router is appended to the current router's middleware array. All middleware executes in registration order (current router's middleware first, then included router's middleware).

      Parameters

      • router: Router

        The Router from which to merge the routes, context and middleware

      • Optionaloptions: { prefix: Path }

        Configuration options for merging the router

        • prefix: Path

          An optional prefix to be added to the paths defined in the router

      Returns void

      import { Router } from '@aws-lambda-powertools/event-handler/experimental-rest';

      const todosRouter = new Router();

      todosRouter.get('/todos', async () => {
      // List API
      });

      todosRouter.get('/todos/{todoId}', async () => {
      // Get API
      });

      const app = new Router();
      app.includeRouter(todosRouter);

      export const handler = async (event: unknown, context: Context) => {
      return app.resolve(event, context);
      };

    post

    • Resolves an API Gateway event by routing it to the appropriate handler and converting the result to an API Gateway proxy result. Handles errors using registered error handlers or falls back to default error handling (500 Internal Server Error).

      Parameters

      • event: unknown

        The Lambda event to resolve

      • context: Context

        The Lambda context

      • Optionaloptions: ResolveOptions

        Optional resolve options for scope binding

      Returns Promise<APIGatewayProxyResult>

      An API Gateway proxy result

    • Resolves an API Gateway event by routing it to the appropriate handler and streaming the response directly to the provided response stream. Used for Lambda response streaming.

      Parameters

      • event: unknown

        The Lambda event to resolve

      • context: Context

        The Lambda context

      • options: ResolveStreamOptions

        Stream resolve options including the response stream

      Returns Promise<void>

    • Registers a global middleware function that will be executed for all routes.

      Global middleware executes before route-specific middleware and follows the onion model where middleware executes in registration order before next() and in reverse order after next().

      Parameters

      • middleware: Middleware

        The middleware function to register globally

      Returns void

      const authMiddleware: Middleware = async ({ params, reqCtx, next }) => {
      // Authentication logic
      if (!isAuthenticated(reqCtx.req)) {
      return new Response('Unauthorized', { status: 401 });
      }
      await next();
      // Cleanup or logging after request completion
      console.log('Request completed');
      };

      router.use(authMiddleware);