Skip to content

Commit 8d55a72

Browse files
Lint javascript
1 parent 41bd292 commit 8d55a72

File tree

5 files changed

+207
-212
lines changed

5 files changed

+207
-212
lines changed

API/api.js

Lines changed: 47 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ const bodyParser = require("body-parser");
1515
const express = require("express");
1616

1717
module.exports = {
18-
/* getApiKey
18+
/* getApiKey
1919
* Middleware method for ExpressJS to check if an API key is provided.
2020
* Only checks for an API key if one is defined in the module's config section.
2121
*/
22-
getApiKey: function() {
22+
getApiKey() {
2323
let thisConfig = this.configOnHd.modules.find(x => x.module === "MMM-Remote-Control");
2424
if (typeof "thisConfig" !== "undefined" &&
2525
"config" in thisConfig){
@@ -36,18 +36,16 @@ module.exports = {
3636
this.secureEndpoints = true;
3737
}
3838
}
39-
4039
},
4140

42-
4341
/* getExternalApiByGuessing()
4442
* This method is called when an API call is made to /module or /modules
4543
* It checks if a string is a Module Name or an Instance Name and returns
4644
* the actual Module Name
4745
*
4846
* @updates this.externalApiRoutes
4947
*/
50-
getExternalApiByGuessing: function() {
48+
getExternalApiByGuessing() {
5149
if (!this.configOnHd) { return undefined; }
5250

5351
let getActions = function(content) {
@@ -105,18 +103,18 @@ module.exports = {
105103
this.updateModuleApiMenu();
106104
},
107105

108-
createApiRoutes: function() {
106+
createApiRoutes() {
109107
var self = this;
110108

111109
this.getApiKey();
112110

113111
this.expressApp.use(bodyParser.urlencoded({ extended: true }));
114112
this.expressApp.use(bodyParser.json());
115-
113+
116114
this.expressApp.use('/api/docs', express.static(path.join(__dirname, '../docs'))); // Docs without apikey
117115

118116
this.expressRouter = express.Router();
119-
117+
120118
// Route for testing the api at http://mirror:8080/api/test
121119
this.expressRouter.route(['/test','/']) // Test without apiKey
122120
.get((req, res) => {
@@ -178,12 +176,12 @@ module.exports = {
178176
'/togglefullscreen',
179177
'/devtools'
180178
]).get((req, res) => {
181-
if(!this.apiKey && this.secureEndpoints) return res.status(403).json({ success: false, message: "Forbidden: API Key Not Provided in Config! Use secureEndpoints to bypass this message" });
179+
if (!this.apiKey && this.secureEndpoints) return res.status(403).json({ success: false, message: "Forbidden: API Key Not Provided in Config! Use secureEndpoints to bypass this message" });
182180
let r = req.path.split("/")[1].toUpperCase();
183181
console.log(req.path);
184182
self.executeQuery(this.checkDelay({ action: r }, req), res);
185183
});
186-
184+
187185
this.expressRouter.route('/classes/:value')
188186
.get((req, res) => {
189187
var classes = self.getConfig().modules.find(m => m.module === "MMM-Remote-Control").config || {};
@@ -194,10 +192,10 @@ module.exports = {
194192
res.status(400).json({ success: false, message: `Invalid value ${val} provided in request. Use /api/classes to see actual values` });
195193
}
196194
});
197-
195+
198196
this.expressRouter.route('/command/:value')
199197
.get((req, res) => {
200-
if(!this.apiKey && this.secureEndpoints) return res.status(403).json({ success: false, message: "Forbidden: API Key Not Provided in Config! Use secureEndpoints to bypass this message" });
198+
if (!this.apiKey && this.secureEndpoints) return res.status(403).json({ success: false, message: "Forbidden: API Key Not Provided in Config! Use secureEndpoints to bypass this message" });
201199
const val = decodeURIComponent(req.params.value)
202200
self.executeQuery({ action: "COMMAND", command: req.params.value }, res);
203201
});
@@ -217,9 +215,9 @@ module.exports = {
217215

218216
this.expressRouter.route('/update/:moduleName?')
219217
.get((req, res) => {
220-
if(!this.apiKey && this.secureEndpoints) return res.status(403).json({ success: false, message: "Forbidden: API Key Not Provided in Config! Use secureEndpoints to bypass this message" });
221-
if(!req.params.moduleName) return self.answerGet({ data: 'mmUpdateAvailable' }, res);
222-
switch(req.params.moduleName) {
218+
if (!this.apiKey && this.secureEndpoints) return res.status(403).json({ success: false, message: "Forbidden: API Key Not Provided in Config! Use secureEndpoints to bypass this message" });
219+
if (!req.params.moduleName) return self.answerGet({ data: 'mmUpdateAvailable' }, res);
220+
switch (req.params.moduleName) {
223221
case 'mm': case 'MM': self.answerGet({ data: 'mmUpdateAvailable' }, res); break;
224222
case 'rc': case 'RC': this.updateModule('MMM-Remote-Control', res); break;
225223
default: this.updateModule(req.params.moduleName, res); break;
@@ -231,40 +229,40 @@ module.exports = {
231229
res.status(400).json({ success: false, message: "Invalid method, use POST" });
232230
})
233231
.post((req, res) => {
234-
if(!this.apiKey && this.secureEndpoints) return res.status(403).json({ success: false, message: "Forbidden: API Key Not Provided in Config! Use secureEndpoints to bypass this message" });
232+
if (!this.apiKey && this.secureEndpoints) return res.status(403).json({ success: false, message: "Forbidden: API Key Not Provided in Config! Use secureEndpoints to bypass this message" });
235233
if (typeof req.body !== 'undefined' && "url" in req.body) {
236234
this.installModule(req.body.url, res);
237235
} else {
238236
res.status(400).json({ success: false, message: "Invalid URL provided in request body" });
239237
}
240238
});
241-
242-
//edit config, payload is completely new config object with your changes(edits).
239+
240+
// edit config, payload is completely new config object with your changes(edits).
243241
this.expressRouter.route('/config/edit')
244242
.get((req, res) => {
245243
res.status(400).json({ success: false, message: "Invalid method, use POST" });
246244
})
247245
.post((req, res) => {
248-
if(!this.apiKey && this.secureEndpoints) return res.status(403).json({ success: false, message: "Forbidden: API Key Not Provided in Config! Use secureEndpoints to bypass this message" });
246+
if (!this.apiKey && this.secureEndpoints) return res.status(403).json({ success: false, message: "Forbidden: API Key Not Provided in Config! Use secureEndpoints to bypass this message" });
249247
if (typeof req.body !== 'undefined' && "payload" in req.body) {
250248
this.answerPost({ data: "config" }, { body: req.body.payload }, res);
251249
} else {
252250
res.status(400).json({ success: false, message: "Invalid URL provided in request body" });
253251
}
254252
});
255-
//edit config
253+
// edit config
256254

257255
this.expressRouter.route('/notification/:notification/:p?/:delayed?')
258256
.get((req, res) => {
259-
if(!this.apiKey && this.secureEndpoints) return res.status(403).json({ success: false, message: "Forbidden: API Key Not Provided in Config! Use secureEndpoints to bypass this message" });
257+
if (!this.apiKey && this.secureEndpoints) return res.status(403).json({ success: false, message: "Forbidden: API Key Not Provided in Config! Use secureEndpoints to bypass this message" });
260258
this.answerNotifyApi(req, res);
261259
})
262260
.post((req, res) => {
263-
if(!this.apiKey && this.secureEndpoints) return res.status(403).json({ success: false, message: "Forbidden: API Key Not Provided in Config! Use secureEndpoints to bypass this message" });
261+
if (!this.apiKey && this.secureEndpoints) return res.status(403).json({ success: false, message: "Forbidden: API Key Not Provided in Config! Use secureEndpoints to bypass this message" });
264262
req.params = {
265-
... req.params,
266-
... req.body
267-
}
263+
...req.params,
264+
...req.body
265+
};
268266
this.answerNotifyApi(req, res);
269267
});
270268

@@ -274,9 +272,9 @@ module.exports = {
274272
})
275273
.post((req, res) => {
276274
req.params = {
277-
... req.params,
278-
... req.body
279-
}
275+
...req.params,
276+
...req.body
277+
};
280278
this.answerModuleApi(req, res);
281279
});
282280

@@ -289,7 +287,7 @@ module.exports = {
289287
.post((req, res) => {
290288
var actionName = "STATUS";
291289
if (typeof req.body !== 'undefined' && "monitor" in req.body) {
292-
if(["OFF","ON","TOGGLE"].includes(req.body.monitor.toUpperCase())) {
290+
if (["OFF", "ON", "TOGGLE"].includes(req.body.monitor.toUpperCase())) {
293291
actionName = req.body.monitor.toUpperCase();
294292
}
295293
} else {
@@ -326,8 +324,8 @@ module.exports = {
326324
}
327325
return query;
328326
},
329-
330-
mergeData: function() {
327+
328+
mergeData() {
331329
var extApiRoutes = this.externalApiRoutes;
332330
var modules = this.configData.moduleData
333331
var query = {success: true, data: []};
@@ -338,20 +336,20 @@ module.exports = {
338336
} else {
339337
query.data.push(Object.assign({},mod, {actions: extApiRoutes[mod.name].actions}));
340338
}
341-
})
342-
339+
});
340+
343341
return query;
344342
},
345343

346-
answerModuleApi: function(req, res) {
344+
answerModuleApi(req, res) {
347345
if (!this.checkInititialized(res)) { return; }
348346
var dataMerged = this.mergeData().data
349347

350348
if (!req.params.moduleName) {
351349
res.json({ success: true, data: dataMerged });
352350
return;
353351
}
354-
352+
355353
let modData = [];
356354
if(req.params.moduleName !== 'all') {
357355
modData = dataMerged.filter(m => {
@@ -365,26 +363,25 @@ module.exports = {
365363
} else {
366364
modData = dataMerged;
367365
}
368-
366+
369367
if (!modData.length) {
370368
res.status(400).json({ success: false, message: "Module Name or Identifier Not Found!" });
371369
return;
372-
}
373-
370+
}
371+
374372
if (!req.params.action) {
375373
res.json({ success: true, data: modData });
376374
return;
377375
}
378-
376+
379377
var action = req.params.action.toUpperCase();
380-
378+
381379
if (["SHOW", "HIDE", "FORCE", "TOGGLE", "DEFAULTS"].indexOf(action) !== -1) { // /api/modules part of the code
382-
383380
if (action === "DEFAULTS") {
384381
this.answerGet({ data: "defaultConfig", module: mod.name }, res);
385382
return;
386383
}
387-
384+
388385
if (req.params.moduleName === "all") {
389386
let query = { module: "all" };
390387
if (action === "FORCE") {
@@ -396,7 +393,7 @@ module.exports = {
396393
this.executeQuery(this.checkDelay(query, req), res);
397394
return;
398395
}
399-
396+
400397
modData.forEach(mod => {
401398
let query = { module: mod.identifier };
402399
if (action === "FORCE") {
@@ -410,20 +407,19 @@ module.exports = {
410407
this.sendSocketNotification("UPDATE");
411408
return;
412409
}
413-
414-
action = modData[0].actions[req.params.action]
415-
410+
411+
action = modData[0].actions[req.params.action];
412+
416413
if (action) {
417414
if ("method" in action && action.method !== req.method) {
418415
res.status(400).json({ success: false, info: `Method ${req.method} is not allowed for ${moduleName}/${req.params.action}.` });
419416
return;
420417
}
421418
this.answerNotifyApi(req, res, action);
422419
}
423-
424420
},
425-
426-
answerNotifyApi: function(req, res, action) {
421+
422+
answerNotifyApi(req, res, action) {
427423
// Build the payload to send with our notification.
428424
let n = "";
429425
if (action) { n = action.notification; } else if ("notification" in req.params) {
@@ -482,15 +478,15 @@ module.exports = {
482478
return;
483479
},
484480

485-
checkInititialized: function(res) {
481+
checkInititialized(res) {
486482
if (!this.initialized) {
487483
this.sendResponse(res, "Not initialized, have you opened or refreshed your browser since the last time you started MagicMirror²?");
488484
return false;
489485
}
490486
return true;
491487
},
492488

493-
updateModuleApiMenu: function() {
489+
updateModuleApiMenu() {
494490
if (!this.thisConfig.showModuleApiMenu) { return; }
495491

496492
this.moduleApiMenu = {

0 commit comments

Comments
 (0)