Created
February 16, 2015 05:31
-
-
Save adhamankar/0686edcca2aeb0fc8b38 to your computer and use it in GitHub Desktop.
Implementing angularjs Interceptor using TypeScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module App { | |
"use strict"; | |
//Method name should be exactly "response" - http://docs.angularjs.org/api/ng/service/$http | |
export interface IInterceptor { | |
request: Function; | |
requestError: Function; | |
response: Function; | |
responseError: Function; | |
} | |
export class AuthenticationInterceptor implements IInterceptor { | |
public static $inject = ["$injector", "$q", "logger"]; | |
public static Factory($injector: ng.auto.IInjectorService, $q: ng.IQService, logger: LogService) { | |
return new AuthenticationInterceptor($injector, $q, logger); | |
} | |
constructor(private $injector: ng.auto.IInjectorService, private $q: ng.IQService, private logger: LogService) { | |
logger.log("initializing AuthenticationInterceptor"); | |
} | |
public request = (requestSuccess): ng.IPromise<any> => { | |
this.logger.log("intercepting request"); | |
return requestSuccess; | |
} | |
public requestError = (requestFailure): ng.IPromise<any> => { | |
this.logger.log("requestError reported"); | |
return requestFailure; | |
} | |
public response = (responseSuccess): ng.IPromise<any> => { | |
this.logger.log("success response reported with status: " + responseSuccess.status); | |
console.log("response: ", responseSuccess); | |
return responseSuccess; | |
} | |
public responseError = (responseFailure): ng.IPromise<any> => { | |
this.logger.log("response Error reported"); | |
if (responseFailure.status === 401) { | |
console.log("401 error"); | |
} | |
return this.$q.reject(responseFailure); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
This example throw Unknown Provider error after minification.
I have following code to fix it.
Please let me know if there's a better workaround.
Thank you.