@bbn/bbn 1.0.476 → 1.0.477
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/bbn.js +1 -1
- package/dist/bbn.js.map +1 -1
- package/dist/fn/ajax/ajax.js +188 -7
- package/dist/fn/ajax/stream.js +4 -1
- package/package.json +1 -1
package/dist/fn/ajax/ajax.js
CHANGED
|
@@ -1,11 +1,192 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
1
10
|
import isObject from '../type/isObject.js';
|
|
2
11
|
import replaceAll from '../string/replaceAll.js';
|
|
3
12
|
import getRequestId from './getRequestId.js';
|
|
4
13
|
import getLoader from './getLoader.js';
|
|
5
14
|
import extend from '../object/extend.js';
|
|
15
|
+
import numProperties from '../object/numProperties.js';
|
|
6
16
|
import _deleteLoader from './_deleteLoader.js';
|
|
7
17
|
import isFunction from '../type/isFunction.js';
|
|
8
18
|
import _addLoader from './_addLoader.js';
|
|
19
|
+
class Cancel extends Error {
|
|
20
|
+
constructor(message) {
|
|
21
|
+
super(message || 'Request canceled');
|
|
22
|
+
this.name = 'Cancel';
|
|
23
|
+
this.__BBN_CANCEL__ = true;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const isCancel = function (value) {
|
|
27
|
+
return !!(value && value.__BBN_CANCEL__ === true);
|
|
28
|
+
};
|
|
29
|
+
const methodsWithBody = ['POST', 'PUT', 'PATCH', 'DELETE'];
|
|
30
|
+
const buildURL = function (url, params) {
|
|
31
|
+
if (!params)
|
|
32
|
+
return url;
|
|
33
|
+
const usp = new URLSearchParams(params).toString();
|
|
34
|
+
return usp ? url + (url.includes('?') ? '&' : '?') + usp : url;
|
|
35
|
+
};
|
|
36
|
+
const normalizeDataAndHeaders = function (data, headersObj) {
|
|
37
|
+
const headers = new Headers(headersObj || {});
|
|
38
|
+
let body = data;
|
|
39
|
+
if (data != null && !(data instanceof FormData) && typeof data === 'object') {
|
|
40
|
+
headers.set('Content-Type', 'application/json');
|
|
41
|
+
body = JSON.stringify(data);
|
|
42
|
+
}
|
|
43
|
+
return { body, headers };
|
|
44
|
+
};
|
|
45
|
+
const fetchRequest = function (method, url, config = {}, aborter) {
|
|
46
|
+
};
|
|
47
|
+
/*
|
|
48
|
+
const xhrRequest = function(method, url, config: any = {}, aborter?: any) {
|
|
49
|
+
const xhr = new XMLHttpRequest();
|
|
50
|
+
const hasBody = methodsWithBody.includes(method);
|
|
51
|
+
|
|
52
|
+
const promise = new Promise((resolve, reject) => {
|
|
53
|
+
xhr.open(method, url, true);
|
|
54
|
+
|
|
55
|
+
// Set headers
|
|
56
|
+
const { body, headers } = normalizeDataAndHeaders(finalConfig.data, finalConfig.headers);
|
|
57
|
+
headers.forEach((value, key) => {
|
|
58
|
+
xhr.setRequestHeader(key, value);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// Upload progress
|
|
62
|
+
if (typeof finalConfig.onUploadProgress === 'function' && xhr.upload) {
|
|
63
|
+
xhr.upload.addEventListener('progress', (event) => {
|
|
64
|
+
const total = event.lengthComputable ? event.total : undefined;
|
|
65
|
+
finalConfig.onUploadProgress({
|
|
66
|
+
loaded: event.loaded,
|
|
67
|
+
total,
|
|
68
|
+
progress: total ? event.loaded / total : undefined
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
xhr.onreadystatechange = () => {
|
|
74
|
+
if (xhr.readyState !== 4) return;
|
|
75
|
+
|
|
76
|
+
const headersObj = parseXhrHeaders(xhr.getAllResponseHeaders());
|
|
77
|
+
const status = xhr.status === 1223 ? 204 : xhr.status; // IE fix, mostly irrelevant now
|
|
78
|
+
const statusText = xhr.statusText || '';
|
|
79
|
+
|
|
80
|
+
let data = xhr.responseText;
|
|
81
|
+
const contentType = headersObj['content-type'] || '';
|
|
82
|
+
if (contentType.includes('application/json')) {
|
|
83
|
+
try {
|
|
84
|
+
data = JSON.parse(xhr.responseText);
|
|
85
|
+
} catch (e) {
|
|
86
|
+
// leave as text if JSON parse fails
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const response = {
|
|
91
|
+
data,
|
|
92
|
+
status,
|
|
93
|
+
statusText,
|
|
94
|
+
headers: headersObj,
|
|
95
|
+
config: finalConfig,
|
|
96
|
+
request: xhr
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
if (status >= 200 && status < 300) {
|
|
100
|
+
resolve(response);
|
|
101
|
+
} else {
|
|
102
|
+
const err = new Error('Request failed with status code ' + status);
|
|
103
|
+
err.response = response;
|
|
104
|
+
err.config = finalConfig;
|
|
105
|
+
err.request = xhr;
|
|
106
|
+
reject(err);
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
xhr.onerror = () => {
|
|
111
|
+
const err = new Error('Network Error');
|
|
112
|
+
err.config = finalConfig;
|
|
113
|
+
err.request = xhr;
|
|
114
|
+
reject(err);
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
xhr.onabort = () => {
|
|
118
|
+
reject(new Cancel('Request canceled'));
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
if (hasBody) {
|
|
122
|
+
xhr.send(body);
|
|
123
|
+
} else {
|
|
124
|
+
xhr.send();
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
promise.cancel = () => xhr.abort();
|
|
129
|
+
return promise;
|
|
130
|
+
};
|
|
131
|
+
*/
|
|
132
|
+
const request = function (method, url, config = {}, aborter) {
|
|
133
|
+
const fetchConfig = {
|
|
134
|
+
method,
|
|
135
|
+
headers: new Headers(config.headers || {}),
|
|
136
|
+
signal: aborter === null || aborter === void 0 ? void 0 : aborter.signal,
|
|
137
|
+
};
|
|
138
|
+
if (config.data != null && method !== 'GET' && method !== 'HEAD') {
|
|
139
|
+
// You can get fancier here (JSON, FormData, etc.)
|
|
140
|
+
if (typeof config.data === 'object' && !(config.data instanceof FormData)) {
|
|
141
|
+
fetchConfig.headers.set('Content-Type', 'application/json');
|
|
142
|
+
fetchConfig.body = JSON.stringify(config.data);
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
fetchConfig.body = config.data;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
// Add query params support if needed
|
|
149
|
+
if (config.params) {
|
|
150
|
+
const usp = new URLSearchParams(config.params).toString();
|
|
151
|
+
url += (url.includes('?') ? '&' : '?') + usp;
|
|
152
|
+
}
|
|
153
|
+
const fetchPromise = fetch(url, fetchConfig).then((res) => __awaiter(this, void 0, void 0, function* () {
|
|
154
|
+
let data;
|
|
155
|
+
const contentType = res.headers.get('content-type') || '';
|
|
156
|
+
if (contentType.includes('application/json')) {
|
|
157
|
+
data = yield res.json();
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
data = yield res.text();
|
|
161
|
+
}
|
|
162
|
+
const response = {
|
|
163
|
+
data,
|
|
164
|
+
status: res.status,
|
|
165
|
+
statusText: res.statusText,
|
|
166
|
+
headers: Object.fromEntries(res.headers.entries()),
|
|
167
|
+
config,
|
|
168
|
+
request: res,
|
|
169
|
+
};
|
|
170
|
+
if (!res.ok) {
|
|
171
|
+
// axios rejects for 4xx/5xx
|
|
172
|
+
const error = new Error('Request failed with status code ' + res.status);
|
|
173
|
+
error.response = response;
|
|
174
|
+
error.config = config;
|
|
175
|
+
error.request = res;
|
|
176
|
+
throw error;
|
|
177
|
+
}
|
|
178
|
+
return response;
|
|
179
|
+
})).catch((err) => {
|
|
180
|
+
if (err.name === 'AbortError') {
|
|
181
|
+
throw new Cancel('Request canceled');
|
|
182
|
+
}
|
|
183
|
+
throw err;
|
|
184
|
+
});
|
|
185
|
+
// axios usually gives you the cancel token separately;
|
|
186
|
+
// here we return both for convenience.
|
|
187
|
+
fetchPromise.cancel = () => aborter.abort();
|
|
188
|
+
return fetchPromise;
|
|
189
|
+
};
|
|
9
190
|
/**
|
|
10
191
|
* Creates an XHR object and returns the Promise.
|
|
11
192
|
*
|
|
@@ -111,14 +292,14 @@ export default function ajax(url, datatype = null, data = null, success = null,
|
|
|
111
292
|
"Content-Type": "text/javascript",
|
|
112
293
|
};
|
|
113
294
|
}
|
|
114
|
-
let
|
|
295
|
+
let method = "get";
|
|
115
296
|
if (isObject(data)) {
|
|
116
|
-
|
|
297
|
+
if (numProperties(data) > 0) {
|
|
298
|
+
options.data = data;
|
|
299
|
+
method = "post";
|
|
300
|
+
}
|
|
117
301
|
}
|
|
118
|
-
|
|
119
|
-
const axiosMethod = args.length === 2 ? "get" : "post";
|
|
120
|
-
let loader = axios[axiosMethod]
|
|
121
|
-
.apply(null, args)
|
|
302
|
+
let loader = request(method, url, options)
|
|
122
303
|
.then(res => {
|
|
123
304
|
_deleteLoader(requestId, res);
|
|
124
305
|
bbn.fn.defaultEndLoadingFunction(url, tst, data, res);
|
|
@@ -141,7 +322,7 @@ export default function ajax(url, datatype = null, data = null, success = null,
|
|
|
141
322
|
return res;
|
|
142
323
|
})
|
|
143
324
|
.catch((err) => {
|
|
144
|
-
let isAbort =
|
|
325
|
+
let isAbort = isCancel(err);
|
|
145
326
|
_deleteLoader(requestId, err.message || err.response.data, isAbort);
|
|
146
327
|
bbn.fn.defaultEndLoadingFunction(url, tst, data, err);
|
|
147
328
|
if (isAbort) {
|
package/dist/fn/ajax/stream.js
CHANGED
|
@@ -23,6 +23,9 @@ import arrayBuffer2String from '../convert/arrayBuffer2String.js';
|
|
|
23
23
|
* @returns {Promise} The Promise created by the generated XHR.
|
|
24
24
|
*/
|
|
25
25
|
const boundary = '\n';
|
|
26
|
+
const isCancel = function (value) {
|
|
27
|
+
return !!(value && value.__BBN_CANCEL__ === true);
|
|
28
|
+
};
|
|
26
29
|
export default function stream(url, success, data, failure, abort, finished) {
|
|
27
30
|
const requestId = getRequestId(url, data, 'json');
|
|
28
31
|
const loaderObj = getLoader(requestId);
|
|
@@ -111,7 +114,7 @@ export default function stream(url, success, data, failure, abort, finished) {
|
|
|
111
114
|
}
|
|
112
115
|
})
|
|
113
116
|
.catch((err) => {
|
|
114
|
-
let isAbort =
|
|
117
|
+
let isAbort = isCancel(err);
|
|
115
118
|
_deleteLoader(requestId, data, isAbort);
|
|
116
119
|
defaultEndLoadingFunction(url, tst, data, err);
|
|
117
120
|
if (isAbort) {
|