Skip to content

Commit be90a90

Browse files
committed
Ability to specify requests matcher
1 parent 7c04543 commit be90a90

5 files changed

+116
-59
lines changed

dist/angular-loading-overlay-http-interceptor.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,20 @@
7474
var BsLoadingOverlayHttpInterceptorInterceptor = (function () {
7575
function BsLoadingOverlayHttpInterceptorInterceptor(config, bsLoadingOverlayService) {
7676
var _this = this;
77+
if (config === void 0) { config = {}; }
7778
this.config = config;
7879
this.bsLoadingOverlayService = bsLoadingOverlayService;
7980
this.requestsCount = 0;
80-
this.request = function (config) {
81-
_this.onRequest();
82-
return config;
81+
this.request = function (requestConfig) {
82+
if (_this.config.requestsMatcher) {
83+
if (_this.config.requestsMatcher(requestConfig)) {
84+
_this.onRequest();
85+
}
86+
}
87+
else {
88+
_this.onRequest();
89+
}
90+
return requestConfig;
8391
};
8492
this.requestError = function (rejection) {
8593
_this.onResponse();

source/BsLoadingOverlayHttpInterceptorFactory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import BsLoadingOverlayHttpInterceptorInterceptor from './BsLoadingOverlayHttpInterceptorInterceptor';
2-
import IBsLoadingOverlayOptions from 'angular-loading-overlay/source/IBsLoadingOverlayOptions';
2+
import IBsLoadingOverlayHttpInterceptorOptions from './IBsLoadingOverlayHttpInterceptorOptions';
33
import {BsLoadingOverlayService} from 'angular-loading-overlay/source/BsLoadingOverlayService';
44

55
const bsLoadingOverlayHttpInterceptorFactoryFactory: ng.IHttpInterceptorFactory =
66
(bsLoadingOverlayService: BsLoadingOverlayService) =>
7-
(config: IBsLoadingOverlayOptions) =>
7+
(config: IBsLoadingOverlayHttpInterceptorOptions) =>
88
new BsLoadingOverlayHttpInterceptorInterceptor(config, bsLoadingOverlayService);
99

1010
bsLoadingOverlayHttpInterceptorFactoryFactory.$inject = ['bsLoadingOverlayService'];

source/BsLoadingOverlayHttpInterceptorInterceptor.spec.ts

Lines changed: 84 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import BsLoadingOverlayHttpInterceptor from './BsLoadingOverlayHttpInterceptorInterceptor';
22
import {BsLoadingOverlayService} from 'angular-loading-overlay/source/BsLoadingOverlayService';
3-
import IBsLoadingOverlayOptions from 'angular-loading-overlay/source/IBsLoadingOverlayOptions';
3+
import IBsLoadingOverlayHttpInterceptorOptions from './IBsLoadingOverlayHttpInterceptorOptions';
44

55
describe('Interceptor', () => {
66
let interceptor: BsLoadingOverlayHttpInterceptor;
77
let service: any;
8-
let options: IBsLoadingOverlayOptions;
8+
let options: IBsLoadingOverlayHttpInterceptorOptions;
99
let requestConfig: ng.IRequestConfig;
1010
let responsePromise: ng.IHttpPromiseCallbackArg<any>;
1111

@@ -24,74 +24,109 @@ describe('Interceptor', () => {
2424
data: 'test data'
2525
};
2626

27+
options = {
28+
referenceId: 'some reference id'
29+
};
30+
2731
interceptor = new BsLoadingOverlayHttpInterceptor(options, service);
2832
});
2933

30-
it('should return provided options on request', () => {
31-
let returnedRequest = interceptor.request(requestConfig);
32-
expect(returnedRequest).toEqual(requestConfig);
33-
});
34+
describe('request', () => {
35+
it('should return provided request config', () => {
36+
let returnedRequestConfig = interceptor.request(requestConfig);
37+
expect(returnedRequestConfig).toEqual(requestConfig);
38+
});
3439

35-
it('should call service start with provided options on request', () => {
36-
interceptor.request(requestConfig);
40+
it('should call service start with provided options', () => {
41+
interceptor.request(requestConfig);
3742

38-
expect(service.start.calledWith(options)).toBeTruthy();
39-
});
43+
expect(service.start.calledWith(options)).toBeTruthy();
44+
});
4045

41-
it('should call service start once on request', () => {
42-
interceptor.request(requestConfig);
46+
it('should call service start once', () => {
47+
interceptor.request(requestConfig);
4348

44-
expect(service.start.calledOnce).toBeTruthy();
45-
});
49+
expect(service.start.calledOnce).toBeTruthy();
50+
});
4651

47-
it('should call service start once on request called twice', () => {
48-
interceptor.request(requestConfig);
49-
interceptor.request(requestConfig);
52+
it('should call service start once on called twice', () => {
53+
interceptor.request(requestConfig);
54+
interceptor.request(requestConfig);
5055

51-
expect(service.start.calledOnce).toBeTruthy();
52-
});
56+
expect(service.start.calledOnce).toBeTruthy();
57+
});
5358

54-
it('should call service start once on request called twice', () => {
55-
interceptor.request(requestConfig);
56-
interceptor.request(requestConfig);
59+
it('should call service start once on called twice', () => {
60+
interceptor.request(requestConfig);
61+
interceptor.request(requestConfig);
5762

58-
expect(service.start.calledOnce).toBeTruthy();
59-
});
63+
expect(service.start.calledOnce).toBeTruthy();
64+
});
6065

61-
it('should call service start once on request called after response', () => {
62-
interceptor.response(responsePromise);
63-
interceptor.request(requestConfig);
66+
it('should call service start once on called after response', () => {
67+
interceptor.response(responsePromise);
68+
interceptor.request(requestConfig);
6469

65-
expect(service.start.calledOnce).toBeTruthy();
66-
});
70+
expect(service.start.calledOnce).toBeTruthy();
71+
});
6772

68-
it('should return provided rejection on request error', () => {
69-
const rejection = {rejectionField: 123};
70-
let returnedRejection = interceptor.requestError(rejection);
71-
expect(returnedRejection).toEqual(rejection);
73+
describe('with matcher provided', () => {
74+
beforeEach(() => {
75+
options = {
76+
requestsMatcher: (requestConfig) => requestConfig.url === 'some url'
77+
};
78+
interceptor = new BsLoadingOverlayHttpInterceptor(options, service);
79+
});
80+
81+
it('should call service start once if requestMatcher returns true matching requestConfig', () => {
82+
interceptor.request({
83+
url: 'some url',
84+
method: 'GET'
85+
});
86+
87+
expect(service.start.calledOnce).toBeTruthy();
88+
});
89+
90+
it('should not call service start if requestMatcher returns false matching requestConfig', () => {
91+
interceptor.request({
92+
url: 'another url, no match here',
93+
method: 'GET'
94+
});
95+
96+
expect(service.start.called).toBeFalsy();
97+
});
98+
});
7299
});
73100

74-
it('should call service stop once on request error after request', () => {
75-
interceptor.request(requestConfig);
76-
interceptor.requestError(requestConfig);
101+
describe('requestError', () => {
102+
it('should return provided rejection', () => {
103+
const rejection = {rejectionField: 123};
104+
let returnedRejection = interceptor.requestError(rejection);
105+
expect(returnedRejection).toEqual(rejection);
106+
});
77107

78-
expect(service.stop.calledOnce).toBeTruthy();
79-
});
108+
it('should call service stop once on called after request', () => {
109+
interceptor.request(requestConfig);
110+
interceptor.requestError(requestConfig);
111+
112+
expect(service.stop.calledOnce).toBeTruthy();
113+
});
80114

81-
it('should call service stop with provided options on request error after request', () => {
82-
interceptor.request(requestConfig);
83-
interceptor.requestError(requestConfig);
115+
it('should call service stop with provided options on called after request', () => {
116+
interceptor.request(requestConfig);
117+
interceptor.requestError(requestConfig);
84118

85-
expect(service.stop.calledWith(options)).toBeTruthy();
86-
});
119+
expect(service.stop.calledWith(options)).toBeTruthy();
120+
});
87121

88-
it('should call service stop once on request error called twice after request called twice', () => {
89-
interceptor.request(requestConfig);
90-
interceptor.request(requestConfig);
91-
interceptor.requestError(requestConfig);
92-
interceptor.requestError(requestConfig);
122+
it('should call service stop once on called twice after request called twice', () => {
123+
interceptor.request(requestConfig);
124+
interceptor.request(requestConfig);
125+
interceptor.requestError(requestConfig);
126+
interceptor.requestError(requestConfig);
93127

94-
expect(service.stop.calledOnce).toBeTruthy();
128+
expect(service.stop.calledOnce).toBeTruthy();
129+
});
95130
});
96131

97132
describe('response', () => {

source/BsLoadingOverlayHttpInterceptorInterceptor.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import IBsLoadingOverlayOptions from 'angular-loading-overlay/source/IBsLoadingOverlayOptions';
1+
import IBsLoadingOverlayHttpInterceptorOptions from './IBsLoadingOverlayHttpInterceptorOptions';
22
import {BsLoadingOverlayService} from 'angular-loading-overlay/source/BsLoadingOverlayService';
33

44
export default class BsLoadingOverlayHttpInterceptorInterceptor implements ng.IHttpInterceptor {
55
constructor(
6-
private config: IBsLoadingOverlayOptions,
6+
private config: IBsLoadingOverlayHttpInterceptorOptions = {},
77
private bsLoadingOverlayService: BsLoadingOverlayService
88
) {}
99

@@ -27,9 +27,16 @@ export default class BsLoadingOverlayHttpInterceptorInterceptor implements ng.IH
2727
this.requestsCount = Math.max(0, newRequestsCount);
2828
}
2929

30-
request = (config: ng.IRequestConfig) => {
31-
this.onRequest();
32-
return config;
30+
request = (requestConfig: ng.IRequestConfig) => {
31+
if (this.config.requestsMatcher) {
32+
if (this.config.requestsMatcher(requestConfig)) {
33+
this.onRequest();
34+
}
35+
} else {
36+
this.onRequest();
37+
}
38+
39+
return requestConfig;
3340
};
3441

3542
requestError = (rejection) => {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import IBsLoadingOverlayOptions from 'angular-loading-overlay/source/IBsLoadingOverlayOptions';
2+
3+
interface IBsLoadingOverlayHttpInterceptorOptions extends IBsLoadingOverlayOptions {
4+
requestsMatcher?: (requestConfig: ng.IRequestConfig) => boolean;
5+
}
6+
7+
export default IBsLoadingOverlayHttpInterceptorOptions;

0 commit comments

Comments
 (0)