Skip to content

Commit

Permalink
feat(request filtering): now some url can be whitlisted
Browse files Browse the repository at this point in the history
  • Loading branch information
kKen94 committed Aug 7, 2020
1 parent 84dafb3 commit 82cf63c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,18 @@ Overlay works with ```position:absolute```, ```top:0```, ```left:0```
</div>
```

## Feature
## Features

- NgxProgress supports multiple requests. If during the bar progress another request is fired, bar will not reset and keep going until all requests are completed.
- NgxProgress can be instantiated multiple times. Only the deepest one will be displayed

#### Requests filtering

You can filter the HTTP requests that would like to be avoided by the interceptor by providing an array of regex patterns:
```
<ngx-progress [whitelist]="['auth', '[a-zA-Z]']"></ngx-progress>
```

## Configuration

### Customization
Expand Down
5 changes: 5 additions & 0 deletions projects/lib/src/lib/ngx-progress.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import { RegisterService } from './register.service';
styleUrls: ['ngx-progress.component.scss'],
})
export class NgxProgressComponent implements OnInit, OnDestroy {
/**
* String array with regex pattern to avoid loader on specified apis
*/
@Input() whitelist: string[] = [];
/**
* Color of spinner and progress bar.
*
Expand Down Expand Up @@ -62,6 +66,7 @@ export class NgxProgressComponent implements OnInit, OnDestroy {
}

ngOnInit(): void {
this.progressService.regexUrl = this.whitelist;
this.register.registerBar(this.self);
}
}
5 changes: 5 additions & 0 deletions projects/lib/src/lib/ngx-progress.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ export class NgxProgressInterceptor implements HttpInterceptor {
request: HttpRequest<any>,
next: HttpHandler,
): Observable<HttpEvent<any>> {
for (const regexUrlItem of this.progressService._regexUrl) {
if (regexUrlItem.test(request.url)) {
return next.handle(request);
}
}
this.progressService.start();
return next.handle(request).pipe(
map(event => {
Expand Down
10 changes: 10 additions & 0 deletions projects/lib/src/lib/ngx-progress.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import { BarService } from './bar/bar.service';

@Injectable({ providedIn: 'root' })
export class NgxProgressService {
// tslint:disable-next-line:variable-name
_regexUrl: RegExp[] = [];
/**
* Is done the mapping from string to regex on variable assignment
* @param patterns: the whitelist
*/
set regexUrl(patterns: string[]) {
this._regexUrl = patterns.map(p => new RegExp(p));
}

private readonly endEmitter = new Subject();
private readonly startEmitter = new Subject();

Expand Down

0 comments on commit 82cf63c

Please sign in to comment.