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

feat(nodejq): add more request options and powerful jq commands #2

Merged
merged 19 commits into from
Mar 3, 2021
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
25 changes: 15 additions & 10 deletions MMM-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Module.register("MMM-json", {
// Default module config.
defaults: {
url: "https://jsonplaceholder.typicode.com/users",
refreshInterval: 1000 * 60 * 5 //5 minutes
refreshInterval: 1000 * 60 * 5 // 5 minutes
},

start: function () {
Expand All @@ -17,38 +17,42 @@ Module.register("MMM-json", {
this.getData();

var self = this;
//Schedule updates
// Schedule updates
setInterval(function () {
self.getData();
self.updateDom();
}, this.config.refreshInterval);
},

//Import additional CSS Styles
// Import additional CSS Styles
getStyles: function () {
return ["mmm-json.css"];
},

//Contact node helper for data
// Contact node helper for data
getData: function () {
Log.info("MMM-json: getting data");

this.sendSocketNotification("MMM_JSON_GET_REQUEST", {
config: this.config
config: this.config,
identifier: this.identifier
});
},

//Handle node helper response
// Handle node helper response
socketNotificationReceived: function (notification, payload) {
if (notification === "MMM_JSON_GET_RESPONSE") {
if (
notification === "MMM_JSON_GET_RESPONSE" &&
payload.identifier == this.identifier
) {
if (payload.error === true) {
console.error(
"MMM-JSON: An Error occured while fetching your response. Please have a look at the server log."
);
this.loaded = false;
} else {
this.loaded = true;
this.response = payload;
this.response = payload.data;
}
this.updateDom(1000);
}
Expand All @@ -59,7 +63,8 @@ Module.register("MMM-json", {
if (this.config.headerIcon) {
return "";
} else {
// TODO: Remove `this.config.header` in a subsequent version (major) as it was wrongly implemented in the config on first release #
// TODO: Remove `this.config.header` in a subsequent version (major) as it
// was wrongly implemented in the config on first release #
return this.data.header ? this.data.header : this.config.header;
}
},
Expand All @@ -72,7 +77,7 @@ Module.register("MMM-json", {
return wrapper;
}

//Display loading while waiting for API response
// Display loading while waiting for API response
if (!this.loaded) {
wrapper.innerHTML = "Loading...";
return wrapper;
Expand Down
218 changes: 134 additions & 84 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# MMM-json

A Module for [MagicMirror](https://github.com/MichMich/MagicMirror) designed to display parts or the whole JSON response from an api.
A Module for [MagicMirror](https://github.com/MichMich/MagicMirror) designed to
display parts or the whole JSON response from an api.

## Sample

Expand All @@ -21,15 +22,15 @@ npm install

```jsonc
{
module: 'MMM-json',
position: 'bottom_left',
config: {
url: "https://jsonplaceholder.typicode.com/users", // Path to your json api
}
module: 'MMM-json',
position: 'bottom_left',
config: {
url: "https://jsonplaceholder.typicode.com/users", // Path to your json api
}
},
```

**Advanced Example:**
**Advanced Example JSONPath:**

```jsonc
{
Expand Down Expand Up @@ -61,95 +62,144 @@ npm install
},
```

**Advanced Example jq:**

```jsonc
{
module: "MMM-json",
position: "bottom_left",
header: "JSON example jq",
config: {
url: "https://jsonplaceholder.typicode.com/users",
headerIcon: "fa-cube",
jq: 'keyBy("name") | mapValues(a => [a.address.street,a.address.suite,a.address.city].join(", "))'
}
},
```

**Advanced Example POST:**

```jsonc
{
module: "MMM-json",
position: "bottom_left",
header: "JSON example POST",
config: {
url: "https://jsonplaceholder.typicode.com/posts",
request: {
method: "POST",
json: {
"search": "something"
}
}
}
},
```

## Configuration

<table width="100%">
<thead>
<tr>
<th>Property</th>
<th width="100%">Description</th>
</tr>
<thead>
<tbody>
<tr>
<td><code>url</code></td>
<td>The url where to get the json response from.
<br><b>Type:</b> <code>string</code>
<br><b>Default:</b> <code>https://jsonplaceholder.typicode.com/users</code>
</td>
</tr>
<tr>
<td><code>refreshInterval</code></td>
<td>The interval with which the url is queried and your values are updated.
<br><b>Type:</b> <code>int</code> (seconds)
<br><b>Default:</b> <code>300000</code> => 5 minutes
</td>
</tr>
<tr>
<td><del><code>header</code></del></td>
<td>
<thead>
<tr>
<th>Property</th>
<th width="100%">Description</th>
</tr>
<thead>
<tbody>
<tr>
<td><code>url</code></td>
<td>The url where to get the json response from.
<br><b>Type:</b> <code>string</code>
<br><b>Default:</b> <code>https://jsonplaceholder.typicode.com/users</code>
</td>
</tr>
<tr>
<td><code>refreshInterval</code></td>
<td>The interval with which the url is queried and your values are updated.
<br><b>Type:</b> <code>int</code> (seconds)
<br><b>Default:</b> <code>300000</code> => 5 minutes
</td>
</tr>
<tr>
<td><del><code>header</code></del></td>
<td>
Deprecated, use the built in <code>header</code>
in the config
</td>
</tr>
<tr>
<td><code>headerIcon</code></td>
<td>The Icon for your Header
<br><b>Type:</b> <code>string</code> <a href="https://fontawesome.com/icons?d=gallery">any FontAwesome Icon</a>
<br><b>Default:</b> <code></code> (none)
</td>
</tr>
<tr>
<td><code>values</code></td>
<td>Custom Configuration of the values you want to display (see below)
<br><b>Type:</b> <code>array</code>
<br><b>Default:</b> <code>[]</code> Which means it displays all first level attributes (or the first element of an array).
</td>
</tr>
</tbody>
</td>
</tr>
<tr>
<td><code>headerIcon</code></td>
<td>The Icon for your Header
<br><b>Type:</b> <code>string</code> <a href="https://fontawesome.com/icons?d=gallery">any FontAwesome Icon</a>
<br><b>Default:</b> <code></code> (none)
</td>
</tr>
<tr>
<td><code>request</code></td>
<td>Custom parameters for the <a href="https://www.npmjs.com/package/request">request</a> call. For example request method, request headers, request body.
<br><b>Type:</b> <code>object</code>
<br><b>Default:</b> <code>{}</code> No additional parameters.
</td>
</tr>
<tr>
<td><code>jq</code></td>
<td>Custom <a href="https://www.npmjs.com/package/jq.node">jq.node command</a> to apply to the data. Used to convert JSON data.
<br><b>Type:</b> <code>string</code>
<br><b>Default:</b> <code>'.'</code> Use value as received.
</td>
</tr>
<tr>
<td><code>values</code></td>
<td>Custom Configuration of the values you want to display (see below)
<br><b>Type:</b> <code>array</code>
<br><b>Default:</b> <code>[]</code> Which means it displays all first level attributes (or the first element of an array).
</td>
</tr>
</tbody>
</table>

### Value Configuration

<table width="100%">
<thead>
<tr>
<th>Value-Property</th>
<th width="100%">Description</th>
</tr>
<thead>
<tbody>
<tr>
<td><code>title</code></td>
<td>The Title of the Property displayed on the screen
<br><b>Type:</b> <code>string</code>
<br><b>Default:</b> The attribute name
</td>
</tr>
<tr>
<td><code>query</code></td>
<td>The <a href="https://restfulapi.net/json-jsonpath/">jsonpath</a> to the value of your json response you want to display. <a href="https://jsonpath.com/">Here you can test your expression</a>
<br><b>Type:</b> <code>string</code>
<br><b>Example:</b> <code>$[?(@.name=='Ervin Howell')].address.geo.lat</code>
</td>
</tr>
<tr>
<td><code>suffix</code></td>
<td>String that will be displayed behind your query value
<br><b>Type:</b> <code>string</code>
<br><b>Example:</b> <code>%</code>
</td>
</tr>
<thead>
<tr>
<th>Value-Property</th>
<th width="100%">Description</th>
</tr>
<thead>
<tbody>
<tr>
<td><code>title</code></td>
<td>The Title of the Property displayed on the screen
<br><b>Type:</b> <code>string</code>
<br><b>Default:</b> The attribute name
</td>
</tr>
<tr>
<td><code>query</code></td>
<td>The <a href="https://restfulapi.net/json-jsonpath/">jsonpath</a> to the value of your json response you want to display. <a href="https://jsonpath.com/">Here you can test your expression</a>
<br><b>Type:</b> <code>string</code>
<br><b>Example:</b> <code>$[?(@.name=='Ervin Howell')].address.geo.lat</code>
</td>
</tr>
<tr>
<td><code>suffix</code></td>
<td>String that will be displayed behind your query value
<br><b>Type:</b> <code>string</code>
<br><b>Example:</b> <code>%</code>
</td>
</tr>
<tr>
<td><code>prefix</code></td>
<td>String that will be displayed in front of your query value
<br><b>Type:</b> <code>string</code>
<br><b>Example:</b> <code>EUR</code>
</td>
</tr>
</tbody>
<td><code>prefix</code></td>
<td>String that will be displayed in front of your query value
<br><b>Type:</b> <code>string</code>
<br><b>Example:</b> <code>EUR</code>
</td>
</tr>
</tbody>
</table>

# Attribution

Attribution of some basic work and inspiration goes to https://github.com/tkrywit/MMM-Solar.
Attribution of some basic work and inspiration goes to
https://github.com/tkrywit/MMM-Solar.
Loading