@bbn/bbn 1.0.342 → 1.0.343

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.
@@ -34,12 +34,10 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
34
34
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
35
  }
36
36
  };
37
- import isObject from '../type/isObject.js';
38
37
  import replaceAll from '../string/replaceAll.js';
39
38
  import getRequestId from './getRequestId.js';
40
39
  import getLoader from './getLoader.js';
41
40
  import extend from '../object/extend.js';
42
- import numProperties from '../object/numProperties.js';
43
41
  import _deleteLoader from './_deleteLoader.js';
44
42
  import isFunction from '../type/isFunction.js';
45
43
  import _addLoader from './_addLoader.js';
@@ -105,29 +103,33 @@ export default function ajax() {
105
103
  args[_i] = arguments[_i];
106
104
  }
107
105
  return __awaiter(this, void 0, void 0, function () {
108
- var url, datatype, data, success, failure, abort, requestId_1, loaderObj, aborter, options, args_1, axiosMethod, loader_1, tst_1;
106
+ var url, datatype, data, success, failure, abort, method, requestId_1, loaderObj, aborter, contentType, options, loader_1, tst_1;
109
107
  return __generator(this, function (_a) {
110
108
  datatype = null;
111
109
  data = null;
112
110
  success = null;
113
111
  failure = null;
114
112
  abort = null;
113
+ method = null;
115
114
  if (args.length === 1 && args[0] && (typeof args[0] === "object") && args[0].url) {
116
115
  url = args[0].url;
117
- if (args[0].abort) {
118
- abort = args[0].abort;
116
+ if (args[0].datatype) {
117
+ datatype = args[0].datatype;
119
118
  }
120
- if (args[0].failure) {
121
- failure = args[0].failure;
119
+ if (args[0].data) {
120
+ data = args[0].data;
122
121
  }
123
122
  if (args[0].success) {
124
123
  success = args[0].success;
125
124
  }
126
- if (args[0].data) {
127
- data = args[0].data;
125
+ if (args[0].failure) {
126
+ failure = args[0].failure;
128
127
  }
129
- if (args[0].datatype) {
130
- datatype = args[0].datatype;
128
+ if (args[0].abort) {
129
+ abort = args[0].abort;
130
+ }
131
+ if (args[0].method) {
132
+ method = args[0].method;
131
133
  }
132
134
  }
133
135
  else {
@@ -149,6 +151,9 @@ export default function ajax() {
149
151
  if (args.length > 5) {
150
152
  abort = args[5];
151
153
  }
154
+ if (args.length > 6) {
155
+ method = args[6];
156
+ }
152
157
  }
153
158
  if (typeof url === "string") {
154
159
  if (url.indexOf("://") === -1) {
@@ -163,43 +168,64 @@ export default function ajax() {
163
168
  if (loaderObj === null || loaderObj === void 0 ? void 0 : loaderObj.loader) {
164
169
  return [2 /*return*/, loaderObj.loader];
165
170
  }
166
- if (bbn.env.token) {
167
- extend(data || {}, { _bbn_token: bbn.env.token });
171
+ if (bbn.env.token && bbn.fn.numProperties(data)) {
172
+ data = extend({}, data, { _bbn_token: bbn.env.token });
173
+ if (!method) {
174
+ method = "POST";
175
+ }
176
+ }
177
+ else if (!method) {
178
+ method = "GET";
168
179
  }
169
180
  aborter = new AbortController();
181
+ contentType = "application/json";
182
+ if (datatype === "json") {
183
+ contentType = "application/json";
184
+ }
185
+ else if (datatype === "text") {
186
+ contentType = "text/javascript";
187
+ }
188
+ else if (datatype === "html") {
189
+ contentType = "text/html";
190
+ }
191
+ else {
192
+ contentType = datatype;
193
+ }
170
194
  options = {
195
+ method: method,
171
196
  responseType: datatype,
172
- signal: aborter.signal
197
+ signal: aborter.signal,
198
+ mode: 'cors', // no-cors, *cors, same-origin
199
+ cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
200
+ credentials: 'same-origin', // include, *same-origin, omit
201
+ redirect: 'follow', // manual, *follow, error
202
+ referrerPolicy: 'no-referrer', // no-referrer, *client
203
+ headers: {
204
+ 'Content-Type': contentType,
205
+ }
173
206
  };
174
- if (datatype === "text") {
175
- options['headers'] = {
176
- accept: "text/javascript",
177
- "Content-Type": "text/javascript",
178
- };
179
- }
180
- args_1 = [url];
181
- if (isObject(data) && numProperties(data) > 0) {
182
- args_1.push(data);
183
- }
184
- args_1.push(options);
185
- axiosMethod = args_1.length === 2 ? "get" : "post";
186
- loader_1 = axios[axiosMethod]
187
- .apply(null, args_1)
188
- .then(function (res) {
207
+ if (data && bbn.fn.numProperties(data)) {
208
+ options.body = JSON.stringify(data);
209
+ }
210
+ loader_1 = fetch(url, options).then(function (res) {
189
211
  _deleteLoader(requestId_1, res);
190
212
  bbn.fn.defaultEndLoadingFunction(url, tst_1, data, res);
191
- switch (res.status) {
192
- case 200:
193
- if (isFunction(success)) {
194
- //bbn.fn.log(["THERE US A SUCCESS FUNCTION", res, loader]);
195
- success(res.data, res.headers);
196
- }
197
- break;
198
- default:
199
- //bbn.fn.log(["THIS IS THE DEFAULT FUNCTION", res, loader]);
200
- bbn.fn.defaultAjaxErrorFunction(loader_1, res);
213
+ if (res.ok) {
214
+ switch (res.status) {
215
+ case 200:
216
+ res.json().then(function (d) {
217
+ if (isFunction(success)) {
218
+ //bbn.fn.log(["THERE US A SUCCESS FUNCTION", res, loader]);
219
+ success(d, res.headers);
220
+ }
221
+ });
222
+ break;
223
+ default:
224
+ //bbn.fn.log(["THIS IS THE DEFAULT FUNCTION", res, loader]);
225
+ bbn.fn.defaultAjaxErrorFunction(loader_1, res);
226
+ }
227
+ return res;
201
228
  }
202
- return res;
203
229
  })
204
230
  .catch(function (err) {
205
231
  var isAbort = axios.isCancel(err);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "1.0.342",
3
+ "version": "1.0.343",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",