@codebit-programando-solucoes/codebit-web 1.1.18 → 1.1.20

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/api/api.d.ts CHANGED
@@ -12,6 +12,8 @@ export interface RequestConfig {
12
12
  signal?: AbortSignal;
13
13
  /** Desired response format */
14
14
  responseType?: 'json' | 'text' | 'blob' | 'arraybuffer' | 'stream';
15
+ /** Callback for upload progress events */
16
+ onUploadProgress?: (progressEvent: ProgressEvent) => void;
15
17
  }
16
18
 
17
19
  /** API response object. */
package/dist/index.cjs CHANGED
@@ -25,6 +25,7 @@ var __webpack_exports__ = {};
25
25
  __webpack_require__.r(__webpack_exports__);
26
26
  __webpack_require__.d(__webpack_exports__, {
27
27
  fetchImageAsBlob: ()=>fetchImageAsBlob,
28
+ getScrollbarWidth: ()=>getScrollbarWidth,
28
29
  loadImage: ()=>loadImage,
29
30
  markdownItBrEmptyLines: ()=>markdownItBrEmptyLines,
30
31
  ErrorCode: ()=>ErrorCode,
@@ -173,8 +174,39 @@ function isBinaryContent(contentType) {
173
174
  ];
174
175
  return binaryTypes.some((type)=>contentType.startsWith(type));
175
176
  }
177
+ function parseResponseHeaders(headerStr) {
178
+ const headers = new Headers();
179
+ if (!headerStr) return headers;
180
+ const lines = headerStr.split('\r\n').filter((line)=>!!line);
181
+ lines.forEach((line)=>{
182
+ const [key, value] = line.split(': ');
183
+ if (key && value) headers.append(key, value);
184
+ });
185
+ return headers;
186
+ }
187
+ async function createApiErrorFromXhr(xhr, request) {
188
+ let responseData;
189
+ try {
190
+ const contentType = xhr.getResponseHeader('content-type');
191
+ responseData = contentType?.includes('application/json') ? JSON.parse(xhr.responseText) : xhr.responseText;
192
+ } catch {
193
+ responseData = xhr.responseText;
194
+ }
195
+ const responseObject = {
196
+ status: xhr.status,
197
+ statusText: xhr.statusText || '',
198
+ headers: parseResponseHeaders(xhr.getAllResponseHeaders()),
199
+ data: responseData
200
+ };
201
+ return new ApiError(responseObject, request);
202
+ }
176
203
  async function makeRequest(url, options = {}) {
177
- const { signal: externalSignal, timeout = API_CONFIG.timeout, headers, responseType, ...restOptions } = options;
204
+ const { signal: externalSignal, timeout = API_CONFIG.timeout, headers, responseType, onUploadProgress, ...restOptions } = options;
205
+ if (onUploadProgress && restOptions.method && [
206
+ 'POST',
207
+ 'PUT',
208
+ 'PATCH'
209
+ ].includes(restOptions.method.toUpperCase()) && options.body) return makeRequestWithProgress(url, options);
178
210
  const { timeoutController, timer, fullUrl } = setupRequestControl(url, externalSignal, timeout);
179
211
  const config = {
180
212
  credentials: 'include',
@@ -204,6 +236,77 @@ async function makeRequest(url, options = {}) {
204
236
  headers: response.headers
205
237
  };
206
238
  }
239
+ function makeRequestWithProgress(url, options) {
240
+ return new Promise((resolve, reject)=>{
241
+ const { signal: externalSignal, timeout = API_CONFIG.timeout, headers, responseType, onUploadProgress, method = 'POST', body } = options;
242
+ const fullUrl = url.startsWith('http') ? url : `${API_CONFIG.baseURL}${url}`;
243
+ const xhr = new XMLHttpRequest();
244
+ if ('blob' === responseType || 'arraybuffer' === responseType) xhr.responseType = responseType;
245
+ else if ('stream' === responseType) console.warn('Stream responseType is not supported with XMLHttpRequest');
246
+ const { timeoutController, timer } = setupRequestControl(fullUrl, externalSignal, timeout);
247
+ const originalAbort = timeoutController.abort;
248
+ timeoutController.abort = function(reason) {
249
+ originalAbort.call(this, reason);
250
+ xhr.abort();
251
+ };
252
+ xhr.open(method, fullUrl, true);
253
+ xhr.withCredentials = true;
254
+ const finalHeaders = buildHeaders(headers);
255
+ Object.keys(finalHeaders).forEach((key)=>{
256
+ if ('content-type' === key.toLowerCase() && body instanceof FormData) return;
257
+ xhr.setRequestHeader(key, finalHeaders[key]);
258
+ });
259
+ const config = {
260
+ headers: finalHeaders,
261
+ body
262
+ };
263
+ prepareBodyAndContentType(config);
264
+ const requestBody = config.body;
265
+ if (onUploadProgress) xhr.upload.addEventListener('progress', onUploadProgress);
266
+ xhr.onload = function() {
267
+ if (timer) clearTimeout(timer);
268
+ if (xhr.status >= 200 && xhr.status < 300) {
269
+ const contentType = xhr.getResponseHeader('content-type');
270
+ let data;
271
+ let inferredResponseType;
272
+ inferredResponseType = contentType?.includes('application/json') ? 'json' : isBinaryContent(contentType) ? 'blob' : 'text';
273
+ const finalResponseType = responseType || inferredResponseType;
274
+ if ('json' === finalResponseType) try {
275
+ data = JSON.parse(xhr.responseText);
276
+ } catch {
277
+ data = xhr.responseText;
278
+ }
279
+ else data = 'blob' === finalResponseType ? xhr.response : 'arraybuffer' === finalResponseType ? xhr.response : xhr.responseText;
280
+ resolve({
281
+ data,
282
+ status: xhr.status,
283
+ headers: parseResponseHeaders(xhr.getAllResponseHeaders())
284
+ });
285
+ } else createApiErrorFromXhr(xhr, options).then(reject);
286
+ };
287
+ xhr.onerror = function() {
288
+ if (timer) clearTimeout(timer);
289
+ const networkError = new Error('Network Error');
290
+ networkError.name = 'TypeError';
291
+ const errorCode = analyzeNetworkError(networkError);
292
+ const apiNetworkError = new ApiNetworkError(networkError);
293
+ apiNetworkError.errorCode = errorCode;
294
+ reject(apiNetworkError);
295
+ };
296
+ xhr.ontimeout = function() {
297
+ if (timer) clearTimeout(timer);
298
+ const timeoutError = new ApiError({
299
+ status: 0,
300
+ statusText: 'TIMEOUT',
301
+ headers: new Headers(),
302
+ data: null
303
+ }, options);
304
+ timeoutError.code = ErrorCode.ERR_TIMEOUT;
305
+ reject(timeoutError);
306
+ };
307
+ xhr.send(requestBody);
308
+ });
309
+ }
207
310
  const Api = {
208
311
  configure: (config)=>{
209
312
  if (void 0 !== config.baseURL) API_CONFIG.baseURL = config.baseURL;
@@ -407,6 +510,27 @@ const markdownItBrEmptyLines = (md)=>{
407
510
  }
408
511
  });
409
512
  };
513
+ function getScrollbarWidth(scrollbarWidth = 'auto') {
514
+ const outer = document.createElement('div');
515
+ outer.style.visibility = 'hidden';
516
+ outer.style.position = 'absolute';
517
+ outer.style.top = '-9999px';
518
+ outer.style.width = '100px';
519
+ outer.style.height = '100px';
520
+ outer.style.overflow = 'scroll';
521
+ outer.style.scrollbarWidth = 'thin';
522
+ const inner = document.createElement('div');
523
+ inner.style.width = '100%';
524
+ inner.style.height = '100%';
525
+ outer.appendChild(inner);
526
+ document.body.appendChild(outer);
527
+ const width = outer.offsetWidth - inner.offsetWidth;
528
+ outer.remove();
529
+ if (width > 0) return width;
530
+ if ('thin' === scrollbarWidth) return 8;
531
+ if ('none' === scrollbarWidth) return 0;
532
+ return 16;
533
+ }
410
534
  exports.Api = __webpack_exports__.Api;
411
535
  exports.ApiError = __webpack_exports__.ApiError;
412
536
  exports.ApiNetworkError = __webpack_exports__.ApiNetworkError;
@@ -415,6 +539,7 @@ exports.Logger = __webpack_exports__.Logger;
415
539
  exports.copyToClipboard = __webpack_exports__.copyToClipboard;
416
540
  exports.createAdvancedPartialRegex = __webpack_exports__.createAdvancedPartialRegex;
417
541
  exports.fetchImageAsBlob = __webpack_exports__.fetchImageAsBlob;
542
+ exports.getScrollbarWidth = __webpack_exports__.getScrollbarWidth;
418
543
  exports.loadImage = __webpack_exports__.loadImage;
419
544
  exports.markdownItBrEmptyLines = __webpack_exports__.markdownItBrEmptyLines;
420
545
  for(var __rspack_i in __webpack_exports__)if (-1 === [
@@ -426,6 +551,7 @@ for(var __rspack_i in __webpack_exports__)if (-1 === [
426
551
  "copyToClipboard",
427
552
  "createAdvancedPartialRegex",
428
553
  "fetchImageAsBlob",
554
+ "getScrollbarWidth",
429
555
  "loadImage",
430
556
  "markdownItBrEmptyLines"
431
557
  ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Calculates the width of the browser's vertical scrollbar.
3
+ *
4
+ * The function creates temporary DOM elements, forces a scrollable container, and measures the difference between the outer container's width and the inner element's width. If the
5
+ * measurement fails (e.g., overlay scrollbars), a fallback value is returned based on the `scrollbarWidth` argument.
6
+ *
7
+ * @param {'auto' | 'none' | 'thin'} [scrollbarWidth='auto'] Desired fallback width when the actual scrollbar size cannot be measured. `'none'` returns **0px**, `'thin'` returns
8
+ * **8px**, and any other value (including `'auto'`) returns **16px**. Default is `'auto'`
9
+ * @returns {number} The width of the vertical scrollbar in pixels, or the fallback value when the measurement is not possible.
10
+ */
11
+ export function getScrollbarWidth(
12
+ scrollbarWidth?: 'auto' | 'none' | 'thin',
13
+ ): number;
@@ -3,3 +3,4 @@ export * from './Clipboard';
3
3
  export * from './Image';
4
4
  export * from './Logger';
5
5
  export * from './markdown-it';
6
+ export * from './Layout';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codebit-programando-solucoes/codebit-web",
3
- "version": "1.1.18",
3
+ "version": "1.1.20",
4
4
  "main": "./dist/index.cjs",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",