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

All bindings exposed even if not in path #480

Closed
jaulz opened this issue Dec 2, 2021 · 2 comments · Fixed by #482
Closed

All bindings exposed even if not in path #480

jaulz opened this issue Dec 2, 2021 · 2 comments · Fixed by #482

Comments

@jaulz
Copy link
Contributor

jaulz commented Dec 2, 2021

Ziggy version

^1.2

Laravel version

^8.73

Description

Right now, Ziggy exposes all bindings of the controller method even if the variable is not part of the path. This is for example the case when a specific parameter is injected via a custom middleware. Hence, the library exposes more information than it is actually necessary.

My suggestion is to really expose only those parameters that are actually part of the path. Would you accept a PR for this?

Ziggy call and context

-

Ziggy configuration

-

Route definition

-
@bakerkretzmar
Copy link
Collaborator

I'm not sure exactly what you mean, can you give an example?

@jaulz
Copy link
Contributor Author

jaulz commented Dec 2, 2021

Sure, I have this controller method which has a custom new middleware that injects a new instance of the Team class. As you can see the path is /create only and does not contain the team parameter:

  #[
    Post(
      '/create',
      name: 'create',
      middleware: ['new:' . Team::class]
    )
  ]
  public function create(CreateRequest $request, Team $team)

However, ziggy generates the following JSON which includes the team binding.

{"api.team.create":{"uri":"/create","methods":["POST"],"domain":"localhost","bindings":{"team":"slug"}}}

The client doesn't need to know about it at all because it is not part of the path.

The reason how I stumbled across this is because I created a TypeScript helper function that uses the generated schema and that lets me know which parameter a specific route requires. In this case it tells me that I need to pass the team parameter though it's not required at all.

If you think this is not worth to implement, would you maybe be open for a PR that lets me specify how the actual JavaScript file is generated (i.e. /~https://github.com/tighten/ziggy/blob/main/src/CommandRouteGenerator.php#L38-L52)? Maybe via a custom Generator class that can be passed via the configuration?

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 a pull request may close this issue.

2 participants