Skip to content

Conversation

@arnabrahman
Copy link
Contributor

@arnabrahman arnabrahman commented Nov 10, 2025

Summary

Previously, we couldn't return an array as an event handler response. This PR fixes that.

Changes

  • update HandlerResponse type, replace JSONObject with JSONValue
  • update error handling function to handle the new type
  • Add unit tests to cover array response

Example:

import type { APIGatewayProxyEvent, Context } from 'aws-lambda';
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { ScanCommand, DynamoDBDocument } from '@aws-sdk/lib-dynamodb';
import { Router } from '@aws-lambda-powertools/event-handler/experimental-rest';

const ddbDocClient = DynamoDBDocument.from(new DynamoDBClient());
const app = new Router();

app.get('/orders', async () => {
  const response = await ddbDocClient.send(
    new ScanCommand({
      TableName: 'OrdersWorkshop',
    })
  );
  if (response.Items && response.Items.length > 0) {
    return response.Items;
  }
  return { message: 'No orders found' };
});

export const lambdaHandler = (
  event: APIGatewayProxyEvent,
  context: Context
) => {
  app.resolve(event, context);
};

Issue number: closes #4593


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@pull-request-size pull-request-size bot added the size/L PRs between 100-499 LOC label Nov 10, 2025
@boring-cyborg boring-cyborg bot added event-handler This item relates to the Event Handler Utility tests PRs that add or change tests labels Nov 10, 2025
@sonarqubecloud
Copy link

@arnabrahman arnabrahman marked this pull request as ready for review November 11, 2025 04:52
@svozza svozza changed the title fix(rest): allow event handler response to return array fix(event-handler): allow event handler response to return array Nov 11, 2025
* @param body - The response body returned by the error handler, of type JSONValue
* @param error - The Error object associated with the response
*/
#errorBodyToResponse(body: JSONValue, error: Error): Response {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's call this errorBodyToWebResponse.

(event: unknown, _context: Context, responseStream: MockResponseStream) =>
app.resolveStream(event, _context, { scope, responseStream });

const createTestLambdaClass = (app: Router, expectedResponse: unknown) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this function to the event-handler/tests/unit/rest/helpers.ts file.

@svozza
Copy link
Contributor

svozza commented Nov 11, 2025

Two very minor comments but other than that, this is pretty much good to go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

event-handler This item relates to the Event Handler Utility size/L PRs between 100-499 LOC tests PRs that add or change tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Unable to return an array in the event handler response

2 participants