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
});
};
var myMap = {
0: 'foo',
1: 'bar',
2: 'etc'
};
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?