Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions src/interceptors/i18n-language.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ export class I18nLanguageInterceptor implements NestInterceptor {
ctx.i18nContext = new I18nContext(ctx.i18nLang, this.i18nService);

if (!this.i18nOptions.skipAsyncHook) {
return I18nContext.createAsync(ctx.i18nContext, async (error) => {
if (error) {
throw error;
}
return next.handle();
return new Observable((observer) => {
I18nContext.createAsync(ctx.i18nContext, async (error) => {
if (error) {
throw error;
}
return next.handle().subscribe(observer);
});
});
}
}
Expand Down
18 changes: 13 additions & 5 deletions tests/app/controllers/hello.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Render,
UseFilters,
UseGuards,
UseInterceptors,
} from '@nestjs/common';
import { GrpcMethod, Payload } from '@nestjs/microservices';
import {
Expand All @@ -21,7 +22,8 @@ import { CreateUserDto } from '../dto/create-user.dto';
import { TestException, TestExceptionFilter } from '../filter/test.filter';
import { TestGuard } from '../guards/test.guard';
import { Hero, HeroById } from '../interfaces/hero.interface';
import {exampleErrorFormatter, exampleResponseBodyFormatter} from '../examples/example.functions';
import { exampleErrorFormatter, exampleResponseBodyFormatter } from '../examples/example.functions';
import { TestInterceptor } from '../interceptors/test.interceptor';

@Controller('hello')
@UseFilters(new TestExceptionFilter())
Expand Down Expand Up @@ -87,6 +89,12 @@ export class HelloController {
return I18nContext.current<I18nTranslations>().translate('test.HELLO');
}

@Get('/request-scope/additional-interceptor')
@UseInterceptors(TestInterceptor)
helloRequestScopeAdditionalInterceptor(): any {
return I18nContext.current<I18nTranslations>().translate('test.HELLO');
}

@Get('/request-scope/typed')
helloRequestScopeTyped(): string {
return I18nContext.current<I18nTranslations>().translate('test.HELLO');
Expand Down Expand Up @@ -189,10 +197,10 @@ export class HelloController {

@Post('/validation-custom-response-body-formatter')
@UseFilters(
new I18nValidationExceptionFilter({
responseBodyFormatter: exampleResponseBodyFormatter,
errorFormatter: exampleErrorFormatter,
}),
new I18nValidationExceptionFilter({
responseBodyFormatter: exampleResponseBodyFormatter,
errorFormatter: exampleErrorFormatter,
}),
)
validationResponseBodyFormatter(@Body() createUserDto: CreateUserDto): any {
return 'This action adds a new user';
Expand Down
14 changes: 14 additions & 0 deletions tests/app/interceptors/test.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {
CallHandler,
ExecutionContext,
Injectable,
NestInterceptor,
} from '@nestjs/common';
import { Observable } from 'rxjs';

@Injectable()
export class TestInterceptor implements NestInterceptor {
intercept(_: ExecutionContext, next: CallHandler): Observable<unknown> {
return next.handle();
}
}
8 changes: 8 additions & 0 deletions tests/i18n-disable-middleware.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ describe('i18n module e2e no middleware', () => {
await request(app.getHttpServer()).get('/hello/guard?lang=nl').expect(500);
});

it(`/GET hello/request-scope/additional-interceptor should return translation`, () => {
return request(app.getHttpServer())
.get('/hello/request-scope/additional-interceptor')
.set('accept-language', 'fr-FR')
.expect(200)
.expect('Bonjour');
});

afterAll(async () => {
await app.close();
});
Expand Down