Skip to content

feat(event-handler): add resolution logic to base router #4349

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

svozza
Copy link
Contributor

@svozza svozza commented Aug 16, 2025

Summary

This PR implements the final resolution logic for the BaseRouter class. The resolve method is no longer abstract and has a concrete implementation where it looks up the route registry using the path and tries to retirve a resolver for the route. If not route is found, we throw a NotFoundErroe. If any exceptions are thrown from the handler, these are caught and handled by the already existing handleError method.

Changes

  • Update BaseRouter to have. oncrete implementation of resolve method
  • Added new conversion functions to produce the APIGatewayProxyResult type that lambda expects
  • Updated the unit tests now that the resolve function is implemented in the base class
  • Added tests to ensure this context is preserved when using decorators.

Issue number: closes #4142


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.

@svozza svozza added this to the Event Handler Beta (priority) milestone Aug 16, 2025
@svozza svozza requested a review from dreamorosi August 16, 2025 16:38
@svozza svozza self-assigned this Aug 16, 2025
@pull-request-size pull-request-size bot added the size/XXL PRs with 1K+ LOC, largely documentation related label Aug 16, 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 Aug 16, 2025
headers,
multiValueHeaders,
body: await response.text(),
isBase64Encoded: false,
Copy link
Contributor Author

@svozza svozza Aug 16, 2025

Choose a reason for hiding this comment

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

I've hardcodedisBase64Encoded for now but we will need to figure out how to deal with this

@@ -133,11 +139,49 @@ abstract class BaseRouter {
};
}

public abstract resolve(
public async resolve(
Copy link
Contributor Author

@svozza svozza Aug 16, 2025

Choose a reason for hiding this comment

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

Now that this function has an implementation, there are no abstract methods remaining. Should BaseRouter still be an abstract class?


try {
const request = proxyEventToWebRequest(event);
const path = new URL(request.url).pathname as Path;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In general, I dislike casting, should these two assignments use type guards?

@svozza svozza force-pushed the event-handler/response-resolver-logic branch from f5d5280 to 14a6f53 Compare August 17, 2025 19:12
@svozza svozza force-pushed the event-handler/response-resolver-logic branch from 14a6f53 to 7a9b621 Compare August 18, 2025 10:31
},
]);

return await handlerResultToProxyResult(result);
Copy link
Contributor Author

@svozza svozza Aug 18, 2025

Choose a reason for hiding this comment

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

At the moment, this function returns the type APIGatewayProxyResult. There is also APIGatewayProxyResultV2, which from my understanding are what API Gateway HTTP APIs, lambda function URLs, ALB lambda integration, and VPC Lattice integrations use.

In future PRs, we can autodetect the type of event we're dealing with here in the resolve function and convert to either the V1 or V2 response based on that. All we need to do is to update proxyEventToWebRequest to handle the different lambda event types.

The user would then not even need to know what type of lambda event they are dealing with and could port to different lambda integrations with zero code changes.

Copy link
Contributor

Choose a reason for hiding this comment

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

At the moment, this function returns the type APIGatewayProxyResult. There is also APIGatewayProxyResultV2, which from my understanding are what API Gateway HTTP APIs, lambda function URLs, ALB lambda integration, and VPC Lattice integrations use.

Not necessarily. While there is some overlap, there are some specific differences, especially when it comes to ALB. ALB implements the HTTP RFC spec closest and doesn't allow body = None, for example, as this will cause HTTP 5xx integration. It must be converted to empty string.

I would suggest allowing code abstraction across different Resolver responses, giving you flexibility. I believe this is something you're already doing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, the other way to do it is to have handlerResultToProxyResult as a method on any Resolver subclass and each type of resolver implements that themselves. You do lose the autodetection this way though because you have to explicitly instantiate that resolver class but as you say you benefit of that approach is more flexibility.

Copy link

this.logger.warn(
'Received an event that is not compatible with this resolver'
);
return;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I followed what the AppSync resolver does here but I'm thinking that we should probably throw here rather than return early.

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/XXL PRs with 1K+ LOC, largely documentation related tests PRs that add or change tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Implement Response Handling Architecture for Event Handler
2 participants