diff --git a/.eslintrc.yml b/.eslintrc.yml new file mode 100644 index 0000000..6135275 --- /dev/null +++ b/.eslintrc.yml @@ -0,0 +1,28 @@ +env: + es2021: true + node: true +extends: + - eslint:recommended + - plugin:@typescript-eslint/recommended +parser: '@typescript-eslint/parser' +parserOptions: + ecmaVersion: latest + sourceType: module +plugins: + - '@typescript-eslint' +rules: + indent: + - error + - 2 + linebreak-style: + - error + - unix + quotes: + - error + - double + semi: + - error + - always + comma-dangle: + - error + - always-multiline diff --git a/README.md b/README.md index 021c9ea..dc08681 100644 --- a/README.md +++ b/README.md @@ -36,11 +36,19 @@ This section includes the issues, changes & improvements I've made, with the tho - Outdated dependencies. > Related dependencies: `body-parser`, `dotenv`, `express`, `mongoose`, `@types/cors`, `@types/express`, > `@typescript-eslint/eslint-plugin`, `@typescript-eslint/parser`, `eslint`, `ts-node-dev`, `typescript` + > + > Note: As of this moment, the latest `typescript` version + > officially supported by the `@typescript-eslint/typescript-estree` is `4.5.5` - Not using exact versions for dependencies. > This issue has somewhat similar effects as the `package-lock.json` issue, > as `^version` will allow any semver compatible version to be installed. +- Missing `eslint` config. + > The `eslint` config allows to enforce the desired code style among all the contributors. - Issues in the `src` directory: - Unused imports. (e.g. unused `lodash` import in the `src/scripts/seed.ts`) + - Using `var` to define variables. + > This way the variable would be defined/redefined globally which could cause problems. + > Instead of `var`, it's best to use `let` or `const`. ### Improvements diff --git a/package-lock.json b/package-lock.json index b6c7ac1..030b975 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,7 +22,7 @@ "@typescript-eslint/parser": "5.13.0", "eslint": "8.10.0", "ts-node-dev": "1.1.8", - "typescript": "4.6.2" + "typescript": "4.5.5" }, "engines": { "node": ">=16.14.0" @@ -2637,9 +2637,9 @@ } }, "node_modules/typescript": { - "version": "4.6.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.6.2.tgz", - "integrity": "sha512-HM/hFigTBHZhLXshn9sN37H085+hQGeJHJ/X7LpBWLID/fbc2acUMfU+lGD98X81sKP+pFa9f0DZmCwB9GnbAg==", + "version": "4.5.5", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.5.5.tgz", + "integrity": "sha512-TCTIul70LyWe6IJWT8QSYeA54WQe8EjQFU4wY52Fasj5UKx88LNYKCgBEHcOMOrFF1rKGbD8v/xcNWVUq9SymA==", "dev": true, "bin": { "tsc": "bin/tsc", @@ -4668,9 +4668,9 @@ } }, "typescript": { - "version": "4.6.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.6.2.tgz", - "integrity": "sha512-HM/hFigTBHZhLXshn9sN37H085+hQGeJHJ/X7LpBWLID/fbc2acUMfU+lGD98X81sKP+pFa9f0DZmCwB9GnbAg==", + "version": "4.5.5", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.5.5.tgz", + "integrity": "sha512-TCTIul70LyWe6IJWT8QSYeA54WQe8EjQFU4wY52Fasj5UKx88LNYKCgBEHcOMOrFF1rKGbD8v/xcNWVUq9SymA==", "dev": true }, "unpipe": { diff --git a/package.json b/package.json index 37daee7..ef150e1 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,9 @@ "seed": "ts-node-dev src/scripts/seed.ts", "start": "node .build/api.js", "build": "tsc", - "build:watch": "npm run build -- --watch" + "build:watch": "npm run build -- --watch", + "lint": "eslint src", + "lint:fix": "npm run lint -- --fix" }, "dependencies": { "body-parser": "1.19.2", @@ -39,6 +41,6 @@ "@typescript-eslint/parser": "5.13.0", "eslint": "8.10.0", "ts-node-dev": "1.1.8", - "typescript": "4.6.2" + "typescript": "4.5.5" } } diff --git a/src/api.ts b/src/api.ts index e9725f6..5a0e250 100644 --- a/src/api.ts +++ b/src/api.ts @@ -22,5 +22,5 @@ app.use(profileRouter); app.use(simulatorRouter); app.listen(PORT, () => - console.log(`✅ Ready on port http://localhost:${PORT}`) + console.log(`✅ Ready on port http://localhost:${PORT}`), ); diff --git a/src/models/Favorite.ts b/src/models/Favorite.ts index ec4ffa3..a227a76 100644 --- a/src/models/Favorite.ts +++ b/src/models/Favorite.ts @@ -12,7 +12,7 @@ const schema = new Schema( }, { timestamps: true, - } + }, ); export const Favorite = mongoose.model("Favorite", schema); diff --git a/src/models/Simulator.ts b/src/models/Simulator.ts index 51b15d0..d86a8a3 100644 --- a/src/models/Simulator.ts +++ b/src/models/Simulator.ts @@ -13,7 +13,7 @@ const schema = new Schema( }, { timestamps: true, - } + }, ); export const Simulator = mongoose.model("Simulator", schema); diff --git a/src/routes/profile.router.ts b/src/routes/profile.router.ts index 3dd2d87..2bffca1 100644 --- a/src/routes/profile.router.ts +++ b/src/routes/profile.router.ts @@ -1,16 +1,16 @@ import { Profile } from "#src/models/Profile.js"; import express from "express"; -export var router = express.Router(); +export const router = express.Router(); router.get("/api/profile", async (req, res) => { - var profile = await Profile.find().lean(); + const profile = await Profile.find().lean(); console.log(profile); res.json({ profile }); }); router.post("/api/profile", async (req, res) => { - var { email, name, nickname } = req.body; + const { email, name, nickname } = req.body; let profile = await Profile.findOne({ $or: [{ email }, { nickname }], diff --git a/src/routes/simulator.router.ts b/src/routes/simulator.router.ts index 562b07e..d9a1125 100644 --- a/src/routes/simulator.router.ts +++ b/src/routes/simulator.router.ts @@ -2,13 +2,13 @@ import { Simulator } from "#src/models/Simulator.js"; import cors from "cors"; import express from "express"; -var app = express(); +const app = express(); app.use(cors()); -export var router = express.Router(); +export const router = express.Router(); router.get("/api/simulator", async (req, res) => { - var simulator = await Simulator.find().lean(); + const simulator = await Simulator.find().lean(); console.log(simulator); res.json({ simulator }); }); @@ -16,20 +16,20 @@ router.get("/api/simulator", async (req, res) => { router.get("/api/simulator/:profile_id", async (req, res) => { console.log("========== "); let query = {}; - var { profile_id } = req.params; + const { profile_id } = req.params; console.log({ profile_id }); query = { profile_id }; - var data = await Simulator.find(query); + const data = await Simulator.find(query); res.json(data); }); router.post("/api/simulator/:profile_id", async (req, res) => { - var { profile_id } = req.params; - var newData = { + const { profile_id } = req.params; + const newData = { ...req.body, profile_id, }; console.log(newData); - var simulator = await Simulator.create(newData); + const simulator = await Simulator.create(newData); res.json(simulator); }); diff --git a/src/scripts/seed.ts b/src/scripts/seed.ts index 29f2dfb..f200c27 100644 --- a/src/scripts/seed.ts +++ b/src/scripts/seed.ts @@ -9,11 +9,11 @@ import mongoose from "mongoose"; mongoose.connect(DBURL); const profile = new Profile({ - name: `String`, - email: `String`, - capital: `123`, - divisa: `String`, - prefered_cryptocurrency: `String`, + name: "String", + email: "String", + capital: "123", + divisa: "String", + prefered_cryptocurrency: "String", }); await profile.save(); @@ -24,22 +24,22 @@ import mongoose from "mongoose"; const simulator = new Simulator({ profile_id: idProfile, - name: `String`, - start_date: `01/05/2021`, - check_date: `01/05/2021`, - cryptocurrency: `String`, - divisa: `String`, - Crypto_price_start: `123`, - Crypto_price_check: `123`, + name: "String", + start_date: "01/05/2021", + check_date: "01/05/2021", + cryptocurrency: "String", + divisa: "String", + Crypto_price_start: "123", + Crypto_price_check: "123", }); await simulator.save(); const favorite = new Favorite({ profile_id: idProfile, - name: `String`, - favorite1: `String`, - favorite2: `String`, - favorite3: `String`, + name: "String", + favorite1: "String", + favorite2: "String", + favorite3: "String", }); await favorite.save();