@brandup/ui-ajax 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.js +211 -198
- package/dist/cjs/index.js.map +1 -1
- package/dist/mjs/index.js +211 -198
- package/dist/mjs/index.js.map +1 -1
- package/dist/types.d.ts +24 -10
- package/package.json +1 -1
package/dist/cjs/index.js
CHANGED
|
@@ -1,123 +1,143 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const createQuery = (query) => {
|
|
4
|
+
const urlParams = new URLSearchParams();
|
|
5
|
+
if (!query)
|
|
6
|
+
return urlParams;
|
|
7
|
+
if (query instanceof FormData) {
|
|
8
|
+
query.forEach((value, key) => {
|
|
9
|
+
if (!key)
|
|
10
|
+
return;
|
|
11
|
+
query.append(key, value.toString());
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
for (const key in query) {
|
|
16
|
+
const val = query[key];
|
|
17
|
+
if (val === null)
|
|
18
|
+
continue;
|
|
19
|
+
if (Array.isArray(val))
|
|
20
|
+
val.forEach(v => urlParams.append(key, v));
|
|
21
|
+
else
|
|
22
|
+
urlParams.append(key, val);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return urlParams;
|
|
26
|
+
};
|
|
27
|
+
const addQuery = (url, query) => {
|
|
28
|
+
if (query) {
|
|
29
|
+
const urlParams = createQuery(query);
|
|
30
|
+
if (urlParams.size) {
|
|
31
|
+
if (url.indexOf("?") === -1)
|
|
32
|
+
url += "?";
|
|
33
|
+
else
|
|
34
|
+
url += "&";
|
|
35
|
+
url += urlParams.toString();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return url;
|
|
39
|
+
};
|
|
40
|
+
const encodeForm = (data) => {
|
|
41
|
+
const query = createQuery(data);
|
|
42
|
+
return query.toString();
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
var helpers = /*#__PURE__*/Object.freeze({
|
|
46
|
+
__proto__: null,
|
|
47
|
+
addQuery: addQuery,
|
|
48
|
+
createQuery: createQuery,
|
|
49
|
+
encodeForm: encodeForm
|
|
50
|
+
});
|
|
51
|
+
|
|
3
52
|
const DEFAULT_TIMEOUT = 30000;
|
|
4
53
|
const FORM_URL = "application/x-www-form-urlencoded";
|
|
5
54
|
const FORM_DATA = "multipart/form-data";
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
55
|
+
const detectRequestType = (options) => {
|
|
56
|
+
if (!options.type && options.data) {
|
|
57
|
+
const body = options.data;
|
|
58
|
+
if (body instanceof Blob)
|
|
59
|
+
options.type = "BLOB";
|
|
60
|
+
else if (body instanceof FormData)
|
|
61
|
+
options.type = null;
|
|
62
|
+
else if (body instanceof HTMLFormElement)
|
|
63
|
+
options.type = "FORM";
|
|
64
|
+
else if (body instanceof Object)
|
|
65
|
+
options.type = "JSON";
|
|
66
|
+
else if (typeof body === "string")
|
|
67
|
+
options.type = "TEXT";
|
|
13
68
|
}
|
|
14
|
-
return data;
|
|
15
69
|
};
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
70
|
+
const prepareRequest = (options, body) => {
|
|
71
|
+
const headers = {};
|
|
72
|
+
if (options.headers) {
|
|
73
|
+
for (const key in options.headers) {
|
|
74
|
+
const value = options.headers[key];
|
|
75
|
+
if (!value)
|
|
76
|
+
continue;
|
|
77
|
+
headers[key] = value;
|
|
78
|
+
}
|
|
23
79
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
if (contentType) {
|
|
43
|
-
if (contentType.includes("json")) {
|
|
44
|
-
responseType = "json";
|
|
45
|
-
responseData = JSON.parse(xhr.responseText);
|
|
46
|
-
}
|
|
47
|
-
else if (contentType.includes("text/plain")) {
|
|
48
|
-
responseType = "text";
|
|
49
|
-
responseData = xhr.responseText;
|
|
50
|
-
}
|
|
51
|
-
else if (contentType.includes("text/html")) {
|
|
52
|
-
responseType = "html";
|
|
53
|
-
responseData = xhr.responseText;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
const headers = {
|
|
58
|
-
get(name) {
|
|
59
|
-
return xhr.getResponseHeader(name);
|
|
60
|
-
},
|
|
61
|
-
has(name) {
|
|
62
|
-
return !!xhr.getResponseHeader(name);
|
|
63
|
-
},
|
|
64
|
-
forEach(callbackfn, thisArg) {
|
|
65
|
-
const headers = xhr.getAllResponseHeaders();
|
|
66
|
-
// Convert the header string into an array
|
|
67
|
-
// of individual headers
|
|
68
|
-
const arr = headers.trim().split(/[\r\n]+/);
|
|
69
|
-
// Create a map of header names to values
|
|
70
|
-
arr.forEach((line) => {
|
|
71
|
-
const parts = line.split(": ");
|
|
72
|
-
const header = parts.shift() || "";
|
|
73
|
-
const value = parts.join(": ");
|
|
74
|
-
callbackfn.call(thisArg, value, header.toLowerCase(), {});
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
options.success({
|
|
79
|
-
status: xhr.status,
|
|
80
|
-
url: xhr.responseURL,
|
|
81
|
-
redirected: false,
|
|
82
|
-
type: responseType,
|
|
83
|
-
contentType,
|
|
84
|
-
headers,
|
|
85
|
-
data: responseData,
|
|
86
|
-
state: options.state
|
|
87
|
-
});
|
|
80
|
+
if (options.type) {
|
|
81
|
+
let contentType = null;
|
|
82
|
+
let accept = null;
|
|
83
|
+
switch (options.type) {
|
|
84
|
+
case "XML":
|
|
85
|
+
contentType = "application/xml; charset=utf-8";
|
|
86
|
+
accept = "application/xml, text/xml, */*; q=0.01";
|
|
87
|
+
break;
|
|
88
|
+
case "JSON":
|
|
89
|
+
contentType = "application/json; charset=utf-8";
|
|
90
|
+
accept = "application/json, text/json, */*; q=0.01";
|
|
91
|
+
body = JSON.stringify(body);
|
|
92
|
+
break;
|
|
93
|
+
case "FORM":
|
|
94
|
+
if (body instanceof HTMLFormElement) {
|
|
95
|
+
//const form = <HTMLFormElement>body;
|
|
96
|
+
//contentType = form.enctype ?? FORM_URL;
|
|
97
|
+
body = new FormData(body);
|
|
88
98
|
}
|
|
99
|
+
else if (body instanceof FormData)
|
|
100
|
+
contentType = FORM_URL;
|
|
101
|
+
if (contentType == FORM_URL)
|
|
102
|
+
body = encodeForm(body);
|
|
103
|
+
else if (contentType == FORM_DATA)
|
|
104
|
+
contentType = null;
|
|
105
|
+
break;
|
|
106
|
+
case "FORMDATA":
|
|
107
|
+
break;
|
|
108
|
+
case "TEXT":
|
|
109
|
+
contentType = "text/plain";
|
|
89
110
|
break;
|
|
90
|
-
}
|
|
91
111
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
if (
|
|
95
|
-
|
|
96
|
-
};
|
|
97
|
-
xhr.open(method, url, true);
|
|
98
|
-
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
|
|
99
|
-
for (const key in prepared.headers) {
|
|
100
|
-
const value = prepared.headers[key];
|
|
101
|
-
if (!value)
|
|
102
|
-
continue;
|
|
103
|
-
xhr.setRequestHeader(key, value);
|
|
112
|
+
if (accept)
|
|
113
|
+
headers["Accept"] = accept;
|
|
114
|
+
if (contentType)
|
|
115
|
+
headers['Content-Type'] = contentType;
|
|
104
116
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
return xhr;
|
|
117
|
+
return {
|
|
118
|
+
headers,
|
|
119
|
+
body
|
|
120
|
+
};
|
|
110
121
|
};
|
|
122
|
+
var internals = {
|
|
123
|
+
DEFAULT_TIMEOUT,
|
|
124
|
+
FORM_URL,
|
|
125
|
+
FORM_DATA,
|
|
126
|
+
detectRequestType,
|
|
127
|
+
prepareRequest
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
/** Request with fetch. */
|
|
111
131
|
const request = async (options, abortSignal) => {
|
|
112
132
|
let url = options.url || location.href;
|
|
113
|
-
url =
|
|
133
|
+
url = addQuery(url, options.query);
|
|
114
134
|
const method = options.method ? options.method.toUpperCase() : "GET";
|
|
115
135
|
let body = options.data;
|
|
116
136
|
if (body && method === "GET")
|
|
117
137
|
throw new Error("GET method is not support request with data.");
|
|
118
|
-
detectRequestType(options);
|
|
119
|
-
const prepared = prepareRequest(options, body);
|
|
120
|
-
const abortSignals = [AbortSignal.timeout(options.timeout ?? DEFAULT_TIMEOUT)];
|
|
138
|
+
internals.detectRequestType(options);
|
|
139
|
+
const prepared = internals.prepareRequest(options, body);
|
|
140
|
+
const abortSignals = [AbortSignal.timeout(options.timeout ?? internals.DEFAULT_TIMEOUT)];
|
|
121
141
|
if (abortSignal)
|
|
122
142
|
abortSignals.push(abortSignal);
|
|
123
143
|
try {
|
|
@@ -192,109 +212,102 @@ const request = async (options, abortSignal) => {
|
|
|
192
212
|
throw new Error(`Error ajax request: ${(error && error.message) ? error.message : error}`);
|
|
193
213
|
}
|
|
194
214
|
};
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
else if (body instanceof Object)
|
|
205
|
-
options.type = "JSON";
|
|
206
|
-
else if (typeof body === "string")
|
|
207
|
-
options.type = "TEXT";
|
|
208
|
-
}
|
|
209
|
-
};
|
|
210
|
-
const prepareRequest = (options, body) => {
|
|
211
|
-
const headers = {};
|
|
212
|
-
if (options.headers) {
|
|
213
|
-
for (const key in options.headers) {
|
|
214
|
-
const value = options.headers[key];
|
|
215
|
-
if (!value)
|
|
216
|
-
continue;
|
|
217
|
-
headers[key] = value;
|
|
218
|
-
}
|
|
215
|
+
|
|
216
|
+
/** Request with XMLHttpRequest. */
|
|
217
|
+
const ajaxRequest = (options) => {
|
|
218
|
+
let url = options.url || location.href;
|
|
219
|
+
let { query } = options;
|
|
220
|
+
if (options.disableCache) {
|
|
221
|
+
if (!query)
|
|
222
|
+
query = {};
|
|
223
|
+
query["_"] = new Date().getTime().toString();
|
|
219
224
|
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
225
|
+
url = addQuery(url, query);
|
|
226
|
+
const method = options.method ? options.method : "GET";
|
|
227
|
+
if (options.data && method === "GET")
|
|
228
|
+
throw new Error("GET method is not support request with data.");
|
|
229
|
+
internals.detectRequestType(options);
|
|
230
|
+
const prepared = internals.prepareRequest(options, options.data);
|
|
231
|
+
const xhr = new XMLHttpRequest();
|
|
232
|
+
xhr.withCredentials = true;
|
|
233
|
+
if (options.timeout === 0 || options.timeout)
|
|
234
|
+
xhr.timeout = options.timeout;
|
|
235
|
+
xhr.onreadystatechange = (e) => {
|
|
236
|
+
switch (xhr.readyState) {
|
|
237
|
+
case XMLHttpRequest.DONE: {
|
|
238
|
+
if (options.success) {
|
|
239
|
+
let responseData = null;
|
|
240
|
+
let responseType = "none";
|
|
241
|
+
const contentType = xhr.getResponseHeader("Content-Type");
|
|
242
|
+
if (xhr.response) {
|
|
243
|
+
if (contentType) {
|
|
244
|
+
if (contentType.includes("json")) {
|
|
245
|
+
responseType = "json";
|
|
246
|
+
responseData = JSON.parse(xhr.responseText);
|
|
247
|
+
}
|
|
248
|
+
else if (contentType.includes("text/plain")) {
|
|
249
|
+
responseType = "text";
|
|
250
|
+
responseData = xhr.responseText;
|
|
251
|
+
}
|
|
252
|
+
else if (contentType.includes("text/html")) {
|
|
253
|
+
responseType = "html";
|
|
254
|
+
responseData = xhr.responseText;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
const headers = {
|
|
259
|
+
get(name) {
|
|
260
|
+
return xhr.getResponseHeader(name);
|
|
261
|
+
},
|
|
262
|
+
has(name) {
|
|
263
|
+
return !!xhr.getResponseHeader(name);
|
|
264
|
+
},
|
|
265
|
+
forEach(callbackfn, thisArg) {
|
|
266
|
+
const headers = xhr.getAllResponseHeaders();
|
|
267
|
+
// Convert the header string into an array
|
|
268
|
+
// of individual headers
|
|
269
|
+
const arr = headers.trim().split(/[\r\n]+/);
|
|
270
|
+
// Create a map of header names to values
|
|
271
|
+
arr.forEach((line) => {
|
|
272
|
+
const parts = line.split(": ");
|
|
273
|
+
const header = parts.shift() || "";
|
|
274
|
+
const value = parts.join(": ");
|
|
275
|
+
callbackfn.call(thisArg, value, header.toLowerCase(), {});
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
options.success({
|
|
280
|
+
status: xhr.status,
|
|
281
|
+
url: xhr.responseURL,
|
|
282
|
+
redirected: false,
|
|
283
|
+
type: responseType,
|
|
284
|
+
contentType,
|
|
285
|
+
headers,
|
|
286
|
+
data: responseData,
|
|
287
|
+
state: options.state
|
|
288
|
+
});
|
|
238
289
|
}
|
|
239
|
-
else if (body instanceof FormData)
|
|
240
|
-
contentType = FORM_URL;
|
|
241
|
-
if (contentType == FORM_URL)
|
|
242
|
-
body = encodeForm(body);
|
|
243
|
-
else if (contentType == FORM_DATA)
|
|
244
|
-
contentType = null;
|
|
245
|
-
break;
|
|
246
|
-
case "FORMDATA":
|
|
247
|
-
break;
|
|
248
|
-
case "TEXT":
|
|
249
|
-
contentType = "text/plain";
|
|
250
290
|
break;
|
|
291
|
+
}
|
|
251
292
|
}
|
|
252
|
-
if (accept)
|
|
253
|
-
headers["Accept"] = accept;
|
|
254
|
-
if (contentType)
|
|
255
|
-
headers['Content-Type'] = contentType;
|
|
256
|
-
}
|
|
257
|
-
return {
|
|
258
|
-
headers,
|
|
259
|
-
body
|
|
260
293
|
};
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
294
|
+
xhr.onabort = (e) => {
|
|
295
|
+
if (options.error)
|
|
296
|
+
options.error(options, "Request aborted");
|
|
297
|
+
};
|
|
298
|
+
xhr.open(method, url, true);
|
|
299
|
+
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
|
|
300
|
+
for (const key in prepared.headers) {
|
|
301
|
+
const value = prepared.headers[key];
|
|
302
|
+
if (!value)
|
|
269
303
|
continue;
|
|
270
|
-
|
|
271
|
-
val.forEach(v => urlParams.append(key, v));
|
|
272
|
-
else
|
|
273
|
-
urlParams.append(key, val);
|
|
274
|
-
}
|
|
275
|
-
return urlParams;
|
|
276
|
-
};
|
|
277
|
-
const extendUrl = (url, query) => {
|
|
278
|
-
if (query) {
|
|
279
|
-
const urlParams = createSearchParams(query);
|
|
280
|
-
if (urlParams.size) {
|
|
281
|
-
if (url.indexOf("?") === -1)
|
|
282
|
-
url += "?";
|
|
283
|
-
else
|
|
284
|
-
url += "&";
|
|
285
|
-
url += urlParams.toString();
|
|
286
|
-
}
|
|
304
|
+
xhr.setRequestHeader(key, value);
|
|
287
305
|
}
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
if (!key)
|
|
294
|
-
return;
|
|
295
|
-
query.append(key, value.toString());
|
|
296
|
-
});
|
|
297
|
-
return query.toString();
|
|
306
|
+
if (method === "GET")
|
|
307
|
+
xhr.send();
|
|
308
|
+
else
|
|
309
|
+
xhr.send(prepared.body);
|
|
310
|
+
return xhr;
|
|
298
311
|
};
|
|
299
312
|
|
|
300
313
|
class AjaxQueue {
|
|
@@ -386,7 +399,7 @@ class AjaxQueue {
|
|
|
386
399
|
}
|
|
387
400
|
|
|
388
401
|
exports.AjaxQueue = AjaxQueue;
|
|
402
|
+
exports.RequestHelper = helpers;
|
|
389
403
|
exports.ajaxRequest = ajaxRequest;
|
|
390
404
|
exports.request = request;
|
|
391
|
-
exports.urlEncode = urlEncode;
|
|
392
405
|
//# sourceMappingURL=index.js.map
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/mjs/index.js
CHANGED
|
@@ -1,121 +1,141 @@
|
|
|
1
|
+
const createQuery = (query) => {
|
|
2
|
+
const urlParams = new URLSearchParams();
|
|
3
|
+
if (!query)
|
|
4
|
+
return urlParams;
|
|
5
|
+
if (query instanceof FormData) {
|
|
6
|
+
query.forEach((value, key) => {
|
|
7
|
+
if (!key)
|
|
8
|
+
return;
|
|
9
|
+
query.append(key, value.toString());
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
for (const key in query) {
|
|
14
|
+
const val = query[key];
|
|
15
|
+
if (val === null)
|
|
16
|
+
continue;
|
|
17
|
+
if (Array.isArray(val))
|
|
18
|
+
val.forEach(v => urlParams.append(key, v));
|
|
19
|
+
else
|
|
20
|
+
urlParams.append(key, val);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return urlParams;
|
|
24
|
+
};
|
|
25
|
+
const addQuery = (url, query) => {
|
|
26
|
+
if (query) {
|
|
27
|
+
const urlParams = createQuery(query);
|
|
28
|
+
if (urlParams.size) {
|
|
29
|
+
if (url.indexOf("?") === -1)
|
|
30
|
+
url += "?";
|
|
31
|
+
else
|
|
32
|
+
url += "&";
|
|
33
|
+
url += urlParams.toString();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return url;
|
|
37
|
+
};
|
|
38
|
+
const encodeForm = (data) => {
|
|
39
|
+
const query = createQuery(data);
|
|
40
|
+
return query.toString();
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
var helpers = /*#__PURE__*/Object.freeze({
|
|
44
|
+
__proto__: null,
|
|
45
|
+
addQuery: addQuery,
|
|
46
|
+
createQuery: createQuery,
|
|
47
|
+
encodeForm: encodeForm
|
|
48
|
+
});
|
|
49
|
+
|
|
1
50
|
const DEFAULT_TIMEOUT = 30000;
|
|
2
51
|
const FORM_URL = "application/x-www-form-urlencoded";
|
|
3
52
|
const FORM_DATA = "multipart/form-data";
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
53
|
+
const detectRequestType = (options) => {
|
|
54
|
+
if (!options.type && options.data) {
|
|
55
|
+
const body = options.data;
|
|
56
|
+
if (body instanceof Blob)
|
|
57
|
+
options.type = "BLOB";
|
|
58
|
+
else if (body instanceof FormData)
|
|
59
|
+
options.type = null;
|
|
60
|
+
else if (body instanceof HTMLFormElement)
|
|
61
|
+
options.type = "FORM";
|
|
62
|
+
else if (body instanceof Object)
|
|
63
|
+
options.type = "JSON";
|
|
64
|
+
else if (typeof body === "string")
|
|
65
|
+
options.type = "TEXT";
|
|
11
66
|
}
|
|
12
|
-
return data;
|
|
13
67
|
};
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
68
|
+
const prepareRequest = (options, body) => {
|
|
69
|
+
const headers = {};
|
|
70
|
+
if (options.headers) {
|
|
71
|
+
for (const key in options.headers) {
|
|
72
|
+
const value = options.headers[key];
|
|
73
|
+
if (!value)
|
|
74
|
+
continue;
|
|
75
|
+
headers[key] = value;
|
|
76
|
+
}
|
|
21
77
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
if (contentType) {
|
|
41
|
-
if (contentType.includes("json")) {
|
|
42
|
-
responseType = "json";
|
|
43
|
-
responseData = JSON.parse(xhr.responseText);
|
|
44
|
-
}
|
|
45
|
-
else if (contentType.includes("text/plain")) {
|
|
46
|
-
responseType = "text";
|
|
47
|
-
responseData = xhr.responseText;
|
|
48
|
-
}
|
|
49
|
-
else if (contentType.includes("text/html")) {
|
|
50
|
-
responseType = "html";
|
|
51
|
-
responseData = xhr.responseText;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
const headers = {
|
|
56
|
-
get(name) {
|
|
57
|
-
return xhr.getResponseHeader(name);
|
|
58
|
-
},
|
|
59
|
-
has(name) {
|
|
60
|
-
return !!xhr.getResponseHeader(name);
|
|
61
|
-
},
|
|
62
|
-
forEach(callbackfn, thisArg) {
|
|
63
|
-
const headers = xhr.getAllResponseHeaders();
|
|
64
|
-
// Convert the header string into an array
|
|
65
|
-
// of individual headers
|
|
66
|
-
const arr = headers.trim().split(/[\r\n]+/);
|
|
67
|
-
// Create a map of header names to values
|
|
68
|
-
arr.forEach((line) => {
|
|
69
|
-
const parts = line.split(": ");
|
|
70
|
-
const header = parts.shift() || "";
|
|
71
|
-
const value = parts.join(": ");
|
|
72
|
-
callbackfn.call(thisArg, value, header.toLowerCase(), {});
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
|
-
options.success({
|
|
77
|
-
status: xhr.status,
|
|
78
|
-
url: xhr.responseURL,
|
|
79
|
-
redirected: false,
|
|
80
|
-
type: responseType,
|
|
81
|
-
contentType,
|
|
82
|
-
headers,
|
|
83
|
-
data: responseData,
|
|
84
|
-
state: options.state
|
|
85
|
-
});
|
|
78
|
+
if (options.type) {
|
|
79
|
+
let contentType = null;
|
|
80
|
+
let accept = null;
|
|
81
|
+
switch (options.type) {
|
|
82
|
+
case "XML":
|
|
83
|
+
contentType = "application/xml; charset=utf-8";
|
|
84
|
+
accept = "application/xml, text/xml, */*; q=0.01";
|
|
85
|
+
break;
|
|
86
|
+
case "JSON":
|
|
87
|
+
contentType = "application/json; charset=utf-8";
|
|
88
|
+
accept = "application/json, text/json, */*; q=0.01";
|
|
89
|
+
body = JSON.stringify(body);
|
|
90
|
+
break;
|
|
91
|
+
case "FORM":
|
|
92
|
+
if (body instanceof HTMLFormElement) {
|
|
93
|
+
//const form = <HTMLFormElement>body;
|
|
94
|
+
//contentType = form.enctype ?? FORM_URL;
|
|
95
|
+
body = new FormData(body);
|
|
86
96
|
}
|
|
97
|
+
else if (body instanceof FormData)
|
|
98
|
+
contentType = FORM_URL;
|
|
99
|
+
if (contentType == FORM_URL)
|
|
100
|
+
body = encodeForm(body);
|
|
101
|
+
else if (contentType == FORM_DATA)
|
|
102
|
+
contentType = null;
|
|
103
|
+
break;
|
|
104
|
+
case "FORMDATA":
|
|
105
|
+
break;
|
|
106
|
+
case "TEXT":
|
|
107
|
+
contentType = "text/plain";
|
|
87
108
|
break;
|
|
88
|
-
}
|
|
89
109
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
if (
|
|
93
|
-
|
|
94
|
-
};
|
|
95
|
-
xhr.open(method, url, true);
|
|
96
|
-
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
|
|
97
|
-
for (const key in prepared.headers) {
|
|
98
|
-
const value = prepared.headers[key];
|
|
99
|
-
if (!value)
|
|
100
|
-
continue;
|
|
101
|
-
xhr.setRequestHeader(key, value);
|
|
110
|
+
if (accept)
|
|
111
|
+
headers["Accept"] = accept;
|
|
112
|
+
if (contentType)
|
|
113
|
+
headers['Content-Type'] = contentType;
|
|
102
114
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
return xhr;
|
|
115
|
+
return {
|
|
116
|
+
headers,
|
|
117
|
+
body
|
|
118
|
+
};
|
|
108
119
|
};
|
|
120
|
+
var internals = {
|
|
121
|
+
DEFAULT_TIMEOUT,
|
|
122
|
+
FORM_URL,
|
|
123
|
+
FORM_DATA,
|
|
124
|
+
detectRequestType,
|
|
125
|
+
prepareRequest
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
/** Request with fetch. */
|
|
109
129
|
const request = async (options, abortSignal) => {
|
|
110
130
|
let url = options.url || location.href;
|
|
111
|
-
url =
|
|
131
|
+
url = addQuery(url, options.query);
|
|
112
132
|
const method = options.method ? options.method.toUpperCase() : "GET";
|
|
113
133
|
let body = options.data;
|
|
114
134
|
if (body && method === "GET")
|
|
115
135
|
throw new Error("GET method is not support request with data.");
|
|
116
|
-
detectRequestType(options);
|
|
117
|
-
const prepared = prepareRequest(options, body);
|
|
118
|
-
const abortSignals = [AbortSignal.timeout(options.timeout ?? DEFAULT_TIMEOUT)];
|
|
136
|
+
internals.detectRequestType(options);
|
|
137
|
+
const prepared = internals.prepareRequest(options, body);
|
|
138
|
+
const abortSignals = [AbortSignal.timeout(options.timeout ?? internals.DEFAULT_TIMEOUT)];
|
|
119
139
|
if (abortSignal)
|
|
120
140
|
abortSignals.push(abortSignal);
|
|
121
141
|
try {
|
|
@@ -190,109 +210,102 @@ const request = async (options, abortSignal) => {
|
|
|
190
210
|
throw new Error(`Error ajax request: ${(error && error.message) ? error.message : error}`);
|
|
191
211
|
}
|
|
192
212
|
};
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
else if (body instanceof Object)
|
|
203
|
-
options.type = "JSON";
|
|
204
|
-
else if (typeof body === "string")
|
|
205
|
-
options.type = "TEXT";
|
|
206
|
-
}
|
|
207
|
-
};
|
|
208
|
-
const prepareRequest = (options, body) => {
|
|
209
|
-
const headers = {};
|
|
210
|
-
if (options.headers) {
|
|
211
|
-
for (const key in options.headers) {
|
|
212
|
-
const value = options.headers[key];
|
|
213
|
-
if (!value)
|
|
214
|
-
continue;
|
|
215
|
-
headers[key] = value;
|
|
216
|
-
}
|
|
213
|
+
|
|
214
|
+
/** Request with XMLHttpRequest. */
|
|
215
|
+
const ajaxRequest = (options) => {
|
|
216
|
+
let url = options.url || location.href;
|
|
217
|
+
let { query } = options;
|
|
218
|
+
if (options.disableCache) {
|
|
219
|
+
if (!query)
|
|
220
|
+
query = {};
|
|
221
|
+
query["_"] = new Date().getTime().toString();
|
|
217
222
|
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
223
|
+
url = addQuery(url, query);
|
|
224
|
+
const method = options.method ? options.method : "GET";
|
|
225
|
+
if (options.data && method === "GET")
|
|
226
|
+
throw new Error("GET method is not support request with data.");
|
|
227
|
+
internals.detectRequestType(options);
|
|
228
|
+
const prepared = internals.prepareRequest(options, options.data);
|
|
229
|
+
const xhr = new XMLHttpRequest();
|
|
230
|
+
xhr.withCredentials = true;
|
|
231
|
+
if (options.timeout === 0 || options.timeout)
|
|
232
|
+
xhr.timeout = options.timeout;
|
|
233
|
+
xhr.onreadystatechange = (e) => {
|
|
234
|
+
switch (xhr.readyState) {
|
|
235
|
+
case XMLHttpRequest.DONE: {
|
|
236
|
+
if (options.success) {
|
|
237
|
+
let responseData = null;
|
|
238
|
+
let responseType = "none";
|
|
239
|
+
const contentType = xhr.getResponseHeader("Content-Type");
|
|
240
|
+
if (xhr.response) {
|
|
241
|
+
if (contentType) {
|
|
242
|
+
if (contentType.includes("json")) {
|
|
243
|
+
responseType = "json";
|
|
244
|
+
responseData = JSON.parse(xhr.responseText);
|
|
245
|
+
}
|
|
246
|
+
else if (contentType.includes("text/plain")) {
|
|
247
|
+
responseType = "text";
|
|
248
|
+
responseData = xhr.responseText;
|
|
249
|
+
}
|
|
250
|
+
else if (contentType.includes("text/html")) {
|
|
251
|
+
responseType = "html";
|
|
252
|
+
responseData = xhr.responseText;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
const headers = {
|
|
257
|
+
get(name) {
|
|
258
|
+
return xhr.getResponseHeader(name);
|
|
259
|
+
},
|
|
260
|
+
has(name) {
|
|
261
|
+
return !!xhr.getResponseHeader(name);
|
|
262
|
+
},
|
|
263
|
+
forEach(callbackfn, thisArg) {
|
|
264
|
+
const headers = xhr.getAllResponseHeaders();
|
|
265
|
+
// Convert the header string into an array
|
|
266
|
+
// of individual headers
|
|
267
|
+
const arr = headers.trim().split(/[\r\n]+/);
|
|
268
|
+
// Create a map of header names to values
|
|
269
|
+
arr.forEach((line) => {
|
|
270
|
+
const parts = line.split(": ");
|
|
271
|
+
const header = parts.shift() || "";
|
|
272
|
+
const value = parts.join(": ");
|
|
273
|
+
callbackfn.call(thisArg, value, header.toLowerCase(), {});
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
options.success({
|
|
278
|
+
status: xhr.status,
|
|
279
|
+
url: xhr.responseURL,
|
|
280
|
+
redirected: false,
|
|
281
|
+
type: responseType,
|
|
282
|
+
contentType,
|
|
283
|
+
headers,
|
|
284
|
+
data: responseData,
|
|
285
|
+
state: options.state
|
|
286
|
+
});
|
|
236
287
|
}
|
|
237
|
-
else if (body instanceof FormData)
|
|
238
|
-
contentType = FORM_URL;
|
|
239
|
-
if (contentType == FORM_URL)
|
|
240
|
-
body = encodeForm(body);
|
|
241
|
-
else if (contentType == FORM_DATA)
|
|
242
|
-
contentType = null;
|
|
243
|
-
break;
|
|
244
|
-
case "FORMDATA":
|
|
245
|
-
break;
|
|
246
|
-
case "TEXT":
|
|
247
|
-
contentType = "text/plain";
|
|
248
288
|
break;
|
|
289
|
+
}
|
|
249
290
|
}
|
|
250
|
-
if (accept)
|
|
251
|
-
headers["Accept"] = accept;
|
|
252
|
-
if (contentType)
|
|
253
|
-
headers['Content-Type'] = contentType;
|
|
254
|
-
}
|
|
255
|
-
return {
|
|
256
|
-
headers,
|
|
257
|
-
body
|
|
258
291
|
};
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
292
|
+
xhr.onabort = (e) => {
|
|
293
|
+
if (options.error)
|
|
294
|
+
options.error(options, "Request aborted");
|
|
295
|
+
};
|
|
296
|
+
xhr.open(method, url, true);
|
|
297
|
+
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
|
|
298
|
+
for (const key in prepared.headers) {
|
|
299
|
+
const value = prepared.headers[key];
|
|
300
|
+
if (!value)
|
|
267
301
|
continue;
|
|
268
|
-
|
|
269
|
-
val.forEach(v => urlParams.append(key, v));
|
|
270
|
-
else
|
|
271
|
-
urlParams.append(key, val);
|
|
272
|
-
}
|
|
273
|
-
return urlParams;
|
|
274
|
-
};
|
|
275
|
-
const extendUrl = (url, query) => {
|
|
276
|
-
if (query) {
|
|
277
|
-
const urlParams = createSearchParams(query);
|
|
278
|
-
if (urlParams.size) {
|
|
279
|
-
if (url.indexOf("?") === -1)
|
|
280
|
-
url += "?";
|
|
281
|
-
else
|
|
282
|
-
url += "&";
|
|
283
|
-
url += urlParams.toString();
|
|
284
|
-
}
|
|
302
|
+
xhr.setRequestHeader(key, value);
|
|
285
303
|
}
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
if (!key)
|
|
292
|
-
return;
|
|
293
|
-
query.append(key, value.toString());
|
|
294
|
-
});
|
|
295
|
-
return query.toString();
|
|
304
|
+
if (method === "GET")
|
|
305
|
+
xhr.send();
|
|
306
|
+
else
|
|
307
|
+
xhr.send(prepared.body);
|
|
308
|
+
return xhr;
|
|
296
309
|
};
|
|
297
310
|
|
|
298
311
|
class AjaxQueue {
|
|
@@ -383,5 +396,5 @@ class AjaxQueue {
|
|
|
383
396
|
}
|
|
384
397
|
}
|
|
385
398
|
|
|
386
|
-
export { AjaxQueue, ajaxRequest, request
|
|
399
|
+
export { AjaxQueue, helpers as RequestHelper, ajaxRequest, request };
|
|
387
400
|
//# sourceMappingURL=index.js.map
|
package/dist/mjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
type AJAXMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | string;
|
|
2
2
|
type AJAXReqestType = "NONE" | "JSON" | "XML" | "FORM" | "FORMDATA" | "TEXT" | "BLOB";
|
|
3
|
-
type
|
|
3
|
+
type ResponseType = "none" | "json" | "blob" | "text" | "html";
|
|
4
4
|
type ResponseDelegate = (response: AjaxResponse) => void;
|
|
5
5
|
type ErrorDelegate = (request: AjaxRequest, reason?: any) => void;
|
|
6
6
|
type QueryData = {
|
|
@@ -25,7 +25,7 @@ interface AjaxResponse<TData = any, TState = any> {
|
|
|
25
25
|
status: number;
|
|
26
26
|
redirected: boolean;
|
|
27
27
|
url: string | null;
|
|
28
|
-
type:
|
|
28
|
+
type: ResponseType;
|
|
29
29
|
contentType: string | null;
|
|
30
30
|
headers: ResponseHeaders;
|
|
31
31
|
data: TData | null;
|
|
@@ -36,15 +36,13 @@ interface ResponseHeaders {
|
|
|
36
36
|
has(name: string): boolean;
|
|
37
37
|
forEach(callbackfn: (value: string, key: string, parent: ResponseHeaders) => void, thisArg?: any): void;
|
|
38
38
|
}
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
|
|
40
|
+
/** Request with fetch. */
|
|
41
41
|
declare const request: (options: AjaxRequest, abortSignal?: AbortSignal) => Promise<AjaxResponse>;
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
errorRequest?: (response: AjaxRequest, reason?: any) => void;
|
|
47
|
-
}
|
|
43
|
+
/** Request with XMLHttpRequest. */
|
|
44
|
+
declare const ajaxRequest: (options: AjaxRequest) => XMLHttpRequest;
|
|
45
|
+
|
|
48
46
|
declare class AjaxQueue {
|
|
49
47
|
private _options;
|
|
50
48
|
private _requests;
|
|
@@ -61,5 +59,21 @@ declare class AjaxQueue {
|
|
|
61
59
|
private __execute;
|
|
62
60
|
private __next;
|
|
63
61
|
}
|
|
62
|
+
interface AjaxQueueOptions {
|
|
63
|
+
canRequest?: (request: AjaxRequest) => void | boolean;
|
|
64
|
+
successRequest?: (request: AjaxRequest, response: AjaxResponse) => void;
|
|
65
|
+
errorRequest?: (response: AjaxRequest, reason?: any) => void;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
declare const createQuery: (query?: QueryData | FormData | null) => URLSearchParams;
|
|
69
|
+
declare const addQuery: (url: string, query?: QueryData | FormData | null) => string;
|
|
70
|
+
declare const encodeForm: (data: FormData) => string;
|
|
71
|
+
|
|
72
|
+
declare const helpers_addQuery: typeof addQuery;
|
|
73
|
+
declare const helpers_createQuery: typeof createQuery;
|
|
74
|
+
declare const helpers_encodeForm: typeof encodeForm;
|
|
75
|
+
declare namespace helpers {
|
|
76
|
+
export { helpers_addQuery as addQuery, helpers_createQuery as createQuery, helpers_encodeForm as encodeForm };
|
|
77
|
+
}
|
|
64
78
|
|
|
65
|
-
export { type AJAXMethod, type AJAXReqestType, AjaxQueue, type AjaxQueueOptions, type AjaxRequest, type AjaxResponse, type ErrorDelegate, type QueryData, type ResponseDelegate, type ResponseHeaders, type
|
|
79
|
+
export { type AJAXMethod, type AJAXReqestType, AjaxQueue, type AjaxQueueOptions, type AjaxRequest, type AjaxResponse, type ErrorDelegate, type QueryData, helpers as RequestHelper, type ResponseDelegate, type ResponseHeaders, type ResponseType, ajaxRequest, request };
|