Skip to content

Commit cd5c719

Browse files
committed
Add http interceptor
1 parent 25cb11e commit cd5c719

12 files changed

+176
-19
lines changed

README.md

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,15 @@ Available spinners:
3131
- [Custom Configuration](#configuration)
3232
- [Input options](#input_options)
3333
- [Automatically show loader for router events](#router_events)
34+
- [Automatically show loader for http requests](#http_requests)
3435
- [Changelog](#changelog)
3536
- [Credits](#credits)
3637
- [License](#license)
3738

3839

3940
<a name="demo"></a>
4041

41-
## Demo <a name="demo"></a>
42+
## Demo
4243

4344
Live demo [here](https://ngx-ui-loader-demo.stackblitz.io).
4445

@@ -59,7 +60,7 @@ npm install --save ngx-ui-loader
5960

6061
<a name="getting_started"></a>
6162

62-
## Getting started <a name="getting_started"></a>
63+
## Getting started
6364

6465
Import the `NgxUiLoaderModule` in your root application module `AppModule`:
6566

@@ -91,7 +92,7 @@ export class AppModule { }
9192

9293
<a name="usage"></a>
9394

94-
## Usage <a name="usage"></a>
95+
## Usage
9596

9697
After importing the `NgxUiLoaderModule`, use `ngx-ui-loader` component in your root app template:
9798

@@ -134,7 +135,7 @@ export class AppComponent implements OnInit {
134135

135136
<a name="api_service"></a>
136137

137-
## API - NgxUiLoaderService <a name="api_service"></a>
138+
## API - NgxUiLoaderService
138139

139140
* `NgxUiLoaderService.start([id]='default')` Starts a foreground loader with progress bar. Users cannot interact with the page when the loader is showed.
140141
* `NgxUiLoaderService.stop([id]='default')` Stops a foreground loader with progress bar.
@@ -147,7 +148,7 @@ export class AppComponent implements OnInit {
147148

148149
<a name="configuration"></a>
149150

150-
## Custom Configuration <a name="configuration"></a>
151+
## Custom Configuration
151152

152153
You can configure `ngx-ui-loader` in the template as below:
153154

@@ -194,7 +195,7 @@ export class AppModule { }
194195

195196
<a name="input_options"></a>
196197

197-
## Input Options <a name="input_options"></a>
198+
## Input Options
198199

199200
| Attribute | Type | Required | Default | Description |
200201
| ---------------- | --------- | -------- | ------------------ | ----------------------------------------------------------------------------------------------- |
@@ -228,7 +229,7 @@ export class AppModule { }
228229

229230
<a name="router_events"></a>
230231

231-
## Automatically show loader for router events <a name="router_events"></a>
232+
## Automatically show loader for router events
232233

233234
If you want the loader to start automatically for navigating between your app routes:
234235

@@ -259,6 +260,41 @@ export class AppModule { }
259260

260261
```
261262

263+
<a name="http_requests"></a>
264+
265+
## Automatically show loader for Http requests
266+
267+
If you want the loader to start automatically for http requests:
268+
269+
```typescript
270+
271+
import { BrowserModule } from '@angular/platform-browser';
272+
import { NgModule } from '@angular/core';
273+
import { HttpClientModule } from '@angular/common/http';
274+
275+
import { AppComponent } from './app.component';
276+
277+
import { NgxUiLoaderModule, NgxUiLoaderHttpModule } from 'ngx-ui-loader';
278+
279+
@NgModule({
280+
declarations: [
281+
AppComponent,
282+
],
283+
imports: [
284+
BrowserModule,
285+
HttpClientModule,
286+
NgxUiLoaderModule, // import NgxUiLoaderModule
287+
NgxUiLoaderHttpModule, // import NgxUiLoaderHttpModule. By default, it will show background loader.
288+
// If you need to show foreground spinner, do as follow:
289+
// NgxUiLoaderHttpModule.forRoot({ showForeground: true })
290+
],
291+
providers: [],
292+
bootstrap: [AppComponent]
293+
})
294+
export class AppModule { }
295+
296+
```
297+
262298
NOTE: And in you root app template, do not forget to include `ngx-ui-loader` component:
263299

264300
```html
@@ -268,7 +304,10 @@ NOTE: And in you root app template, do not forget to include `ngx-ui-loader` com
268304

269305
<a name="changelog"></a>
270306

271-
## Changelog <a name="changelog"></a>
307+
## Changelog
308+
309+
### v.1.1.5
310+
* Be able to show loader automatically for http requests - Http interceptor
272311

273312
### v.1.1.2
274313
* Add more spinner types (total 22 spinners)
@@ -309,7 +348,7 @@ NOTE: And in you root app template, do not forget to include `ngx-ui-loader` com
309348

310349
<a name="credits"></a>
311350

312-
## Credits <a name="credits"></a>
351+
## Credits
313352

314353
* Tobias Ahlin - [CSS spinkit](https://github.com/tobiasahlin/SpinKit)
315354
* Daniel Cardoso - [Load awesome](https://github.com/danielcardoso/load-awesome)
@@ -318,7 +357,7 @@ NOTE: And in you root app template, do not forget to include `ngx-ui-loader` com
318357

319358
<a name="license"></a>
320359

321-
## License <a name="license"></a>
360+
## License
322361

323362
MIT &copy; [t-ho](mailto:toan.hmt@gmail.com)
324363

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngx-ui-loader-app",
3-
"version": "1.1.4",
3+
"version": "1.1.5",
44
"scripts": {
55
"ng": "ng",
66
"start": "ng serve",

projects/ngx-ui-loader/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "ngx-ui-loader",
33
"description": "An all-in-one and fully customizable loader/spinner for Angular 5+ (5+ and 6+ are tested) applications. It supports foreground, background spinner/loader and indicative progress bar.",
4-
"version": "1.1.4",
4+
"version": "1.1.5",
55
"repository": {
66
"type": "git",
77
"url": "https://github.com/t-ho/ngx-ui-loader.git"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { InjectionToken } from '@angular/core';
2+
import { NgxUiLoaderHttpConfig } from './ngx-ui-loader-http-config';
3+
4+
/**
5+
* Injection token for ngx-ui-loader-http configuration
6+
*/
7+
export const NGX_UI_LOADER_HTTP_CONFIG_TOKEN = new InjectionToken<NgxUiLoaderHttpConfig>('ngxUiLoaderHttpDefault.config');
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* The interface of ngx-ui-loader configuration
3+
*/
4+
export interface NgxUiLoaderHttpConfig {
5+
showForeground?: boolean;
6+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Injectable, Inject, Optional } from '@angular/core';
2+
import { HttpInterceptor, HttpEvent, HttpHandler, HttpRequest } from '@angular/common/http';
3+
import { Observable } from 'rxjs';
4+
import { finalize } from 'rxjs/operators';
5+
import { NgxUiLoaderService } from '../core/ngx-ui-loader.service';
6+
import { NGX_UI_LOADER_HTTP_CONFIG_TOKEN } from './ngx-ui-loader-http-config.token';
7+
import { NgxUiLoaderHttpConfig } from './ngx-ui-loader-http-config';
8+
9+
@Injectable()
10+
export class NgxUiLoaderHttpInterceptor implements HttpInterceptor {
11+
12+
private count: number;
13+
private defaultConfig: NgxUiLoaderHttpConfig;
14+
15+
constructor(@Inject(NGX_UI_LOADER_HTTP_CONFIG_TOKEN) @Optional() config: NgxUiLoaderHttpConfig,
16+
private ngxUiLoaderService: NgxUiLoaderService) {
17+
18+
this.count = 0;
19+
this.defaultConfig = {
20+
showForeground: false
21+
};
22+
23+
if (config) {
24+
Object.assign(this.defaultConfig, config);
25+
}
26+
}
27+
28+
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
29+
this.count++;
30+
if (this.defaultConfig.showForeground) {
31+
this.ngxUiLoaderService.start('$_http-loader');
32+
} else {
33+
this.ngxUiLoaderService.startBackground('$_http-loader');
34+
}
35+
36+
return next.handle(req).pipe(finalize(() => {
37+
this.count--;
38+
if (this.count === 0) {
39+
if (this.defaultConfig.showForeground) {
40+
this.ngxUiLoaderService.stop('$_http-loader');
41+
} else {
42+
this.ngxUiLoaderService.stopBackground('$_http-loader');
43+
}
44+
}
45+
}));
46+
}
47+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { NgModule, ModuleWithProviders } from '@angular/core';
2+
import { HTTP_INTERCEPTORS } from '@angular/common/http';
3+
4+
import { NgxUiLoaderHttpInterceptor } from './ngx-ui-loader-http.interceptor';
5+
import { NgxUiLoaderHttpConfig } from './ngx-ui-loader-http-config';
6+
import { NGX_UI_LOADER_HTTP_CONFIG_TOKEN } from './ngx-ui-loader-http-config.token';
7+
8+
@NgModule({
9+
providers: [
10+
{
11+
provide: HTTP_INTERCEPTORS,
12+
useClass: NgxUiLoaderHttpInterceptor,
13+
multi: true
14+
}
15+
],
16+
})
17+
export class NgxUiLoaderHttpModule {
18+
19+
/**
20+
* forRoot
21+
* @param httpConfig Configuration
22+
* @returns A module with its provider dependencies
23+
*/
24+
static forRoot(httpConfig: NgxUiLoaderHttpConfig): ModuleWithProviders {
25+
return {
26+
ngModule: NgxUiLoaderHttpModule,
27+
providers: [
28+
{
29+
provide: NGX_UI_LOADER_HTTP_CONFIG_TOKEN,
30+
useValue: httpConfig
31+
}
32+
]
33+
};
34+
}
35+
}

projects/ngx-ui-loader/src/lib/router/ngx-ui-loader-router.module.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ export class NgxUiLoaderRouterModule {
2626
}
2727

2828
constructor(
29-
@Optional() @Inject(NGX_UI_LOADER_ROUTER_CONFIG_TOKEN) defaultConfig: NgxUiLoaderRouterConfig,
29+
@Optional() @Inject(NGX_UI_LOADER_ROUTER_CONFIG_TOKEN) config: NgxUiLoaderRouterConfig,
3030
router: Router,
3131
ngxUiLoaderService: NgxUiLoaderService) {
3232

33-
let config: NgxUiLoaderRouterConfig = {
33+
let defaultConfig: NgxUiLoaderRouterConfig = {
3434
showForeground: true
3535
};
3636

37-
if (defaultConfig) {
38-
config = Object.assign(config, defaultConfig);
37+
if (config) {
38+
defaultConfig = Object.assign(defaultConfig, config);
3939
}
4040

4141
router.events

projects/ngx-ui-loader/src/public_api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ export { NgxUiLoaderConfig } from './lib/core/ngx-ui-loader-config';
88
export { SPINNER_TYPES, NGX_POSITIONS, PB_DIRECTIONS } from './lib/core/ngx-ui-loader.contants';
99
export { NgxUiLoaderRouterModule } from './lib/router/ngx-ui-loader-router.module';
1010
export { NgxUiLoaderRouterConfig } from './lib/router/ngx-ui-loader-router-config';
11+
export { NgxUiLoaderHttpModule } from './lib/http/ngx-ui-loader-http.module';
12+
export { NgxUiLoaderHttpConfig } from './lib/http/ngx-ui-loader-http-config';

src/app/app.module.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ import { BrowserModule } from '@angular/platform-browser';
22
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
33
import { NgModule } from '@angular/core';
44
import { RouterModule, Routes } from '@angular/router';
5+
import { HttpClientModule } from '@angular/common/http';
56

67
import { MatToolbarModule, MatButtonModule, MatIconModule, MatInputModule,
78
MatSelectModule, MatSlideToggleModule, MatSliderModule,
89
MatCheckboxModule, MatListModule } from '@angular/material';
910
import { FlexLayoutModule } from '@angular/flex-layout';
1011
import { ColorPickerModule } from 'ngx-color-picker';
1112

12-
import { NgxUiLoaderModule, NgxUiLoaderConfig, SPINNER_TYPES, NGX_POSITIONS, PB_DIRECTIONS, NgxUiLoaderRouterModule } from 'ngx-ui-loader';
13+
import { NgxUiLoaderModule, NgxUiLoaderConfig, SPINNER_TYPES, NGX_POSITIONS,
14+
PB_DIRECTIONS, NgxUiLoaderRouterModule, NgxUiLoaderHttpModule } from 'ngx-ui-loader';
1315

1416
import { AppComponent } from './app.component';
1517
import { FormsModule } from '@angular/forms';
@@ -69,9 +71,11 @@ const routes: Routes = [
6971
MatCheckboxModule,
7072
MatListModule,
7173
RouterModule.forRoot(routes),
74+
HttpClientModule,
7275

7376
NgxUiLoaderModule.forRoot(ngxUiLoaderConfig),
74-
NgxUiLoaderRouterModule // import this module for showing loader automatically when navigating between app routes
77+
NgxUiLoaderRouterModule, // import this module for showing loader automatically when navigating between app routes
78+
NgxUiLoaderHttpModule,
7579
],
7680
providers: [NgxUiLoaderDemoService],
7781
bootstrap: [AppComponent]

0 commit comments

Comments
 (0)