Skip to content

Commit

Permalink
Merge pull request #23 from renomureza/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
renomureza authored Apr 22, 2022
2 parents c0ad9ca + fa9da04 commit c0f53a0
Show file tree
Hide file tree
Showing 52 changed files with 4,469 additions and 122 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
coverage
src/data
22 changes: 22 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"env": {
"node": true,
"jest": true
},
"extends": [
"airbnb-base",
"plugin:jest/recommended",
"plugin:prettier/recommended"
],
"plugins": ["jest", "prettier"],
"parserOptions": {
"ecmaVersion": 2020
},
"rules": {
"no-console": "error",
"func-names": "off",
"no-underscore-dangle": "off",
"consistent-return": "off",
"jest/expect-expect": "off"
}
}
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Convert text file line endings to lf
* text eol=lf
*.js text
28 changes: 28 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Test

on:
push:
pull_request:
branches:
- "**"

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [14.x, 16.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: yarn install
- run: yarn test
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
.env
.env
coverage
4 changes: 4 additions & 0 deletions .husky/post-checkout
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn install
4 changes: 4 additions & 0 deletions .husky/post-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

git status
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn lint-staged
7 changes: 7 additions & 0 deletions .lintstagedrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"*.js": [
"eslint --cache --fix",
"jest --findRelatedTests",
"prettier --write"
]
}
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
coverage
src/data
3 changes: 3 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"recommendations": ["dbaeumer.vscode-eslint", "esbenp.prettier-vscode"]
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div align="center">
<a href="./">
<img src="./assets/logo.png">
<img src="./src/assets/logo.png">
</a>
<h1 align="center">Quran API ID</h1>
<p align="center">
Expand Down
9 changes: 0 additions & 9 deletions config/config.js

This file was deleted.

10 changes: 0 additions & 10 deletions controllers/random.controller.js

This file was deleted.

36 changes: 0 additions & 36 deletions controllers/surahs.controller.js

This file was deleted.

15 changes: 15 additions & 0 deletions ecosystem.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"apps": [
{
"name": "app",
"script": "src/index.js",
"instances": 1,
"autorestart": true,
"watch": false,
"time": true,
"env": {
"NODE_ENV": "production"
}
}
]
}
3 changes: 3 additions & 0 deletions env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# development | test | production - required
NODE_ENV=

# default: http://localhost:3005
BASE_URL=

Expand Down
22 changes: 0 additions & 22 deletions index.js

This file was deleted.

14 changes: 14 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
testEnvironment: "node",
testEnvironmentOptions: {
NODE_ENV: "test",
},
restoreMocks: true,
coveragePathIgnorePatterns: [
"node_modules",
"src/config",
"src/app.js",
"tests",
],
coverageReporters: ["text", "lcov", "clover", "html"],
};
35 changes: 29 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "quran-api-id",
"version": "1.0.0",
"main": "index.js",
"main": "./src/index.js",
"license": "MIT",
"author": "R.M. Reza ([email protected])",
"homepage": "https://github.com/renomureza/quran-api-id#readme",
Expand All @@ -13,17 +13,40 @@
"scripts": {
"dev": "nodemon .",
"start": "node .",
"build:quran": "node ./data/scripts/quranBuilder",
"scrape:surah": "node ./data/scripts/surahKemenagScraper",
"scrape:tafsir": "node ./data/scripts/tafsirKemenagScraper"
"test": "jest -i --colors --verbose --detectOpenHandles",
"test:watch": "jest -i --watchAll --verbose",
"test:coverage": "jest -i --coverage",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"prettier": "prettier --check **/*.js",
"prettier:fix": "prettier --write **/*.js",
"prepare": "husky install",
"build:quran": "node ./src/data/scripts/quranBuilder",
"scrape:surah": "node ./src/data/scripts/surahKemenagScraper",
"scrape:tafsir": "node ./src/data/scripts/tafsirKemenagScraper"
},
"dependencies": {
"axios": "^0.26.1",
"cors": "^2.8.5",
"dotenv": "^16.0.0",
"express": "^4.17.3"
"express": "^4.17.3",
"http-status": "^1.5.1",
"morgan": "^1.10.0",
"winston": "^3.7.2",
"yup": "^0.32.11"
},
"devDependencies": {
"nodemon": "^2.0.15"
"eslint": "^8.13.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^26.1.4",
"eslint-plugin-prettier": "^4.0.0",
"husky": "^7.0.4",
"jest": "^27.5.1",
"lint-staged": "^12.4.0",
"nodemon": "^2.0.15",
"prettier": "^2.6.2",
"supertest": "^6.2.2"
}
}
33 changes: 33 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const express = require("express");
const cors = require("cors");
const httpStatus = require("http-status");
const routes = require("./routes");
const config = require("./config/config");
const morgan = require("./config/morgan");
const ApiError = require("./utils/ApiError");
const { errorConverter, errorHandler } = require("./middlewares/error");

