Skip to content

Commit 6260bfe

Browse files
committed
fix(interceptor): Fixed issue with interceptors being reversed due to the way Array.reverse works in
1 parent 1bac48d commit 6260bfe

File tree

5 files changed

+88
-78
lines changed

5 files changed

+88
-78
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"dependencies": {
2323
"@angular/core": "^4.0.0",
2424
"@angular/http": "^4.0.0",
25-
"rxjs": "^5.0.1"
25+
"rxjs": "^5.0.0"
2626
},
2727
"devDependencies": {
2828
"@angular/compiler": "^4.0.0",

src/interceptor-request-internal.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ import { InterceptorUtils } from './interceptor-utils';
66

77
export class InterceptorRequestInternal extends InterceptorRequest {
88

9-
/**
10-
* Hack
11-
* TODO: Point to typescript bug
12-
*/
139
constructor() {
1410
super(null);
1511
}

src/interceptor-response-wrapper-builder-internal.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,17 @@ export class InterceptorResponseWrapperBuilderInternal extends InterceptorRespon
2121
} else if (from instanceof InterceptorResponseWrapper) {
2222
InterceptorUtils.assign(builder, <InterceptorResponseWrapper>from);
2323
} else {
24+
console.log('Copying from request to response');
2425
const request: InterceptorRequestInternal = new InterceptorRequestInternal();
2526
InterceptorUtils.assign(request, from);
2627
InterceptorUtils.assign(builder, from);
27-
if (request.getShortCircuitAtCurrentStep()) {
28-
builder.shortCircuitTriggeredBy(interceptorStep - 1)
29-
.forceRequestCompletion(request.getAlsoForceRequestCompletion());
28+
if (request.getAlreadyShortCircuited() || request.getShortCircuitAtCurrentStep()) {
29+
if (request.getAlreadyShortCircuited()) {
30+
builder.shortCircuitTriggeredBy(request.getShortCircuitTriggeredBy())
31+
} else {
32+
builder.shortCircuitTriggeredBy(interceptorStep - 1);
33+
}
34+
builder.forceRequestCompletion(request.getAlsoForceRequestCompletion());
3035
}
3136
}
3237
return builder;

src/interceptor-service.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ import { RealResponseObservableTransformer } from './real-response-observable-tr
3535
* Wrapper around native angular `Http` service.
3636
* Allows you to add `Interceptor`s that lets you do
3737
* 1. Transform request before it reaches the actual request, such as, add headers transparently
38-
* 2. Transform response, such as stripout the top level object & return the payload (or) raise errors if `response.status` is not `ok`
38+
* 2. Transform response, such as strip out the top level object & return the payload (or) raise errors if `response.status` is not `ok`
3939
* 3. To short circuit the request flow based on runtime data
4040
* 4. To selective caching/log request/responses
4141
* 5. Augment the real `Observable<Response>` that native angular `http.request(..)` returns
4242
* 6. Store intermediate data that can be shared across the interceptors
4343
* 7. Generate handcrafted response incase of error/base of your runtime decision
4444
*
4545
* The service executes methods in the interceptor chain in the following manner
46-
* 1. For each of the listed interceptor, tranforms the request by invoking\
46+
* 1. For each of the listed interceptor, transforms the request by invoking\
4747
* `beforeRequest(..)` on each interceptor in the same order they are added
4848
* 2. Invokes native angular `http.request(..)` with the result of last interceptor's `beforeRequest(..)` response
4949
* 3. Invokes `onResponse(..)` on each interceptor in the reverse order they are added
@@ -61,6 +61,7 @@ export class InterceptorService extends Http {
6161

6262
/** Parent overrides **/
6363
request(url: string | Request, options?: RequestOptionsArgs | InterceptorRequestOptionsArgs): Observable<Response> {
64+
console.log('Marking request for', url, 'I am', this);
6465
let interceptorOptions: InterceptorRequestOptionsArgs;
6566
if (!options) {
6667
interceptorOptions = {};
@@ -85,11 +86,12 @@ export class InterceptorService extends Http {
8586
);
8687
observer.add(() => {
8788
subscription.unsubscribe();
88-
this.interceptors.reverse().forEach((interceptor, index) => {
89+
for (let index = this.interceptors.length - 1; index >= 0; index--) {
90+
const interceptor = this.interceptors[index];
8991
if (interceptor.onUnsubscribe !== undefined) {
9092
interceptor.onUnsubscribe(index, url, options);
9193
}
92-
});
94+
}
9395
});
9496
return this;
9597
});
@@ -186,23 +188,28 @@ export class InterceptorService extends Http {
186188
private httpRequest(request: InterceptorRequest): Observable<Response> {
187189
return this.runBeforeInterceptors(request)
188190
.flatMap<InterceptorRequest, InterceptorResponseWrapper>((transformedRequest: InterceptorRequest, _: number) => {
191+
console.log(`Received request`, transformedRequest);
189192
const transformedRequestInternal = <InterceptorRequestInternal>transformedRequest;
190193
const interceptorRequestInternalBuilder = InterceptorRequestBuilderInternal.new(transformedRequestInternal);
191194

192195
if (interceptorRequestInternalBuilder.getErr() || interceptorRequestInternalBuilder.getAlreadyShortCircuited()) {
193196
const responseWrapper = InterceptorResponseWrapperBuilderInternal
194197
.newInternal(this.interceptors.length, transformedRequestInternal)
195198
.build();
199+
console.log('Response wrapper', responseWrapper);
196200
return Observable.of(responseWrapper);
197201
} else if (interceptorRequestInternalBuilder.getShortCircuitAtCurrentStep()) {
202+
// if the last interceptor in the chain asked to skip the flow, we can handle it here
198203
const responseWrapper = InterceptorResponseWrapperBuilderInternal
199204
.newInternal(this.interceptors.length, transformedRequestInternal)
200205
.build();
206+
console.log('Response wrapper', responseWrapper);
201207
return Observable.of(responseWrapper);
202208
}
203209

204210
let response$ = super.request(transformedRequest.url, transformedRequest.options);
205211
if (this._realResponseObservableTransformer) {
212+
console.log('Applying transformation to real request using', this._realResponseObservableTransformer);
206213
response$ = this._realResponseObservableTransformer.transform(response$, transformedRequest, new this.HttpDirect(), this);
207214
}
208215

@@ -259,6 +266,7 @@ export class InterceptorService extends Http {
259266

260267
if (interceptor.beforeRequest !== undefined) {
261268
try {
269+
console.log('Invoking beforeRequest on', interceptor);
262270
const processedRequest = interceptor.beforeRequest(request, index);
263271
let processedRequest$: Observable<InterceptorRequest>;
264272

@@ -316,6 +324,7 @@ export class InterceptorService extends Http {
316324
try {
317325
if (transformedResponseWrapper.forceRequestCompletion) {
318326
if (interceptor.onForceCompleteOrForceReturn !== undefined) {
327+
console.log('Invoking onForceCompleteOrForceReturn on', interceptor);
319328
interceptor.onForceCompleteOrForceReturn(transformedResponseWrapper, index);
320329
}
321330
if (index === 0) { // complete the observable, since this is the first interceptor (last in the response chain)
@@ -325,6 +334,7 @@ export class InterceptorService extends Http {
325334
}
326335
} else if (transformedResponseWrapper.forceReturnResponse) {
327336
if (interceptor.onForceCompleteOrForceReturn !== undefined) {
337+
console.log('Invoking onForceCompleteOrForceReturn on', interceptor);
328338
interceptor.onForceCompleteOrForceReturn(transformedResponseWrapper, index);
329339
}
330340
return Observable.of(transformedResponseWrapper);
@@ -334,13 +344,16 @@ export class InterceptorService extends Http {
334344

335345
if (transformedResponseWrapper.err) {
336346
if (interceptor.onErr !== undefined) {
347+
console.log('Invoking onErr on', interceptor);
337348
processedResponse = interceptor.onErr(transformedResponseWrapper, index);
338349
}
339350
} else if (transformedResponseWrapper.isShortCircuited()) {
340351
if (interceptor.onShortCircuit !== undefined) {
352+
console.log('Invoking onShortCircuit on', interceptor);
341353
processedResponse = interceptor.onShortCircuit(transformedResponseWrapper, index);
342354
}
343355
} else if (interceptor.onResponse !== undefined) {
356+
console.log('Invoking onResponse on', interceptor);
344357
processedResponse = interceptor.onResponse(transformedResponseWrapper, index);
345358
}
346359

0 commit comments

Comments
 (0)