Comments (7)
@fulls1z3 I have the same or a similar problem.
Your HTTP interceptor will/might solve the problem when ConfigService is not yet initialized.
It does not solve the problem with a call to GetSettings (within mergeMap) with a resolved ConfigService as the HTTP request initiated by the ConfigHttpLoader has not yet returned data.
I have a service, in the constructor I receive an initialized ConfigService, but the call to GetSettings fails, as no config is yet available. (I think the OP has the same problem).
Is there a possibility to wait for the HTTP request to finish before calling getSettings or have getSettings to wait until the config is available ?
from ngx-config.
@danielmhair first of all thanks for suggesting this feature, and I really appreciate each of them 😄
I think I more or less understood your case.
Actually, any of those ConfigLoader
s are appended to APP_INITIALIZER
lifecycle hook of the app, so in any of your services you should be able to access the configuration through the scalar method getSettings()
.
If the app is already initalized, then there're no problems.
However, there are some race conditions (ex: server-side rendering, injecting HTTP interceptors, etc.) where the app has not been initalized yet. For such cases I suggest you to pass an injector to the service and delay the invocation of getSettings
method.
To make an example, and provide reference I'm hereby sharing a sample HTTP interceptor below:
// angular
import { Injectable, Injector } from '@angular/core';
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
// libs
import { Observable } from 'rxjs';
import { mergeMap } from 'rxjs/operators';
import { get, getOr } from 'lodash/fp';
import { ConfigService } from '@ngx-config/core';
import { Auth0Service } from '@ngx-auth/auth0';
@Injectable()
export class JwtInterceptor implements HttpInterceptor {
constructor(private readonly injector: Injector) {
}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return Observable.create((observer: any) => {
const item = this.injector.get(ConfigService);
observer.next(item);
observer.complete();
})
.pipe(
mergeMap((res: ConfigService) => {
const settings = res.getSettings('');
const baseUrl = getOr('', 'backend.baseUrl', settings);
if (get('auth', settings) && baseUrl && request.url.indexOf(baseUrl) >= 0) {
const auth = this.injector.get(Auth0Service);
if (auth && auth.accessToken)
request = request.clone({
setHeaders: {Authorization: `Bearer ${auth.accessToken}`}
});
}
return next.handle(request);
})
);
}
}
Don't forget to let me know about the results! 👍
from ngx-config.
Instead of ConfigHttpLoader i tried with ConfigStaticLoader.
The situation is the same, the Promise has not been resolved at the time the Service (used in an ngrx Effects) initializes.
from ngx-config.
I have the same problem... I cannot use angular-oauth2-oidc module initialization using JSON configurations.
Any news?
from ngx-config.
I managed to resolve this by using the loadSettings()
method of the loader
property in ConfigService
which returns a Promise. Don't know what possible side effects this might have but it worked for me.
from ngx-config.
@fulls1z3
Is somebody working on this? Similar issues #147
from ngx-config.
As a stop gap solution CustomConfigHttpLoader
import { ConfigHttpLoader } from '@ngx-config/http-loader';
import pDefer from 'p-defer';
export class CustomConfigHttpLoader extends ConfigHttpLoader {
readonly onLoadSettings = pDefer();
async loadSettings(): Promise<void> {
const result = await super.loadSettings();
this.onLoadSettings.resolve();
return result;
}
}
in other app initializer
(configService.loader as CustomConfigHttpLoader).onLoadSettings.promise.then(() => {
console.log('loaded');
}));
from ngx-config.
Related Issues (20)
- testing issue with config.json HOT 3
- when is it planned to get @ngx-config/fs-loader: 6.0.0 on npmjs.org? HOT 7
- does http loader still work with an XHR JSON response? HOT 1
- need event/promise when config is loaded HOT 1
- ConfigModule should be importable in every module HOT 2
- Breaking changes in 8.2.0
- Function calls are not supported in decorators but 'ConfigModule' was called. HOT 6
- Version for angular 10 HOT 3
- How can you pass a configuration setting to a configurable modules forRoot method during import
- Support for Angular 12 HOT 1
- Unrichable endpoint
- rxjs subscribe deprecated - will be removed in v8
- Support for Angular 14
- provide unit tests and coverage for `fs-loader`
- module resolution error during AoT compilation
- merge-loader doesn't merge HOT 1
- make use of Google's JS and Material2 style guides
- use compodoc to generate project docs
- caching issues HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ngx-config.