0

I am developing a Angular 2/Ionic 2 + JEE 7 project and I have a very specific scenario:

I have a httpClient layer that encapsulates every call to backend, there is a const named REST_BASE_PATHthat I would like to point to my localhost when in development environment and to a specific address when in production.

That said I would like to know what is the best and most automatic way of accomplish that..

Cœur
  • 37,241
  • 25
  • 195
  • 267
Marcos J.C Kichel
  • 6,887
  • 8
  • 38
  • 78

1 Answers1

1

You could define a custom request options to centralize this:

export class AppRequestOptions extends BaseRequestOptions {
  constructor(private @Inject('webApiBaseUrl') webApiBaseUrl:string) {
  }

  merge(options?:RequestOptionsArgs):RequestOptions {
    options.url = this.webApiBaseUrl + options.url;
    return super.merge(options);
  }
}

The webApiBaseUrl value you inject could be defined when bootstrapping your application:

bootstrap(AppComponent, [
  HTTP_PROVIDERS,
  provide('webApiBaseUrl', { useValue: 'https://bookapi.apispark.net/v1' })
]);

You need to update the variable when package your application with the value for the production environment.

Here is a question regarding packaging that could help you at this level:

Community
  • 1
  • 1
Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
  • I do not see how this solution shows clearly how the environment dev/production is solved asked by Marcos. – Elisabeth Dec 30 '17 at 22:37