forked from radiosilence/xr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxr.js
175 lines (153 loc) · 4.57 KB
/
xr.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
(function (global, factory) {
if (typeof define === 'function' && define.amd) {
define(['exports', 'module'], factory);
} else if (typeof exports !== 'undefined' && typeof module !== 'undefined') {
factory(exports, module);
} else {
var mod = {
exports: {}
};
factory(mod.exports, mod);
global.xr = mod.exports;
}
})(this, function (exports, module) {
/**
* xr (c) James Cleveland 2015
* URL: https://github.com/radiosilence/xr
* License: BSD
*/
'use strict';
var Methods = {
GET: 'GET',
POST: 'POST',
PUT: 'PUT',
DELETE: 'DELETE',
PATCH: 'PATCH',
OPTIONS: 'OPTIONS'
};
var Events = {
READY_STATE_CHANGE: 'readystatechange',
LOAD_START: 'loadstart',
PROGRESS: 'progress',
ABORT: 'abort',
ERROR: 'error',
LOAD: 'load',
TIMEOUT: 'timeout',
LOAD_END: 'loadend'
};
var defaults = {
method: Methods.GET,
data: undefined,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
dump: JSON.stringify,
load: JSON.parse,
xmlHttpRequest: function xmlHttpRequest() {
return new XMLHttpRequest();
},
promise: function promise(fn) {
return new Promise(fn);
}
};
function res(xhr) {
return {
status: xhr.status,
response: xhr.response,
xhr: xhr
};
}
function assign(l) {
for (var _len = arguments.length, rs = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
rs[_key - 1] = arguments[_key];
}
for (var i in rs) {
if (!({}).hasOwnProperty.call(rs, i)) continue;
var r = rs[i];
if (typeof r !== 'object') continue;
for (var k in r) {
if (!({}).hasOwnProperty.call(r, k)) continue;
l[k] = r[k];
}
}
return l;
}
function urlEncode(params) {
var paramStrings = [];
for (var k in params) {
if (!({}).hasOwnProperty.call(params, k)) continue;
paramStrings.push(encodeURIComponent(k) + '=' + encodeURIComponent(params[k]));
}
return paramStrings.join('&');
}
var config = {};
function configure(opts) {
config = assign({}, config, opts);
}
function promise(args, fn) {
return (args && args.promise ? args.promise : config.promise || defaults.promise)(fn);
}
function xr(args) {
return promise(args, function (resolve, reject) {
var opts = assign({}, defaults, config, args);
var xhr = opts.xmlHttpRequest();
xhr.open(opts.method, opts.params ? opts.url.split('?')[0] + '?' + urlEncode(opts.params) : opts.url, true);
xhr.addEventListener(Events.LOAD, function () {
if (xhr.status >= 200 && xhr.status < 300) {
var _data = null;
if (xhr.responseText) {
_data = opts.raw === true ? xhr.responseText : opts.load(xhr.responseText);
}
resolve(_data);
} else {
reject(res(xhr));
}
});
xhr.addEventListener(Events.ABORT, function () {
return reject(res(xhr));
});
xhr.addEventListener(Events.ERROR, function () {
return reject(res(xhr));
});
xhr.addEventListener(Events.TIMEOUT, function () {
return reject(res(xhr));
});
for (var k in opts.headers) {
if (!({}).hasOwnProperty.call(opts.headers, k)) continue;
xhr.setRequestHeader(k, opts.headers[k]);
}
for (var k in opts.events) {
if (!({}).hasOwnProperty.call(opts.events, k)) continue;
xhr.addEventListener(k, opts.events[k].bind(null, xhr), false);
}
var data = typeof opts.data === 'object' && !opts.raw ? opts.dump(opts.data) : opts.data;
if (data !== undefined) xhr.send(data);else xhr.send();
});
}
xr.assign = assign;
xr.urlEncode = urlEncode;
xr.configure = configure;
xr.Methods = Methods;
xr.Events = Events;
xr.defaults = defaults;
xr.get = function (url, params, args) {
return xr(assign({ url: url, method: Methods.GET, params: params }, args));
};
xr.put = function (url, data, args) {
return xr(assign({ url: url, method: Methods.PUT, data: data }, args));
};
xr.post = function (url, data, args) {
return xr(assign({ url: url, method: Methods.POST, data: data }, args));
};
xr.patch = function (url, data, args) {
return xr(assign({ url: url, method: Methods.PATCH, data: data }, args));
};
xr.del = function (url, args) {
return xr(assign({ url: url, method: Methods.DELETE }, args));
};
xr.options = function (url, args) {
return xr(assign({ url: url, method: Methods.OPTIONS }, args));
};
module.exports = xr;
});