@bbn/bbn 1.0.298 → 1.0.300

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.
@@ -1,57 +1,14 @@
1
1
  /**
2
- * Creates an XHR object and returns the Promise.
3
- *
4
- * Checks the URL, makes an ID, creates a loader, sets the general callbacks,
5
- * makes a POST if data is given a GET otherwise (GET data should be added
6
- * directly in the URL), and returns the Promise.
7
- *
8
- * @method ajax
2
+ * @method stream
9
3
  * @global
10
4
  * @memberof bbn.fn
11
- * @example
12
- * ```javascript
13
- * // Promise
14
- * bbn.fn.ajax(
15
- * 'my/location',
16
- * 'json',
17
- * {id: 7},
18
- * d => {
19
- * console.log(d);
20
- * alert("Success!");
21
- * },
22
- * err => {
23
- * console.log(err);
24
- * alert("Failure!");
25
- * },
26
- * () => {
27
- * alert("Request aborted!");
28
- * }
29
- * )
30
- * ```
31
- *
32
- * @example
33
- * ```javascript
34
- * // Promise
35
- * bbn.fn.ajax('my/location')
36
- * .then(
37
- * d => {
38
- * console.log(d);
39
- * alert("Success!");
40
- * }
41
- * )
42
- * .catch(
43
- * err => {
44
- * }
45
- * )
46
- * ```
47
5
  *
48
6
  * @param {String} url The URL to be requested by XHR
49
- * @param {String} datatype The type of data expected
50
- * @param {Object} data The data to send through POST
51
7
  * @param {Function} success The function to execute if the request goes well (200)
8
+ * @param {Object} data The data to send through POST
52
9
  * @param {Function} failure The function to execute if the request goes bad
53
10
  * @param {Function} abort The function to execute if the request is aborted
54
11
  *
55
12
  * @returns {Promise} The Promise created by the generated XHR.
56
13
  */
57
- export default function stream(url: any, success: any, data: any, failure: any, abort: any): Promise<any>;
14
+ export default function stream(url: any, success: any, data: any, failure: any, abort: any): any;
@@ -1,178 +1,98 @@
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
- var __generator = (this && this.__generator) || function (thisArg, body) {
11
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
12
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
- function verb(n) { return function (v) { return step([n, v]); }; }
14
- function step(op) {
15
- if (f) throw new TypeError("Generator is already executing.");
16
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
17
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18
- if (y = 0, t) op = [op[0] & 2, t.value];
19
- switch (op[0]) {
20
- case 0: case 1: t = op; break;
21
- case 4: _.label++; return { value: op[1], done: false };
22
- case 5: _.label++; y = op[1]; op = [0]; continue;
23
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
- default:
25
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
- if (t[2]) _.ops.pop();
30
- _.trys.pop(); continue;
31
- }
32
- op = body.call(thisArg, _);
33
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
- }
36
- };
37
1
  import getRequestId from './getRequestId.js';
38
2
  import getLoader from './getLoader.js';
39
3
  import _deleteLoader from './_deleteLoader.js';
40
4
  import isFunction from '../type/isFunction.js';
41
5
  import _addLoader from './_addLoader.js';
42
6
  import defaultStartLoadingFunction from '../default/defaultStartLoadingFunction.js';
