Angular 4 http batch requests

Issue

To optimize my API calls, I would like to make batch requests using this module
https://github.com/jonsamwell/ngx-http-batcher#examples.
As per the example, the configuration is set in AppModule.

export function httpBatchConfigurationFactory() {
  return new HttpBatchConfigurationCollection([
    new HttpBatchConfiguration({
      rootEndpointUrl: "https://api.myservice.com",
      batchEndpointUrl: "https://api.myservice.com/$batch"
    })]);
};

And in the providers, I have set

{ provide: HttpBatchConfigurationCollection, useFactory: httpBatchConfigurationFactory },
    { provide: Http, useClass: HttpBatcher }

Everything works fine, when this configuration is static.
My application identifies the rootEnPointUrl, only after the user has logged into the application.

How can I add a new HttpBatchConfiguration to this HttpBatchConfigurationCollection, after user authentication?

Solution

Why not use an injectable service instead of providing a factory during module setup? So that you can change configuration whenever you like even after app initialised.

Answered By – Xinan

Answer Checked By – Mildred Charles (AngularFixing Admin)

Leave a Reply

Your email address will not be published.