added middleware
This commit is contained in:
parent
2b8da1bafa
commit
a400b6842d
@ -4,9 +4,10 @@ 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],
|
||||
imports: [AuthModule, DbModule, OracleModule],
|
||||
controllers: [],
|
||||
providers: [],
|
||||
})
|
||||
@ -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 },
|
||||
// );
|
||||
}
|
||||
}
|
||||
|
||||
12
src/main.ts
12
src/main.ts
@ -10,7 +10,7 @@ import { BadRequestException } from './exceptions/badRequest.exception';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule, {
|
||||
logger:['error','warn'],
|
||||
logger: ['error', 'warn'],
|
||||
cors: {
|
||||
// origin: 'https://dev.alphaomegainfosys.com/',
|
||||
origin: '*',
|
||||
@ -20,6 +20,7 @@ async function bootstrap() {
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
function extractConstraints(validationErrors: ValidationError[]) {
|
||||
const constraints: { [key: string]: string }[] = [];
|
||||
|
||||
@ -57,7 +58,7 @@ async function bootstrap() {
|
||||
})
|
||||
.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(
|
||||
@ -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);
|
||||
|
||||
30
src/middleware/reqBodyKeysToUppercase.middleware.ts
Normal file
30
src/middleware/reqBodyKeysToUppercase.middleware.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
@ -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: [],
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user