7
+ import defaultEndLoadingFunction from '../default/defaultEndLoadingFunction.js';
8
+ import defaultAjaxAbortFunction from '../default/defaultAjaxAbortFunction.js';
9
+ import defaultAjaxErrorFunction from '../default/defaultAjaxErrorFunction.js';
43
10
  /**
44
- * Creates an XHR object and returns the Promise.
45
- *
46
- * Checks the URL, makes an ID, creates a loader, sets the general callbacks,
47
- * makes a POST if data is given a GET otherwise (GET data should be added
48
- * directly in the URL), and returns the Promise.
49
- *
50
- * @method ajax
11
+ * @method stream
51
12
  * @global
52
13
  * @memberof bbn.fn
53
- * @example
54
- * ```javascript
55
- * // Promise
56
- * bbn.fn.ajax(
57
- * 'my/location',
58
- * 'json',
59
- * {id: 7},
60
- * d => {
61
- * console.log(d);
62
- * alert("Success!");
63
- * },
64
- * err => {
65
- * console.log(err);
66
- * alert("Failure!");
67
- * },
68
- * () => {
69
- * alert("Request aborted!");
70
- * }
71
- * )
72
- * ```
73
- *
74
- * @example
75
- * ```javascript
76
- * // Promise
77
- * bbn.fn.ajax('my/location')
78
- * .then(
79
- * d => {
80
- * console.log(d);
81
- * alert("Success!");
82
- * }
83
- * )
84
- * .catch(
85
- * err => {
86
- * }
87
- * )
88
- * ```
89
14
  *
90
15
  * @param {String} url The URL to be requested by XHR
91
- * @param {String} datatype The type of data expected
92
- * @param {Object} data The data to send through POST
93
16
  * @param {Function} success The function to execute if the request goes well (200)
17
+ * @param {Object} data The data to send through POST
94
18
  * @param {Function} failure The function to execute if the request goes bad
95
19
  * @param {Function} abort The function to execute if the request is aborted
96
20
  *
97
21
  * @returns {Promise} The Promise created by the generated XHR.
98
22
  */
