-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
238 lines (195 loc) · 6.82 KB
/
index.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
238
Tny('http')
.provider('$$httpErrorHandler', function() {
function errorHandler(xhr) {
console.log(xhr.status);
}
return {
set : function(fn) {
errorHandler = fn || Tny.noop;
},
get : function() {
return errorHandler;
}
}
})
;
Tny('http')
.provider('$$httpHeaders',function(){
var headers = {
// 'Content-type': 'application/json;charset=utf-8'
};
return {
setHeader : function(header,value) {
headers[header] = value;
},
getHeaders : function(){
return headers
}
}
})
;
Tny('http')
.provider('$$httpInterceptors', function(){
var interceptors = {
// 'interceptorName' : {
// pre : function(options, xhr){
// },
// post : function(response, options, xhr) {
// return formattedData;
// }
// }
}
function validateInterceptor(interceptorObj){
if (!interceptorObj.pre && !interceptorObj.post ) {
throw 'HTTP interceptors must have at least one "pre" or "post" function';
}
}
return {
getInterceptors : function(){
return interceptors;
},
addInterceptor : function(name, interceptorObj) {
validateInterceptor(interceptorObj);
interceptors[name] = interceptorObj;
},
removeInterceptor : function(name) {
delete interceptors[name];
}
}
})
;
Tny('http')
.factory('$httpJsonInterceptor', ['$$httpInterceptors',function($$httpInterceptors) {
function pre(options, xhr) {
if (options.dataType.toUpperCase() === 'JSON' && options.method !== 'GET') {
xhr.setRequestHeader('Content-type', 'application/json;charset=utf-8');
options.data = JSON.stringify(options.data);
}
}
function post(response, options, xhr) {
var output;
if (options.dataType.toUpperCase() === 'JSON') {
try {
output = JSON.parse(response);
} catch(e){
//erorr
}
}
return output || response;
}
return function(){
$$httpInterceptors.addInterceptor('jsonDataTypeFormatter',{
pre : pre,
post : post
})
}
}])
;
Tny('http')
.service('$http', ['$$httpHeaders', '$$httpInterceptors', '$$httpErrorHandler', function($$httpHeaders, $$httpInterceptors, $$httpErrorHandler) {
function getXhrType() {
if (typeof XMLHttpRequest !== 'undefined') {
return new XMLHttpRequest();
}
var versions = [
"MSXML2.XmlHttp.6.0",
"MSXML2.XmlHttp.5.0",
"MSXML2.XmlHttp.4.0",
"MSXML2.XmlHttp.3.0",
"MSXML2.XmlHttp.2.0",
"Microsoft.XmlHttp"
];
var xhr;
for (var i = 0; i < versions.length; i++) {
try {
xhr = new ActiveXObject(versions[i]);
break;
} catch (e) {
}
}
return xhr;
};
function transformDataToQuery (url, data) {
if (!data) return url;
var query = [];
for (var key in data) {
query.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key]));
}
return url + (query.length ? '?' + query.join('&') : '');
};
function setHeaders(xhr, headers) {
if (headers) {
for(key in headers) {
if (headers.hasOwnProperty(key)) {
xhr.setRequestHeader(key, headers[key]);
}
}
}
};
function applyPreInterceptors(options, xhr){
var interceptors = $$httpInterceptors.getInterceptors();
for (var key in interceptors) {
interceptors[key].pre(options, xhr);
}
};
function applyPostInterceptors(options, xhr){
var interceptors = $$httpInterceptors.getInterceptors();
var data = xhr.responseText;
for (var key in interceptors) {
data = interceptors[key].post(data, options, xhr) || data;
}
options.callback(data, xhr);
};
function handleResponse(options, xhr) {
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 ) {
if (xhr.status >=200 && xhr.status<207) {
applyPostInterceptors(options,xhr);
} else {
var out = options.error(xhr);
if (out !== false) {
$$httpErrorHandler.get()(xhr);
}
}
}
}
};
this.send = function(userOpts) {
var options = {};
var defaults = {
callback : Tny.noop,
error : Tny.noop,
method : 'GET',
data : "",
async : true,
dataType : 'JSON',
headers : {}
}
//extend with defaults
options.callback = userOpts.callback || defaults.callback;
options.error = userOpts.error || defaults.error;
options.method = userOpts.method || defaults.method;
options.data = userOpts.data || defaults.data;
options.async = userOpts.async || defaults.async;
options.dataType = userOpts.dataType || defaults.dataType;
options.headers = userOpts.headers || defaults.headers;
options.url = userOpts.url;
if (!options.url) {
throw "HTTP requires URL";
}
if (options.method == 'GET') {
options.url = transformDataToQuery(options.url, options.data);
}
var xhr = getXhrType();
xhr.open(options.method, options.url, options.async);
applyPreInterceptors(options, xhr);
setHeaders($$httpHeaders.getHeaders());
setHeaders(options.headers);
handleResponse(options, xhr);
xhr.send(options.data);
};
}]);
Tny('http')
.run(['$httpJsonInterceptor',function($httpJsonInterceptor){
$httpJsonInterceptor();
}])