@muscara/htmx-jsonata 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,30 +2,45 @@
2
2
 
3
3
  [htmx extension](https://htmx.org/extensions) that provides [jsonata](https://jsonata.org) query functionality for htmx elements
4
4
 
5
- add the following to your project to begin. **make sure you import after htmx and jsonata**
5
+ > [!TIP]
6
+ > make sure htmx-jsonata is imported after htmx and jsonata.
7
+
6
8
  ```html
7
9
  <!-- ... include jsonata -->
8
10
  <!-- ... include htmx -->
9
11
  <script src="https://unpkg.com/@muscara/htmx-jsonata/htmx-jsonata.min.js"></script>
10
12
  ```
11
13
 
12
- > only GET request method is supported
13
-
14
- ## usage
15
-
16
- attributes:
17
-
18
- - `jn-url` (required): the JSON API URL to fetch data from.
19
-
20
- - `jn-expression`: jsonata expression to filter, transform, or query the JSON response.
14
+ ## attributes
21
15
 
16
+ - `jn-url` (required): the url to fetch data from.
17
+ - `jn-query`: jsonata expression to filter and transform the data
22
18
  - `jn-path`: specifies a path to nested data (supports dot notation).
19
+ - `jn-each`: marks an element as a template to be cloned for each item in an array response.
20
+
21
+ ## example
23
22
 
24
- example use:
23
+ [sample API response](https://my-json-server.typicode.com/kevinmuscara/htmx-jsonata/events)
25
24
 
26
25
  ```html
27
- <div jn-url="https://jsonplaceholder.typicode.com/users" jn-expression="$[name='Leanne Graham']">
28
- <h1 jn-path="name"></h1>
29
- <p jn-path="phone"></p>
26
+ <div
27
+ jn-url="https://my-json-server.typicode.com/kevinmuscara/htmx-jsonata/events"
28
+ jn-query="
29
+ $[type='login'].{
30
+ 'user': user,
31
+ 'totalDuration': $sum(duration)
32
+ }
33
+ ~> $distinct()"
34
+ >
35
+ <ul>
36
+ <li jn-each>
37
+ <span jn-path="user"></span>: <span jn-path="totalDuration"></span> min
38
+ </li>
39
+ </ul>
30
40
  </div>
31
41
  ```
42
+
43
+ Output:
44
+ * alice: 15 min
45
+ * charlie: 8 min
46
+ * bob: 20 min
package/db.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "users": [
3
+ {
4
+ "id": 1,
5
+ "name": "Kevin Muscara",
6
+ "username": "muscara",
7
+ "email": "kevin@muscara.com",
8
+ "address": {
9
+ "street": "Light road",
10
+ "suite": "Apt. 556",
11
+ "city": "Cincinnati",
12
+ "zipcode": "45245"
13
+ }
14
+ }
15
+ ],
16
+ "events": [
17
+ {
18
+ "type": "login",
19
+ "user": "alice",
20
+ "duration": 15
21
+ },
22
+ {
23
+ "type": "purchase",
24
+ "user": "bob",
25
+ "amount": 120
26
+ },
27
+ {
28
+ "type": "login",
29
+ "user": "charlie",
30
+ "duration": 8
31
+ },
32
+ {
33
+ "type": "login",
34
+ "user": "bob",
35
+ "duration": 20
36
+ }
37
+ ]
38
+ }
package/htmx-jsonata.js CHANGED
@@ -21,7 +21,7 @@
21
21
  }
22
22
 
23
23
  function processJsonataElement(element, url) {
24
- const jsonataExpression = element.getAttribute('jn-expression');
24
+ const jsonataExpression = element.getAttribute('jn-query');
25
25
 
26
26
  fetch(url)
27
27
  .then(response => {
@@ -41,10 +41,6 @@
41
41
  Promise.resolve(evaluateResult).then(evalResult => {
42
42
  result = evalResult;
43
43
 
44
- if (Array.isArray(result) && result.length === 1) {
45
- result = result[0];
46
- }
47
-
48
44
  populateElements(element, result);
49
45
  });
50
46
  } catch (error) {
@@ -61,6 +57,38 @@
61
57
  }
62
58
 
63
59
  function populateElements(element, result) {
60
+ const eachElement = element.querySelector('[jn-each]');
61
+
62
+ if (eachElement && Array.isArray(result)) {
63
+ const parent = eachElement.parentElement;
64
+ const template = eachElement.cloneNode(true);
65
+ parent.innerHTML = '';
66
+
67
+ result.forEach(item => {
68
+ const clone = template.cloneNode(true);
69
+ clone.removeAttribute('jn-each');
70
+
71
+ const pathElements = clone.querySelectorAll('[jn-path]');
72
+ pathElements.forEach(pathElt => {
73
+ const propertyName = pathElt.getAttribute('jn-path');
74
+ if (item && propertyName) {
75
+ const value = getNestedProperty(item, propertyName);
76
+ if (value !== undefined && value !== null) {
77
+ if (pathElt.tagName === 'INPUT' || pathElt.tagName === 'TEXTAREA') {
78
+ pathElt.value = value;
79
+ } else {
80
+ pathElt.textContent = value;
81
+ }
82
+ }
83
+ }
84
+ });
85
+
86
+ parent.appendChild(clone);
87
+ });
88
+
89
+ return;
90
+ }
91
+
64
92
  const propertyElements = element.querySelectorAll('[jn-path]');
65
93
 
66
94
  propertyElements.forEach(propElt => {
@@ -1 +1 @@
1
- !function(){const t=window.jsonata||("undefined"!=typeof jsonata?jsonata:null);function e(e,r){const o=e.getAttribute("jn-expression");fetch(r).then((t=>{if(!t.ok)throw new Error(`error: ${t.status}`);return t.json()})).then((r=>{let a=r;if(o)try{const s=t(o).evaluate(r);Promise.resolve(s).then((t=>{a=t,Array.isArray(a)&&1===a.length&&(a=a[0]),n(e,a)}))}catch(t){return void console.error("Error evaluating jsonata expression:",t)}else n(e,a)})).catch((t=>{console.error("Error fetching or processing jsonata data:",t)}))}function n(t,e){t.querySelectorAll("[jn-path]").forEach((t=>{const n=t.getAttribute("jn-path");if(e&&n){const r=function(t,e){if(!t||!e)return;let n=t;const r=e.split(".");for(const t of r){const e=t.match(/^(.+?)\[(\d+)\]$/);if(e){const t=e[1],r=parseInt(e[2],10);if(!n||"object"!=typeof n||!(t in n))return;if(n=n[t],!(Array.isArray(n)&&r>=0&&r<n.length))return;n=n[r]}else{if(!n||"object"!=typeof n||!(t in n))return;n=n[t]}}return n}(e,n);null!=r&&("INPUT"===t.tagName||"TEXTAREA"===t.tagName?t.value=r:t.textContent=r)}}))}function r(){document.querySelectorAll("[jn-url]").forEach((t=>{const n=t.getAttribute("jn-url");n&&e(t,n)}))}t?("undefined"!=typeof htmx&&htmx.defineExtension("jsonata",{onEvent:function(t,n){if("htmx:afterProcessNode"===t){const t=n.detail.elt,r=t.getAttribute("jn-url");r&&e(t,r)}return!0}}),"loading"===document.readyState?document.addEventListener("DOMContentLoaded",(()=>r())):r()):console.error("htmx-jsonata extension requires the jsonata library. Include it before this extension.")}();
1
+ !function(){const t=window.jsonata||("undefined"!=typeof jsonata?jsonata:null);function e(e,o){const r=e.getAttribute("jn-query");fetch(o).then((t=>{if(!t.ok)throw new Error(`error: ${t.status}`);return t.json()})).then((o=>{let a=o;if(r)try{const c=t(r).evaluate(o);Promise.resolve(c).then((t=>{a=t,n(e,a)}))}catch(t){return void console.error("Error evaluating jsonata expression:",t)}else n(e,a)})).catch((t=>{console.error("Error fetching or processing jsonata data:",t)}))}function n(t,e){const n=t.querySelector("[jn-each]");if(n&&Array.isArray(e)){const t=n.parentElement,r=n.cloneNode(!0);return t.innerHTML="",void e.forEach((e=>{const n=r.cloneNode(!0);n.removeAttribute("jn-each");n.querySelectorAll("[jn-path]").forEach((t=>{const n=t.getAttribute("jn-path");if(e&&n){const r=o(e,n);null!=r&&("INPUT"===t.tagName||"TEXTAREA"===t.tagName?t.value=r:t.textContent=r)}})),t.appendChild(n)}))}t.querySelectorAll("[jn-path]").forEach((t=>{const n=t.getAttribute("jn-path");if(e&&n){const r=o(e,n);null!=r&&("INPUT"===t.tagName||"TEXTAREA"===t.tagName?t.value=r:t.textContent=r)}}))}function o(t,e){if(!t||!e)return;let n=t;const o=e.split(".");for(const t of o){const e=t.match(/^(.+?)\[(\d+)\]$/);if(e){const t=e[1],o=parseInt(e[2],10);if(!n||"object"!=typeof n||!(t in n))return;if(n=n[t],!(Array.isArray(n)&&o>=0&&o<n.length))return;n=n[o]}else{if(!n||"object"!=typeof n||!(t in n))return;n=n[t]}}return n}function r(){document.querySelectorAll("[jn-url]").forEach((t=>{const n=t.getAttribute("jn-url");n&&e(t,n)}))}t?("undefined"!=typeof htmx&&htmx.defineExtension("jsonata",{onEvent:function(t,n){if("htmx:afterProcessNode"===t){const t=n.detail.elt,o=t.getAttribute("jn-url");o&&e(t,o)}return!0}}),"loading"===document.readyState?document.addEventListener("DOMContentLoaded",(()=>r())):r()):console.error("htmx-jsonata extension requires the jsonata library. Include it before this extension.")}();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@muscara/htmx-jsonata",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "description": "htmx extension that provides jsonata query functionality for htmx elements",
5
5
  "main": "htmx-jsonata.min.js",
6
6
  "scripts": {