-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added an interceptor for HTTP requests
- Loading branch information
Showing
5 changed files
with
99 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { FeatureToggleHeaderRequestInterceptor } from './feature-toggle-header-request.interceptor'; | ||
import { FEATURE_TOGGLE_MODULE_OPTIONS } from './feature-toggle.constants'; | ||
import { FeatureToggleService } from './feature-toggle.service'; | ||
|
||
describe('FeatureToggleHeaderRequestInterceptor', () => { | ||
let app: TestingModule; | ||
let featureToggleHeaderRequestInterceptor: FeatureToggleHeaderRequestInterceptor; | ||
beforeAll(async () => { | ||
app = await Test.createTestingModule({ | ||
providers: [ | ||
{ | ||
provide: FeatureToggleService, | ||
useClass: FeatureToggleService | ||
}, | ||
{ | ||
provide: FeatureToggleHeaderRequestInterceptor, | ||
useClass: FeatureToggleHeaderRequestInterceptor | ||
}, | ||
{ | ||
provide: FEATURE_TOGGLE_MODULE_OPTIONS, | ||
useValue: {} | ||
} | ||
] | ||
}).compile(); | ||
|
||
featureToggleHeaderRequestInterceptor = | ||
app.get<FeatureToggleHeaderRequestInterceptor>( | ||
FeatureToggleHeaderRequestInterceptor | ||
); | ||
}); | ||
it('should be defined', () => { | ||
expect(featureToggleHeaderRequestInterceptor).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { | ||
CallHandler, | ||
ExecutionContext, | ||
Injectable, | ||
NestInterceptor | ||
} from '@nestjs/common'; | ||
import { Observable } from 'rxjs'; | ||
import { FeatureToggleService } from './feature-toggle.service'; | ||
import { FeatureEntity } from './entities'; | ||
|
||
@Injectable() | ||
export class FeatureToggleHeaderRequestInterceptor implements NestInterceptor { | ||
constructor(private featureToggleService: FeatureToggleService) {} | ||
async intercept( | ||
context: ExecutionContext, | ||
next: CallHandler | ||
): Promise<Observable<any>> { | ||
const headers = context.switchToHttp().getRequest().headers; | ||
Object.keys(headers) | ||
.filter((key) => | ||
key.includes( | ||
this.featureToggleService.getHttpContextConfig() | ||
?.keywordToBeSearchedInHeader ?? 'feature_' | ||
) | ||
) | ||
.forEach(async (key) => { | ||
const feature = (await this.featureToggleService.getFeature( | ||
key.toUpperCase() | ||
)) as FeatureEntity; | ||
|
||
if ( | ||
typeof feature !== 'undefined' && | ||
feature.isAcceptHTTPRequestContext() | ||
) { | ||
feature.setValue(!!parseInt(headers[key] as string)); | ||
} | ||
}); | ||
|
||
return next.handle(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters