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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/ext/path-params.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
htmx.defineExtension('path-params', {
onEvent: function(name, evt) {
if (name === "htmx:configRequest") {
evt.detail.path = evt.detail.path.replace(/{([^}]+)}/g, function (_, param) {
var val = evt.detail.parameters[param];
delete evt.detail.parameters[param];
return val === undefined ? "{" + param + "}" : encodeURIComponent(val);
})
}
}
});
51 changes: 51 additions & 0 deletions test/ext/path-params.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
describe("path-params extension", function() {
beforeEach(function () {
this.server = makeServer();
clearWorkArea();
});
afterEach(function () {
this.server.restore();
clearWorkArea();
});

it('uses parameter to populate path variable', function () {
var request;
htmx.on("htmx:beforeRequest", function (evt) {
request = evt;
});
var div = make("<div hx-ext='path-params' hx-get='/items/{itemId}' hx-vals='{\"itemId\":42}'></div>")
div.click();
should.equal(request.detail.requestConfig.path, '/items/42');
});

it('parameter is removed when used', function () {
var request;
htmx.on("htmx:beforeRequest", function (evt) {
request = evt;
});
var div = make("<div hx-ext='path-params' hx-get='/items/{itemId}' hx-vals='{\"itemId\":42, \"other\":43}'></div>")
div.click();
should.equal(request.detail.requestConfig.parameters.other, 43);
should.equal(request.detail.requestConfig.parameters.itemId, undefined);
});

it('parameter value is encoded', function () {
var request;
htmx.on("htmx:beforeRequest", function (evt) {
request = evt;
});
var div = make("<div hx-ext='path-params' hx-get='/items/{itemId}' hx-vals='{\"itemId\":\"a/b\"}'></div>")
div.click();
should.equal(request.detail.requestConfig.path, '/items/a%2Fb');
});

it('missing variables are ignored', function () {
var request;
htmx.on("htmx:beforeRequest", function (evt) {
request = evt;
});
var div = make("<div hx-ext='path-params' hx-get='/items/{itemId}/{subitem}' hx-vals='{\"itemId\":42}'></div>")
div.click();
should.equal(request.detail.requestConfig.path, '/items/42/{subitem}');
});
});
3 changes: 3 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ <h2>Mocha Test Suite</h2>
<script src="../src/ext/response-targets.js"></script>
<script src="ext/response-targets.js"></script>

<script src="../src/ext/path-params.js"></script>
<script src="ext/path-params.js"></script>

<!-- events last so they don't screw up other tests -->
<script src="core/events.js"></script>

Expand Down
1 change: 1 addition & 0 deletions www/content/extensions/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ See the individual extension documentation for more details.
| [`restored`](@/extensions/restored.md) | allows you to trigger events when the back button has been pressed
| [`server-sent-events`](@/extensions/server-sent-events.md) | uni-directional server push messaging via [EventSource](https://developer.mozilla.org/en-US/docs/Web/API/EventSource)
| [`web-sockets`](@/extensions/web-sockets.md) | bi-directional connection to WebSocket servers
| [`path-params`](@/extensions/path-params.md) | allows to use parameters for path variables instead of sending them in query or body

</div>

Expand Down
21 changes: 21 additions & 0 deletions www/content/extensions/path-params.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
+++
title = path-params
+++

This extension uses request parameters to populate path variables. Used parameters are removed so they won't be sent in the query string or body anymore.

## Install

```html
<script src="https://unpkg.com/htmx.org/dist/ext/path-params.js">
```

## Usage

This would invoke URL `/items/42?foo=bar`

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