99
23
  export default function stream(url, success, data, failure, abort) {
100
- return __awaiter(this, void 0, void 0, function () {
101
- var requestId, loaderObj, aborter, loader, tst;
102
- var _this = this;
103
- return __generator(this, function (_a) {
104
- requestId = getRequestId(url, data, 'json');
105
- loaderObj = getLoader(requestId);
106
- //log("IN AJAX", loaderObj? loaderObj.loader : "NO LOADER")
107
- if (loaderObj === null || loaderObj === void 0 ? void 0 : loaderObj.loader) {
108
- return [2 /*return*/, loaderObj.loader];
109
- }
110
- aborter = new AbortController();
111
- loader = fetch(url, {
112
- method: 'POST', // *GET, POST, PUT, DELETE, etc.
113
- mode: 'cors', // no-cors, *cors, same-origin
114
- cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
115
- credentials: 'same-origin', // include, *same-origin, omit
116
- headers: {
117
- 'Content-Type': 'application/json'
118
- // 'Content-Type': 'application/x-www-form-urlencoded',
119
- },
120
- redirect: 'follow', // manual, *follow, error
121
- referrerPolicy: 'no-referrer', // no-referrer, *client
122
- signal: aborter.signal,
123
- body: JSON.stringify(data || {}) // body data type must match "Content-Type" header
124
- })
125
- .then(function (response) { return __awaiter(_this, void 0, void 0, function () {
126
- var reader_1, isFn_1;
127
- return __generator(this, function (_a) {
128
- if (response.body) {
129
- reader_1 = response.body.getReader();
130
- isFn_1 = isFunction(success);
131
- reader_1.read().then(function pump(_a) {
132
- var done = _a.done, value = _a.value;
133
- if (isFn_1) {
134
- success(value);
135
- }
136
- if (done) {
137
- // Do something with last chunk of data then exit reader
138
- _deleteLoader(requestId, value);
139
- bbn.fn.defaultEndLoadingFunction(url, tst, data, value);
140
- return;
141
- }
142
- // Read some more, and call this function again
143
- return reader_1.read().then(pump);
144
- });
145
- }
146
- return [2 /*return*/];
147
- });
148
- }); })
149
- .catch(function (err) {
150
- var isAbort = axios.isCancel(err);
151
- _deleteLoader(requestId, err.message || err.response.data, isAbort);
152
- bbn.fn.defaultEndLoadingFunction(url, tst, data, err);
153
- if (isAbort) {
154
- var ok = 1;
155
- if (isFunction(abort)) {
156
- ok = abort(err.message, url);
157
- }
158
- if (ok) {
159
- bbn.fn.defaultAjaxAbortFunction(err.message, url);
160
- }
24
+ var requestId = getRequestId(url, data, 'json');
25
+ var loaderObj = getLoader(requestId);
26
+ //log("IN AJAX", loaderObj? loaderObj.loader : "NO LOADER")
27
+ if (loaderObj === null || loaderObj === void 0 ? void 0 : loaderObj.loader) {
28
+ return loaderObj.loader;
29
+ }
30
+ if (!data) {
31
+ data = {};
32
+ }
33
+ if (bbn.env.token) {
34
+ data._bbn_token = bbn.env.token;
35
+ }
36
+ var aborter = new AbortController();
37
+ var loader = fetch(url, {
38
+ method: 'POST', // *GET, POST, PUT, DELETE, etc.
39
+ mode: 'cors', // no-cors, *cors, same-origin
40
+ cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
41
+ credentials: 'same-origin', // include, *same-origin, omit
42
+ headers: {
43
+ 'Content-Type': 'application/json'
44
+ // 'Content-Type': 'application/x-www-form-urlencoded',
45
+ },
46
+ redirect: 'follow', // manual, *follow, error
47
+ referrerPolicy: 'no-referrer', // no-referrer, *client
48
+ signal: aborter.signal,
49
+ body: JSON.stringify(data || {}) // body data type must match "Content-Type" header
50
+ })
51
+ .then(function (response) {
52
+ if (response.body) {
53
+ var reader_1 = response.body.getReader();
54
+ var isFn_1 = isFunction(success);
55
+ reader_1.read().then(function pump(_a) {
56
+ var done = _a.done, value = _a.value;
57
+ if (isFn_1) {
58
+ success(value);
161
59
  }
162
- else {
163
- var ok = 1;
164
- if (isFunction(failure)) {
165
- ok = failure(err.request, err);
166
- }
167
- if (ok) {
168
- bbn.fn.defaultAjaxErrorFunction(err.request, err.response ? err.response.data : "", err.response ? err.response.status : err);
169
- }
60
+ if (done) {
61
+ // Do something with last chunk of data then exit reader
62
+ _deleteLoader(requestId, value);
63
+ defaultEndLoadingFunction(url, tst, data, value);
64
+ return;
170
65
  }
66
+ // Read some more, and call this function again
67
+ return reader_1.read().then(pump);
171
68
  });
172
- tst = _addLoader(requestId, loader, aborter);
173
- defaultStartLoadingFunction(url, tst, data, requestId);
174
- return [2 /*return*/, loader];
175
- });
69
+ }
70
+ })
71
+ .catch(function (err) {
72
+ var isAbort = axios.isCancel(err);
73
+ _deleteLoader(requestId, err.message || err.response.data, isAbort);
74
+ defaultEndLoadingFunction(url, tst, data, err);
75
+ if (isAbort) {
76
+ var ok = 1;
77
+ if (isFunction(abort)) {
78
+ ok = abort(err.message, url);
79
+ }
80
+ if (ok) {
81
+ defaultAjaxAbortFunction(err.message, url);
82
+ }
83
+ }
84
+ else {
85
+ var ok = 1;
86
+ if (isFunction(failure)) {
87
+ ok = failure(err.request, err);
88
+ }
89
+ if (ok) {
90
+ defaultAjaxErrorFunction(err.request, err.response ? err.response.data : "", err.response ? err.response.status : err);
91
+ }
92
+ }
176
93
  });
94
+ var tst = _addLoader(requestId, loader, aborter);
95
+ defaultStartLoadingFunction(url, tst, data, requestId);
96
+ return loader;
177
97
  }
178
98
  ;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "1.0.298",
3
+ "version": "1.0.300",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",