Skip to content

Commit

Permalink
Add sample middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Vishwas1 committed Jan 15, 2023
1 parent 4e73e7c commit 9a6b7b3
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 14 deletions.
19 changes: 16 additions & 3 deletions src/app-auth/app-auth.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import { Module } from '@nestjs/common';
import { MiddlewareConsumer, Module, NestModule, RequestMethod } from '@nestjs/common';
import { AppAuthService } from './services/app-auth/app-auth.service';
import { AppAuthController } from './controllers/app-auth/app-auth.controller';

import { ValidateHeadersMiddleware } from './middlewares/validate-headers/validate-headers.middleware';
@Module({
providers: [AppAuthService],
controllers: [AppAuthController]
})
export class AppAuthModule {}
export class AppAuthModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
//// Appy middleware on all routes
consumer
.apply(ValidateHeadersMiddleware)
.forRoutes(AppAuthController);

//// or Apply on specific routes
// consumer.apply(ValidateHeadersMiddleware).forRoutes({
// path: '/app-auth/register',
// method: RequestMethod.POST,
// })
}
}
2 changes: 1 addition & 1 deletion src/app-auth/controllers/app-auth/app-auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Controller, Get, ValidationPipe, Post, UsePipes, Body, Req, Res, HttpStatus } from '@nestjs/common';
import { CreateAppDto } from 'src/app-auth/dtos/CreateApp.dto';
import { AppAuthService } from 'src/app-auth/services/app-auth/app-auth.service';
import { Request, Response } from 'express'

@Controller('app-auth')
export class AppAuthController {
constructor(private readonly appAuthService: AppAuthService){}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ValidateHeadersMiddleware } from './validate-headers.middleware';

describe('ValidateHeadersMiddleware', () => {
it('should be defined', () => {
expect(new ValidateHeadersMiddleware()).toBeDefined();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Injectable, NestMiddleware } from '@nestjs/common';

@Injectable()
export class ValidateHeadersMiddleware implements NestMiddleware {
use(req: any, res: any, next: () => void) {
console.log('Dummy ValidateHeadersMiddleware');
next();
}
}
11 changes: 1 addition & 10 deletions src/app-auth/services/app-auth/app-auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,7 @@ import { IApp } from '../../types/App.types';
import { CreateAppDto } from '../../dtos/CreateApp.dto';
@Injectable()
export class AppAuthService {
private apps = [
{
userId: "user123",
appName: "app1",
appId: "app-1",
appSecret: "app-secret-1",
edvId: "edv-1",
kmsId: "kms-1",
}
]
private apps: IApp[] = []

createAnApp(createAppDto: CreateAppDto){
const newApp = {
Expand Down

0 comments on commit 9a6b7b3

Please sign in to comment.