@bbn/bbn 1.0.475 → 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/date.d.ts +2 -0
- package/dist/date.js +24 -2
- package/dist/fn/ajax/ajax.js +188 -7
- package/dist/fn/ajax/stream.js +4 -1
- package/package.json +1 -1
package/dist/date.d.ts
CHANGED
package/dist/date.js
CHANGED
|
@@ -15,6 +15,7 @@ import each from './fn/loop/each.js';
|
|
|
15
15
|
import substr from './fn/string/substr.js';
|
|
16
16
|
import isNumber from './fn/type/isNumber.js';
|
|
17
17
|
import isDate from './fn/type/isDate.js';
|
|
18
|
+
import isPrimitive from './fn/type/isPrimitive.js';
|
|
18
19
|
const patterns = [
|
|
19
20
|
// MariaDB DATETIME "YYYY-MM-DD HH:MM:SS"
|
|
20
21
|
{
|
|
@@ -142,6 +143,8 @@ const unitsCorrespondence = {
|
|
|
142
143
|
'Month': 'm',
|
|
143
144
|
'MONTHS': 'm',
|
|
144
145
|
'MONTH': 'm',
|
|
146
|
+
'MMMM': 'm',
|
|
147
|
+
'MMM': 'm',
|
|
145
148
|
'MM': 'm',
|
|
146
149
|
'M': 'm',
|
|
147
150
|
'm': 'm',
|
|
@@ -341,6 +344,20 @@ class bbnDateTool {
|
|
|
341
344
|
get YY() {
|
|
342
345
|
return substr(this.year().toString(), 2, 2);
|
|
343
346
|
}
|
|
347
|
+
get MMMM() {
|
|
348
|
+
const opt = {
|
|
349
|
+
month: 'long',
|
|
350
|
+
};
|
|
351
|
+
const d = new Intl.DateTimeFormat([bbn.env.lang, ...navigator.languages], opt);
|
|
352
|
+
return d.format(__classPrivateFieldGet(this, _bbnDateTool_value, "f"));
|
|
353
|
+
}
|
|
354
|
+
get MMM() {
|
|
355
|
+
const opt = {
|
|
356
|
+
month: 'short',
|
|
357
|
+
};
|
|
358
|
+
const d = new Intl.DateTimeFormat([bbn.env.lang, ...navigator.languages], opt);
|
|
359
|
+
return d.format(__classPrivateFieldGet(this, _bbnDateTool_value, "f"));
|
|
360
|
+
}
|
|
344
361
|
get MM() {
|
|
345
362
|
const m = parseInt(this.month().toString());
|
|
346
363
|
return m < 10 ? '0' + m.toString() : m.toString();
|
|
@@ -474,8 +491,13 @@ class bbnDateTool {
|
|
|
474
491
|
return;
|
|
475
492
|
}
|
|
476
493
|
if (part in unitsCorrespondence) {
|
|
477
|
-
|
|
478
|
-
|
|
494
|
+
if (part in this && isPrimitive(this[part])) {
|
|
495
|
+
str += this[part];
|
|
496
|
+
}
|
|
497
|
+
else {
|
|
498
|
+
const suffix = formatsMap[unitsCorrespondence[part]];
|
|
499
|
+
str += this[suffix];
|
|
500
|
+
}
|
|
479
501
|
}
|
|
480
502
|
else {
|
|
481
503
|
str += part;
|
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) {
|