Forked from adhamankar/AuthenticationInterceptor.ts
Created
October 18, 2017 10:39
-
-
Save bioyeneye/a87409db4759c7dc7750bb4610d1e2ed 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
https://docs.angularjs.org/api/ng/service/$http