I have a configuration module with a factory that executes when the application is initialized (APP_INITIALIZER)
export function ConfigFactory(config: ConfigService) {
const res = () => config.load();
return res;
}
...
{
provide: APP_INITIALIZER,
useFactory: ConfigFactory,
deps: [ConfigService],
multi: true
}
Everything works ok if i print the data on a page/component.
The problem is when i try to use the configuration in a Service that gets called from an HttpInterceptor:
{
provide: HTTP_INTERCEPTORS,
useClass: TokenInterceptor,
multi: true
},
Interceptor:
constructor(private authenticationService: AuthenticationService) {}
intercept(request: HttpRequest<any> , next: HttpHandler): Observable<HttpEvent<any>> {
this.authenticationService.getAccessToken().subscribe((token: string) => { this.accessToken = token.toString(); });
this.authenticationService.getCurrentUser().subscribe((currentU: string) => { this.currentUser = currentU.toString(); });
if (this.accessToken) {
request = request.clone({
setHeaders: {
Authorization: `Bearer ${this.accessToken}`,
actor: this.currentUser,
// modulo: 'AltaActivoFijo'
}
});
}
return next.handle(request);
}
AuthenticationService:
export class AuthenticationService implements AuthService {
API_URL = this.configService.tokenServer;
....
The problem is that configService is Undefined.
Is there any way to postpone an interceptors initilization until APP_INITIALIZER is finished?
My goal is to have a unique build for all environments.
After trying different alternatives:
I couldn't find a good, maintainable and neat solution for the problem.