Proper use of transformers against interceptors

When sending POST to the endpoint at the service level to update the user profile, I need to remove certain values ​​from the request payload (the profile with the required changes from the client) and reconnect them to the response payload (updated profile from the server). I am currently executing behavior using Angular request and response filters , for example:

myService.updateProfile = function (profile) {

    return $http({
        method: 'POST',
        withCredentials: true,
        url: root + 'users/profile',
        data: profile,
        transformRequest : requestTransformer,
        transformResponse : responseTransformer
    });

};

// the map used during transformation below
var myMap = {
    0: 'foo', 
    1: 'bar',
    2: 'etc'
};

// prependTransform() and appendTransform() are similar to the example provided in Angular transformer docs here:
// https://docs.angularjs.org/api/ng/service/$http#overriding-the-default-transformations-per-request
var requestTransformer = httpTransformer.prependTransform($http.defaults.transformRequest, function(profileRequest) {
    profileRequest.myKey = myMap.indexOf(profileRequest.myValue);
    delete profileRequest.myValue;

    return profileRequest;
});
var responseTransformer = httpTransformer.appendTransform($http.defaults.transformResponse, function(profileResponse) {
    profileRequest.myValue = myMap[profileRequest.myKey];
    delete profileRequest.myKey;

    return profileResponse;
});

I add a transformer to the default request transformers and add a transformer to the default response transformers. My question is: is there a better way to do this? Perhaps instead of interceptors as indicated here ,? If so, how?

+4
1

, , , , . HTTP- HTTP-/ (auth, ..).

"" API/.

$provide.factory('userProfileInterceptor', function() {
  return {
    request: function(config) {
       if (config.url.indexOf('/users/profile') >=0){
           if (config.params.myValue) delete config.params.myValue;
       }
       return config;
    },
    response: function(response) {
       if (response.config.url.indexOf('/users/profile') >=0){
           delete response.data.myKey;
       }
       return response;
    }
  };
});

$httpProvider.interceptors.push('userProfileInterceptor');
0

All Articles