GithubHelp home page GithubHelp logo

allow returning promise about ngx-config HOT 7 CLOSED

fulls1z3 avatar fulls1z3 commented on July 20, 2024
allow returning promise

from ngx-config.

Comments (7)

FritzHerbers avatar FritzHerbers commented on July 20, 2024 1

@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.

fulls1z3 avatar fulls1z3 commented on July 20, 2024

@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 ConfigLoaders 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.

FritzHerbers avatar FritzHerbers commented on July 20, 2024

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.

salem84 avatar salem84 commented on July 20, 2024

I have the same problem... I cannot use angular-oauth2-oidc module initialization using JSON configurations.
Any news?

from ngx-config.

djmcgrath101 avatar djmcgrath101 commented on July 20, 2024

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.

unlight avatar unlight commented on July 20, 2024

@fulls1z3
Is somebody working on this? Similar issues #147

from ngx-config.

unlight avatar unlight commented on July 20, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.