const app = express();

if (config.NODE_ENV !== "test") {
app.use(morgan.successHandler);
app.use(morgan.errorHandler);
}

app.use(express.json());

app.use(cors());

app.disable("x-powered-by");

app.use("/", routes);

app.use((req, res, next) => {
next(new ApiError(httpStatus.NOT_FOUND, "Not found"));
});

app.use(errorConverter);

app.use(errorHandler);

module.exports = app;
File renamed without changes
27 changes: 27 additions & 0 deletions src/config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const dotenv = require("dotenv");
const path = require("path");
const yup = require("yup");

dotenv.config({ path: path.resolve(".env") });

const envVarsSchema = yup
.object()
.shape({
NODE_ENV: yup
.string()
.oneOf(["production", "development", "test"])
.required(),
PORT: yup.number().default(3005),
BASE_URL: yup.string().default("http://localhost:3005"),
})
.noUnknown();

const getConfig = () => {
try {
return envVarsSchema.validateSync(process.env);
} catch (error) {
throw new Error(`Config validation error: ${error.message}`);
}
};

module.exports = getConfig();
28 changes: 28 additions & 0 deletions src/config/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const winston = require("winston");
const config = require("./config");

const enumerateErrorFormat = winston.format((info) => {
if (info instanceof Error) {
Object.assign(info, { message: info.stack });
}
return info;
});

const logger = winston.createLogger({
level: config.NODE_ENV === "development" ? "debug" : "info",
format: winston.format.combine(
enumerateErrorFormat(),
config.NODE_ENV === "development"
? winston.format.colorize()
: winston.format.uncolorize(),
winston.format.splat(),
winston.format.printf(({ level, message }) => `${level}: ${message}`)
),
transports: [
new winston.transports.Console({
stderrLevels: ["error"],
}),
],
});

module.exports = logger;
25 changes: 25 additions & 0 deletions src/config/morgan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const morgan = require("morgan");
const config = require("./config");
const logger = require("./logger");

morgan.token("message", (req, res) => res.locals.errorMessage || "");

const getIpFormat = () =>
config.NODE_ENV === "production" ? ":remote-addr - " : "";
const successResponseFormat = `${getIpFormat()}:method :url :status - :response-time ms`;
const errorResponseFormat = `${getIpFormat()}:method :url :status - :response-time ms - message: :message`;

const successHandler = morgan(successResponseFormat, {
skip: (req, res) => res.statusCode >= 400,
stream: { write: (message) => logger.info(message.trim()) },
});

const errorHandler = morgan(errorResponseFormat, {
skip: (req, res) => res.statusCode < 400,
stream: { write: (message) => logger.error(message.trim()) },
});

module.exports = {
successHandler,
errorHandler,
};
File renamed without changes.
Loading

1 comment on commit c0f53a0

@vercel
Copy link

@vercel vercel bot commented on c0f53a0 Apr 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

quran-api-id – ./

quran-api-id-git-main-renomureza.vercel.app
quran-api-id-renomureza.vercel.app
quran-api-id.vercel.app

Please sign in to comment.