Skip to content

feat(event-handler): implement route matching & resolution system for rest handler #4297

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

Merged
merged 12 commits into from
Aug 11, 2025

Conversation

svozza
Copy link
Contributor

@svozza svozza commented Aug 8, 2025

Summary

Implements the route matching and resolution system by following the pattern of the appsync-event handler and centralising the resolution logic in the RouteHandlerRegistry. This different from the proposed solution in #4140, which envisioned multiple classes involved in the routing and matching logic. This design is simpler and has less moving parts.

Changes

  • RouteHandlerRegistry
    • Changes the internal representation of the registry to contain registries for static and dynamic routes. The register function is responsible for deciding which goes where.
    • Added a method to determine the specificity of dynamic routes: routes with fewer parameters and more path segments are considered more specific.
    • When routes are added to the dynamic routes array, the addition is done in a way to ensure the array stays sorted in ascending order of specificity, i.e., the most specific route will be the first element etc. Doing so removes the need for the refresh method proposed that would have been called each time a route was added. This is also done in the register method.
    • The resolve method takes a HTTP method and a path
      • first we check the static route registry to find the handler
      • if that fails we check the dynamic registry, if we find a matching handler we extract any parameters from the path, parse and validate them, and return the handler, the parsed parameters and the raw parameters.
      • if that fails we return null.
  • We have added two new functions to the utils module, although I am leaning towards moving them to be private methods in the RouteHandlerRegistry class as I don't think they're going to be used anywhere else:
    • processParams, which URL decodes any parameters in the path
    • validateParams, which throws a validation error if the parameters have invalid values
  • Added unit tests with particular focus on ensuring the correctness of the specificity sorting. Note: the diff of the test file is so larger that GitHub automatically hides it.

Note

There was one part of the initial implementation that I wasn't able to incorporate. There was a recursive type that was defined but it was never used anywhere so I wasn't sure what to do with it:

type ExtractParams<T extends string> = 
  T extends `${infer Start}:${infer Param}/${infer Rest}`
    ? { [K in Param]: string } & ExtractParams<`${Start}${Rest}`>
    : T extends `${infer Start}:${infer Param}`
    ? { [K in Param]: string }
    : {};

Issue number: closes #4140


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 self-assigned this Aug 8, 2025
@pull-request-size pull-request-size bot added the size/XL PRs between 500-999 LOC, often PRs that grown with feedback label Aug 8, 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 8, 2025
@svozza svozza force-pushed the event-hanlder/route-matching-resolution branch from 1294c96 to 2d494eb Compare August 8, 2025 23:09
@svozza svozza force-pushed the event-hanlder/route-matching-resolution branch from 76bf247 to 9a1c059 Compare August 10, 2025 13:22
@svozza svozza requested a review from dreamorosi August 11, 2025 08:56
@svozza svozza force-pushed the event-hanlder/route-matching-resolution branch from 7e78c3c to c8d8bb4 Compare August 11, 2025 09:09
dreamorosi
dreamorosi previously approved these changes Aug 11, 2025
Copy link
Contributor

@dreamorosi dreamorosi left a comment

Choose a reason for hiding this comment

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

Amazing work here, especially with the deep dive in time complexity and optimization.

The code looks good even though I haven't tested it in a function, I think for now we can merge it and progress - either way we'll have a team bug bash when we're near the release.

The only thing potentially missing, which I am ok to tackle in a separate issue is type safety.

The linked issue had an ugly recursive type (which I could swear I saw a question from you about but can't find anymore) that basically aimed at giving the experience described in the RFC in the section "Dynamic routes" which would allow you to do this in the route handler:

Image

As I said, I'm ok to work on this separately and in the near future - I have a local PoC with this working (in terms of types) that I made over a year ago when I was working on the RFC, so we can potentially take some inspiration there.

@svozza svozza force-pushed the event-hanlder/route-matching-resolution branch from 885dfb0 to dd3d169 Compare August 11, 2025 09:15
@svozza
Copy link
Contributor Author

svozza commented Aug 11, 2025

The only thing potentially missing, which I am ok to tackle in a separate issue is type safety.

The linked issue had an ugly recursive type (which I could swear I saw a question from you about but can't find anymore) that basically aimed at giving the experience described in the #3500 in the section "Dynamic routes" which would allow you to do this in the route handler:

Yeah, I mentioned that recursive type in the PR body at the top, I wasn't sure what to do with it. That autocomplete use case is so nice, defo something we want! But agreed we can tackle it in a separate PR, this one is big enough as it is.

@dreamorosi dreamorosi self-requested a review August 11, 2025 09:56
dreamorosi
dreamorosi previously approved these changes Aug 11, 2025
Copy link
Contributor

@dreamorosi dreamorosi left a comment

Choose a reason for hiding this comment

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

Thanks for working on this!

@dreamorosi dreamorosi self-requested a review August 11, 2025 10:21
Copy link

@svozza svozza merged commit b8ca368 into main Aug 11, 2025
34 checks passed
@svozza svozza deleted the event-hanlder/route-matching-resolution branch August 11, 2025 10:23
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/XL PRs between 500-999 LOC, often PRs that grown with feedback tests PRs that add or change tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Implement Route Matching & Resolution System for Event Handler
2 participants