Skip to content

Commit d99a386

Browse files
committed
Merge pull request #23 from khrome/master
added support for .qs parameter as well as .form (as an object)
2 parents b85b08a + 5966b45 commit d99a386

File tree

1 file changed

+65
-2
lines changed

1 file changed

+65
-2
lines changed

index.js

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
var XHR = XMLHttpRequest
1616
if (!XHR) throw new Error('missing XMLHttpRequest')
17-
18-
module.exports = request
1917
request.log = {
2018
'trace': noop, 'debug': noop, 'info': noop, 'warn': noop, 'error': noop
2119
}
@@ -80,6 +78,70 @@ function request(options, callback) {
8078
else if(typeof options.body !== 'string')
8179
options.body = JSON.stringify(options.body)
8280
}
81+
82+
//BEGIN QS Hack
83+
var serialize = function(obj) {
84+
var str = [];
85+
for(var p in obj)
86+
if (obj.hasOwnProperty(p)) {
87+
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
88+
}
89+
return str.join("&");
90+
}
91+
92+
if(options.qs){
93+
var qs = (typeof options.qs == 'string')? options.qs : serialize(options.qs);
94+
if(options.uri.indexOf('?') !== -1){ //no get params
95+
options.uri = options.uri+'&'+qs;
96+
}else{ //existing get params
97+
options.uri = options.uri+'?'+qs;
98+
}
99+
}
100+
//END QS Hack
101+
102+
//BEGIN FORM Hack
103+
var multipart = function(obj) {
104+
//todo: support file type (useful?)
105+
var result = {};
106+
result.boundry = '-------------------------------'+Math.floor(Math.random()*1000000000);
107+
var lines = [];
108+
for(var p in obj){
109+
if (obj.hasOwnProperty(p)) {
110+
lines.push(
111+
'--'+result.boundry+"\n"+
112+
'Content-Disposition: form-data; name="'+p+'"'+"\n"+
113+
"\n"+
114+
obj[p]+"\n"
115+
);
116+
}
117+
}
118+
lines.push( '--'+result.boundry+'--' );
119+
result.body = lines.join('');
120+
result.length = result.body.length;
121+
result.type = 'multipart/form-data; boundary='+result.boundry;
122+
return result;
123+
}
124+
125+
if(options.form){
126+
if(typeof options.form == 'string') throw('form name unsupported');
127+
if(options.method === 'POST'){
128+
var encoding = (options.encoding || 'application/x-www-form-urlencoded').toLowerCase();
129+
options.headers['content-type'] = encoding;
130+
switch(encoding){
131+
case 'application/x-www-form-urlencoded':
132+
options.body = serialize(options.form).replace(/%20/g, "+");
133+
break;
134+
case 'multipart/form-data':
135+
var multi = multipart(options.form);
136+
//options.headers['content-length'] = multi.length;
137+
options.body = multi.body;
138+
options.headers['content-type'] = multi.type;
139+
break;
140+
default : throw new Error('unsupported encoding:'+encoding);
141+
}
142+
}
143+
}
144+
//END FORM Hack
83145

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

410472
return enc;
411473
}
474+
module.exports = request;

0 commit comments

Comments
 (0)