-
Notifications
You must be signed in to change notification settings - Fork 41
feat: reject guard #231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: reject guard #231
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import { SetMetadata } from '@nestjs/common'; | ||
|
|
||
| export const Reject = () => SetMetadata('reject', true); |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,31 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| CanActivate, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ExecutionContext, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| HttpException, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| HttpStatus, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Injectable, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } from '@nestjs/common'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { Reflector } from '@nestjs/core'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { I18nTranslations } from '../.generate/i18n.generated'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { I18nContext } from 'nestjs-i18n'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Injectable() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export class RejectRequestGuard implements CanActivate { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| constructor(private readonly reflector: Reflector) {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async canActivate(ctx: ExecutionContext): Promise<boolean> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const i18n = I18nContext.current<I18nTranslations>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const rejectRequest = this.reflector.getAllAndOverride('reject', [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ctx.getHandler(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ctx.getClass(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!rejectRequest) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+15
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add request context validation and logging The guard should validate the request context and log rejected requests for monitoring purposes. async canActivate(ctx: ExecutionContext): Promise<boolean> {
+ const request = ctx.switchToHttp().getRequest();
+ if (!request) {
+ throw new Error('Invalid execution context');
+ }
const i18n = I18nContext.current<I18nTranslations>();
+ if (!i18n) {
+ throw new Error('I18n context not available');
+ }
const rejectRequest = this.reflector.getAllAndOverride('reject', [
ctx.getHandler(),
ctx.getClass(),
]);
if (!rejectRequest) {
return true;
}
+ // Log rejected requests
+ console.log(`Request rejected: ${request.method} ${request.url}`);
throw new HttpException(📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new HttpException( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| i18n.t('exception.preview.reject-this-request', { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lang: I18nContext.current().lang, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| HttpStatus.BAD_REQUEST | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Use constant for metadata key
The 'reject' string should be imported from the decorator file to maintain consistency and avoid duplication.