@bbn/bbn 1.0.479 → 1.0.481
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/com.d.ts +5 -0
- package/dist/{req.js → com.js} +78 -60
- package/dist/fn/ajax/ajax.js +3 -182
- package/dist/fn/ajax/upload.js +4 -2
- package/dist/index.js +2 -2
- package/package.json +1 -1
- package/dist/req.d.ts +0 -2
package/dist/com.d.ts
ADDED
package/dist/{req.js → com.js}
RENAMED
|
@@ -14,9 +14,6 @@ class Cancel extends Error {
|
|
|
14
14
|
this.__BBN_CANCEL__ = true;
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
|
-
const isCancel = (value) => {
|
|
18
|
-
return !!(value && value.__BBN_CANCEL__ === true);
|
|
19
|
-
};
|
|
20
17
|
const parseXhrHeaders = (raw) => {
|
|
21
18
|
const headers = {};
|
|
22
19
|
if (!raw)
|
|
@@ -48,6 +45,63 @@ const normalizeDataAndHeaders = (data, headersObj) => {
|
|
|
48
45
|
return { body, headers };
|
|
49
46
|
};
|
|
50
47
|
const fetchRequest = (method, url, config = {}, aborter) => {
|
|
48
|
+
const fetchConfig = {
|
|
49
|
+
method,
|
|
50
|
+
headers: new Headers(config.headers || {}),
|
|
51
|
+
signal: aborter === null || aborter === void 0 ? void 0 : aborter.signal,
|
|
52
|
+
};
|
|
53
|
+
const hasBody = methodsWithBody.includes(method);
|
|
54
|
+
if (config.data != null && hasBody) {
|
|
55
|
+
// You can get fancier here (JSON, FormData, etc.)
|
|
56
|
+
if (typeof config.data === 'object' && !(config.data instanceof FormData)) {
|
|
57
|
+
fetchConfig.headers.set('Content-Type', 'application/json');
|
|
58
|
+
fetchConfig.body = JSON.stringify(config.data);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
fetchConfig.body = config.data;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
// Add query params support if needed
|
|
65
|
+
if (config.params) {
|
|
66
|
+
const usp = new URLSearchParams(config.params).toString();
|
|
67
|
+
url += (url.includes('?') ? '&' : '?') + usp;
|
|
68
|
+
}
|
|
69
|
+
const fetchPromise = fetch(url, fetchConfig).then((res) => __awaiter(void 0, void 0, void 0, function* () {
|
|
70
|
+
let data;
|
|
71
|
+
const contentType = res.headers.get('content-type') || '';
|
|
72
|
+
if (contentType.includes('application/json')) {
|
|
73
|
+
data = yield res.json();
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
data = yield res.text();
|
|
77
|
+
}
|
|
78
|
+
const response = {
|
|
79
|
+
data,
|
|
80
|
+
status: res.status,
|
|
81
|
+
statusText: res.statusText,
|
|
82
|
+
headers: Object.fromEntries(res.headers.entries()),
|
|
83
|
+
config,
|
|
84
|
+
request: res,
|
|
85
|
+
};
|
|
86
|
+
if (!res.ok) {
|
|
87
|
+
// axios rejects for 4xx/5xx
|
|
88
|
+
const error = new Error('Request failed with status code ' + res.status);
|
|
89
|
+
error.response = response;
|
|
90
|
+
error.config = config;
|
|
91
|
+
error.request = res;
|
|
92
|
+
throw error;
|
|
93
|
+
}
|
|
94
|
+
return response;
|
|
95
|
+
})).catch((err) => {
|
|
96
|
+
if (err.name === 'AbortError') {
|
|
97
|
+
throw new Cancel('Request canceled');
|
|
98
|
+
}
|
|
99
|
+
throw err;
|
|
100
|
+
});
|
|
101
|
+
// axios usually gives you the cancel token separately;
|
|
102
|
+
// here we return both for convenience.
|
|
103
|
+
fetchPromise.cancel = () => aborter.abort();
|
|
104
|
+
return fetchPromise;
|
|
51
105
|
};
|
|
52
106
|
const xhrRequest = (method, url, config = {}, aborter) => {
|
|
53
107
|
const xhr = new XMLHttpRequest();
|
|
@@ -268,63 +322,27 @@ onProgress, onChunkProgress, headers = {}, uploadId }) => {
|
|
|
268
322
|
};
|
|
269
323
|
return promise;
|
|
270
324
|
};
|
|
271
|
-
const
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
if (config.data != null && hasBody) {
|
|
279
|
-
// You can get fancier here (JSON, FormData, etc.)
|
|
280
|
-
if (typeof config.data === 'object' && !(config.data instanceof FormData)) {
|
|
281
|
-
fetchConfig.headers.set('Content-Type', 'application/json');
|
|
282
|
-
fetchConfig.body = JSON.stringify(config.data);
|
|
283
|
-
}
|
|
284
|
-
else {
|
|
285
|
-
fetchConfig.body = config.data;
|
|
286
|
-
}
|
|
325
|
+
const isCancel = (value) => {
|
|
326
|
+
return !!(value && value.__BBN_CANCEL__ === true);
|
|
327
|
+
};
|
|
328
|
+
const run = (method, url, config = {}, aborter) => {
|
|
329
|
+
// Optionally handle baseURL
|
|
330
|
+
if (config.baseURL && !/^https?:\/\//i.test(url)) {
|
|
331
|
+
url = config.baseURL.replace(/\/+$/, '') + '/' + url.replace(/^\/+/, '');
|
|
287
332
|
}
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
333
|
+
url = buildURL(url, config.params);
|
|
334
|
+
const wantsUploadProgress = typeof config.onUploadProgress === 'function' &&
|
|
335
|
+
methodsWithBody.includes(method);
|
|
336
|
+
if (wantsUploadProgress) {
|
|
337
|
+
// Use XHR when upload progress is requested
|
|
338
|
+
return xhrRequest(method, url, config, aborter);
|
|
292
339
|
}
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
}
|
|
302
|
-
const response = {
|
|
303
|
-
data,
|
|
304
|
-
status: res.status,
|
|
305
|
-
statusText: res.statusText,
|
|
306
|
-
headers: Object.fromEntries(res.headers.entries()),
|
|
307
|
-
config,
|
|
308
|
-
request: res,
|
|
309
|
-
};
|
|
310
|
-
if (!res.ok) {
|
|
311
|
-
// axios rejects for 4xx/5xx
|
|
312
|
-
const error = new Error('Request failed with status code ' + res.status);
|
|
313
|
-
error.response = response;
|
|
314
|
-
error.config = config;
|
|
315
|
-
error.request = res;
|
|
316
|
-
throw error;
|
|
317
|
-
}
|
|
318
|
-
return response;
|
|
319
|
-
})).catch((err) => {
|
|
320
|
-
if (err.name === 'AbortError') {
|
|
321
|
-
throw new Cancel('Request canceled');
|
|
322
|
-
}
|
|
323
|
-
throw err;
|
|
324
|
-
});
|
|
325
|
-
// axios usually gives you the cancel token separately;
|
|
326
|
-
// here we return both for convenience.
|
|
327
|
-
fetchPromise.cancel = () => aborter.abort();
|
|
328
|
-
return fetchPromise;
|
|
340
|
+
else {
|
|
341
|
+
// Default to fetch
|
|
342
|
+
return fetchRequest(method, url, config, aborter);
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
export default {
|
|
346
|
+
isCancel,
|
|
347
|
+
run
|
|
329
348
|
};
|
|
330
|
-
export default request;
|
package/dist/fn/ajax/ajax.js
CHANGED
|
@@ -1,12 +1,3 @@
|
|
|
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
|
-
};
|
|
10
1
|
import isObject from '../type/isObject.js';
|
|
11
2
|
import replaceAll from '../string/replaceAll.js';
|
|
12
3
|
import getRequestId from './getRequestId.js';
|
|
@@ -16,177 +7,7 @@ import numProperties from '../object/numProperties.js';
|
|
|
16
7
|
import _deleteLoader from './_deleteLoader.js';
|
|
17
8
|
import isFunction from '../type/isFunction.js';
|
|
18
9
|
import _addLoader from './_addLoader.js';
|
|
19
|
-
|
|
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
|
-
};
|
|
10
|
+
import com from '../../com.js';
|
|
190
11
|
/**
|
|
191
12
|
* Creates an XHR object and returns the Promise.
|
|
192
13
|
*
|
|
@@ -311,7 +132,7 @@ export default function ajax(url, datatype = null, data = null, success = null,
|
|
|
311
132
|
method = "post";
|
|
312
133
|
}
|
|
313
134
|
}
|
|
314
|
-
let loader =
|
|
135
|
+
let loader = com.run(method, url, options)
|
|
315
136
|
.then(res => {
|
|
316
137
|
_deleteLoader(requestId, res);
|
|
317
138
|
bbn.fn.defaultEndLoadingFunction(url, tst, data, res);
|
|
@@ -334,7 +155,7 @@ export default function ajax(url, datatype = null, data = null, success = null,
|
|
|
334
155
|
return res;
|
|
335
156
|
})
|
|
336
157
|
.catch((err) => {
|
|
337
|
-
let isAbort = isCancel(err);
|
|
158
|
+
let isAbort = com.isCancel(err);
|
|
338
159
|
_deleteLoader(requestId, err.message || err.response.data, isAbort);
|
|
339
160
|
bbn.fn.defaultEndLoadingFunction(url, tst, data, err);
|
|
340
161
|
if (isAbort) {
|
package/dist/fn/ajax/upload.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import objectToFormData from '../form/objectToFormData.js';
|
|
2
2
|
import log from '../browser/log.js';
|
|
3
|
+
import com from '../../com.js';
|
|
3
4
|
/**
|
|
4
5
|
* Uploads a file synchronously through an XHR indicating progress.
|
|
5
6
|
*
|
|
@@ -17,8 +18,9 @@ import log from '../browser/log.js';
|
|
|
17
18
|
* @returns {Promise}
|
|
18
19
|
*/
|
|
19
20
|
export default function upload(url, file, success = null, failure = null, progress = null) {
|
|
20
|
-
|
|
21
|
-
return
|
|
21
|
+
const fn = () => {
|
|
22
|
+
return com.run('post', url || bbn.env.path, {
|
|
23
|
+
data: objectToFormData({ file }),
|
|
22
24
|
headers: {
|
|
23
25
|
'Content-Type': 'multipart/form-data',
|
|
24
26
|
},
|
package/dist/index.js
CHANGED
|
@@ -39,7 +39,7 @@ import $ from './$.js';
|
|
|
39
39
|
import lng from './lng.js';
|
|
40
40
|
import vars from './vars.js';
|
|
41
41
|
import env from './env.js';
|
|
42
|
-
import
|
|
42
|
+
import com from './com.js';
|
|
43
43
|
import db from './db.js';
|
|
44
44
|
import fn from './fn.js';
|
|
45
45
|
import date from './date.js';
|
|
@@ -54,7 +54,7 @@ const bbn = {
|
|
|
54
54
|
lng,
|
|
55
55
|
var: vars,
|
|
56
56
|
date,
|
|
57
|
-
|
|
57
|
+
com,
|
|
58
58
|
env,
|
|
59
59
|
db,
|
|
60
60
|
fn,
|
package/package.json
CHANGED
package/dist/req.d.ts
DELETED