-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
226 lines (204 loc) · 9.13 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
'use strict';
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
;(function (name, __context_, definition) {
if (typeof module !== 'undefined' && module.exports) {
module.exports = definition();
} else if (typeof layui !== 'undefined') {
layui.define(function (exports) {
exports(name, definition());
});
} else {
__context_ && (__context_[name] = definition());
}
})("minare", (typeof window === 'undefined' ? 'undefined' : _typeof(window)) !== "object" ? undefined : window, function () {
var instance = function instance() {
var config = function config() {
return {
base_url: "",
handler: {
onComplete: null, onTimeout: null, onUploadProgress: null, statusHandlerMapping: {}
},
header: {},
timeout: 0
};
};
function wrapEmptyIfNotFunction(f) {
return f && typeof f === "function" ? f : function () {};
}
var getFunction = function getFunction(f, nullable) {
return f && typeof f === "function" ? f : nullable ? null : function () {};
};
var getStatusProperty = function getStatusProperty(status) {
return "status_" + status;
};
var getHandlerFromConfig = function getHandlerFromConfig(config, status) {
return config["handler"] ? config["handler"].statusHandlerMapping[getStatusProperty(status)] : null;
};
var globalConfig = new config();
var configure = {
when: function when(status, handler) {
globalConfig.handler.statusHandlerMapping[getStatusProperty(status)] = wrapEmptyIfNotFunction(handler);
return this;
},
whenTimeout: function whenTimeout(handler) {
globalConfig.handler.onTimeout = wrapEmptyIfNotFunction(handler);
return this;
},
whenComplete: function whenComplete(handler) {
globalConfig.handler.onComplete = wrapEmptyIfNotFunction(handler);
return this;
},
whenUploadProgress: function whenUploadProgress(handler) {
globalConfig.handler.onUploadProgress = wrapEmptyIfNotFunction(handler);
return this;
},
url: function url(baseURL) {
if (typeof baseURL === "function") {
baseURL = baseURL();
} else if (typeof baseURL !== "string") {
baseURL = "";
}
globalConfig.base_url = baseURL;
return this;
},
timeout: function timeout(_timeout) {
globalConfig.timeout = typeof _timeout === "number" ? _timeout : 0;
return this;
},
addRequestHeader: function addRequestHeader(key, value) {
if (key && value) {
globalConfig.header[key] = value;
}
return this;
}
};
var minare = function minare(method, url, body) {
var xhr = new XMLHttpRequest();
xhr.open(method, globalConfig.base_url + url);
var isJsonResponse = true;
var _config = new config();
var getResponse = function getResponse(xhr) {
if (isJsonResponse) {
try {
return JSON.parse(xhr.responseText);
} catch (e) {
console.warn("error convert to json data,return response text");
return xhr.responseText;
}
}
return xhr.responseText;
};
return {
onSuccess: function onSuccess(f) {
_config.handler.statusHandlerMapping[getStatusProperty(200)] = getFunction(f, true);
return this;
},
onError: function onError(f) {
_config.handler.statusHandlerMapping[getStatusProperty(500)] = getFunction(f, true);
_config.handler.statusHandlerMapping[getStatusProperty(502)] = getFunction(f, true);
_config.handler.statusHandlerMapping[getStatusProperty(503)] = getFunction(f, true);
return this;
},
onComplete: function onComplete(f) {
_config.handler.onComplete = getFunction(f, true);
return this;
},
onTimeout: function onTimeout(f) {
_config.handler.onTimeout = getFunction(f, true);
return this;
},
onUploadProgress: function onUploadProgress(f) {
_config.handler.onUploadProgress = getFunction(f, true);
},
setHeader: function setHeader(key, value) {
xhr.setRequestHeader(key, value);
return this;
},
stringResponse: function stringResponse() {
isJsonResponse = false;
return this;
},
$xhr: function $xhr(f) {
f(xhr);
return this;
},
execute: function execute() {
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
var status = xhr.status;
var handler = getHandlerFromConfig(_config, status);
var globalHandler = getHandlerFromConfig(globalConfig, status);
var resp = getResponse(xhr);
if (handler) {
handler(resp, status);
} else {
globalHandler && globalHandler(resp, status);
}
if (_config.handler.onComplete) {
_config.handler.onComplete(resp, status);
} else {
globalConfig.handler.onComplete && globalConfig.handler.onComplete(resp, status);
}
}
};
var headerProperty = Object.getOwnPropertyNames(globalConfig.header);
for (var i = 0; i < headerProperty.length; i++) {
var name = headerProperty[i];
var value = globalConfig.header[name];
xhr.setRequestHeader(name, typeof value === "function" ? value({ method: method, url: url, body: body }) : value);
}
xhr.timeout = globalConfig.timeout;
xhr.ontimeout = function (ev) {
if (_config.handler.onTimeout) {
_config.handler.onTimeout(ev);
} else {
globalConfig.handler.onTimeout && globalConfig.handler.onTimeout(ev);
}
};
xhr.upload.onprogress = function (ev) {
if (_config.handler.onUploadProgress) {
_config.handler.onUploadProgress(ev);
} else {
globalConfig.handler.onUploadProgress && globalConfig.handler.onUploadProgress(ev);
}
};
if (body === undefined) {
xhr.send(null);
} else if (body instanceof FormData || body instanceof File || body instanceof Blob || typeof body === "string") {
xhr.send(body);
} else {
var sendBody = body;
try {
sendBody = JSON.stringify(body);
} catch (e) {}
xhr.send(sendBody);
}
}
};
};
return {
newInstance: function newInstance() {
return instance();
},
config: function config(f) {
if (typeof f === "function") {
f(configure);
}
return Object.assign({}, globalConfig);
},
newGet: function newGet(url) {
return new minare("GET", url);
},
newPost: function newPost(url, body) {
return new minare("POST", url, body);
},
newPut: function newPut(url, body) {
return new minare("PUT", url, body);
},
newDelete: function newDelete(url, body) {
return new minare("DELETE", url, body);
}
};
};
return instance();
});