@bbn/bbn 1.0.480 → 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/com.js CHANGED
@@ -45,6 +45,63 @@ 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
+ 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;
48
105
  };
49
106
  const xhrRequest = (method, url, config = {}, aborter) => {
50
107
  const xhr = new XMLHttpRequest();
@@ -269,63 +326,21 @@ const isCancel = (value) => {
269
326
  return !!(value && value.__BBN_CANCEL__ === true);
270
327
  };
271
328
  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
- }
329
+ // Optionally handle baseURL
330
+ if (config.baseURL && !/^https?:\/\//i.test(url)) {
331
+ url = config.baseURL.replace(/\/+$/, '') + '/' + url.replace(/^\/+/, '');
287
332
  }
288
- // Add query params support if needed
289
- if (config.params) {
290
- const usp = new URLSearchParams(config.params).toString();
291
- url += (url.includes('?') ? '&' : '?') + usp;
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);
339
+ }
340
+ else {
341
+ // Default to fetch
342
+ return fetchRequest(method, url, config, aborter);
292
343
  }
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
344
  };
330
345
  export default {
331
346
  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.481",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",