-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from renomureza/refactor
Refactor
- Loading branch information
Showing
52 changed files
with
4,469 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
coverage | ||
src/data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
.env | ||
.env | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/sh | ||
. "$(dirname "$0")/_/husky.sh" | ||
|
||
yarn install |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/sh | ||
. "$(dirname "$0")/_/husky.sh" | ||
|
||
git status |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/sh | ||
. "$(dirname "$0")/_/husky.sh" | ||
|
||
yarn lint-staged |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"*.js": [ | ||
"eslint --cache --fix", | ||
"jest --findRelatedTests", | ||
"prettier --write" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
coverage | ||
src/data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"recommendations": ["dbaeumer.vscode-eslint", "esbenp.prettier-vscode"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Oops, something went wrong.
c0f53a0
There was a problem hiding this comment.
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