This repository has been archived by the owner on Mar 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsexhr.js
107 lines (97 loc) · 2.74 KB
/
sexhr.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
/**
* Sexhr.js
*
* A simple but useful promise-enabled wrapper around XHR. Takes care of a lot
* of common tasks without abstracting away any power or freedom.
* -----------------------------------------------------------------------------
*
* Copyright (c) 2015, Lyon Bros LLC. (http://www.lyonbros.com)
*
* Licensed under The MIT License.
* Redistributions of files must retain the above copyright notice.
*/
(function() {
"use strict";
this.Sexhr = function(options)
{
options || (options = {});
return new Promise(function(resolve, reject) {
var url = options.url;
var method = (options.method || 'get').toUpperCase();
var emulate = options.emulate || true;
if(!options.url) throw new Error('no url given');
url = url.replace(/#.*$/, '');
var qs = [];
if(options.querydata)
{
qs = Object.keys(options.querydata)
.map(function(key) {
return key + '=' + encodeURIComponent(options.querydata[key]);
});
}
if(emulate && ['GET', 'POST'].indexOf(method) < 0)
{
qs.push('_method='+method);
method = 'POST';
}
if(qs.length)
{
var querystring = qs.join('&');
if(url.match(/\?/))
{
url = url.replace(/&$/) + '&' + querystring;
}
else
{
url += '?' + querystring;
}
}
var xhr = new XMLHttpRequest();
xhr.open(method, url, true);
xhr.responseType = options.response_type || '';
if(options.timeout) xhr.timeout = options.timeout;
Object.keys(options.headers || {}).forEach(function(k) {
xhr.setRequestHeader(k, options.headers[k]);
});
xhr.onload = function(e)
{
if((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304)
{
var value = xhr.response;
return resolve([value, xhr]);
}
else if(xhr.status >= 400)
{
reject({xhr: xhr, code: xhr.status, msg: xhr.response});
}
};
xhr.onabort = function(e)
{
reject({xhr: xhr, code: -2, msg: 'aborted'});
};
xhr.onerror = function(e)
{
reject({xhr: xhr, code: -1, msg: 'error'});
};
xhr.ontimeout = function(e)
{
reject({xhr: xhr, code: -3, msg: 'timeout'});
};
// set xhr.on[progress|abort|etc]
Object.keys(options).forEach(function(k) {
if(k.substr(0, 2) != 'on') return false;
if(['onload', 'onerror', 'onabort', 'ontimeout'].indexOf(k) >= 0) return false;
var fn = options[k];
xhr[k] = function(e) { fn(e, xhr); };
});
// set xhr.upload.on[progress|abort|etc]
Object.keys(options.upload || {}).forEach(function(k) {
if(k.substr(0, 2) != 'on') return false;
var fn = options[k];
xhr.upload[k] = function(e) { fn(e, xhr); };
});
if(options.override) options.override(xhr);
xhr.send(options.data);
});
};
}).apply((typeof exports != 'undefined') ? exports : this);