added middleware

This commit is contained in:
Kallesh B S 2025-06-05 11:40:15 +05:30
parent 2b8da1bafa
commit a400b6842d
4 changed files with 52 additions and 10 deletions

View File

@ -4,6 +4,7 @@ import { OracleModule } from './oracle/oracle.module';
import { AuthModule } from './auth/auth.module';
import { MssqlModule } from './mssql/mssql.module';
import { OriginCheckMiddleware } from './middleware/OriginCheck.middleware';
import { ReqBodyKeysToUppercaseMiddleware } from './middleware/reqBodyKeysToUppercase.middleware';
@Module({
imports: [AuthModule, DbModule, OracleModule],
@ -15,5 +16,18 @@ export class AppModule {
consumer
.apply(OriginCheckMiddleware)
.forRoutes('*'); // Apply to all routes
consumer
.apply(ReqBodyKeysToUppercaseMiddleware)
.forRoutes('*'); // Apply to all routes
// .forRoutes('users'); // Applies to all methods on /users
// .forRoutes(UsersController); // Applies to all routes in this controller
// .forRoutes(
// { path: 'users', method: RequestMethod.GET },
// { path: 'auth/login', method: RequestMethod.POST },
// );
}
}

View File

@ -20,6 +20,7 @@ async function bootstrap() {
},
});
function extractConstraints(validationErrors: ValidationError[]) {
const constraints: { [key: string]: string }[] = [];
@ -82,11 +83,8 @@ async function bootstrap() {
.addServer('http://localhost:3000', 'Development Server 1')
.build();
const options: SwaggerDocumentOptions = {
autoTagControllers: false,
};
const documentFactory = () =>
SwaggerModule.createDocument(app, config, options);
const options: SwaggerDocumentOptions = { autoTagControllers: false };
const documentFactory = () => SwaggerModule.createDocument(app, config, options);
SwaggerModule.setup('api', app, documentFactory);
await app.listen(process.env.PORT ?? 3000);

View File

@ -0,0 +1,30 @@
import { Injectable, NestMiddleware } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';
@Injectable()
export class ReqBodyKeysToUppercaseMiddleware implements NestMiddleware {
use(req: Request, res: Response, next: NextFunction) {
if (req.body && typeof req.body === 'object' && !Array.isArray(req.body)) {
console.log('I am here will start operation shortly...');
req.body = this.uppercaseKeys(req.body);
}
next();
}
private uppercaseKeys(obj: Record<string, any>): Record<string, any> {
const newObj: Record<string, any> = {};
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
const upperKey = key.toUpperCase();
const value = obj[key];
newObj[upperKey] =
value !== null && typeof value === 'object' && !Array.isArray(value)
? this.uppercaseKeys(value)
: value;
}
}
return newObj;
}
}

View File

@ -11,15 +11,15 @@ import { CarnetApplicationModule } from './carnet-application/carnet-application
@Module({
imports: [
UserMaintenanceModule,
DbModule,
CarnetApplicationModule,
UserMaintenanceModule,
HomePageModule,
UscibManagedSpModule,
ParamTableModule,
ManageFeeModule,
ManageHoldersModule,
ManageClientsModule,
CarnetApplicationModule,
],
providers: [],
controllers: [],