-
Notifications
You must be signed in to change notification settings - Fork 82
/
request.jquery.js
237 lines (185 loc) · 6.51 KB
/
request.jquery.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
(function() {
var define = window.define
if(!define) define = function(deps, definer) {
if(!window.jQuery)
throw new Error("Can't find jQuery");
return definer(window.jQuery);
}
define(['jquery'], function(jQuery) {
//
// request.jquery
//
var DEFAULT_TIMEOUT = 3 * 60 * 1000; // 3 minutes
function request(options, callback) {
var options_onResponse = options.onResponse; // Save this for later.
if(typeof options === 'string')
options = {'uri':options};
else
options = JSON.parse(JSON.stringify(options)); // Use a duplicate for mutating.
if(options.url) {
options.uri = options.url;
delete options.url;
}
if(!options.uri && options.uri !== "")
throw new Error("options.uri is a required argument");
if(options.json) {
options.body = JSON.stringify(options.json);
delete options.json;
}
if(typeof options.uri != "string")
throw new Error("options.uri must be a string");
; ['proxy', '_redirectsFollowed', 'maxRedirects', 'followRedirect'].forEach(function(opt) {
if(options[opt])
throw new Error("options." + opt + " is not supported");
})
options.method = options.method || 'GET';
options.headers = options.headers || {};
if(options.headers.host)
throw new Error("Options.headers.host is not supported");
// onResponse is just like the callback but that is not quite what Node request does.
callback = callback || options_onResponse;
/*
// Browsers do not like this.
if(options.body)
options.headers['content-length'] = options.body.length;
*/
var headers = {};
var beforeSend = function(xhr, settings) {
if(!options.headers.authorization && options.auth) {
debugger
options.headers.authorization = 'Basic ' + b64_enc(options.auth.username + ':' + options.auth.password);
}
for (var key in options.headers)
xhr.setRequestHeader(key, options.headers[key]);
}
// Establish a place where the callback arguments will go.
var result = [];
function fix_xhr(xhr) {
var fixed_xhr = {};
for (var key in xhr)
fixed_xhr[key] = xhr[key];
fixed_xhr.statusCode = xhr.status;
return fixed_xhr;
}
var onSuccess = function(data, reason, xhr) {
result = [null, fix_xhr(xhr), data];
}
var onError = function (xhr, reason, er) {
var body = undefined;
if(reason == 'timeout') {
er = er || new Error("Request timeout");
} else if(reason == 'error') {
if(xhr.status > 299 && xhr.responseText.length > 0) {
// Looks like HTTP worked, so there is no error as far as request is concerned. Simulate a success scenario.
er = null;
body = xhr.responseText;
}
} else {
er = er || new Error("Unknown error; reason = " + reason);
}
result = [er, fix_xhr(xhr), body];
}
var onComplete = function(xhr, reason) {
if(result.length === 0)
result = [new Error("Result does not exist at completion time")];
return callback && callback.apply(this, result);
}
var cors_creds = !!( options.creds || options.withCredentials );
return jQuery.ajax({ 'async' : true
, 'cache' : (options.cache || false)
, 'contentType': (options.headers['content-type'] || 'application/x-www-form-urlencoded')
, 'type' : options.method
, 'url' : options.uri
, 'data' : (options.body || undefined)
, 'timeout' : (options.timeout || request.DEFAULT_TIMEOUT)
, 'dataType' : 'text'
, 'processData': false
, 'beforeSend' : beforeSend
, 'success' : onSuccess
, 'error' : onError
, 'complete' : onComplete
, 'xhrFields' : { 'withCredentials': cors_creds
}
});
};
request.withCredentials = false;
request.DEFAULT_TIMEOUT = DEFAULT_TIMEOUT;
var shortcuts = [ 'get', 'put', 'post', 'head' ];
shortcuts.forEach(function(shortcut) {
var method = shortcut.toUpperCase();
var func = shortcut.toLowerCase();
request[func] = function(opts) {
if(typeof opts === 'string')
opts = {'method':method, 'uri':opts};
else {
opts = JSON.parse(JSON.stringify(opts));
opts.method = method;
}
var args = [opts].concat(Array.prototype.slice.apply(arguments, [1]));
return request.apply(this, args);
}
})
request.json = function(options, callback) {
options = JSON.parse(JSON.stringify(options));
options.headers = options.headers || {};
options.headers['accept'] = options.headers['accept'] || 'application/json';
if(options.method !== 'GET')
options.headers['content-type'] = 'application/json';
return request(options, function(er, resp, body) {
if(!er)
body = JSON.parse(body)
return callback && callback(er, resp, body);
})
}
request.couch = function(options, callback) {
return request.json(options, function(er, resp, body) {
if(er)
return callback && callback(er, resp, body);
if((resp.status < 200 || resp.status > 299) && body.error)
// The body is a Couch JSON object indicating the error.
return callback && callback(body, resp);
return callback && callback(er, resp, body);
})
}
jQuery(document).ready(function() {
jQuery.request = request;
})
return request;
});
//
// Utility
//
// MIT License from http://phpjs.org/functions/base64_encode:358
function b64_enc (data) {
// Encodes string using MIME base64 algorithm
var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var o1, o2, o3, h1, h2, h3, h4, bits, i = 0, ac = 0, enc="", tmp_arr = [];
if (!data) {
return data;
}
// assume utf8 data
// data = this.utf8_encode(data+'');
do { // pack three octets into four hexets
o1 = data.charCodeAt(i++);
o2 = data.charCodeAt(i++);
o3 = data.charCodeAt(i++);
bits = o1<<16 | o2<<8 | o3;
h1 = bits>>18 & 0x3f;
h2 = bits>>12 & 0x3f;
h3 = bits>>6 & 0x3f;
h4 = bits & 0x3f;
// use hexets to index into b64, and append result to encoded string
tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
} while (i < data.length);
enc = tmp_arr.join('');
switch (data.length % 3) {
case 1:
enc = enc.slice(0, -2) + '==';
break;
case 2:
enc = enc.slice(0, -1) + '=';
break;
}
return enc;
}
})();