0

Real quick, using nswag to generate a client service, trying to configure the api base url by using injection token.

The generated code by nswag:

All of these ideally should NOT be touched, otherwise you will lose the changes on every regeneration. Ideally the baseURL should come from Dependency Injection.

export const API_BASE_UR = new InjectionToken<string>('API_BASE_URL');

constructor(@Inject(HttpClient) http: HttpClient, @Optional() @Inject(API_BASE_URL) baseUrl?: string) {
    this.http = http;
    this.baseUrl = baseUrl !== undefined && baseUrl !== null ? baseUrl : "";
}

So, let's configure the dependency injection

//Import the service to get the API_BASE_URL
import {API_BASE_URL} from 'blablablá....';

// Configure provider section
providers: [
    {
      provide: API_BASE_URL,
      useValue: 'http://testUrl'
    }

Expected: baseUrl to have "http://testUrl"

Actual: baseUrl is null

What is missing here in order to configure this properly?

Yogurtu
  • 2,656
  • 3
  • 23
  • 23

1 Answers1

0

I found my error, this case mentioned is a module-federation site.

Module federation sites will have a module for each application, I was configuring the injection in the HOST only.

If you have MF site, remember to configure the same injection in al REMOTES sites.

Yogurtu
  • 2,656
  • 3
  • 23
  • 23