//request is a node module
var request = require('request');
/**
* send REST web service message to server
* @function sendMessage
* @param {string} method - GET, POST, PUT, DELETE
* @param {string} url - server URL
* @param {object} json - The data to be send
* @param {object} cb - callback function
*@param{object}formData-The data related to file upload
* @returns void
*/
function sendMessage(type,url,method,json,retries,formData,cb){
var options={
url:url,
formData:formData,
json:true,
switch (method) {
case 'get':
request.get(options, cb);
break;
case 'post':
envelope.token = json.token;
envelope.package = json.package;
options.body = envelope;
request.post(options, cb);
break;
case 'put':
envelope.package = json.package;
envelope.token = json.token;
options.body = envelope;
request.put(options, cb);
break;
case 'delete':
request.delete(options, cb);
break;
}
Here sendMessage() fuction already written and well used in all the modules without FormData parameter(sendMessage(type,url,method,json,retries,cb)
),but for file upload we need to pass the file path using formData without alter all the other functions is that possible.
Create a new method that accepts an object as a parameter, which analyses the object and decides whether to proxy to the original function or perform your new requirement.
Mark the original method as deprecated and document to use your new method, and when the time is right, calls to the original method can be replaced. When they are all replaced, refactor your new function to remove the proxy and, when confident, remove the old method.
Try and abstract as much functionality as makes sense from the original method to keep the code DRY.
If possible, write a test that both original and new methods can pass.