|
14 | 14 |
|
15 | 15 | var XHR = XMLHttpRequest
|
16 | 16 | if (!XHR) throw new Error('missing XMLHttpRequest')
|
17 |
| - |
18 |
| -module.exports = request |
19 | 17 | request.log = {
|
20 | 18 | 'trace': noop, 'debug': noop, 'info': noop, 'warn': noop, 'error': noop
|
21 | 19 | }
|
@@ -80,6 +78,70 @@ function request(options, callback) {
|
80 | 78 | else if(typeof options.body !== 'string')
|
81 | 79 | options.body = JSON.stringify(options.body)
|
82 | 80 | }
|
| 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 |
83 | 145 |
|
84 | 146 | // If onResponse is boolean true, call back immediately when the response is known,
|
85 | 147 | // not when the full request is complete.
|
@@ -409,3 +471,4 @@ function b64_enc (data) {
|
409 | 471 |
|
410 | 472 | return enc;
|
411 | 473 | }
|
| 474 | +module.exports = request; |
0 commit comments