Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Export APIGatewayEventRequestContext schema #3409

Closed
1 of 2 tasks
martinnj-adsk opened this issue Dec 9, 2024 · 2 comments · Fixed by #3507
Closed
1 of 2 tasks

Feature request: Export APIGatewayEventRequestContext schema #3409

martinnj-adsk opened this issue Dec 9, 2024 · 2 comments · Fixed by #3507
Assignees
Labels
feature-request This item refers to a feature request for an existing or new utility parser This item relates to the Parser Utility pending-release This item has been merged and will be released soon

Comments

@martinnj-adsk
Copy link

martinnj-adsk commented Dec 9, 2024

Use case

If you have a custom authorizer lambda that adds context to an API Gateway Event, the event won't be valid if using APIGatewayProxyEventSchema, as the authorizer key is now invalid. Exporting the schema will allow me to extend it, and then define my authorizer context manually.

Solution/User Experience

Export APIGatewayEventRequestContext schema.

Powertools for AWS Lambda (TypeScript) version

2.10.0

AWS Lambda function runtime

20.x

Alternative solutions

Acknowledgment

Future readers

Please react with 👍 and your use case to help us understand customer demand.

@martinnj-adsk martinnj-adsk added feature-request This item refers to a feature request for an existing or new utility triage This item has not been triaged by a maintainer, please wait labels Dec 9, 2024
@dreamorosi
Copy link
Contributor

dreamorosi commented Jan 21, 2025

Hi @martinnj-adsk, apologies for the delay.

I have tried to reproduce the behavior you described and I am not able to make the validation fail even when adding context.

I have two functions, one authorizer:

export const handler = async (event) => {
  return {
    principalId: 'user',
    policyDocument: {
      Version: '2012-10-17',
      Statement: [
        {
          Action: 'execute-api:Invoke',
          Effect: 'Allow',
          Resource: event.methodArn,
        },
      ],
    },
    context: {
      foo: 'bar',
    },
  }
}

and an API:

import { APIGatewayProxyEventSchema } from '@aws-lambda-powertools/parser/schemas/api-gateway';

export const handler = async (event) => {
  try {
    const parsed = APIGatewayProxyEventSchema.parse(event);

    console.log(parsed.requestContext.authorizer);

    return {
      statusCode: 200,
    }
  } catch (error) {
    console.error(error);
    return {
      statusCode: 400,
      body: JSON.stringify({
        message: 'Invalid request',
      }),
    };
  }
}

When making a request, the parsing in the API function passes and the log looks like this:

{ principalId: 'user', integrationLatency: 0, foo: 'bar' }

This is because the schemas are not set to .strict().

With that said, I agree that we should export the APIGatewayEventRequestContext so that you can extend the schema and actually type the requestContext.authorizer object with your own context keys.

@dreamorosi dreamorosi self-assigned this Jan 21, 2025
@dreamorosi dreamorosi added confirmed The scope is clear, ready for implementation parser This item relates to the Parser Utility and removed triage This item has not been triaged by a maintainer, please wait labels Jan 21, 2025
@dreamorosi dreamorosi moved this from Triage to Working on it in Powertools for AWS Lambda (TypeScript) Jan 21, 2025
@dreamorosi dreamorosi moved this from Working on it to Pending review in Powertools for AWS Lambda (TypeScript) Jan 21, 2025
@github-project-automation github-project-automation bot moved this from Pending review to Coming soon in Powertools for AWS Lambda (TypeScript) Jan 22, 2025
Copy link
Contributor

⚠️ COMMENT VISIBILITY WARNING ⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

@github-actions github-actions bot added pending-release This item has been merged and will be released soon and removed confirmed The scope is clear, ready for implementation labels Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request This item refers to a feature request for an existing or new utility parser This item relates to the Parser Utility pending-release This item has been merged and will be released soon
Projects
Status: Coming soon
2 participants