added middleware
This commit is contained in:
parent
2b8da1bafa
commit
a400b6842d
@ -4,6 +4,7 @@ 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],
|
||||||
@ -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 },
|
||||||
|
// );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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 }[] = [];
|
||||||
|
|
||||||
@ -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);
|
||||||
|
|||||||
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({
|
@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: [],
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user