Skip to content

Instantly share code, notes, and snippets.

@adhamankar
Created February 16, 2015 05:31
Show Gist options
  • Save adhamankar/0686edcca2aeb0fc8b38 to your computer and use it in GitHub Desktop.
Save adhamankar/0686edcca2aeb0fc8b38 to your computer and use it in GitHub Desktop.
Implementing angularjs Interceptor using TypeScript
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);
}
}
}
@jasperjn
Copy link

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.

AuthenticationInterceptor.Factory.$inject = ["$injector", "$q", "logger"];

@mnaser
Copy link

mnaser commented Dec 5, 2015

Same as above :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment