Skip to content

Commit

Permalink
Api-version-controller-tsoa (#176)
Browse files Browse the repository at this point in the history
* enhance - create a tsoa authentication middleware (#152)

* enhance - create a tsoa authentication middleware

* Feat/migrate tsoa (#155)

* Feat: migrate super admin controller to tsoa.

* Feat: migrate admin controller to tsoa.

* Fix: improve role crud controllers.

* Fix: resolve typescript error in auth controller.

* Test admin controller methods.

* Test the controllers.

* Fixed swagger response annotation string.

* Update codebase with TSOA migration and other enhancements (#157)

* 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

* fix - return dev depandancies to thier place

* enchance - remove useless tests

* fix nodemon reloading & use RegisterRoutes

* fix - TypeError: Class extends value
undefined is not a constructor or null

* fix - update relative paths
fix swagger live reload

* enhance - create a tsoa authentication middleware

* fix - update relative paths
fix swagger live reload

* fix - multiprocces nodmon for docs reload

using concurrently i managed to create two nodemon proccess

one for reloading the doc and other the app

to reduce time of restarting the server

* fix - delete auto generated routes - gitignore

* help wanted - disabling route versioning
because tsoa dosnt support it

* code cleaning - dead code in auth controller

* enhance - remove old generated routes.ts
in routes folder

* enhance - add an authority decorator

* fix - logout request

* fix - reload swagger web page

* v1 of converting auth controllers

* feat - finish auth controllers v1

* fix - build project unable to exit

* fix logger level for prod env

* Delete unused routes and controllers.

* convert base controller functions
from a request handler type to normal functions

* finish auth controller

* migrate calendar controllers v1

* update routes brfore testing

* validate testing && fix some typo errors

* fix - add new aproach of validation authorities
and restrictions ( use InspectAuthority decorator)

* Add API routes and responses for multiple controllers

* create the base for notification model(#162)

* Update MongoDB URI and add MongoDB service to docker-compose.yml

* refactor: convert logical operator to optional chainining (#2)

* ci: add .deepsource.toml

* refactor: convert logical operator to optional chainining

The [optional chaining](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining) operator can be used to perform null checks before accessing a property, or calling a function.

---------

Co-authored-by: deepsource-io[bot] <42547082+deepsource-io[bot]@users.noreply.github.com>
Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>

* Update Docker ports and MongoDB URIs

* make sure all tests are passing

* feat: use v1 instead of 1.0.0 & use api version from env instead of use package.json version, to have more control over changing it
add api-version to repose details

* refactor: Add API version control middleware to handle default and redirect to latest version

Co-authored-by: muttaqin1 <[email protected]>

* update tsoa to latest version and add api version validation

* add api-version to response and add unite tests to api version

---------

Co-authored-by: Muttaqin <[email protected]>
Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>
Co-authored-by: deepsource-io[bot] <42547082+deepsource-io[bot]@users.noreply.github.com>
  • Loading branch information
4 people authored Jun 28, 2024
1 parent 3ffdd98 commit f43c11f
Show file tree
Hide file tree
Showing 8 changed files with 737 additions and 1,029 deletions.
2 changes: 1 addition & 1 deletion backend-app/.env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NODE_ENV = "development"
API_VERSION = "1.0.0"
API_VERSION = "v1"
MONGO_URI = "mongodb://localhost:27017/S-W-O"
MONGO_URI_TEST = "mongodb://localhost:27017/S-W-O-TEST"
PORT = 5000
Expand Down
2 changes: 1 addition & 1 deletion backend-app/config/app_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const parseBoolean = (value: string): boolean => value === 'true';

export const logFilePath = join(__dirname, '../server-logs');
export const CURRENT_ENV = process.env.NODE_ENV?.toLowerCase();
export const API_VERSION = process.env.npm_package_version;
export const API_VERSION = process.env.API_VERSION;
export const DATABASE = process.env.MONGO_URI;
export const MONGO_URI_TEST = process.env.MONGO_URI_TEST;
export const PORT = process.env.PORT;
Expand Down
25 changes: 23 additions & 2 deletions backend-app/middlewares/api_version_controll.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Request, Response, NextFunction } from 'express';
import { API_VERSION } from '@config/app_config';
import AppError from '@root/utils/app_error';

/**
* Middleware to set default API version in the request URL if not provided.
Expand All @@ -8,11 +9,31 @@ import { API_VERSION } from '@config/app_config';
* @param {Response} res - Express response object.
* @param {NextFunction} next - Express next function to pass control to the next middleware or route handler.
*/
const handleAPIVersion = (req: Request, _res: Response, next: NextFunction) => {
const handleAPIVersion = (req: Request, res: Response, next: NextFunction) => {
if (!req.headers['api-version']) {
req.headers['api-version'] = API_VERSION;
res.setHeader('api-version', API_VERSION);
}
next();

// Redirect to the default API version if the API version is missing in the URL
if (
req.url.includes('/docs') ||
req.url.includes('/docs-json') ||
req.url.includes(`/${req.headers['api-version']}`)
)
return next();

// validate the API version
if (!/^v[0-9]+$/.test(String(req.headers['api-version'])) || req.headers['api-version'] > API_VERSION) {
throw new AppError(400, 'Invalid API version');
}

// Check if the API version is the latest
if (req.headers['api-version'] === API_VERSION) {
return next();
}

return res.redirect(307, `${req.url}/${req.headers['api-version']}`);
};

export default handleAPIVersion;
3 changes: 2 additions & 1 deletion backend-app/middlewares/global_error_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ const errorHandler = (
status: (err as any).statusCode,
title: httpStatus.getStatusText((err as any).statusCode),
details: {
...((err as any).path && { path: (err as any).path }),
...((err as any).path && { path: (err as any).path.replace(/\/v\d+/, '') }),
"api-version": req.headers['api-version'],
...(CURRENT_ENV === 'development' && {
error: {
'0': 'Do not forget to remove this in production!',
Expand Down
Loading

0 comments on commit f43c11f

Please sign in to comment.