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

feat: add the ability to create API routes with multiple route parameters #271

Merged

Conversation

jonerickson
Copy link
Contributor

This PR aims to one, add a new feature, while two, allow more use cases for this package. Currently, when defining resources, if the route has another route parameter included, the package will not resolve the correct route parameter within the controller function definitions. Currently they are hardcoded to not allow more route parameters.

The current user story I am trying to fix with this PR is to define API routes like such: https://xxxxxx.com/api/{apiVersion}/users/{id}.

We want our end users to be able to specify the API version using a route parameter. Within our application, we look at apiVersion and determine which HTTP resource to return to them.

In the current state of the package, within the HandlesStandardOperations::show($request, $key) method, $key will resolve to, in our user story, v1, instead of the User ID.

To fix this, we have introduced a backwards compatible key resolver that changes the function definition to HandlesStandardOperations::show($request, ...$args) to allow multiple route parameters to be resolved. Within a bound KeyResolver class in the container, some simple logic is done to return which parameter should be used as the $key for the further operations.

If a user needs to customize the KeyResolver, they simply bind a new instance of their own custom implementation. I have included tests to illustrate.

@jonerickson
Copy link
Contributor Author

This PR is ready for review. There were two test case fails. One a seg fault, and the other a unique constraint violation. Both edge cases. I am confident if they were rerun they would pass. Thanks,

@alexzarbn
Copy link
Member

Hi @jonerickson,

Thank you for the PR!

I wonder if it would be possible to make the KeyResolver work with parameter names rather than indices?

@jonerickson
Copy link
Contributor Author

Great suggestion. Will update the PR!

@jonerickson
Copy link
Contributor Author

jonerickson commented Sep 19, 2024

Hey @alexzarbn, after mulling this over, I am not coming up with any solution other than what I suggested. Using an out-of-the-box implementation of this package, using the show endpoint as an example, we always know that the first argument in the method will be the key to resolve.

As users implement this package, we have no way of knowing what the parameter name is they want to use in the show endpoint to resolve correctly. We can obviously see all the parameters in the current route being requested, but we do not know which one the user wants to resolve as the show ID. Hence allowing the user to bind their own implementation of the key resolver.

In my case where I have all my routes as (using show as an example) .../{apiVersion}/users/{id}....I always know my key to be resolved will be the second parameter. And each parameter name will be different for each resource I am writing a route for.

Does this make sense? Can you think of another way to do it?

@jonerickson
Copy link
Contributor Author

Hey @alexzarbn any second thoughts on this? I have been using my fork of this feature in prod and it has worked great.

@alexzarbn
Copy link
Member

Hi @jonerickson,

Sorry for the late response - past few months were quite hectic.

Okay, that makes sense, thank you for looking into it! Merging ~

@alexzarbn alexzarbn merged commit 686c16c into tailflow:main Nov 4, 2024
72 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants