-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_ajax.js.html
361 lines (294 loc) · 29.8 KB
/
http_ajax.js.html
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>http/ajax.js - eXir</title>
<meta name="description" content="Javascript Library" />
<meta property="og:title" content=""/>
<meta property="og:type" content="website"/>
<meta property="og:image" content=""/>
<meta property="og:url" content=""/>
<script src="scripts/prettify/prettify.js"></script>
<script src="scripts/prettify/lang-css.js"></script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
<script src="scripts/nav.js" defer></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
<div class="navicon"></div>
</label>
<label for="nav-trigger" class="overlay"></label>
<nav >
<input type="text" id="nav-search" placeholder="Search" />
<h2><a href="index.html">Home</a></h2><h2><a href="https://github.com/kasra-sh/exir" target="_blank" class="menu-item" id="website_link" >Project Website</a></h2><h3>Tutorials</h3><ul><li><a href="tutorial-t00_introduction.html">Introduction</a></li><li><a href="tutorial-t01_installation.html">Installation</a></li><li><a href="tutorial-t02_getting_started.html">Getting Started</a></li><li><a href="tutorial-t03_core.html">Core module</a></li></ul><h3>Modules</h3><ul><li><a href="core.module_core_cases.html">core/cases</a><ul class='methods'><li data-type='method' style='display: none;'><a href="core.module_core_cases.html#.kebab">kebab</a></li><li data-type='method' style='display: none;'><a href="core.module_core_cases.html#.snake">snake</a></li><li data-type='method' style='display: none;'><a href="core.module_core_cases.html#.camel">camel</a></li><li data-type='method' style='display: none;'><a href="core.module_core_cases.html#.pascal">pascal</a></li></ul></li><li><a href="core.module_core_collections.html">core/collections</a><ul class='methods'><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~item">item</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~contains">contains</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~add">add</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~remove">remove</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~toggle">toggle</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~emptyOf">emptyOf</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~concat">concat</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~objectValues">objectValues</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~forN">forN</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~forEachRange">forEachRange</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~forEach">forEach</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~forEachRight">forEachRight</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~firstIndex">firstIndex</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~first">first</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~startsWith">startsWith</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~lastIndex">lastIndex</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~last">last</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~endsWith">endsWith</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~reverse">reverse</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~any">any</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~all">all</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~filter">filter</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~omit">omit</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~filterRight">filterRight</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~maxIndex">maxIndex</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~max">max</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~minIndex">minIndex</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~min">min</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~map">map</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~flatMap">flatMap</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~reduce">reduce</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~reduceRight">reduceRight</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~keyValuePairs">keyValuePairs</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~entries">entries</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~translateObject">translateObject</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~deepMerge">deepMerge</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~deepClone">deepClone</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~join">join</a></li><li data-type='method' style='display: none;'><a href="core.module_core_collections.html#~groupBy">groupBy</a></li></ul></li><li><a href="core.module_core_functions.html">core/functions</a><ul class='methods'><li data-type='method' style='display: none;'><a href="core.module_core_functions.html#~funcBodyEquals">funcBodyEquals</a></li><li data-type='method' style='display: none;'><a href="core.module_core_functions.html#~throttle">throttle</a></li><li data-type='method' style='display: none;'><a href="core.module_core_functions.html#~debounce">debounce</a></li><li data-type='method' style='display: none;'><a href="core.module_core_functions.html#~bindArgs">bindArgs</a></li></ul></li><li><a href="core.module_core_logging.html">core/logging</a><ul class='methods'><li data-type='method' style='display: none;'><a href="core.module_core_logging.html#~showTrace">showTrace</a></li><li data-type='method' style='display: none;'><a href="core.module_core_logging.html#~showInfo">showInfo</a></li><li data-type='method' style='display: none;'><a href="core.module_core_logging.html#~showWarn">showWarn</a></li><li data-type='method' style='display: none;'><a href="core.module_core_logging.html#~showError">showError</a></li><li data-type='method' style='display: none;'><a href="core.module_core_logging.html#~silent">silent</a></li><li data-type='method' style='display: none;'><a href="core.module_core_logging.html#~lvl">lvl</a></li><li data-type='method' style='display: none;'><a href="core.module_core_logging.html#~trace">trace</a></li><li data-type='method' style='display: none;'><a href="core.module_core_logging.html#~info">info</a></li><li data-type='method' style='display: none;'><a href="core.module_core_logging.html#~warn">warn</a></li><li data-type='method' style='display: none;'><a href="core.module_core_logging.html#~error">error</a></li></ul></li><li><a href="core.module_core_strings.html">core/strings</a><ul class='methods'><li data-type='method' style='display: none;'><a href="core.module_core_strings.html#~isDecimal">isDecimal</a></li><li data-type='method' style='display: none;'><a href="core.module_core_strings.html#~isBinary">isBinary</a></li><li data-type='method' style='display: none;'><a href="core.module_core_strings.html#~isOctal">isOctal</a></li><li data-type='method' style='display: none;'><a href="core.module_core_strings.html#~isHex">isHex</a></li><li data-type='method' style='display: none;'><a href="core.module_core_strings.html#~isEmail">isEmail</a></li><li data-type='method' style='display: none;'><a href="core.module_core_strings.html#~startsWith">startsWith</a></li><li data-type='method' style='display: none;'><a href="core.module_core_strings.html#~endsWith">endsWith</a></li><li data-type='method' style='display: none;'><a href="core.module_core_strings.html#~contains">contains</a></li></ul></li><li><a href="core.module_core_types.html">core/types</a><ul class='methods'><li data-type='method' style='display: none;'><a href="core.module_core_types.html#~typeName">typeName</a></li><li data-type='method' style='display: none;'><a href="core.module_core_types.html#~isUnd">isUnd</a></li><li data-type='method' style='display: none;'><a href="core.module_core_types.html#~isNull">isNull</a></li><li data-type='method' style='display: none;'><a href="core.module_core_types.html#~isVal">isVal</a></li><li data-type='method' style='display: none;'><a href="core.module_core_types.html#~isNum">isNum</a></li><li data-type='method' style='display: none;'><a href="core.module_core_types.html#~isStr">isStr</a></li><li data-type='method' style='display: none;'><a href="core.module_core_types.html#~isFun">isFun</a></li><li data-type='method' style='display: none;'><a href="core.module_core_types.html#~isObj">isObj</a></li><li data-type='method' style='display: none;'><a href="core.module_core_types.html#~isArr">isArr</a></li><li data-type='method' style='display: none;'><a href="core.module_core_types.html#~isPrim">isPrim</a></li><li data-type='method' style='display: none;'><a href="core.module_core_types.html#~isList">isList</a></li><li data-type='method' style='display: none;'><a href="core.module_core_types.html#~isMutableList">isMutableList</a></li><li data-type='method' style='display: none;'><a href="core.module_core_types.html#~isSet">isSet</a></li><li data-type='method' style='display: none;'><a href="core.module_core_types.html#~isMap">isMap</a></li><li data-type='method' style='display: none;'><a href="core.module_core_types.html#~isError">isError</a></li><li data-type='method' style='display: none;'><a href="core.module_core_types.html#~isEl">isEl</a></li><li data-type='method' style='display: none;'><a href="core.module_core_types.html#~isEls">isEls</a></li><li data-type='method' style='display: none;'><a href="core.module_core_types.html#~hasField">hasField</a></li><li data-type='method' style='display: none;'><a href="core.module_core_types.html#~isEmpty">isEmpty</a></li><li data-type='method' style='display: none;'><a href="core.module_core_types.html#~dict">dict</a></li></ul></li><li><a href="http.module_http_ajax.html">http/ajax</a></li><li><a href="http.module_http_client.html">http/client</a></li><li><a href="module-http_methods.html">http/methods</a><ul class='methods'><li data-type='method' style='display: none;'><a href="module-http_methods.html#~makeRequest">makeRequest</a></li><li data-type='method' style='display: none;'><a href="module-http_methods.html#~sendRequest">sendRequest</a></li><li data-type='method' style='display: none;'><a href="module-http_methods.html#~Get">Get</a></li><li data-type='method' style='display: none;'><a href="module-http_methods.html#~Post">Post</a></li><li data-type='method' style='display: none;'><a href="module-http_methods.html#~Delete">Delete</a></li><li data-type='method' style='display: none;'><a href="module-http_methods.html#~Put">Put</a></li><li data-type='method' style='display: none;'><a href="module-http_methods.html#~Patch">Patch</a></li><li data-type='method' style='display: none;'><a href="module-http_methods.html#~sendGet">sendGet</a></li><li data-type='method' style='display: none;'><a href="module-http_methods.html#~sendDelete">sendDelete</a></li><li data-type='method' style='display: none;'><a href="module-http_methods.html#~sendPost">sendPost</a></li><li data-type='method' style='display: none;'><a href="module-http_methods.html#~sendPut">sendPut</a></li><li data-type='method' style='display: none;'><a href="module-http_methods.html#~sendPatch">sendPatch</a></li></ul></li><li><a href="dom.module_DOM.html">DOM</a><ul class='methods'><li data-type='method' style='display: none;'><a href="dom.module_DOM.html#~append">append</a></li><li data-type='method' style='display: none;'><a href="dom.module_DOM.html#~getAttributes">getAttributes</a></li><li data-type='method' style='display: none;'><a href="dom.module_DOM.html#~hasAttr">hasAttr</a></li><li data-type='method' style='display: none;'><a href="dom.module_DOM.html#~getAttr">getAttr</a></li><li data-type='method' style='display: none;'><a href="dom.module_DOM.html#~setAttr">setAttr</a></li><li data-type='method' style='display: none;'><a href="dom.module_DOM.html#~attrs">attrs</a></li><li data-type='method' style='display: none;'><a href="dom.module_DOM.html#~cls">cls</a></li><li data-type='method' style='display: none;'><a href="dom.module_DOM.html#~addClass">addClass</a></li><li data-type='method' style='display: none;'><a href="dom.module_DOM.html#~removeClass">removeClass</a></li><li data-type='method' style='display: none;'><a href="dom.module_DOM.html#~hasClass">hasClass</a></li><li data-type='method' style='display: none;'><a href="dom.module_DOM.html#~toggleClass">toggleClass</a></li><li data-type='method' style='display: none;'><a href="dom.module_DOM.html#~setEvent">setEvent</a></li><li data-type='method' style='display: none;'><a href="dom.module_DOM.html#~clearEvent">clearEvent</a></li><li data-type='method' style='display: none;'><a href="dom.module_DOM.html#~hasEvent">hasEvent</a></li><li data-type='method' style='display: none;'><a href="dom.module_DOM.html#~patch">patch</a></li><li data-type='method' style='display: none;'><a href="dom.module_DOM.html#~relLeft">relLeft</a></li><li data-type='method' style='display: none;'><a href="dom.module_DOM.html#~absLeft">absLeft</a></li><li data-type='method' style='display: none;'><a href="dom.module_DOM.html#~relWidth">relWidth</a></li><li data-type='method' style='display: none;'><a href="dom.module_DOM.html#~absWidth">absWidth</a></li><li data-type='method' style='display: none;'><a href="dom.module_DOM.html#~relRight">relRight</a></li><li data-type='method' style='display: none;'><a href="dom.module_DOM.html#~absRight">absRight</a></li><li data-type='method' style='display: none;'><a href="dom.module_DOM.html#~relTop">relTop</a></li><li data-type='method' style='display: none;'><a href="dom.module_DOM.html#~absTop">absTop</a></li><li data-type='method' style='display: none;'><a href="dom.module_DOM.html#~relHeight">relHeight</a></li><li data-type='method' style='display: none;'><a href="dom.module_DOM.html#~absHeight">absHeight</a></li><li data-type='method' style='display: none;'><a href="dom.module_DOM.html#~relBottom">relBottom</a></li><li data-type='method' style='display: none;'><a href="dom.module_DOM.html#~absBottom">absBottom</a></li><li data-type='method' style='display: none;'><a href="dom.module_DOM.html#~$">$</a></li><li data-type='method' style='display: none;'><a href="dom.module_DOM.html#~$$">$$</a></li><li data-type='method' style='display: none;'><a href="dom.module_DOM.html#~queryOf">queryOf</a></li><li data-type='method' style='display: none;'><a href="dom.module_DOM.html#~style">style</a></li><li data-type='method' style='display: none;'><a href="dom.module_DOM.html#~hasStyle">hasStyle</a></li><li data-type='method' style='display: none;'><a href="dom.module_DOM.html#~parseQuery">parseQuery</a></li></ul></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li></ul><h3>Namespaces</h3><ul><li><a href="core.html">core</a></li><li><a href="http.html">http</a></li><li><a href="dom.html">dom</a></li></ul><h3>Classes</h3><ul><li><a href="core.Extension.html">Extension</a><ul class='methods'><li data-type='method' style='display: none;'><a href="core.Extension.html#define">define</a></li></ul></li><li><a href="core.module_core_types-Enum.html">Enum</a></li><li><a href="http.module_http_ajax-Ajax.html">Ajax</a><ul class='methods'><li data-type='method' style='display: none;'><a href="http.module_http_ajax-Ajax.html#header">header</a></li><li data-type='method' style='display: none;'><a href="http.module_http_ajax-Ajax.html#headers">headers</a></li><li data-type='method' style='display: none;'><a href="http.module_http_ajax-Ajax.html#onSuccess">onSuccess</a></li><li data-type='method' style='display: none;'><a href="http.module_http_ajax-Ajax.html#onUploadSuccess">onUploadSuccess</a></li><li data-type='method' style='display: none;'><a href="http.module_http_ajax-Ajax.html#onFail">onFail</a></li><li data-type='method' style='display: none;'><a href="http.module_http_ajax-Ajax.html#onProgress">onProgress</a></li><li data-type='method' style='display: none;'><a href="http.module_http_ajax-Ajax.html#onUploadProgress">onUploadProgress</a></li><li data-type='method' style='display: none;'><a href="http.module_http_ajax-Ajax.html#withContent">withContent</a></li><li data-type='method' style='display: none;'><a href="http.module_http_ajax-Ajax.html#xmlData">xmlData</a></li><li data-type='method' style='display: none;'><a href="http.module_http_ajax-Ajax.html#formData">formData</a></li><li data-type='method' style='display: none;'><a href="http.module_http_ajax-Ajax.html#jsonData">jsonData</a></li><li data-type='method' style='display: none;'><a href="http.module_http_ajax-Ajax.html#urlEncodedData">urlEncodedData</a></li><li data-type='method' style='display: none;'><a href="http.module_http_ajax-Ajax.html#send">send</a></li><li data-type='method' style='display: none;'><a href="http.module_http_ajax-Ajax.html#sendAsync">sendAsync</a></li></ul></li><li><a href="http.module_http_client-XHttpClient.html">XHttpClient</a><ul class='methods'><li data-type='method' style='display: none;'><a href="http.module_http_client-XHttpClient.html#send">send</a></li><li data-type='method' style='display: none;'><a href="http.module_http_client-XHttpClient.html#get">get</a></li><li data-type='method' style='display: none;'><a href="http.module_http_client-XHttpClient.html#post">post</a></li><li data-type='method' style='display: none;'><a href="http.module_http_client-XHttpClient.html#put">put</a></li><li data-type='method' style='display: none;'><a href="http.module_http_client-XHttpClient.html#patch">patch</a></li><li data-type='method' style='display: none;'><a href="http.module_http_client-XHttpClient.html#delete">delete</a></li><li data-type='method' style='display: none;'><a href="http.module_http_client-XHttpClient.html#cancel">cancel</a></li></ul></li><li><a href="http.HttpContent.html">HttpContent</a></li><li><a href="http.HttpRq.html">HttpRq</a><ul class='methods'><li data-type='method' style='display: none;'><a href="http.HttpRq.html#setMethod">setMethod</a></li><li data-type='method' style='display: none;'><a href="http.HttpRq.html#setUrl">setUrl</a></li><li data-type='method' style='display: none;'><a href="http.HttpRq.html#setArg">setArg</a></li><li data-type='method' style='display: none;'><a href="http.HttpRq.html#setHeader">setHeader</a></li><li data-type='method' style='display: none;'><a href="http.HttpRq.html#getHeader">getHeader</a></li><li data-type='method' style='display: none;'><a href="http.HttpRq.html#setContent">setContent</a></li><li data-type='method' style='display: none;'><a href="http.HttpRq.html#jsonContent">jsonContent</a></li><li data-type='method' style='display: none;'><a href="http.HttpRq.html#xmlContent">xmlContent</a></li><li data-type='method' style='display: none;'><a href="http.HttpRq.html#formContent">formContent</a></li><li data-type='method' style='display: none;'><a href="http.HttpRq.html#formMultiPartContent">formMultiPartContent</a></li><li data-type='method' style='display: none;'><a href="http.HttpRq.html#formUrlEncodedContent">formUrlEncodedContent</a></li></ul></li><li><a href="http.HttpRs.html">HttpRs</a></li><li><a href="dom.Attributes.html">Attributes</a><ul class='methods'><li data-type='method' style='display: none;'><a href="dom.Attributes.html#keys">keys</a></li><li data-type='method' style='display: none;'><a href="dom.Attributes.html#all">all</a></li><li data-type='method' style='display: none;'><a href="dom.Attributes.html#set">set</a></li><li data-type='method' style='display: none;'><a href="dom.Attributes.html#get">get</a></li><li data-type='method' style='display: none;'><a href="dom.Attributes.html#has">has</a></li><li data-type='method' style='display: none;'><a href="dom.Attributes.html#remove">remove</a></li></ul></li><li><a href="dom.Classes.html">Classes</a><ul class='methods'><li data-type='method' style='display: none;'><a href="dom.Classes.html#.split">split</a></li><li data-type='method' style='display: none;'><a href="dom.Classes.html#.of">of</a></li><li data-type='method' style='display: none;'><a href="dom.Classes.html#contains">contains</a></li><li data-type='method' style='display: none;'><a href="dom.Classes.html#add">add</a></li><li data-type='method' style='display: none;'><a href="dom.Classes.html#remove">remove</a></li><li data-type='method' style='display: none;'><a href="dom.Classes.html#toggle">toggle</a></li></ul></li><li><a href="dom.module_DOM-QueryParams.html">QueryParams</a></li></ul><h3>Global</h3><ul><li><a href="global.html#HttpMethod">HttpMethod</a></li><li><a href="global.html#HttpRequestOptions">HttpRequestOptions</a></li></ul>
</nav>
<div id="main">
<h1 class="page-title">http/ajax.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* Wrapper class for XHR
* @module http/ajax
* @memberOf http
*/
const {HttpRq, HttpContent} = require("./request");
const {HttpRs} = require("./response");
const T = require("../core/types");
const {forEach} = require("../core/collections");
/**
* A wrapper class for {@link XMLHttpRequest} to facilitate sending requests and handling events
* @class
*/
class Ajax {
/**
* @constructor
* @param {HttpMethod|HttpRq} m - Request method string or {@link http.HttpRq}
* @param {String} [url]
* @param {Object} [params] - Request parameters object
* @param {Object?} [headers] - Headers object
* @param {HttpContent?} [content] - Optional http content {@link HttpContent}
*/
constructor(m, url, params = {}, headers = {}, content = new HttpContent()) {
/** @type {HttpRq}
* @see {http.HttpRq}
*/
this.rq = {}
/** @type {http.HttpRs}
* @see {http.HttpRs}
*/
this.rs = {}
if (m instanceof HttpRq) {
this.rq = m
} else {
this.rq = new HttpRq(m, url, params, headers, content);
}
// Fields
this.rs = {readyState: 0};
this.xhr = new XMLHttpRequest();
this.preparedCallback = function (rq) {
};
this.progressCallback = function (ev, rq) {
};
this.uploadProgressCallback = function (xhr) {
};
this.successCallback = function (rq, rs) {
};
this.uploadFinishCallback = function (xhr) {
};
this.failCallback = function (rq, rs) {
};
Object.defineProperty(this, 'xhr', {enumerable: false})
}
// Methods
/**
* Set header
* @param {String} n - header name
* @param {String} [v] - header value
* @returns {Ajax}
*/
header(n, v) {
this.rq.setHeader(n, v);
return this;
};
/**
* Add/Set headers
* @param {Object} hdrs - headers object
* @returns {Ajax}
*/
headers(hdrs = {}) {
forEach(hdrs, (v, k) => {
this.rq.setHeader(k, v);
});
return this;
};
/**
* @param {Function} callbackRqRs
* @returns {Ajax}
*/
onSuccess(callbackRqRs) {
this.successCallback = callbackRqRs;
return this;
};
/**
* @param {Function} callbackRqRs
* @returns {Ajax}
*/
onUploadSuccess(callbackRqRs) {
this.uploadFinishCallback = callbackRqRs;
return this;
};
/**
* @param {Function} callbackRqRs
* @returns {Ajax}
*/
onFail(callbackRqRs) {
this.failCallback = callbackRqRs;
return this;
};
/**
* @param {Function} callbackRqRs
* @returns {Ajax}
*/
onProgress(callbackRqRs) {
this.progressCallback = callbackRqRs;
return this;
};
/**
* @param {Function} callbackRqRs
* @returns {Ajax}
*/
onUploadProgress(callbackRqRs) {
this.uploadProgressCallback = callbackRqRs;
return this;
};
/**
* Set custom content {@link HttpContent}
* @param {HttpContent} content
* @returns {Ajax}
*/
withContent(content={}) {
switch (content.type) {
case 'json': this.rq.jsonContent(content.data); break;
case 'xml': this.rq.xmlContent(content.data); break;
case 'form': this.rq.formContent(content.data); break;
case 'form_multipart': this.rq.formMultiPartContent(content.data); break;
case 'form_urlencoded': this.rq.formUrlEncodedContent(content.data); break;
default: this.rq.setContent(content.type, content.data);
}
return this;
}
/**
* Set xml request data
* @param {XMLDocument} data
* @returns {Ajax}
*/
xmlData(data) {
this.rq.xmlContent(data);
return this;
}
/**
* Set form-data request data
* @param {String|Node} form
* @returns {Ajax}
*/
formData(form) {
this.rq.formContent(form);
return this;
};
/**
* Set json request data
* @param {String|Object} data
* @returns {Ajax}
*/
jsonData(data) {
this.rq.jsonContent(data);
return this;
};
/**
* Set url-encoded request data
* @param {Object} data - simple data object
* @returns {Ajax}
*/
urlEncodedData(data) {
this.rq.formUrlEncodedContent(data);
return this;
};
_prepare(reset) {
if (this.isPrepared && !reset) {
// self.onprepare && self.onprepare(self.rq);
return this
}
// prepare url
let url = this.rq.url;
if (this.rq.args && !T.isEmpty(this.rq.args)) {
url.indexOf('?') >= 0 || (url += '?');
url += this.rq.buildUrlEncoded();
}
reset && (this.xhr = new XMLHttpRequest());
this.xhr.open(this.rq.method, url);
// prepare headers
for (let h in this.rq.headers) {
if (this.rq.headers.hasOwnProperty(h))
this.xhr.setRequestHeader(h, this.rq.headers[h]);
}
this.isPrepared = true;
this.preparedCallback && this.preparedCallback(this.rq);
// preparedCallback && preparedCallback();
return this;
};
/**
* Send XHR request
* @param {Function} [finishCallback] - called after all other callbacks
* @returns {Ajax}
*/
send(finishCallback) {
this._prepare();
let ajax = this;
let xhr = this.xhr;
this.xhr.onreadystatechange = function (ev) {
// onloadend
if (xhr.readyState === 4) {
let callback;
if (xhr.status >= 200 && xhr.status <= 399) {
callback = ajax.successCallback;
} else {
callback = ajax.failCallback;
}
ajax.rs = new HttpRs(xhr);
finishCallback && finishCallback(ajax.rq, ajax.rs, ajax.xhr);
callback && callback(ajax.rq, ajax.rs, ajax.xhr);
}
};
this.xhr.onprogress = function (ev) {
ajax.progressCallback && ajax.progressCallback(ev, ajax);
};
this.xhr.upload.onprogress = function (ev) {
ajax.uploadProgressCallback && ajax.uploadProgressCallback(ev, ajax);
};
this.xhr.upload.onloadend = function (ev) {
ajax.uploadFinishCallback && ajax.uploadFinishCallback(ev, ajax);
};
try {
this.xhr.send(this.rq.content.data);
} catch (e) {
this.onFail(e);
}
return ajax;
}
/**
* Send request with Promise
* @return {Promise<Ajax>}
*/
async sendAsync() {
const ajax = this;
const promise = new Promise((res, rej) => {
ajax.onSuccess(()=>{
return res(ajax)
});
ajax.onFail(()=>rej(ajax));
});
ajax.send();
return promise;
}
}
module.exports = {Ajax: Ajax};</code></pre>
</article>
</section>
</div>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.6</a> using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>
<script>prettyPrint();</script>
<script src="scripts/polyfill.js"></script>
<script src="scripts/linenumber.js"></script>
<script src="scripts/search.js" defer></script>
<script src="scripts/collapse.js" defer></script>
</body>
</html>