I\'m having trouble writing an angular http interceptor in plain TypeScript. The code I\'m trying to convert is the following:
.config([\'$httpProvider\', fu
The config settings are as follows
.config(['$httpProvider', function ($httpProvider: ng.IHttpProvider) {
$httpProvider.interceptors.push(AuthenticationInterceptor.Factory);
}])
and your actual class should look as follows
module Common {
'use strict';
export class AuthenticationInterceptor {
private static _instance: AuthenticationInterceptor;
public static Factory($q: ng.IQService) {
AuthenticationInterceptor._instance = new AuthenticationInterceptor($q);
return AuthenticationInterceptor._instance;
}
constructor(private $q: ng.IQService) {
}
//Method name should be exactly "response" - http://docs.angularjs.org/api/ng/service/$http
public response(response) {
var self = Common.AuthenticationInterceptor._instance;
console.log(response);
return response || self.$q.when(response);
}
public responseError(rejection) {
var self = Common.AuthenticationInterceptor._instance;
console.log(rejection.status);
if (rejection.status === 401) {
}
// Otherwise, default behavior
return self.$q.reject(rejection);
}
}
It is necessary to get the instance of the class with the full namespace because when receiving a callback from Angular 'this' is undefined.