mtpultz
mtpultz

Reputation: 18328

AngularJS 1.3.13 - Is it possible to add multiple interceptors to a particular $resource?

I've got a bunch of different interceptors that I only want to use on particular $resource endpoints. I read up on the interceptor property of $resource and have this working, but in a couple cases I'd like to have two interceptors in place for one endpoint response that would just chain like normal global interceptors. Is this possible?

Inside the actions of the User $resource I'd like to have something like I'ved added to the login endpoint:

var actions = {

    login: {
        method: 'POST',
        url: '/api/auth/login',
        interceptor: [AuthLoginInterceptor,AuthUserInterceptor]
    },
    logout: {
        method: 'GET',
        url: '/api/auth/logout',
        interceptor: AuthLogoutInterceptor
    },
    update: {
        method: 'PUT',
        interceptor: AuthUserInterceptor
    },...

I know I could just include the code from one in the other, so worst case I just refactor a solution, but if it's possible I'd rather keep it the way it is.

Upvotes: 1

Views: 697

Answers (1)

Matthew King
Matthew King

Reputation: 1362

What about something like this?:

{
  method: 'POST',
  url: '/api/auth/login',
  interceptor: {
    response: function (config) {
      return AuthLoginInterceptor.response(AuthUserInterceptor.response(config));
    }
  }
}

It seems to be working in a minimal test project I am using.


Answer based on my comment above. Hoping it can be of use to future visitors to this question.

Upvotes: 4

Related Questions