@muscara/htmx-jsonata 0.0.4 → 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:
14
+ ## attributes
17
15
 
18
- - `jsonata-url` (required): the JSON API URL to fetch data from.
16
+ - `jn-url` (required): the url to fetch data from.
17
+ - `jn-query`: jsonata expression to filter and transform the data
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.
19
20
 
20
- - `jsonata`: jsonata expression to filter, transform, or query the JSON response.
21
+ ## example
21
22
 
22
- - `jsonata-property`: specifies which property from the parsed result to display in the element.
23
-
24
- example use:
23
+ [sample API response](https://my-json-server.typicode.com/kevinmuscara/htmx-jsonata/events)
25
24
 
26
25
  ```html
27
- <div jsonata-url="https://jsonplaceholder.typicode.com/users" jsonata="$[name='Leanne Graham']">
28
- <h1 jsonata-property="name"></h1>
29
- <p jsonata-property="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
@@ -11,7 +11,7 @@
11
11
  onEvent: function(name, evt) {
12
12
  if (name === 'htmx:afterProcessNode') {
13
13
  const elt = evt.detail.elt;
14
- const jsonataUrl = elt.getAttribute('jsonata-url');
14
+ const jsonataUrl = elt.getAttribute('jn-url');
15
15
 
16
16
  if (jsonataUrl) processJsonataElement(elt, jsonataUrl)
17
17
  }
@@ -21,7 +21,7 @@
21
21
  }
22
22
 
23
23
  function processJsonataElement(element, url) {
24
- const jsonataExpression = element.getAttribute('jsonata');
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,10 +57,42 @@
61
57
  }
62
58
 
63
59
  function populateElements(element, result) {
64
- const propertyElements = element.querySelectorAll('[jsonata-property]');
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
+
92
+ const propertyElements = element.querySelectorAll('[jn-path]');
65
93
 
66
94
  propertyElements.forEach(propElt => {
67
- const propertyName = propElt.getAttribute('jsonata-property');
95
+ const propertyName = propElt.getAttribute('jn-path');
68
96
 
69
97
  if (result && propertyName) {
70
98
  const value = getNestedProperty(result, propertyName);
@@ -115,9 +143,9 @@
115
143
  }
116
144
 
117
145
  function initializeJsonataElements() {
118
- const elements = document.querySelectorAll('[jsonata-url]');
146
+ const elements = document.querySelectorAll('[jn-url]');
119
147
  elements.forEach(elt => {
120
- const url = elt.getAttribute('jsonata-url');
148
+ const url = elt.getAttribute('jn-url');
121
149
  if (url) processJsonataElement(elt, url);
122
150
  });
123
151
  }
@@ -1 +1 @@
1
- !function(){const t=window.jsonata||("undefined"!=typeof jsonata?jsonata:null);function e(e,o){const r=e.getAttribute("jsonata");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 s=t(r).evaluate(o);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("[jsonata-property]").forEach((t=>{const n=t.getAttribute("jsonata-property");if(e&&n){const o=function(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}(e,n);null!=o&&("INPUT"===t.tagName||"TEXTAREA"===t.tagName?t.value=o:t.textContent=o)}}))}function o(){document.querySelectorAll("[jsonata-url]").forEach((t=>{const n=t.getAttribute("jsonata-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("jsonata-url");o&&e(t,o)}return!0}}),"loading"===document.readyState?document.addEventListener("DOMContentLoaded",(()=>o())):o()):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.4",
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": {