I would like to use a service in the export function. This gives me a HTTP_INTERCEPTOR error.
How could I use this service without causing the cyclic dependency error. The reason I would like to use a service here is to obtain dynamic values from a rest call.
Module:
export function MSALInstanceFactory(service: AzureService): IPublicClientApplication {
return new PublicClientApplication({
auth: service.getConfiguration(),
cache: {
cacheLocation: BrowserCacheLocation.LocalStorage,
storeAuthStateInCookie: isIE, // set to true for IE 11. Remove this line to use Angular Universal
}
});
}
#NgModule({
declarations: [
AzureComponent
],
exports: [
AzureComponent
],
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: MsalInterceptor,
multi: true
},
{
provide: MSAL_INSTANCE,
useFactory: MSALInstanceFactory,
deps: [AzureService]
},
AzureService,
IdentityManagerApiRestService,
]
})
export class AzureModule { }
Service:
#Injectable()
export class AzureService {
constructor(private identityManagerApiRestService: IdentityManagerApiRestService) { }
getConfiguration(): BrowserAuthOptions {
let options: BrowserAuthOptions;
this.identityManagerApiRestService.getAuthenticationConfiguration().subscribe(response => {
options = {
clientId: response.authenticationConfiguration.clientId,
authority: response.authenticationConfiguration.authority,
redirectUri: response.authenticationConfiguration.redirectUri,
}
});
return options;
}
}
Related
I need to do some unit test to a kafka implementation in my project with NestJS but I don't know how to do it.
I have a Service thats inject a Client Kafka
export class Service {
private static readonly logger = new Logger(ProducerService.name);
constructor(
#Inject('kafka-registrar') private client: ClientKafka,
private someOtherService: SomeOtherService,
) {}
Module
#Module({
imports: [
ClientsModule.register([
{
name: 'kafka-registrar',
transport: Transport.KAFKA,
options: {
client: {
clientId: 'hero',
brokers: ['localhost:9092'],
},
consumer: {
groupId: '1',
},
},
},
]),
SomeOtherService,
],
providers: [Service],
})
export class Module {}
Unit test
describe('Test Controller', () => {
let clientKafka: ClientKafka;
let someOtherService: SomeOtherService;
let producerService: ProducerService;
beforeEach(async () => {
const moduleRef = await Test.createTestingModule({
providers: [
ProducerService,
{
provide: SchemaRegistryService,
useValue: {
encodeWithId: jest.fn(),
},
},
{
provide: ClientKafka,
useValue: {
emit: jest.fn(),
},
},
],
}).compile()
clientKafka = moduleRef.get(ClientKafka);
schemaRegistryService = moduleRef.get(SchemaRegistryService);
producerService = moduleRef.get(ProducerService);
});
The project give me this error:
Error: Nest can't resolve dependencies of the ProducerService (?, SchemaRegistryService). Please make sure that the argument kafka-registrar at index [0] is available in the RootTestModule context.
Potential solutions:
- If kafka-registrar is a provider, is it part of the current RootTestModule?
- If kafka-registrar is exported from a separate #Module, is that module imported within RootTestModule?
#Module({
imports: [ /* the Module containing kafka-registrar */ ]
})
I don't know how to resolve this in NestJS. For example in Java,I belive that this can be with #Mock ClientKafka clientKafka bit I dont have any other experience with NestJS... Please helpme! :)
In your test file, you can change provide: ClientKafka to this provide: 'kafka-registrar'.
const moduleRef = await Test.createTestingModule({
providers: [
ProducerService,
{
provide: SchemaRegistryService,
useValue: {
encodeWithId: jest.fn(),
},
},
{
provide: 'kafka-registrar',
useValue: {
emit: jest.fn(),
},
},
],
}).compile()
I am trying to use an exception filter in my NestJS app. I have to translate my exception message into another language based on the request value. I have two languages file en and fr.
I have initialized i18N in my app.module.ts as per the following:
#Module({
imports: [
I18nModule.forRoot({
fallbackLanguage: 'en',
parser: I18nJsonParser,
parserOptions: {
path: path.join(__dirname, '/i18n/'),
},
resolvers: [
PathResolver,
{ use: QueryResolver, options: ['lang', 'locale', 'l'] },
AcceptLanguageResolver
]
}),
],
controllers: [AppController],
providers: [AppService,
{
provide: APP_FILTER,
useClass: NotFoundExceptionFilter,
},
{
provide: APP_FILTER,
useClass: HttpExceptionFilter,
}
],
})
export class AppModule { }
My Exception filter class looks like this:
#Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter<HttpException> {
constructor(private readonly i18n: I18nService) { }
async catch(exception: HttpException, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse();
const request = ctx.getRequest();
const statusCode = exception.getStatus();
await this.i18n.translate('message.Unauthorized', { lang: 'fr' }).then(message => {
console.log('message -> ', message);
response.status(403).send({
status: 6,
message: message,
data: null
});
})
}
}
I am throwing an exception from the LocalAuthGuard file:
#Injectable()
export class LocalAuthGuard implements CanActivate {
canActivate(context: ExecutionContext,): boolean | Promise<boolean> | Observable<boolean> {
const request = context.switchToHttp().getRequest().body;
if(isEmpty(request.authkey)){
return false;
}else{
return true;
}
}
}
I have just put here my sample code from the project. When I run this project by some specific URL, I am not getting messages in the specific language. I am getting the following output in my console log.
message -> message.Unauthorized
It should return the message in fr language.
Can anybody help me, where am I going wrong?
I forgot to add the path in nest-cli.json. If we put the path in that file as below then the above code perfectly works fine.
{
"collection": "#nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"assets": ["i18n/**/*","ssl/**/*"]
}
}
I have the following IP and Ports but I want to carry this with environment variables so that they can be edited from there
import { ClientsModuleOptions, Transport } from "#nestjs/microservices"
export const GatewayOptions: ClientsModuleOptions = [
{
name: 'MICRO-ADMIN',
transport: Transport.TCP,
options: {
host: '127.20.20.2',
port: 4000,
},
},
{
name: 'MICRO-DEV',
transport: Transport.TCP,
options: {
host: '127.30.30.3',
port: 5000,
},
},
];
I import this configuration to the module.
import { Module } from '#nestjs/common';
import { ClientsModule } from '#nestjs/microservices';
import { GatewayOptions } from 'src/utils/gateway/gateway';
import { AuthModule } from './../auth/auth.module';
import { CategoryModule } from './../category/category.module';
import { GameController } from './game.controller';
import { GameService } from './game.service';
#Module({
imports: [
AuthModule,
CategoryModule,
ClientsModule.register(GatewayOptions)
],
controllers: [GameController],
providers: [GameService],
exports: [GameService],
})
export class GameModule {}
You need to use register async to use ConfigService
Follow the documentation: https://docs.nestjs.com/microservices/basics#client
#Module({
providers: [
{
provide: 'MATH_SERVICE',
useFactory: (configService: ConfigService) => {
const mathSvcOptions = configService.getMathSvcOptions();
return ClientProxyFactory.create(mathSvcOptions);
},
inject: [ConfigService],
}
]
...
})
Here is how you can configure the ConfigService: https://docs.nestjs.com/techniques/configuration#configuration
I am trying to do something fancy: a DynamicModule that chooses a Controller/Providers depending on a provider. The provider reads a config in a DB at onModuleInit, so there is no way to hard-code its values.
It is similar to this issue: https://github.com/nestjs/nest/issues/601 which was never answered...
I have tried a few approaches and the closest I have got is this:
export class FancyModule implements DynamicModule {
public module: Type<FancyModule>;
public controllers: Type<unknown>[];
public providers: Provider<unknown>[];
constructor (
#Inject(StartupConfigService) private readonly startup: StartupConfigService,
) {
this.module = FancyModule;
if (startup.featureFlagEnabled) {
this.controllers = [ FancyFeatureController ];
this.providers = [ FancyFeatureService ];
} else {
this.controllers = [ ErrorController ];
this.providers = [];
}
}
}
Second approach:
app.module.ts
#Module({
imports: [
StartupConfigModule.register(startupConfigOption),
FancyModule.registerAsync({
imports: [StartupConfigModule],
useFactory: function (startupConfigService: StartupConfigService) {
return startupConfigService.config
},
inject: [StartupConfigService],
}),
],
})
fancy.module.ts
export class FancyModule {
static registerAsync (config: IAppConfig): DynamicModule {
if (config.featureFlagEnabled) {
return {
module: FancyModule,
controllers: [ FancyFeatureController ],
providers: [ FancyFeatureService ],
};
} else {
return {
module: FancyModule,
controllers: [ ],
providers: [ ],
};
}
}
}
This injects as expected but the controllers and providers are never spun up. Is the meta-data evaluated before the constructor? Is that a bug?
It turns out what I'm trying to do is, in fact, impossible. The evaluation of the FancyModule.registerAsync() method happens before the onModuleInit od the StartupconfigService (which sets the feature flag).
A DynamicModule cannot use the contents of an instantiated provider to register() itself.
Other reading:
Fundamentals - Dynamic Modules
Advanced - Dynamic Modules
After configured cache globally like the docs, the CacheInterceptor throws an error if i use it outside the app.module.
app.module.ts
const cacheConfig = {
store: redisStore,
host: 'localhost',
port: 6379
}
#Module({
imports: [
CacheModule.register(cacheConfig),
CustomerModule,
],
providers: [
{
provide: APP_INTERCEPTOR,
useClass: CacheInterceptor
}
]
})
export class AppModule {}
customer.module.ts
#Module({
imports: [TypeOrmModule.forFeature([CustomerRepository]), TypeOrmModule.forFeature([User])],
controllers: [CustomerController]
})
export class CustomerModule {}
customer.controller.ts
#Controller('customer')
export class CustomerController {
constructor(
#InjectRepository(CustomerRepository) private customerRepository: CustomerRepository,
#InjectRepository(User) private userRepository: Repository<User>
) {}
#Get()
#UseInterceptors(CacheInterceptor)
async get(): Promise<any> {
const user = await this.userRepository.findOne({ where: { id: 1 }, relations: ['customer'] })
console.log(user.customer.name)
const customer = await this.customerRepository.findOne({ where: { id: 1 }, select: ['id', 'name'] })
return { customer: customer.name, email: user.email }
}
}
I would like using the CacheInterceptor along any modules without import the CacheModule each one.
Nest can't resolve dependencies of the APP_INTERCEPTOR (UUID: 6aa42c77-1bac-4098-b217-1b01eb268240) (?, Reflector). Please make sure that the argument at index [0] is available in the CustomerModule context.
If you have { provide: APP_INTERCEPTOR, useClass: CacheInterceptor } you don't need to add in the #UseInterceptors() decorator in your controller. You should have the CahceInterceptor working by default with the rest of the set up