Created
July 29, 2015 12:50
-
-
Save eloone/4b528e4b41ddd32a401d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(angular){ | |
angular.module('app.lib.http.interceptorRequestFormatter', ['nvision.lib.xmlRequestFormatter']) | |
.config(['$httpProvider',function($httpProvider) { | |
$httpProvider.interceptors.push('InterceptorRequestFormatter'); | |
}]) | |
.factory('InterceptorRequestFormatter', ['XmlRequestFormatter', InterceptorRequestFormatterProvider]); | |
function InterceptorRequestFormatterProvider(XmlRequestFormatter){ | |
// Formats the request data | |
// @reqData is a json string | |
// @return is a json string | |
function formatDataToXml(reqData){ | |
var endJson = XmlRequestFormatter.reqDataToXml(JSON.parse(reqData)); | |
return JSON.stringify(endJson); | |
} | |
return { | |
request: function(config) { | |
var allowedWriteMethods = ['POST', 'PATCH', 'DELETE']; | |
if(_.contains(allowedWriteMethods, config.method) && | |
!_.some(config.transformRequest, function(reqHandler){ | |
return reqHandler === formatDataToXml; | |
}) | |
) { | |
//try to use unshift to not have to do stringify | |
config.transformRequest.push(formatDataToXml); | |
} | |
return config; | |
} | |
}; | |
} | |
}(window.angular)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment