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

add extension to populate path variables with request parameters #1307

Merged
merged 2 commits into from
Dec 14, 2023

Conversation

jyrimatti
Copy link
Contributor

This PR adds an extension to use request parameters to populate path variables. Used parameters are removed so they won't be sent in the query string or body anymore. Unrecognised variables are just ignored leaving the placeholder as it is.

For example, this:

<div hx-ext="path-params">
    <a hx-get="/items/{itemId}" hx-vals='{"itemId": "42", "foo": "bar"}'>test</div>
</div>

would invoke the URL /items/42?foo=bar.

Motivation is mainly that it's more or less just an API design choice whether to use query parameters, path variables, or both. When the API is under your own control and a "backend for frontend" kind of an API not meant to be used in other ways, you can of course make the decision to use just query parameters. But when interacting with external or more general APIs also path variables are often needed.

It might be nicer if Htmx supported this out-of-the-box as suggested in #1202 , but this extension provides a usable alternative.

What do you think? Any suggestions for improvements? Any ideas for a better name for the extension?

@Telroshan Telroshan added the extension Consideration for an extension label Sep 20, 2023
@alexpetros
Copy link
Collaborator

I don't mind putting this on one the website, because it comes up a lot and I think it's a good demonstration how to use extension to implement some new semantics.

If you rebase it, I'll put it up as a candidate to be merged.

@Telroshan Telroshan added the ready for review Issues that are ready to be considered for merging label Dec 7, 2023
@1cg 1cg merged commit 7866258 into bigskysoftware:dev Dec 14, 2023
1 check passed
@ddevienne
Copy link

The motivating example above uses hard-coded values. Is it possible to get them from a <form><input> instead?
(w/o Javascript, using only HTMX). The HTML I serve is to demonstrate a JSON API (barebone Postman / Swagger UI,
but w/o the OpenAPI spec), and I'd like users to enter route params via forms. Is that possible?

Perhaps via a #name syntax, that would read the value from <input id="name"/> ?

@jyrimatti
Copy link
Contributor Author

The motivating example above uses hard-coded values. Is it possible to get them from a <form><input> instead? (w/o Javascript, using only HTMX). The HTML I serve is to demonstrate a JSON API (barebone Postman / Swagger UI, but w/o the OpenAPI spec), and I'd like users to enter route params via forms. Is that possible?

Perhaps via a #name syntax, that would read the value from <input id="name"/> ?

Yes, of course. The example isn't a realistic use case, just an easy example. The extension uses the data provided by Htmx, wherever it gets it (form fields, hx-vals...). It justs "moves" a parameter to the path if there's an appropriate placeholder present.

@jyrimatti jyrimatti deleted the path-params branch January 21, 2024 17:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension Consideration for an extension ready for review Issues that are ready to be considered for merging
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants