Skip to content

Commit b480e8a

Browse files
authored
Merge pull request #74 from mpalourdio/filter_methods
Filter requests by HTTP method - Fixes #74
2 parents 0332cc0 + 80c833f commit b480e8a

File tree

8 files changed

+247
-195
lines changed

8 files changed

+247
-195
lines changed

CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
# Changelog
22

3+
## v2.2.0
4+
5+
This release adds the possibility to filter http requests that should not be handled by the interceptor by providing an array of HTTP methods to the component's ``filteredMethods`` property.
6+
37
## v2.1.0
48

59
This release introduces the **minimum duration** option. It gives the possibility to force a minimum duration during which the spinner should be visible.
6-
You can mix this parameter with the **debounce delay** option :
10+
You can mix this parameter with the **debounce delay** option:
711

812
```xml
913
<spinner
@@ -97,7 +101,7 @@ The responsible ``Subject`` has been replaced by a ``ReplaySubject``.
97101
The module is now splitted in sub-modules for more convenience. See [usage](https://github.com/mpalourdio/ng-http-loader#usage).
98102
It's an **opt-in** feature. The "old" module import method, by simply declaring ``NgHttpLoaderModule``, is still fully supported.
99103

100-
**BC break** : paths of components and services have changed.
104+
**BC break**: paths of components and services have changed.
101105
- Components are now located in the ``components`` folders.
102106
- Services are now located in the ``services`` folders.
103107

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ import { NgHttpLoaderModule } from 'ng-http-loader'; <============
7272
export class AppModule { }
7373
```
7474

75-
In your app.component.html, simply add :
75+
In your app.component.html, simply add:
7676
```xml
7777
<spinner></spinner>
7878
```
@@ -120,20 +120,25 @@ You can define your own loader component in place of the built-in ones. The need
120120
- Create your component
121121
- Add it to the [entryComponent](https://angular.io/guide/ngmodule-faq#what-is-an-entry-component) definition in your module definition
122122
- Reference your component in a public property in your ``app.component.ts``
123-
- Reference the property in the spinner component like this :
123+
- Reference the property in the spinner component like this:
124124
```xml
125125
<spinner [entryComponent]="myAwesomeComponent"></spinner>
126126
```
127127

128128
You can find some short examples [here](https://gist.github.com/mpalourdio/2c0bec03d610b24ff49db649fbb69a48) and [here](https://gist.github.com/mpalourdio/e05b4495de2abeeecfcf92d70e4ef93e).
129129

130-
## Requests filtering
130+
## Requests filtering by URL or by HTTP method
131131

132-
You can also filter the http requests that shouldn't be caught by the interceptor by providing **an array of regex patterns**:
132+
You can filter the http requests that shouldn't be caught by the interceptor by providing **an array of regex patterns**:
133133
```xml
134134
<spinner [filteredUrlPatterns]="['\\d', '[a-zA-Z]', 'my-api']"></spinner>
135135
```
136136

137+
You can also filter the http requests by providing **an array of HTTP methods** (case insensitive):
138+
```xml
139+
<spinner [filteredMethods]="['gEt', 'POST', 'PuT']"></spinner>
140+
```
141+
137142
## Manually show and hide the spinner
138143

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ng-http-loader",
3-
"version": "2.1.0",
3+
"version": "2.2.0",
44
"scripts": {
55
"ng": "ng",
66
"build": "ng build",

src/lib/components/spinner/spinner.component.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ export class SpinnerComponent implements OnDestroy, OnInit {
3232
@Input()
3333
public filteredUrlPatterns: string[] = [];
3434
@Input()
35+
public filteredMethods: string[] = [];
36+
@Input()
3537
public debounceDelay = 0;
3638
@Input()
3739
public minDuration = 0;
@@ -61,6 +63,11 @@ export class SpinnerComponent implements OnDestroy, OnInit {
6163
this.pendingInterceptorService.filteredUrlPatterns.push(new RegExp(e));
6264
});
6365
}
66+
67+
if (!(this.filteredMethods instanceof Array)) {
68+
throw new TypeError('`filteredMethods` must be an array.');
69+
}
70+
this.pendingInterceptorService.filteredMethods = this.filteredMethods;
6471
}
6572

6673
ngOnDestroy(): void {

src/lib/services/pending-interceptor.service.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export class PendingInterceptorService implements HttpInterceptor {
1919
private _pendingRequests = 0;
2020
private _pendingRequestsStatus: ReplaySubject<boolean> = new ReplaySubject<boolean>(1);
2121
private _filteredUrlPatterns: RegExp[] = [];
22+
private _filteredMethods: string[] = [];
2223
private _forceByPass: boolean;
2324

2425
/** @deprecated Deprecated in favor of pendingRequestsStatus$ */
@@ -38,6 +39,10 @@ export class PendingInterceptorService implements HttpInterceptor {
3839
return this._filteredUrlPatterns;
3940
}
4041

42+
set filteredMethods(httpMethods: string[]) {
43+
this._filteredMethods = httpMethods;
44+
}
45+
4146
set forceByPass(value: boolean) {
4247
this._forceByPass = value;
4348
}
@@ -48,8 +53,16 @@ export class PendingInterceptorService implements HttpInterceptor {
4853
});
4954
}
5055

56+
private shouldBypassMethod(req: HttpRequest<any>): boolean {
57+
return this._filteredMethods.some(e => {
58+
return e.toUpperCase() === req.method.toUpperCase();
59+
});
60+
}
61+
5162
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
52-
const shouldBypass = this.shouldBypassUrl(req.urlWithParams) || this._forceByPass;
63+
const shouldBypass = this.shouldBypassUrl(req.urlWithParams)
64+
|| this.shouldBypassMethod(req)
65+
|| this._forceByPass;
5366

5467
if (!shouldBypass) {
5568
this._pendingRequests++;

0 commit comments

Comments
 (0)