Skip to content

🍡 Smart angular HTTP interceptor - Intercepts automagically HTTP requests and shows a spinkit spinner / loader / progress bar

License

Notifications You must be signed in to change notification settings

mpalourdio/ng-http-loader

Repository files navigation

ng-http-loader

Build Status Coverage Status npm npm

Changelog

Please read the changelog

Contributing

Use the fork, Luke. PR without tests will likely not be merged.

Installation

To install this library, run:

$ npm install ng-http-loader --save / yarn add ng-http-loader

What does it do ?

This package provides an HTTP Interceptor, and a spinner component (All from SpinKit at the moment). The HTTP interceptor listens to all HTTP requests and shows a spinner / loader indicator during pending http requests.

Angular 4 / Angular 5 / Angular 6

The latest compatible version with angular 4 is 0.3.4. If you want to use Angular 5, use versions 0.4.0 and above.

The latest compatible version with angular 5 is version 0.9.1.

Versions 1.0.0+ and 2.0.0+ are angular 6 / RxJS 6 compatible only.

If you experience errors like below, please double check the version you use.

ERROR in Error: Metadata version mismatch for module [...]/angular/node_modules/ng-http-loader/ng-http-loader.module.d.ts, found version x, expected y [...]

Requirements - HttpClientModule

Performing http requests with the HttpClientModule API is mandatory. Otherwise,the spinner will not be fired at all.

See this blog post for an HttpClientModule introduction.

Usage

From your Angular AppModule:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
[...]
import { AppComponent } from './app.component';
import { HttpClientModule } from '@angular/common/http'; <============
import { NgHttpLoaderModule } from 'ng-http-loader'; <============

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule, <============ (Perform http requests with this module)
    NgHttpLoaderModule, <============
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

In your app.component.html, simply add:

<spinner></spinner>

Customizing the spinner

You can customize the background-color, the spinner type, the debounce delay (ie. after how many milliseconds the spinner will be visible, if needed), the minimum duration (ie. how many milliseconds should the spinner be visible at least):

<spinner 
    [backgroundColor]="'#ff0000'"
    [spinner]="spinkit.skWave"
    [debounceDelay]="100"
    [minDuration]="300">
</spinner>

To use this syntax, you must reference the Spinkit const as a public property in your app.component.ts:

import { Spinkit } from 'ng-http-loader'; <============

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css'],
})
export class AppComponent {
    public spinkit = Spinkit; <============
    [...]
}

The different spinners available are referenced in this class.

Otherwise, you can simply reference the chosen spinner as a simple string:

<spinner backgroundColor="#ff0000" spinner="sk-wave"></spinner>

Defining your own spinner

You can define your own loader component in place of the built-in ones. The needed steps are:

  • Create your component
  • Add it to the entryComponent definition in your module definition
  • Reference your component in a public property in your app.component.ts
  • Reference the property in the spinner component like this:
<spinner [entryComponent]="myAwesomeComponent"></spinner>

You can find some short examples here and here.

Requests filtering by URL or by HTTP method

You can filter the http requests that shouldn't be caught by the interceptor by providing an array of regex patterns:

<spinner [filteredUrlPatterns]="['\\d', '[a-zA-Z]', 'my-api']"></spinner>

You can also filter the http requests by providing an array of HTTP methods (case insensitive):

<spinner [filteredMethods]="['gEt', 'POST', 'PuT']"></spinner>

Manually show and hide the spinner

You can manually show and hide the spinner component if needed. You must use the SpinnerVisibilityService for this purpose.

Sometimes, when manually showing the spinner, an http request could be performed in background, and when finished, the spinner would automagically disappear.

For this reason, when calling SpinnerVisibilityService#show(), it prevents the http interceptor from being triggered unless you explicitly call SpinnerVisibilityService#hide().

import { SpinnerVisibilityService } from 'ng-http-loader';

@Component({
    selector: 'my-component',
    templateUrl: './my.component.html',
    styleUrls: ['./my.component.css'],
})
export class MyComponent {

    constructor(private spinner: SpinnerVisibilityService) {
        // show the spinner
        spinner.show();
        //////////////
        // http requests performed between won't have any side effect on the spinner
        /////////////
        // hide the spinner
        spinner.hide();
    }
}

Misc

Each Spinkit component defined in SPINKIT_COMPONENTS can be used independently.

Credits

Tobias Ahlin, the awesome creator of SpinKit.
David Herges, the awesome developer of ng-packagr.