Skip to content

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

@sdangol

Description

@sdangol

Expected Behavior

I should be able to return an array when using event handlers.

Current Behavior

I get a typescript error when trying to return an array from the handler

Code snippet

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);
};

Steps to Reproduce

  1. Create a lambda function with the code provided
  2. There's an typescript error on the handler for get orders

Possible Solution

We have defined the HandlerResponse type as type HandlerResponse = Response | JSONObject;
Instead, it should be type HandlerResponse = Response | JSONValue; to support both array and objects

Powertools for AWS Lambda (TypeScript) version

latest

AWS Lambda function runtime

22.x

Packaging format used

npm

Execution logs

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingconfirmedThe scope is clear, ready for implementationevent-handlerThis item relates to the Event Handler Utility

Type

No type

Projects

Status

Working on it

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions