Skip to content

Commit

Permalink
contribute to #130 Typescript migration (#131)
Browse files Browse the repository at this point in the history
* enhance - use tsconfig-paths

* fix - do tsconfig-path registeration

* inhence - add dependent bot auto merge

* inhen - call the paths registeration from server.ts

* inhence - update logger

* fix - convert base_controller to ts

* fix - type validation

* inhence - complete 3 files in #130
- auth_controller
- github_controller
- create_default_user

* inhence - complete 5 files in
TypeScript Migration Tasks #130
- user_controller
- role_model
- user_model
- create_roles
- role

* inhence convert files to ts
TypeScript Migration Tasks #130

* inhence - convert files to ts
TypeScript Migration Tasks #130

* inhence - convert utils dir to ts  #130

* fix - logger not defined
inhence - TypeScript Migration Tasks #130

* fix - update apperror Fix app error #103

* fix - app error & use module paths
Fix - app error #103
Database Seeding Enhancement: Improving REST API Initialization #108
 Suggestion - Organizing Interfaces in an "Interfaces" Folder #129

* fix -
- create interfaces for models, vendors and github repo
- change testing framwork

* enhnace - code optimixation AppError

* enchance - convert swagger to ts 1st try

* fix - convert to swagger-jsdoc

* enhance - complete other routes

* fix - create typings folder to manage .d.ts

* fix some ts-ignore errors

* fix - 2 ts ignore errors

* fix - 3 ts ignore errors

* fix - ts convert files #130

* testing tsoa

* fix - github env

* fix - use npm ci

* fix - add env config for tests

* test github variable

* fix - run workflow when review approved

* fix - server tsoa swagger docs

* fix - remove dummy error handler

* fix - github env vars & secrets
  • Loading branch information
bellaabdelouahab authored Oct 8, 2023
1 parent 0ff5e7d commit a789de6
Show file tree
Hide file tree
Showing 111 changed files with 6,394 additions and 5,260 deletions.
1 change: 0 additions & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ name: Node.js API CI

on:
workflow_dispatch:


jobs:
approved:
Expand Down
4 changes: 1 addition & 3 deletions .husky/pre-push
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@
. "$(dirname "$0")/_/husky.sh"

cd backend-app
# disbale jest for now as it is not working
# npx jest

npm test
12 changes: 6 additions & 6 deletions backend-app/.env.example
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
NODE_ENV = "development"
API_VERSION = "v1"
API_VERSION = "1.0.0"
MONGO_URI = "mongodb://localhost:27017/S-W-O"
MONGO_URI_TEST = "mongodb://localhost:27017/S-W-O-TEST"
PORT = 5000
ADMIN_EMAIL = "[email protected]"
ADMIN_PASSWORD = "password123418746"
ACCESS_TOKEN_SECRET = "0d9acc2b4953578b7ed29c443b313082e3bd14c07f9747a6b65797a254137a9d175d95ce3faaf5f60f6d5f57dfa4bcd58151a3cc262aecb428592cee6f7c964b040ac46b4efb64171e8f3660ccf948189737d8d747f3f7f7c267277b4f7ff6000f2df0aa9b58b28e86f58f4a9e07748a31c771fc74306722d4416e024d17ce26"
ACCESS_TOKEN_SECRET = "YourAccessTokenSecretKey"
ACCESS_TOKEN_EXPIRY_TIME = "1d"
REFRESH_TOKEN_SECRET = "360c3a51d4cb6b57a6f899996a59ebaa0a97aa15cb00462f4c5d4e76195ac01c80f78c2887c39fc8c8fd4035ba8214634c713cddcf07de957d37d094f30ada70855caee0d5484ecddad57ed1e4361ebb55c9d568f357aaece7cdbe207f50191fb711eda1cb17997371fb80706a282c17ebf7b8bab2e84642869640797bb7bd2d"
REFRESH_TOKEN_SECRET = "YourRefreshTokenSecretKey"
REFRESH_TOKEN_EXPIRY_TIME = "7d"
REQUIRE_ACTIVATION = false
RATE_LIMIT_PER_HOUR = 500
GITHUB_OAUTH_CLIENT_ID = "Iv1.6f4b4b8b0b1b4b8b"
GITHUB_OAUTH_CLIENT_SECRET = "6f4b4b8b0b1b4b8b6f4b4b8b0b1b4b8b"
GITHUB_OAUTH_REDIRECT_URL = "http://localhost:3000/auth/github/callback"
OAUTH_CLIENT_ID_GITHUB = "Iv1.6f4b4b8b0b1b4b8b"
OAUTH_CLIENT_SECRET_GITHUB = "6f4b4b8b0b1b4b8b6f4b4b8b0b1b4b8b"
OAUTH_REDIRECT_URL_GITHUB = "http://localhost:3000/auth/github/callback"
3 changes: 1 addition & 2 deletions backend-app/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module.exports = {
'accessor-pairs': 'error',
'array-callback-return': 'error',
'block-scoped-var': 'error',
'class-methods-use-this': 'error',
complexity: ['error', 10],
'consistent-return': 'off',
'default-case': 'error',
Expand Down Expand Up @@ -52,7 +51,7 @@ module.exports = {
'no-sequences': 'error',
'no-throw-literal': 'error',
'no-unmodified-loop-condition': 'error',
'no-unused-expressions': 'error',
'no-unused-expressions': 'warn',
'no-unused-labels': 'error',
'no-useless-call': 'error',
'no-useless-concat': 'error',
Expand Down
82 changes: 0 additions & 82 deletions backend-app/.eslintrc.json

This file was deleted.

4 changes: 1 addition & 3 deletions backend-app/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ coverage
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
build

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm
Expand Down
5 changes: 0 additions & 5 deletions backend-app/.husky/pre-commit

This file was deleted.

5 changes: 0 additions & 5 deletions backend-app/.husky/pre-push

This file was deleted.

6 changes: 6 additions & 0 deletions backend-app/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extension": ["ts"],
"spec": ["tests/**/*.spec.ts", "tests/**/*.spec.js"],
"require": "tsconfig-paths/register",
"file": ["tests/env.test.ts"]
}
Empty file removed backend-app/README.md
Empty file.
52 changes: 26 additions & 26 deletions backend-app/app.js → backend-app/app.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
const globalErrHandler = require('./middlewares/global_error_handler');
const AppError = require('./utils/app_error');
const express = require('express');
const limiter = require('./middlewares/rate_limit');
const bearerToken = require('express-bearer-token');
const compression = require('compression');
const helmet = require('helmet');
const mongoSanitize = require('express-mongo-sanitize');
const xss = require('xss-clean');
const hpp = require('hpp');
const cors = require('cors');
const morgan = require('./middlewares/morgan');
const swaggerDocs = require('./utils/swagger');
const handleAPIVersion = require('./middlewares/api_version_controll');
const { COOKIE_SECRET, CURRENT_ENV } = require('./config/app_config');
const cookieParser = require('cookie-parser');
const routesVersioning = require('express-routes-versioning')();
import globalErrHandler from './middlewares/global_error_handler';
import AppError from './utils/app_error';
import express, { Request, Response, NextFunction } from 'express';
import limiter from './middlewares/rate_limit';
import bearerToken from 'express-bearer-token';
import compression from 'compression';
import helmet from 'helmet';
import mongoSanitize from 'express-mongo-sanitize';
import xss from 'xss-clean';
import hpp from 'hpp';
import cors from 'cors';
import Morgan from './middlewares/morgan';
import swaggerDocs from './utils/swagger/index';
import handleAPIVersion from './middlewares/api_version_controll';
import { COOKIE_SECRET, CURRENT_ENV } from './config/app_config';
import cookieParser from 'cookie-parser';
import routesVersioning from 'express-routes-versioning';
import indexRouter from './routes/index';

const app = express();

Expand All @@ -24,7 +25,7 @@ app.use(express.json());
app.use(cookieParser(COOKIE_SECRET));

// use morgan for logging
app.use(morgan);
app.use(Morgan);

// Allow Cross-Origin requests
app.use(cors());
Expand Down Expand Up @@ -68,9 +69,8 @@ app.use(handleAPIVersion);
// handle bearer token
app.use(bearerToken());

app.get('/', (req, res) => {
app.get('/', (_req: Request, res: Response) => {
res.status(200).json({
status: 'success',
message: 'Welcome to the backend app',
env: CURRENT_ENV,
});
Expand All @@ -79,20 +79,20 @@ app.get('/', (req, res) => {
// routes
app.use(
`/api`,
routesVersioning({
'1.0.0': require('./routes/index'),
routesVersioning()({
'1.0.0': indexRouter,
})
);

// configure swagger docs
swaggerDocs(app);

// handle undefined Routes
app.use('*', (req, res, next) => {
const err = new AppError(404, 'fail', 'Route Not Found', req.originalUrl);
next(err, req, res, next);
app.use('*', (req: Request, _res: Response, next: NextFunction) => {
const err = new AppError(404, 'Route Not Found', req.originalUrl);
next(err);
});

app.use(globalErrHandler);

module.exports = app;
export default app;
28 changes: 20 additions & 8 deletions backend-app/config/app_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,17 @@ import dotenv from 'dotenv';
import fs from 'fs';

// load env file
const envFile = fs.existsSync('.env') ? '.env' : '.env.example';
dotenv.config({ path: join(__dirname, `../${envFile}`) });
const envMode = process.env.NODE_ENV?.toLowerCase();
const envFile =
envMode === 'production'
? '.env.production'
: fs.existsSync('.env')
? '.env'
: '.env.example';

if (envFile) {
dotenv.config({ path: join(__dirname, `../${envFile}`) });
}

// parse boolean values
const parseBoolean = (value: string): boolean => value === 'true';
Expand All @@ -17,12 +26,15 @@ export const MONGO_URI_TEST = process.env.MONGO_URI_TEST;
export const PORT = process.env.PORT;
export const ADMIN_EMAIL = process.env.ADMIN_EMAIL;
export const ADMIN_PASSWORD = process.env.ADMIN_PASSWORD;
export const REQUIRE_ACTIVATION = parseBoolean(process.env.REQUIRE_ACTIVATION);
export const RATE_LIMIT_PER_HOUR = process.env.RATE_LIMIT_PER_HOUR;
export const GITHUB_OAUTH_CLIENT_ID = process.env.GITHUB_OAUTH_CLIENT_ID;
export const GITHUB_OAUTH_CLIENT_SECRET =
process.env.GITHUB_OAUTH_CLIENT_SECRET;
export const GITHUB_OAUTH_REDIRECT_URL = process.env.GITHUB_OAUTH_REDIRECT_URL;
export const REQUIRE_ACTIVATION = parseBoolean(
process.env.REQUIRE_ACTIVATION as string
);
export const RATE_LIMIT_PER_HOUR = process.env
.RATE_LIMIT_PER_HOUR as unknown as number;
export const OAUTH_CLIENT_ID_GITHUB = process.env.OAUTH_CLIENT_ID_GITHUB;
export const OAUTH_CLIENT_SECRET_GITHUB =
process.env.OAUTH_CLIENT_SECRET_GITHUB;
export const OAUTH_REDIRECT_URL_GITHUB = process.env.OAUTH_REDIRECT_URL_GITHUB;
export const ACCESS_TOKEN_SECRET = process.env.ACCESS_TOKEN_SECRET;
export const ACCESS_TOKEN_EXPIRY_TIME = process.env.ACCESS_TOKEN_EXPIRY_TIME;
export const REFRESH_TOKEN_SECRET = process.env.REFRESH_TOKEN_SECRET;
Expand Down
4 changes: 4 additions & 0 deletions backend-app/config/logger_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ const consoleOptions = {
format.timestamp({
format: 'HH:mm:ss MM-DD-YYYY',
}),
format((info) => {
info.level = info.level.toUpperCase();
return info;
})(),
format.colorize({ all: true }),
format.timestamp(),
formatLogMessage
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/**
* @enum {string}
*/
const Actions = {
DELETE_USER: 'DELETE_USER',
BAN_USER: 'BAN_USER',
Expand All @@ -9,6 +6,5 @@ const Actions = {
REMOVE_SUPER_ADMIN: 'REMOVE_SUPER_ADMIN',
MANAGE_ROLES: 'MANAGE_ROLES',
};
Object.freeze(Actions);

module.exports = Actions;
export default Actions;
25 changes: 0 additions & 25 deletions backend-app/constants/default_roles.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* @param {import('mongoose').Schema} model
* @param {Schema<Document>} model
* @returns {void}
* @description Add common fields to a model
*
**/
exports.apply = (model) => {
const apply = (model: any): void => {
model.add({
deleted: {
type: Boolean,
Expand All @@ -25,3 +25,7 @@ exports.apply = (model) => {
},
});
};

export default {
apply,
};
Loading

0 comments on commit a789de6

Please sign in to comment.