Forked from adhamankar/AuthenticationInterceptor.ts
Created
February 29, 2016 13:19
-
-
Save venkatesh22/80fa56bea93f3a27e0f9 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