@brandup/ui-ajax 2.0.1 → 2.0.3

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/README.md CHANGED
@@ -25,7 +25,7 @@ await request({
25
25
  credentials?: RequestCredentials;
26
26
  timeout?: number | null;
27
27
  headers?: { [key: string]: string } | null;
28
- type?: AJAXReqestType | null;
28
+ type?: AJAXRequestType | null;
29
29
  data?: string | object | Blob | FormData | HTMLFormElement | null;
30
30
  abort?: AbortSignal;
31
31
  success?: ResponseDelegate | null;
@@ -0,0 +1,125 @@
1
+ 'use strict';
2
+
3
+ var helpers = require('./helpers.js');
4
+ var internals = require('./internals.js');
5
+
6
+ /**
7
+ * Performs an AJAX request using `XMLHttpRequest`, always with credentials.
8
+ *
9
+ * The response body is parsed by `Content-Type` into JSON, `text/plain` or `text/html`;
10
+ * unlike the fetch-based {@link request}, there is no `blob` response type. `disableCache`
11
+ * appends a `_=<timestamp>` cache-busting query parameter (rather than `cache: "no-store"`).
12
+ * Results and errors are delivered through `options.success` / `options.error`; this
13
+ * function does not return a promise.
14
+ *
15
+ * @param options Request options. `GET` requests must not carry `data`. `timeout` of `0` disables the timeout.
16
+ * @returns The underlying `XMLHttpRequest`, which can be used to `abort()` the request.
17
+ */
18
+ const ajaxRequest = (options) => {
19
+ let url = options.url || location.href;
20
+ let { query } = options;
21
+ if (options.disableCache) {
22
+ if (!query)
23
+ query = {};
24
+ query["_"] = Date.now().toString();
25
+ }
26
+ url = helpers.addQuery(url, query);
27
+ const method = options.method ? options.method.toUpperCase() : "GET";
28
+ if (options.data && (method === "GET" || method === "HEAD"))
29
+ throw new Error(`${method} method does not support a request body.`);
30
+ internals.default.detectRequestType(options);
31
+ const prepared = internals.default.prepareRequest(options, options.data);
32
+ const xhr = new XMLHttpRequest();
33
+ xhr.withCredentials = true;
34
+ if (options.timeout === 0 || options.timeout)
35
+ xhr.timeout = options.timeout;
36
+ xhr.onreadystatechange = (_e) => {
37
+ if (xhr.readyState !== XMLHttpRequest.DONE)
38
+ return;
39
+ if (options.success) {
40
+ let responseData = null;
41
+ let responseType = "none";
42
+ const contentType = xhr.getResponseHeader("Content-Type");
43
+ if (xhr.response && contentType) {
44
+ if (contentType.includes("json")) {
45
+ responseType = "json";
46
+ try {
47
+ responseData = JSON.parse(xhr.responseText);
48
+ }
49
+ catch (e) {
50
+ if (options.error)
51
+ options.error(options, e);
52
+ return;
53
+ }
54
+ }
55
+ else if (contentType.includes("text/plain")) {
56
+ responseType = "text";
57
+ responseData = xhr.responseText;
58
+ }
59
+ else if (contentType.includes("text/html")) {
60
+ responseType = "html";
61
+ responseData = xhr.responseText;
62
+ }
63
+ }
64
+ const xhrRef = xhr;
65
+ const headers = {
66
+ get(name) {
67
+ return xhrRef.getResponseHeader(name);
68
+ },
69
+ has(name) {
70
+ return !!xhrRef.getResponseHeader(name);
71
+ },
72
+ forEach(callbackfn, thisArg) {
73
+ xhrRef.getAllResponseHeaders()
74
+ .trim()
75
+ .split(/[\r\n]+/)
76
+ .filter(Boolean)
77
+ .forEach(line => {
78
+ const idx = line.indexOf(": ");
79
+ const key = idx >= 0 ? line.substring(0, idx).toLowerCase() : line.toLowerCase();
80
+ const value = idx >= 0 ? line.substring(idx + 2) : "";
81
+ callbackfn.call(thisArg, value, key, {});
82
+ });
83
+ }
84
+ };
85
+ options.success({
86
+ status: xhr.status,
87
+ url: xhr.responseURL,
88
+ redirected: false,
89
+ type: responseType,
90
+ contentType,
91
+ headers,
92
+ data: responseData,
93
+ state: options.state
94
+ });
95
+ }
96
+ };
97
+ xhr.onabort = (_e) => {
98
+ if (options.error)
99
+ options.error(options, new Error("Request aborted"));
100
+ };
101
+ xhr.onerror = (_e) => {
102
+ if (options.error)
103
+ options.error(options, new Error("Request network error"));
104
+ };
105
+ xhr.ontimeout = (_e) => {
106
+ if (options.error)
107
+ options.error(options, new Error("Request timeout"));
108
+ };
109
+ xhr.open(method, url, true);
110
+ xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
111
+ for (const key in prepared.headers) {
112
+ const value = prepared.headers[key];
113
+ if (!value)
114
+ continue;
115
+ xhr.setRequestHeader(key, value);
116
+ }
117
+ if (method === "GET")
118
+ xhr.send();
119
+ else
120
+ xhr.send(prepared.body);
121
+ return xhr;
122
+ };
123
+
124
+ exports.ajaxRequest = ajaxRequest;
125
+ //# sourceMappingURL=ajax-request.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ajax-request.js","sources":["../../../source/ajax-request.ts"],"sourcesContent":[null],"names":["helpers.addQuery","internals"],"mappings":";;;;;AAIA;;;;;;;;;;;AAWG;AACI,MAAM,WAAW,GAAG,CAAC,OAAoB,KAAI;IACnD,IAAI,GAAG,GAAG,OAAO,CAAC,GAAG,IAAI,QAAQ,CAAC,IAAI;AACtC,IAAA,IAAI,EAAE,KAAK,EAAE,GAAG,OAAO;AAEvB,IAAA,IAAI,OAAO,CAAC,YAAY,EAAE;AACzB,QAAA,IAAI,CAAC,KAAK;YAAE,KAAK,GAAG,EAAE;QACtB,KAAK,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE;IACnC;IAEA,GAAG,GAAGA,gBAAgB,CAAC,GAAG,EAAE,KAAK,CAAC;AAElC,IAAA,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,WAAW,EAAE,GAAG,KAAK;AAEpE,IAAA,IAAI,OAAO,CAAC,IAAI,KAAK,MAAM,KAAK,KAAK,IAAI,MAAM,KAAK,MAAM,CAAC;AAC1D,QAAA,MAAM,IAAI,KAAK,CAAC,GAAG,MAAM,CAAA,wCAAA,CAA0C,CAAC;AAErE,IAAAC,iBAAS,CAAC,iBAAiB,CAAC,OAAO,CAAC;AACpC,IAAA,MAAM,QAAQ,GAAGA,iBAAS,CAAC,cAAc,CAAC,OAAO,EAAE,OAAO,CAAC,IAAI,CAAC;AAEhE,IAAA,MAAM,GAAG,GAAG,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C,CAAC;AACtD,IAAA,CAAC;AAED,IAAA,GAAG,CAAC,OAAO,GAAG,CAAC,EAAiB,KAAI;QACnC,IAAI,OAAO,CAAC,KAAK;YAChB,OAAO,CAAC,KAAK,CAAC,OAAO,EAAE,IAAI,KAAK,CAAC,uBAAuB,CAAC,CAAC;AAC5D,IAAA,CAAC;AAED,IAAA,GAAG,CAAC,SAAS,GAAG,CAAC,EAAiB,KAAI;QACrC,IAAI,OAAO,CAAC,KAAK;YAChB,OAAO,CAAC,KAAK,CAAC,OAAO,EAAE,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAC;AACtD,IAAA,CAAC;IAED,GAAG,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,IAAI,CAAC;AAC3B,IAAA,GAAG,CAAC,gBAAgB,CAAC,kBAAkB,EAAE,gBAAgB,CAAC;AAE1D,IAAA,KAAK,MAAM,GAAG,IAAI,QAAQ,CAAC,OAAO,EAAE;QACnC,MAAM,KAAK,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC;AACnC,QAAA,IAAI,CAAC,KAAK;YACT;AACD,QAAA,GAAG,CAAC,gBAAgB,CAAC,GAAG,EAAE,KAAK,CAAC;IACjC;IAEA,IAAI,MAAM,KAAK,KAAK;QACnB,GAAG,CAAC,IAAI,EAAE;;AAEV,QAAA,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC;AAExB,IAAA,OAAO,GAAG;AACX;;;;"}
@@ -0,0 +1,69 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Builds a `URLSearchParams` from query data or a `FormData`.
5
+ *
6
+ * Array values produce repeated keys; `null` values are skipped.
7
+ *
8
+ * @param query Source parameters.
9
+ * @returns The populated `URLSearchParams` (empty when `query` is nullish).
10
+ */
11
+ const createQuery = (query) => {
12
+ const urlParams = new URLSearchParams();
13
+ if (!query)
14
+ return urlParams;
15
+ if (query instanceof FormData) {
16
+ query.forEach((value, key) => {
17
+ if (!key)
18
+ return;
19
+ urlParams.append(key, value.toString());
20
+ });
21
+ }
22
+ else {
23
+ for (const key in query) {
24
+ const val = query[key];
25
+ if (val === null)
26
+ continue;
27
+ if (Array.isArray(val))
28
+ val.forEach(v => urlParams.append(key, v));
29
+ else
30
+ urlParams.append(key, val);
31
+ }
32
+ }
33
+ return urlParams;
34
+ };
35
+ /**
36
+ * Appends query parameters to a URL, choosing `?` or `&` as appropriate.
37
+ *
38
+ * @param url Base URL.
39
+ * @param query Parameters to append; nothing is added when empty.
40
+ * @returns The resulting URL.
41
+ */
42
+ const addQuery = (url, query) => {
43
+ if (query) {
44
+ const urlParams = createQuery(query);
45
+ if (urlParams.size) {
46
+ if (url.indexOf("?") === -1)
47
+ url += "?";
48
+ else
49
+ url += "&";
50
+ url += urlParams.toString();
51
+ }
52
+ }
53
+ return url;
54
+ };
55
+ /**
56
+ * Encodes a `FormData` as an `application/x-www-form-urlencoded` string.
57
+ *
58
+ * @param data Form data to encode.
59
+ * @returns The URL-encoded form string.
60
+ */
61
+ const encodeForm = (data) => {
62
+ const query = createQuery(data);
63
+ return query.toString();
64
+ };
65
+
66
+ exports.addQuery = addQuery;
67
+ exports.createQuery = createQuery;
68
+ exports.encodeForm = encodeForm;
69
+ //# sourceMappingURL=helpers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"helpers.js","sources":["../../../source/helpers.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAEA;;;;;;;AAOG;AACH,MAAM,WAAW,GAAG,CAAC,KAAmC,KAAI;AAC3D,IAAA,MAAM,SAAS,GAAG,IAAI,eAAe,EAAE;AAEvC,IAAA,IAAI,CAAC,KAAK;AACT,QAAA,OAAO,SAAS;AAEjB,IAAA,IAAI,KAAK,YAAY,QAAQ,EAAE;QAC9B,KAAK,CAAC,OAAO,CAAC,CAAC,KAAyB,EAAE,GAAW,KAAI;AACxD,YAAA,IAAI,CAAC,GAAG;gBACP;YACD,SAAS,CAAC,MAAM,CAAC,GAAG,EAAE,KAAK,CAA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