Sharing Top Content from the Angular-sphere.

The better way to write an API service in Angular 4

  • The better way to write an API service in Angular 4It’s more than a year now since I’ve been coding Angular 2.
  • Angular is a full UI framework which promotes many best practices and coupled with Typescript makes it the perfect choice for the large-scale and easily scalable applications.During my continuous learning process, I created an organized and well-structured Angular API service which I refactored with each iteration.
  • You can download complete code here.The Custom HTTP provider:First, create a custom HTTP service ( http.service.ts) extending Angular’s inbuilt http service so that I can control all the http calls going through the application at one place and can do some pre and post processing at one go.Ok, in above…
  • This injector is the part of my AppModule and you can include it in AppModule like this:Base serviceNext, we will create base.service.ts which will have different methods corresponding to different http verb methods:As you can see, instead of using Angular’s inbuilt Http service I am using my custom HttpService which…
  • My server API always sends a response in the specific format (ServerResponse interface), which makes it easy for me to process and parse errors in case of any server errors.Server Response InterfaceError Handler ServiceLogin API CallFinally, this is how we can consume our API service to make a login API call.

It’s more than a year now since I’ve been coding Angular 2. Each day I learned something new. Angular is a full UI framework which promotes many best practices and coupled with Typescript makes it…

The better way to write an API service in Angular 4It’s more than a year now since I’ve been coding Angular 2. Each day I learned something new. Angular is a full UI framework which promotes many best practices and coupled with Typescript makes it the perfect choice for the large-scale and easily scalable applications.During my continuous learning process, I created an organized and well-structured Angular API service which I refactored with each iteration. So let’s see what exactly I built. You can download complete code here.The Custom HTTP provider:First, create a custom HTTP service ( http.service.ts) extending Angular’s inbuilt http service so that I can control all the http calls going through the application at one place and can do some pre and post processing at one go.Ok, in above HttpService as you can see I am processing all the API requests inside request() method before making the actual API call. Also, this service gives me the ability to handle all API server errors at one place (catchError()). I am also using something called injector to inject different services on the fly. This injector is the part of my AppModule and you can include it in AppModule like this:Base serviceNext, we will create base.service.ts which will have different methods corresponding to different http verb methods:As you can see, instead of using Angular’s inbuilt Http service I am using my custom HttpService which we just created above. My server API always sends a response in the specific format (ServerResponse interface), which…

The better way to write an API service in Angular 4

Comments are closed, but trackbacks and pingbacks are open.