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

Allow multiple uses of withTemplateParameters() #167

Open
Sonictherocketman opened this issue Oct 7, 2021 · 1 comment
Open

Allow multiple uses of withTemplateParameters() #167

Sonictherocketman opened this issue Oct 7, 2021 · 1 comment

Comments

@Sonictherocketman
Copy link

Is your feature request related to a problem or a specific use case? Please describe.

Yes. This issue could affect any workflow that would require multiple calls to follow() in their API calls.

Describe the solution you'd like

Currently, Traverson isn't able to configure multiple requests with different parameters, and later parameters override previous ones. This isn't exactly ideal since multiple endpoints can have similar request params (i.e. for pagination, search, etc).

Traverson should support the chaining of withTemplateParameters() calls that only apply to the previous requests without existing parameters of that type (i.e. no parameter overriding if they already exist for a request).

This would enable a much more elegant syntax for requests (shown below).

  const response =  Traverson.from(rootUrl)
    .follow('link_1')
    .withTemplateParameters({ page: 0 })
    .follow('link_2')
    .withTemplateParameters({ page:1 })
    .getResource()
    .result;

Describe alternatives you've considered

Currently this functionality can be achieved with two requests, which is both unwieldy and counter to the goals of Traverson's stated goals.

  const { url } =  Traverson.from(rootUrl)
    .follow('link_1')
    .withTemplateParameters({ page: 0 })
    .getResource()
    .result;
  const response =  Traverson.from(url)
    .follow('link_2')
    .withTemplateParameters({ page: 1 })
    .getResource()
    .result;

Additional context

N/A

@travi
Copy link
Member

travi commented Oct 7, 2021

while it is not clear from the docs, it is possible to pass an array of template parameters, similar to withRequestOptions, so that each item of the array aligns with each http request

you can see an example of this under https://github.com/traverson/traverson/blob/master/user-guide.markdown#uri-templates

if you would be interested in helping us make the API documentation of this capability more clear, a contribution would be welcome

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

No branches or pull requests

2 participants