Skip to content

Commit

Permalink
Updated with new features
Browse files Browse the repository at this point in the history
  • Loading branch information
filoscoder committed Aug 7, 2022
1 parent 1069b5e commit 98a98e7
Show file tree
Hide file tree
Showing 45 changed files with 677 additions and 226 deletions.
33 changes: 33 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module",
"project": "./tsconfig.json"
},
"plugins": ["import", "prettier"],
"ignorePatterns": ["node_modules/*", "dist/*", "jest*", "ecosystem*"],
"extends": [
"plugin:@typescript-eslint/recommended",
"prettier"
],
"rules": {
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/no-empty-interface": "off",
"@typescript-eslint/ban-types": "off",
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-unused-vars": ["error", {
"argsIgnorePattern": "^_"
}],
"import/order": ["error"],
"prettier/prettier": [
"error",
{
"endOfLine": "auto"
}
]
}
}
51 changes: 0 additions & 51 deletions .eslintrc.yml

This file was deleted.

6 changes: 4 additions & 2 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"parser": "typescript",
"semi": true,
"singleQuote": true,
"singleQuote": false,
"tabWidth": 2,
"trailingComma": "all"
"trailingComma": "all",
"printWidth": 80
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.experimental.stickyScroll.enabled": true
}
21 changes: 21 additions & 0 deletions __tests__/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import supertest, { SuperAgentTest } from "supertest";

import { createApp } from "@/app";

// let dbConnection: Mongoose;

export const initAgent = async (): Promise<SuperAgentTest> => {
const app = createApp();
const agent = supertest.agent(app);
// dbConnection = await mongoDbConnection();
// await agent.post("/api/v2/auth/signin").send({
// email: testUser.email,
// password: testUser.password,
// });

return agent;
};

// export const closeClients = async (): Promise<void> => {
// await dbConnection.disconnect();
// };
66 changes: 66 additions & 0 deletions __tests__/home.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { SuperAgentTest } from "supertest";
import HttpStatus, { OK, BAD_REQUEST } from "http-status/lib";
import { initAgent } from "./helpers";
import CONFIG from "@/config";

let agent: SuperAgentTest;

beforeAll(async () => {
agent = await initAgent();
});

describe("[UNIT] => HOME", () => {
describe("GET: '/'", () => {
it("Should return API app information", async () => {
const response = await agent.get(`/api/${CONFIG.APP.VER}`);
expect(response.status).toBe(OK);
expect(Object.keys(response.body.data)).toEqual([
"NAME",
"VERSION",
"VER",
"DESCRIPTION",
"AUTHORS",
"PORT",
"ENV",
]);
});

it.each`
query | field | expectedStatus
${"name"} | ${"NAME"} | ${OK}
${"version"} | ${"VERSION"} | ${OK}
${"description"} | ${"DESCRIPTION"} | ${OK}
${"authors"} | ${"AUTHORS"} | ${OK}
${"port"} | ${"PORT"} | ${OK}
${"env"} | ${"ENV"} | ${OK}
`(
"Should return CONFIG.APP[$field] value when query.key is `$query`",
async ({
query,
field,
expectedStatus,
}: {
query: string;
field: keyof typeof CONFIG.APP;
expectedStatus: string;
}) => {
const response = await agent
.get(`/api/${CONFIG.APP.VER}`)
.query({ key: query });

expect(response.body.status).toBe(expectedStatus);
expect(response.body.data[field]).toBe(CONFIG.APP[field]);
},
);

it("Should return 400 status Validation Error", async () => {
const invalidQuery = "invalid-field";
const response = await agent
.get(`/api/${CONFIG.APP.VER}`)
.query({ key: invalidQuery });

expect(response.body.status).toBe(BAD_REQUEST);
expect(response.body.message).toBe(HttpStatus[BAD_REQUEST]);
});
});
});
35 changes: 35 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const { defaults: tsjPreset } = require("ts-jest/presets");

module.exports = {
globals: {
"ts-jest": {
tsconfig: "tsconfig.json",
diagnostics: false,
},
},
preset: "ts-jest",
moduleFileExtensions: ["ts", "js", "json"],
transform: {
"^.+\\.(ts|tsx)$": "ts-jest",
...tsjPreset.transform,
},
testPathIgnorePatterns: ["dist"],
testMatch: ["**/__tests__/**/*.test.(ts|js)"],
testEnvironment: "node",
moduleNameMapper: {
"@/(.*)": "<rootDir>/src/$1",
},
watchPlugins: [
"jest-watch-typeahead/filename",
"jest-watch-typeahead/testname",
],
coverageThreshold: {
global: {
branches: 1,
functions: 1,
lines: 1,
statements: 1,
},
},
coverageReporters: ["json", "lcov", "text", "clover"],
};
2 changes: 1 addition & 1 deletion nodemon.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"watch": ["src/**/*.ts", ".env"],
"ext": "ts,js,.env",
"verbose": false,
"exec": "ts-node -r tsconfig-paths/register src/index.ts"
"exec": "ts-node -r tsconfig-paths/register --files src/index.ts"
}
37 changes: 29 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
"license": "MIT",
"scripts": {
"precommit": "lint-staged",
"lint": "tslint -c tslint.json 'src/**/*.{ts,js}'; exit 0",
"lint:fix": "tslint --fix -c tslint.json 'src/**/*.ts'; exit 0",
"lint": "eslint 'src/**/*.{ts,js}' '__tests__/**/*.{ts,js}'",
"lint:fix": "eslint --fix 'src/**/*.{ts,js}' '__tests__/**/*.{ts,js}' --quiet",
"transpile": "tsc",
"clean": "rimraf dist",
"build": "NODE_ENV=production run-s prettify clean transpile",
"watch": "tsc --watch",
"test": "NODE_ENV=test jest --coverage --runInBand --detectOpenHandles",
"test:watchAll": "NODE_ENV=test jest --watchAll --runInBand --detectOpenHandles",
"start": "yarn prettify & transpile & nodemon",
"service:start": "pm2 start ecosystem.config.js",
"service:reload": "pm2 reload ecosystem.config.js",
Expand All @@ -21,7 +23,7 @@
"service:list": "pm2 list ecosystem.config.js",
"service:delete": "pm2 delete ecosystem.config.js",
"service:logs": "pm2 logs",
"prettify": "prettier --single-quote --write \"src/**/*.{ts,js,json}\""
"prettify": "prettier --write \"src/**/*.{ts,js,json}\" \"__tests__/**/*.{ts,js,json}\""
},
"lint-staged": {
"*.{ts,js}": [
Expand All @@ -40,36 +42,55 @@
],
"dependencies": {
"bcrypt": "^5.0.0",
"connect-timeout": "^1.9.0",
"cors": "^2.8.3",
"dotenv": "^10.0.0",
"express": "^4.15.3",
"express-pino-logger": "^7.0.0",
"express-validator": "^6.14.2",
"helmet": "^4.3.1",
"http-status": "^1.5.0",
"http-status": "^1.5.2",
"jsonwebtoken": "^8.5.1",
"lodash-es": "^4.17.21",
"morgan": "^1.8.2",
"rimraf": "^3.0.2",
"lodash-es": "^4.17.21"
"pino-pretty": "^8.1.0",
"rimraf": "^3.0.2"
},
"devDependencies": {
"@babel/preset-env": "^7.18.10",
"@babel/preset-typescript": "^7.18.6",
"@types/bcrypt": "^5.0.0",
"@types/connect-timeout": "^0.0.36",
"@types/cors": "^2.8.1",
"@types/dotenv": "^8.2.0",
"@types/express": "^4.17.11",
"@types/express-pino-logger": "^4.0.3",
"@types/helmet": "^4.0.0",
"@types/http-status": "^1.1.2",
"@types/jest": "^28.1.6",
"@types/jsonwebtoken": "^8.5.0",
"@types/lodash-es": "^4.17.4",
"@types/module-alias": "^2.0.1",
"@types/morgan": "^1.7.32",
"@types/node": "^16.4.13",
"eslint": "^7.18.0",
"@types/supertest": "^2.0.12",
"@typescript-eslint/eslint-plugin": "^5.32.0",
"@typescript-eslint/parser": "^5.32.0",
"eslint": "^8.21.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-prettier": "^4.2.1",
"husky": "^7.0.1",
"jest": "^28.1.3",
"jest-watch-typeahead": "^2.0.0",
"lint-staged": "^11.1.2",
"module-alias": "^2.2.2",
"nodemon": "^2.0.7",
"npm-run-all": "^4.1.5",
"pm2": "^5.1.0",
"prettier": "^2.2.1",
"prettier": "^2.7.1",
"supertest": "^6.2.4",
"ts-jest": "^28.0.7",
"ts-node": "^10.2.0",
"tsc": "^2.0.3",
"tsconfig-paths": "^3.10.1",
Expand Down
26 changes: 17 additions & 9 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
import * as errorHandler from '@/middlewares/errorHandler';

import cors from 'cors';
import express from 'express';
import helmet from 'helmet';
import morgan from 'morgan';
import routes from '@/routes';
import cors from "cors";
import express from "express";
import helmet from "helmet";
import morgan from "morgan";
import timeout from "connect-timeout";
import CONFIG from "./config";
import { expressPinoLogger } from "./helpers";
import * as errorHandler from "@/middlewares/errorHandler";
import routes from "@/routes";

export const createApp = (): express.Application => {
const app = express();

app.use(cors());
app.use(helmet());
app.use(morgan('dev'));
app.use(express.json());
app.use(
express.urlencoded({
extended: true,
}),
);

if (CONFIG.APP.ENV !== "test") {
app.use(morgan("dev"));
app.use(expressPinoLogger);
}

app.use(timeout(CONFIG.SERVER.TIMEOUT));

// API Routes
app.use('/', routes);
app.use(`/api/${CONFIG.APP.VER}`, routes);

// Error Middleware
app.use(errorHandler.genericErrorHandler);
Expand Down
23 changes: 23 additions & 0 deletions src/components/home/controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { OK } from "http-status/lib";
import { HomeServices } from "./services";
import { getAppInfoQuery } from "@/types/request/home";
import { apiResponse } from "@/helpers/apiResponse";

export class HomeController {
/**
* @description Gets the API information.
* @param {Req} req
* @param {Res} res
*/
static getAppInfo = async (req: Req, res: Res, next: NextFn) => {
try {
const appInfoKey = req.query.key as getAppInfoQuery;
const homeServices = new HomeServices();
const result = await homeServices.getAppInfo(appInfoKey);

res.status(OK).json(apiResponse(result));
} catch (error) {
next(error);
}
};
}
2 changes: 2 additions & 0 deletions src/components/home/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { HomeController } from "./controller";
export { appKeyValidator } from "./validators";
Loading

0 comments on commit 98a98e7

Please sign in to comment.