Modified the post, put, head and del shortcuts to support uri optional param #180

Merged
merged 1 commit into from Feb 20, 2012

Projects

None yet

2 participants

@twilson63
Contributor

Now you should be able to do the following:

request.post("http://example.com/widgets", function(e,r,b) { console.log(b) });
//or
request.post("http://example.com/widgets", { body: 'foo=bar'}, function(e,r,b) { console.log(b) });
//and still do
request.post({ uri: "http://example.com/widgets", body: 'foo=bar'}, function(e,r,b) { console.log(b) });

created a new function called initParams to reduce duplication:

function initParams(uri, options, callback) {
  if ((typeof options === 'function') && !callback) callback = options;
  if (typeof options === 'object') {
    options.uri = uri;
  } else if (typeof uri === 'string') {
    options = {uri:uri};
  } else {
    options = uri;
    uri = options.uri;
  }
  return { uri: uri, options: options, callback: callback };
}

and each shortcut method calls this method to organize the method params

request.post = function (uri, options, callback) {
  var params = initParams(uri, options, callback);
  params.options.method = 'POST';
  return request(params.uri, params.options, params.callback)
}
@mikeal mikeal merged commit 56d868d into request:master Feb 20, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment