In my application I have services (that are making calls to the rest service) that are inherited from the custom base service that is defining common methods and properties that are used across all other services. One of such parameters was base url for my rest service which was using a variable under environment. However now came in requirement that makes this url coming from the config file. So I would have to get it using httpclient, which presents an issue that now at the time when actual service is trying to use base url it is not yet resolved. Is there a way how to wait till that call resolves? My code looks like:
base-service.service.ts:
export class BaseService{
protected apiUrlBase: string;
constructor(protected _http: HttpClient) {
// some logic
this.getConfigData()
.subscribe(
x => {
this.apiUrlBase = x.apiUrlBase;
}
);
}
getConfigData(): Observable<any> {
return this._http.get(
'../../assets/config.json'
)
.do(x => {
})
.catch(error => {
alert('Unable to read configuration file:' + JSON.stringify(error));
return Observable.throw(error);
});
}
}
some-data.service.ts:
@Injectable()
export class TestService extends BaseApiService {
private get _someDataServiceUrl(): string {
return this.apiUrlBase + '/api/v1/somedata';
}
constructor(_http: HttpClient) {
super(_http);
}
getSomeData(): Observable<ISomeData[]> {
return this._http.get<ISomeData[]>(this._someDataServiceUrl)
.do(this.handleSuccessResponse)
.catch(this.handleErrorResponse);
}
}
Any suggestions on how I can wait in my service till apiUrl is getting resolved, or what should I do to be able to use my baseservice as well as use the data that child services are relying on to be resolved till they use it?