Browse Source

refactor: switch to ESM in process of updating deps. (body-parser, command-line-basics, express, express-rate-limit, luxon)

BREAKING CHANGE:

Requires ESM and Node 14.8; also updates devDeps.
Brett Zamir 3 years ago
parent
commit
8bdc301fac
10 changed files with 353 additions and 541 deletions
  1. 5 1
      .eslintrc.cjs
  2. 37 16
      api/controllers/bDateController.js
  3. 1 0
      api/models/bDateModel.js
  4. 9 5
      api/routes/bDateRoutes.js
  5. 8 6
      bin/bahai-date-api.js
  6. 5 4
      bin/optionDefinitions.js
  7. 6 8
      createServer.js
  8. 21 20
      package.json
  9. 260 478
      pnpm-lock.yaml
  10. 1 3
      server.js

+ 5 - 1
.eslintrc.js → .eslintrc.cjs

@@ -1,6 +1,10 @@
 'use strict';
+
 module.exports = {
-  extends: ['ash-nazg/sauron-node-script-overrides'],
+  extends: ['ash-nazg/sauron-node-overrides'],
+  parserOptions: {
+    ecmaVersion: 2021
+  },
   rules: {
     'import/no-commonjs': 0,
     'node/exports-style': 0,

+ 37 - 16
api/controllers/bDateController.js

@@ -1,7 +1,6 @@
-'use strict';
-const luxon = require('luxon');
+import * as luxon from 'luxon';
 
-const {LocalBadiDate} = require('badidate');
+import {LocalBadiDate} from 'badidate';
 
 /**
  * @typedef {PlainObject} DateConfig
@@ -11,17 +10,17 @@ const {LocalBadiDate} = require('badidate');
  */
 
 /**
- * @param {Date} date
+ * @param {Date} dte
  * @param {DateConfig} dateCfg
  * @returns {LocalBadiDate}
  */
-function createDateObject (date, {
+function createDateObject (dte, {
   // Bahjí
   latitude = 32.9434,
   longitude = 35.0924,
   timezoneId = 'Asia/Jerusalem'
 } = {}) {
-  const luxonDate = luxon.DateTime.fromJSDate(date);
+  const luxonDate = luxon.DateTime.fromJSDate(dte);
   return new LocalBadiDate(luxonDate, latitude, longitude, timezoneId);
 }
 
@@ -65,9 +64,14 @@ function sanitizeInteger (s) {
   return Number.parseInt(s);
 }
 
-exports.test = function (req, res) {
+/**
+ * @param {Request} req
+ * @param {Response} res
+ * @returns {void}
+ */
+function test (req, res) {
   res.json({message: 'Hi there'});
-};
+}
 
 /**
 * @typedef {PlainObject} BadiDateInfo
@@ -99,7 +103,7 @@ exports.test = function (req, res) {
  * @param {DateConfig} dateObj
  * @returns {BadiDateResponse}
  */
-const getTodayJSON = exports.getTodayJSON = function (dateObj = {}) {
+const getTodayJSON = function (dateObj = {}) {
   const now = new Date();
 
   const latitude = sanitizeFloat(dateObj.latitude);
@@ -135,19 +139,34 @@ const getTodayJSON = exports.getTodayJSON = function (dateObj = {}) {
   };
 };
 
-exports.today = function (req, res) {
+/**
+ * @param {Request} req
+ * @param {Response} res
+ * @returns {void}
+ */
+function today (req, res) {
   const {json, nowBadi} = getTodayJSON(req.query);
   // eslint-disable-next-line no-console -- CLI
   console.log('Today: ' + nowBadi.badiDate.format());
   res.json(json);
-};
+}
 
-exports.todayHtml = function (req, res) {
+/**
+ * @param {Request} req
+ * @param {Response} res
+ * @returns {void}
+ */
+function todayHtml (req, res) {
   res.set('content-type', 'text/html;charset=utf-8');
   res.end(JSON.stringify(getTodayJSON(req.query).json, null, 2));
-};
+}
 
-exports.date = function (req, res) {
+/**
+ * @param {Request} req
+ * @param {Response} res
+ * @returns {void}
+ */
+function date (req, res) {
   const dateInfo = getDate(req.query);
   // eslint-disable-next-line no-console -- CLI
   console.log(
@@ -155,7 +174,7 @@ exports.date = function (req, res) {
       dateInfo.nowBadi.badiDate.format()
   );
   res.json(dateInfo.json);
-};
+}
 
 /**
 * @typedef {DateConfig} FullDateConfig
@@ -171,7 +190,7 @@ exports.date = function (req, res) {
  * @param {FullDateConfig} dateObj
  * @returns {BadiDateResponse}
  */
-const getDate = exports.getDate = function (dateObj) {
+const getDate = function (dateObj) {
   const year = sanitizeInteger(dateObj.year);
   const month = sanitizeInteger(dateObj.month) - 1;
   const day = sanitizeInteger(dateObj.day);
@@ -213,3 +232,5 @@ const getDate = exports.getDate = function (dateObj) {
     }
   };
 };
+
+export {test, getTodayJSON, today, todayHtml, date, getDate};

+ 1 - 0
api/models/bDateModel.js

@@ -0,0 +1 @@
+/* eslint-disable import/unambiguous, unicorn/no-empty-file -- Empty file */

+ 9 - 5
api/routes/bDateRoutes.js

@@ -1,8 +1,10 @@
-'use strict';
+import * as bDate from '../controllers/bDateController.js';
 
-const bDate = require('../controllers/bDateController.js');
-
-module.exports = function (app) {
+/**
+ * @param {ExpressApp} app
+ * @returns {void}
+ */
+function routes (app) {
   // API test
   app.route('/test')
     .get(bDate.test);
@@ -15,4 +17,6 @@ module.exports = function (app) {
 
   app.route('/today')
     .get(bDate.todayHtml);
-};
+}
+
+export default routes;

+ 8 - 6
bin/bahai-date-api.js

@@ -1,13 +1,15 @@
 #!/usr/bin/env node
-'use strict';
 
-const {join} = require('path');
-const {cliBasics} = require('command-line-basics');
-const {
+import {dirname, join} from 'path';
+import {fileURLToPath} from 'url';
+import {cliBasics} from 'command-line-basics';
+import {
   getDate, getTodayJSON
-} = require('../api/controllers/bDateController.js');
+} from '../api/controllers/bDateController.js';
 
-const optionDefinitions = cliBasics(
+const __dirname = dirname(fileURLToPath(import.meta.url));
+
+const optionDefinitions = await cliBasics(
   join(__dirname, './optionDefinitions.js')
 );
 

+ 5 - 4
bin/optionDefinitions.js

@@ -1,6 +1,8 @@
-'use strict';
+import {readFileSync} from 'fs';
 
-const pkg = require('../package.json');
+const pkg = JSON.parse(readFileSync(
+  new URL('../package.json', import.meta.url)
+));
 
 /* eslint-disable jsdoc/require-property -- Schema is already below */
 /**
@@ -32,5 +34,4 @@ const cliSections = [
   }
 ];
 
-exports.definitions = optionDefinitions;
-exports.sections = cliSections;
+export {optionDefinitions as definitions, cliSections as sections};

+ 6 - 8
createServer.js

@@ -1,10 +1,8 @@
-'use strict';
+import express from 'express';
+import bodyParser from 'body-parser';
+import rateLimit from 'express-rate-limit';
 
-const express = require('express'),
-  bodyParser = require('body-parser'),
-  RateLimit = require('express-rate-limit');
-
-const routes = require('./api/routes/bDateRoutes.js'); // importing routes
+import routes from './api/routes/bDateRoutes.js'; // importing routes
 
 /**
 * @returns {ExpressApp}
@@ -12,7 +10,7 @@ const routes = require('./api/routes/bDateRoutes.js'); // importing routes
 function createServer () {
   const app = express();
 
-  app.use(new RateLimit({
+  app.use(rateLimit({
     windowMs: 1 * 60 * 1000, // 1 minute
     max: 20
   }));
@@ -25,4 +23,4 @@ function createServer () {
   return app;
 }
 
-module.exports = createServer;
+export default createServer;

+ 21 - 20
package.json

@@ -3,6 +3,7 @@
   "version": "1.0.0",
   "description": "RESTful Bahá'í date API",
   "main": "api/controllers/bDateController.js",
+  "type": "module",
   "bin": {
     "bahai-date": "./bin/bahai-date-api.js"
   },
@@ -17,7 +18,7 @@
     "test": "echo \"Error: No test specified\" && exit 1"
   },
   "engines": {
-    "node": ">=12.0.0"
+    "node": ">=14.8.0"
   },
   "repository": {
     "type": "git",
@@ -38,33 +39,33 @@
   },
   "homepage": "https://github.com/dragfyre/bahai-date-api#readme",
   "devDependencies": {
-    "@brettz9/eslint-plugin": "^1.0.3",
-    "command-line-publish": "^0.7.0",
-    "eslint": "^7.28.0",
-    "eslint-config-ash-nazg": "^29.17.0",
+    "@brettz9/eslint-plugin": "^1.0.4",
+    "command-line-publish": "^1.1.0",
+    "eslint": "^8.8.0",
+    "eslint-config-ash-nazg": "^32.3.0",
     "eslint-config-standard": "^16.0.3",
     "eslint-plugin-array-func": "^3.1.7",
-    "eslint-plugin-compat": "^3.9.0",
+    "eslint-plugin-compat": "^4.0.2",
     "eslint-plugin-eslint-comments": "^3.2.0",
-    "eslint-plugin-html": "^6.1.2",
-    "eslint-plugin-import": "^2.23.4",
-    "eslint-plugin-jsdoc": "^35.1.3",
-    "eslint-plugin-markdown": "^2.2.0",
-    "eslint-plugin-no-unsanitized": "^3.1.5",
+    "eslint-plugin-html": "^6.2.0",
+    "eslint-plugin-import": "^2.25.4",
+    "eslint-plugin-jsdoc": "^37.7.1",
+    "eslint-plugin-markdown": "^2.2.1",
+    "eslint-plugin-no-unsanitized": "^4.0.1",
     "eslint-plugin-no-use-extend-native": "^0.5.0",
     "eslint-plugin-node": "^11.1.0",
-    "eslint-plugin-promise": "^5.1.0",
-    "eslint-plugin-radar": "^0.2.1",
+    "eslint-plugin-promise": "^6.0.0",
+    "eslint-plugin-sonarjs": "^0.11.0",
     "eslint-plugin-standard": "^4.1.0",
-    "eslint-plugin-unicorn": "^33.0.1",
-    "nodemon": "^2.0.7"
+    "eslint-plugin-unicorn": "^40.1.0",
+    "nodemon": "^2.0.15"
   },
   "dependencies": {
     "badidate": "^3.0.2",
-    "body-parser": "^1.19.0",
-    "command-line-basics": "^0.8.0",
-    "express": "^4.17.1",
-    "express-rate-limit": "^5.2.6",
-    "luxon": "^1.27.0"
+    "body-parser": "^1.19.1",
+    "command-line-basics": "^1.0.2",
+    "express": "^4.17.2",
+    "express-rate-limit": "^6.2.0",
+    "luxon": "^2.3.0"
   }
 }

File diff suppressed because it is too large
+ 260 - 478
pnpm-lock.yaml


+ 1 - 3
server.js

@@ -1,6 +1,4 @@
-'use strict';
-
-const createServer = require('./createServer.js');
+import createServer from './createServer.js';
 
 const port = process.argv[2] || 1844;
 

Some files were not shown because too many files changed in this diff