Skip to content

Commit

Permalink
Merge pull request #23 from khrome/master
Browse files Browse the repository at this point in the history
added support for .qs parameter as well as .form (as an object)
  • Loading branch information
jhs committed Feb 13, 2014
2 parents b85b08a + 5966b45 commit d99a386
Showing 1 changed file with 65 additions and 2 deletions.
67 changes: 65 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

var XHR = XMLHttpRequest
if (!XHR) throw new Error('missing XMLHttpRequest')

module.exports = request
request.log = {
'trace': noop, 'debug': noop, 'info': noop, 'warn': noop, 'error': noop
}
Expand Down Expand Up @@ -80,6 +78,70 @@ function request(options, callback) {
else if(typeof options.body !== 'string')
options.body = JSON.stringify(options.body)
}

//BEGIN QS Hack
var serialize = function(obj) {
var str = [];
for(var p in obj)
if (obj.hasOwnProperty(p)) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
}
return str.join("&");
}

if(options.qs){
var qs = (typeof options.qs == 'string')? options.qs : serialize(options.qs);
if(options.uri.indexOf('?') !== -1){ //no get params
options.uri = options.uri+'&'+qs;
}else{ //existing get params
options.uri = options.uri+'?'+qs;
}
}
//END QS Hack

//BEGIN FORM Hack
var multipart = function(obj) {
//todo: support file type (useful?)
var result = {};
result.boundry = '-------------------------------'+Math.floor(Math.random()*1000000000);
var lines = [];
for(var p in obj){
if (obj.hasOwnProperty(p)) {
lines.push(
'--'+result.boundry+"\n"+
'Content-Disposition: form-data; name="'+p+'"'+"\n"+
"\n"+
obj[p]+"\n"
);
}
}
lines.push( '--'+result.boundry+'--' );
result.body = lines.join('');
result.length = result.body.length;
result.type = 'multipart/form-data; boundary='+result.boundry;
return result;
}

if(options.form){
if(typeof options.form == 'string') throw('form name unsupported');
if(options.method === 'POST'){
var encoding = (options.encoding || 'application/x-www-form-urlencoded').toLowerCase();
options.headers['content-type'] = encoding;
switch(encoding){
case 'application/x-www-form-urlencoded':
options.body = serialize(options.form).replace(/%20/g, "+");
break;
case 'multipart/form-data':
var multi = multipart(options.form);
//options.headers['content-length'] = multi.length;
options.body = multi.body;
options.headers['content-type'] = multi.type;
break;
default : throw new Error('unsupported encoding:'+encoding);
}
}
}
//END FORM Hack

// If onResponse is boolean true, call back immediately when the response is known,
// not when the full request is complete.
Expand Down Expand Up @@ -409,3 +471,4 @@ function b64_enc (data) {

return enc;
}
module.exports = request;

0 comments on commit d99a386

Please sign in to comment.