Ver código fonte

- Breaking change: Check argument to script (`process.argv`) instead of environmental variable for port (recommended to avoid env. vars.: https://eslint.org/docs/rules/no-process-env )
- Linting (ESLint): Switch to ash-nazg config (which inherits eslint:recommended); lint any HTML/Markdown JavaScript

Though mostly stylistic changes, also pointed to `body-parser` needing to be in deps.

Brett Zamir 6 anos atrás
pai
commit
59aed69275
6 arquivos alterados com 991 adições e 93 exclusões
  1. 3 6
      .eslintrc.js
  2. 89 41
      api/controllers/bDateController.js
  3. 4 4
      api/routes/bDateRoutes.js
  4. 861 32
      package-lock.json
  5. 23 4
      package.json
  6. 11 6
      server.js

+ 3 - 6
.eslintrc.js

@@ -1,10 +1,7 @@
 module.exports = {
-  extends: ["eslint:recommended"],
-  env: {
-    node: true
-  },
+  extends: ['ash-nazg/sauron-node', 'plugin:node/recommended-script'],
   rules: {
-    semi: ['error'],
-    indent: ['error', 2]
+    'import/no-commonjs': 0,
+    'node/exports-style': 0
   }
 };

+ 89 - 41
api/controllers/bDateController.js

@@ -1,64 +1,112 @@
 'use strict';
-var BadiCal = require('../../vendor/assets/badi-cal/index.js');
+const BadiCal = require('../../vendor/assets/badi-cal/index.js');
 
-function sanitize_input(s) {
-  if (s==''||s===undefined||s===null||s==false||isNaN(s)) { return 0; }
-  else { return parseInt(s,10); }
+/**
+ * @param {any} s
+ * @returns {Integer}
+ */
+function sanitizeInput (s) {
+  if (s === '' || s === undefined || s === null || s === false || isNaN(s)) {
+    return 0;
+  }
+  return parseInt(s);
 }
 
-exports.test = function(req, res) {
-  res.json({ message: 'Hi there' });
+exports.test = function (req, res) {
+  res.json({message: 'Hi there'});
 };
 
+/**
+* @typedef {PlainObject} BadiDateObject
+* @property {string} message
+* @property {PlainObject} badi_date
+* @property {Integer} badi_date.year
+* @property {Integer} badi_date.month
+* @property {Integer} badi_date.day
+* @property {string} badi_date.month_name
+* @property {PlainObject} greg_date
+* @property {Integer} greg_date.year
+* @property {Integer} greg_date.month
+* @property {Integer} greg_date.day
+* @property {Integer} greg_date.hour
+* @property {Integer} greg_date.minute
+* @property {Integer} greg_date.second
+*/
+
+/**
+ * @returns {BadiDateObject}
+ */
 function getTodayJSON () {
-  var now = new Date();
-  var here = new Object();
-  here['latitude'] = '40.712'; // New York
-  here['longitude'] = '-74.006';
+  const now = new Date();
+  const here = {
+    latitude: '40.712', // New York
+    longitude: '-74.006'
+  };
 
-  var now_badi = BadiCal.BadiDate.fromGregorianDate(now, here);
-  console.log("Today: " + now_badi.toString());
+  const nowBadi = BadiCal.BadiDate.fromGregorianDate(now, here);
+
+  // eslint-disable-next-line no-console
+  console.log('Today: ' + nowBadi.toString());
   return {
-    message: "Today is " + now_badi.toString(),
-    badi_date:{
-      year:now_badi.getYear(),
-      month:now_badi.getMonth(),
-      day:now_badi.getDay(),
-      month_name:now_badi.getMonthName()
+    message: 'Today is ' + nowBadi.toString(),
+    badi_date: {
+      year: nowBadi.getYear(),
+      month: nowBadi.getMonth(),
+      day: nowBadi.getDay(),
+      month_name: nowBadi.getMonthName()
     },
-    greg_date:{
-      year:now.getFullYear(),
-      month:now.getMonth()+1,
-      day:now.getDay(),
-      hour:now.getHours(),
-      minute:now.getMinutes(),
-      second:now.getSeconds()
+    greg_date: {
+      year: now.getFullYear(),
+      month: now.getMonth() + 1,
+      day: now.getDay(),
+      hour: now.getHours(),
+      minute: now.getMinutes(),
+      second: now.getSeconds()
     }
   };
 }
 
-exports.today = function(req, res) {
+exports.today = function (req, res) {
   res.json(getTodayJSON());
 };
 
-exports.todayHtml = function(req, res) {
+exports.todayHtml = function (req, res) {
   res.end(JSON.stringify(getTodayJSON(), null, 2));
 };
 
-exports.date = function(req, res) {
-  var year = sanitize_input(req.query['year']);
-  var month = sanitize_input(req.query['month'])-1;
-  var day = sanitize_input(req.query['day']);
-  var hour = sanitize_input(req.query['hour']);
-  var minute = sanitize_input(req.query['minute']);
-  var second = sanitize_input(req.query['second']);
+exports.date = function (req, res) {
+  const year = sanitizeInput(req.query.year);
+  const month = sanitizeInput(req.query.month) - 1;
+  const day = sanitizeInput(req.query.day);
+  const hour = sanitizeInput(req.query.hour);
+  const minute = sanitizeInput(req.query.minute);
+  const second = sanitizeInput(req.query.second);
 
-  var now = new Date(year,month,day,hour,minute,second);
-  var here = new Object();
-  here['latitude'] = '40.712'; // New York
-  here['longitude'] = '-74.006';
+  const now = new Date(year, month, day, hour, minute, second);
+  const here = {
+    latitude: '40.712', // New York
+    longitude: '-74.006'
+  };
+
+  const nowBadi = BadiCal.BadiDate.fromGregorianDate(now, here);
+  res.json({
+    message: 'The date is: ' + nowBadi.toString(),
+    badi_date: {
+      year: nowBadi.getYear(),
+      month: nowBadi.getMonth(),
+      day: nowBadi.getDay(),
+      month_name: nowBadi.getMonthName()
+    },
+    greg_date: {
+      year,
+      month: month + 1,
+      day,
+      hour,
+      minute,
+      second
+    }
+  });
 
-  var now_badi = BadiCal.BadiDate.fromGregorianDate(now, here);
-  res.json({ message: "The date is: " + now_badi.toString(),badi_date:{year:now_badi.getYear(),month:now_badi.getMonth(),day:now_badi.getDay(),month_name:now_badi.getMonthName()},greg_date:{year:year,month:month+1,day:day,hour:hour,minute:minute,second:second}});
-  console.log("Date: " + now.toString() + " -> " + now_badi.toString());
+  // eslint-disable-next-line no-console
+  console.log('Date: ' + now.toString() + ' -> ' + nowBadi.toString());
 };

+ 4 - 4
api/routes/bDateRoutes.js

@@ -1,14 +1,14 @@
 'use strict';
-var bDate = require('../controllers/bDateController');
+const bDate = require('../controllers/bDateController');
 
-module.exports = function(app) {
+module.exports = function (app) {
   // API test
   app.route('/test')
     .get(bDate.test);
-  // Today's Badi date
+  // Today's Badí' date
   app.route('/today')
     .post(bDate.today);
-  // Arbitrary Badi date
+  // Arbitrary Badí' date
   app.route('/date')
     .get(bDate.date);
 

Diferenças do arquivo suprimidas por serem muito extensas
+ 861 - 32
package-lock.json


+ 23 - 4
package.json

@@ -4,9 +4,9 @@
   "description": "RESTful Baha'i date API",
   "main": "index.js",
   "scripts": {
-    "lint": "eslint .",
-    "test": "echo \"Error: No test specified\" && exit 1",
-    "start": "nodemon server.js"
+    "lint": "eslint --ext=js,md,html .",
+    "start": "nodemon server.js",
+    "test": "echo \"Error: No test specified\" && exit 1"
   },
   "engines": {
     "node": ">=8.0.0"
@@ -30,10 +30,29 @@
   },
   "homepage": "https://github.com/dragfyre/bahai-date-api#readme",
   "devDependencies": {
+    "@mysticatea/eslint-plugin": "^13.0.0",
     "eslint": "^6.8.0",
-    "nodemon": "^2.0.2"
+    "eslint-config-ash-nazg": "^16.4.0",
+    "eslint-config-standard": "^14.1.0",
+    "eslint-plugin-array-func": "^3.1.3",
+    "eslint-plugin-compat": "^3.3.0",
+    "eslint-plugin-eslint-comments": "^3.1.2",
+    "eslint-plugin-html": "^6.0.0",
+    "eslint-plugin-import": "^2.19.1",
+    "eslint-plugin-jsdoc": "^20.0.5",
+    "eslint-plugin-markdown": "^1.0.1",
+    "eslint-plugin-no-unsanitized": "^3.0.2",
+    "eslint-plugin-no-use-extend-native": "^0.4.1",
+    "eslint-plugin-node": "^11.0.0",
+    "eslint-plugin-promise": "^4.2.1",
+    "eslint-plugin-sonarjs": "^0.5.0",
+    "eslint-plugin-standard": "^4.0.1",
+    "eslint-plugin-unicorn": "^15.0.1",
+    "nodemon": "^2.0.2",
+    "typescript": "^3.7.4"
   },
   "dependencies": {
+    "body-parser": "^1.19.0",
     "deep-extend": "^0.6.0",
     "express": "^4.17.1",
     "express-rate-limit": "^5.0.0",

+ 11 - 6
server.js

@@ -1,20 +1,25 @@
-var express = require('express'),
-  app = express(),
-  port = process.env.PORT || 1844,
+'use strict';
+
+const express = require('express'),
   bodyParser = require('body-parser'),
   RateLimit = require('express-rate-limit');
 
+const routes = require('./api/routes/bDateRoutes'); // importing route
+
+const app = express(),
+  port = process.argv[2] || 1844;
+
 app.use(new RateLimit({
   windowMs: 1 * 60 * 1000, // 1 minute
   max: 20
 }));
 
-app.use(bodyParser.urlencoded({ extended: true }));
+app.use(bodyParser.urlencoded({extended: true}));
 app.use(bodyParser.json());
 
-var routes = require('./api/routes/bDateRoutes'); //importing route
-routes(app); //register the route
+routes(app); // register the route
 
 app.listen(port);
 
+// eslint-disable-next-line no-console
 console.log('Baha\'i Date RESTful API server started: Port ' + port);

Alguns arquivos não foram mostrados porque muitos arquivos mudaram nesse diff