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,9 +4,10 @@ import { OracleModule } from './oracle/oracle.module';
import { AuthModule } from './auth/auth.module'; import { AuthModule } from './auth/auth.module';
import { MssqlModule } from './mssql/mssql.module'; import { MssqlModule } from './mssql/mssql.module';
import { OriginCheckMiddleware } from './middleware/OriginCheck.middleware'; import { OriginCheckMiddleware } from './middleware/OriginCheck.middleware';
import { ReqBodyKeysToUppercaseMiddleware } from './middleware/reqBodyKeysToUppercase.middleware';
@Module({ @Module({
imports: [ AuthModule, DbModule, OracleModule], imports: [AuthModule, DbModule, OracleModule],
controllers: [], controllers: [],
providers: [], providers: [],
}) })
@ -15,5 +16,18 @@ export class AppModule {
consumer consumer
.apply(OriginCheckMiddleware) .apply(OriginCheckMiddleware)
.forRoutes('*'); // Apply to all routes .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

@ -10,7 +10,7 @@ import { BadRequestException } from './exceptions/badRequest.exception';
async function bootstrap() { async function bootstrap() {
const app = await NestFactory.create(AppModule, { const app = await NestFactory.create(AppModule, {
logger:['error','warn'], logger: ['error', 'warn'],
cors: { cors: {
// origin: 'https://dev.alphaomegainfosys.com/', // origin: 'https://dev.alphaomegainfosys.com/',
origin: '*', origin: '*',
@ -20,6 +20,7 @@ async function bootstrap() {
}, },
}); });
function extractConstraints(validationErrors: ValidationError[]) { function extractConstraints(validationErrors: ValidationError[]) {
const constraints: { [key: string]: string }[] = []; const constraints: { [key: string]: string }[] = [];
@ -57,7 +58,7 @@ async function bootstrap() {
}) })
.filter(Boolean); .filter(Boolean);
throw new BadRequestException(`Bad Request${newResult[0].message !== 'Bad Request' ? " : "+newResult[0].message : ""}`); throw new BadRequestException(`Bad Request${newResult[0].message !== 'Bad Request' ? " : " + newResult[0].message : ""}`);
}; };
app.useGlobalPipes( app.useGlobalPipes(
@ -82,11 +83,8 @@ async function bootstrap() {
.addServer('http://localhost:3000', 'Development Server 1') .addServer('http://localhost:3000', 'Development Server 1')
.build(); .build();
const options: SwaggerDocumentOptions = { const options: SwaggerDocumentOptions = { autoTagControllers: false };
autoTagControllers: false, const documentFactory = () => SwaggerModule.createDocument(app, config, options);
};
const documentFactory = () =>
SwaggerModule.createDocument(app, config, options);
SwaggerModule.setup('api', app, documentFactory); SwaggerModule.setup('api', app, documentFactory);
await app.listen(process.env.PORT ?? 3000); 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({ @Module({
imports: [ imports: [
UserMaintenanceModule,
DbModule, DbModule,
CarnetApplicationModule,
UserMaintenanceModule,
HomePageModule, HomePageModule,
UscibManagedSpModule, UscibManagedSpModule,
ParamTableModule, ParamTableModule,
ManageFeeModule, ManageFeeModule,
ManageHoldersModule, ManageHoldersModule,
ManageClientsModule, ManageClientsModule,
CarnetApplicationModule,
], ],
providers: [], providers: [],
controllers: [], controllers: [],