@bbn/bbn 1.0.480 → 1.0.482

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/com.js CHANGED
@@ -45,6 +45,64 @@ const normalizeDataAndHeaders = (data, headersObj) => {
45
45
  return { body, headers };
46
46
  };
47
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
+ bbn.fn.log('DATA', config.data);
57
+ if (typeof config.data === 'object' && !(config.data instanceof FormData)) {
58
+ fetchConfig.headers.set('Content-Type', 'application/json');
59
+ fetchConfig.body = JSON.stringify(config.data);
60
+ }
61
+ else {
62
+ fetchConfig.body = config.data;
63
+ }
64
+ }
65
+ // Add query params support if needed
66
+ if (config.params) {
67
+ const usp = new URLSearchParams(config.params).toString();
68
+ url += (url.includes('?') ? '&' : '?') + usp;
69
+ }
70
+ const fetchPromise = fetch(url, fetchConfig).then((res) => __awaiter(void 0, void 0, void 0, function* () {
71
+ let data;
72
+ const contentType = res.headers.get('content-type') || '';
73
+ if (contentType.includes('application/json')) {
74
+ data = yield res.json();
75
+ }
76
+ else {
77
+ data = yield res.text();
78
+ }
79
+ const response = {
80
+ data,
81
+ status: res.status,
82
+ statusText: res.statusText,
83
+ headers: Object.fromEntries(res.headers.entries()),
84
+ config,
85
+ request: res,
86
+ };
87
+ if (!res.ok) {
88
+ // axios rejects for 4xx/5xx
89
+ const error = new Error('Request failed with status code ' + res.status);
90
+ error.response = response;
91
+ error.config = config;
92
+ error.request = res;
93
+ throw error;
94
+ }
95
+ return response;
96
+ })).catch((err) => {
97
+ if (err.name === 'AbortError') {
98
+ throw new Cancel('Request canceled');
99
+ }
100
+ throw err;
101
+ });
102
+ // axios usually gives you the cancel token separately;
103
+ // here we return both for convenience.
104
+ fetchPromise.cancel = () => aborter.abort();
105
+ return fetchPromise;
48
106
  };
49
107
  const xhrRequest = (method, url, config = {}, aborter) => {
50
108
  const xhr = new XMLHttpRequest();
@@ -269,63 +327,21 @@ const isCancel = (value) => {
269
327
  return !!(value && value.__BBN_CANCEL__ === true);
270
328
  };
271
329
  const run = (method, url, config = {}, aborter) => {
272
- const fetchConfig = {
273
- method,
274
- headers: new Headers(config.headers || {}),
275
- signal: aborter === null || aborter === void 0 ? void 0 : aborter.signal,
276
- };
277
- const hasBody = methodsWithBody.includes(method);
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
- }
330
+ // Optionally handle baseURL
331
+ if (config.baseURL && !/^https?:\/\//i.test(url)) {
332
+ url = config.baseURL.replace(/\/+$/, '') + '/' + url.replace(/^\/+/, '');
287
333
  }
288
- // Add query params support if needed
289
- if (config.params) {
290
- const usp = new URLSearchParams(config.params).toString();
291
- url += (url.includes('?') ? '&' : '?') + usp;
334
+ url = buildURL(url, config.params);
335
+ const wantsUploadProgress = typeof config.onUploadProgress === 'function' &&
336
+ methodsWithBody.includes(method);
337
+ if (wantsUploadProgress) {
338
+ // Use XHR when upload progress is requested
339
+ return xhrRequest(method, url, config, aborter);
340
+ }
341
+ else {
342
+ // Default to fetch
343
+ return fetchRequest(method, url, config, aborter);
292
344
  }
293
- const fetchPromise = fetch(url, fetchConfig).then((res) => __awaiter(void 0, void 0, void 0, function* () {
294
- let data;
295
- const contentType = res.headers.get('content-type') || '';
296
- if (contentType.includes('application/json')) {
297
- data = yield res.json();
298
- }
299
- else {
300
- data = yield res.text();
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;
329
345
  };
330
346
  export default {
331
347
  isCancel,
@@ -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
- let fn = () => {
21
- return axios.post(url || bbn.env.path, objectToFormData(file), {
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "1.0.480",
3
+ "version": "1.0.482",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",