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

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/index.cjs CHANGED
@@ -24,17 +24,22 @@ var __webpack_require__ = {};
24
24
  var __webpack_exports__ = {};
25
25
  __webpack_require__.r(__webpack_exports__);
26
26
  __webpack_require__.d(__webpack_exports__, {
27
- fetchImageAsBlob: ()=>fetchImageAsBlob,
28
- getScrollbarWidth: ()=>getScrollbarWidth,
29
- loadImage: ()=>loadImage,
27
+ removeHashParam: ()=>removeHashParam,
30
28
  markdownItBrEmptyLines: ()=>markdownItBrEmptyLines,
31
- ErrorCode: ()=>ErrorCode,
29
+ getHashParams: ()=>getHashParams,
32
30
  ApiNetworkError: ()=>ApiNetworkError,
33
- Logger: ()=>Logger,
34
31
  copyToClipboard: ()=>copyToClipboard,
32
+ loadImage: ()=>loadImage,
33
+ createAdvancedPartialRegex: ()=>createAdvancedPartialRegex,
34
+ hasHashParam: ()=>hasHashParam,
35
+ ErrorCode: ()=>ErrorCode,
36
+ addHashParam: ()=>addHashParam,
37
+ getScrollbarWidth: ()=>getScrollbarWidth,
38
+ Logger: ()=>Logger,
39
+ getHashParamValue: ()=>getHashParamValue,
35
40
  Api: ()=>Api,
36
41
  ApiError: ()=>ApiError,
37
- createAdvancedPartialRegex: ()=>createAdvancedPartialRegex
42
+ fetchImageAsBlob: ()=>fetchImageAsBlob
38
43
  });
39
44
  class ApiError extends Error {
40
45
  constructor(response, request = null){
@@ -151,14 +156,19 @@ function setupRequestControl(url, externalSignal, timeout = API_CONFIG.timeout)
151
156
  fullUrl
152
157
  };
153
158
  }
159
+ function isNativeBodyType(body) {
160
+ return 'string' == typeof body || body instanceof URLSearchParams || body instanceof Blob || body instanceof ArrayBuffer || ArrayBuffer.isView(body);
161
+ }
154
162
  function prepareBodyAndContentType(config) {
155
- if (config.body) {
156
- if (config.body instanceof FormData) {
157
- if (config.headers['Content-Type']) delete config.headers['Content-Type'];
158
- } else if ('object' == typeof config.body) {
159
- config.body = JSON.stringify(config.body);
160
- config.headers['Content-Type'] = 'application/json';
161
- }
163
+ if (!config.body) return;
164
+ if (config.body instanceof FormData) {
165
+ if (config.headers['Content-Type']) delete config.headers['Content-Type'];
166
+ return;
167
+ }
168
+ if (isNativeBodyType(config.body)) return;
169
+ if ('object' == typeof config.body) {
170
+ config.body = JSON.stringify(config.body);
171
+ config.headers['Content-Type'] = 'application/json';
162
172
  }
163
173
  }
164
174
  function isBinaryContent(contentType) {
@@ -244,24 +254,24 @@ function makeRequestWithProgress(url, options) {
244
254
  if ('blob' === responseType || 'arraybuffer' === responseType) xhr.responseType = responseType;
245
255
  else if ('stream' === responseType) console.warn('Stream responseType is not supported with XMLHttpRequest');
246
256
  const { timeoutController, timer } = setupRequestControl(fullUrl, externalSignal, timeout);
257
+ let abortReason = null;
247
258
  const originalAbort = timeoutController.abort;
248
259
  timeoutController.abort = function(reason) {
260
+ abortReason = reason;
249
261
  originalAbort.call(this, reason);
250
262
  xhr.abort();
251
263
  };
252
264
  xhr.open(method, fullUrl, true);
253
265
  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
266
  const config = {
260
- headers: finalHeaders,
267
+ headers: buildHeaders(headers),
261
268
  body
262
269
  };
263
270
  prepareBodyAndContentType(config);
264
271
  const requestBody = config.body;
272
+ Object.keys(config.headers).forEach((key)=>{
273
+ xhr.setRequestHeader(key, config.headers[key]);
274
+ });
265
275
  if (onUploadProgress) xhr.upload.addEventListener('progress', onUploadProgress);
266
276
  xhr.onload = function() {
267
277
  if (timer) clearTimeout(timer);
@@ -293,6 +303,12 @@ function makeRequestWithProgress(url, options) {
293
303
  apiNetworkError.errorCode = errorCode;
294
304
  reject(apiNetworkError);
295
305
  };
306
+ xhr.onabort = function() {
307
+ if (timer) clearTimeout(timer);
308
+ const abortError = new Error('TIMEOUT' === abortReason ? 'timeout exceeded' : 'The operation was aborted');
309
+ abortError.name = 'TIMEOUT' === abortReason ? 'TimeoutError' : 'AbortError';
310
+ reject(new ApiNetworkError(abortError));
311
+ };
296
312
  xhr.ontimeout = function() {
297
313
  if (timer) clearTimeout(timer);
298
314
  const timeoutError = new ApiError({
@@ -304,6 +320,13 @@ function makeRequestWithProgress(url, options) {
304
320
  timeoutError.code = ErrorCode.ERR_TIMEOUT;
305
321
  reject(timeoutError);
306
322
  };
323
+ if (timeoutController.signal.aborted) {
324
+ if (timer) clearTimeout(timer);
325
+ const abortError = new Error('The operation was aborted');
326
+ abortError.name = 'AbortError';
327
+ reject(new ApiNetworkError(abortError));
328
+ return;
329
+ }
307
330
  xhr.send(requestBody);
308
331
  });
309
332
  }
@@ -531,29 +554,79 @@ function getScrollbarWidth(scrollbarWidth = 'auto') {
531
554
  if ('none' === scrollbarWidth) return 0;
532
555
  return 16;
533
556
  }
557
+ const getHashParams = ()=>{
558
+ const hash = globalThis.location.hash.substring(1);
559
+ return new URLSearchParams(hash);
560
+ };
561
+ const hasHashParam = (param)=>{
562
+ const params = getHashParams();
563
+ return params.has(param);
564
+ };
565
+ const getHashParamValue = (param)=>{
566
+ const params = getHashParams();
567
+ return params.get(param);
568
+ };
569
+ const addHashParam = (param, value)=>{
570
+ const params = getHashParams();
571
+ if (null == value) {
572
+ if (!params.has(param)) {
573
+ const paramString = params.toString();
574
+ const separator = paramString ? '&' : '';
575
+ globalThis.location.hash = paramString + separator + encodeURIComponent(param);
576
+ }
577
+ } else if ('' === value) {
578
+ params.set(param, '');
579
+ const paramEntries = [];
580
+ for (const [key, val] of params.entries())if ('' === val) paramEntries.push(encodeURIComponent(key));
581
+ else paramEntries.push(encodeURIComponent(key) + '=' + encodeURIComponent(val));
582
+ globalThis.location.hash = paramEntries.join('&');
583
+ } else {
584
+ params.set(param, value);
585
+ globalThis.location.hash = params.toString();
586
+ }
587
+ };
588
+ const removeHashParam = (param)=>{
589
+ const params = getHashParams();
590
+ params.delete(param);
591
+ const paramEntries = [];
592
+ for (const [key, value] of params.entries())if ('' === value) paramEntries.push(encodeURIComponent(key));
593
+ else paramEntries.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
594
+ if (paramEntries.length > 0) globalThis.location.hash = paramEntries.join('&');
595
+ else globalThis.history.replaceState(null, '', globalThis.location.pathname + globalThis.location.search);
596
+ };
534
597
  exports.Api = __webpack_exports__.Api;
535
598
  exports.ApiError = __webpack_exports__.ApiError;
536
599
  exports.ApiNetworkError = __webpack_exports__.ApiNetworkError;
537
600
  exports.ErrorCode = __webpack_exports__.ErrorCode;
538
601
  exports.Logger = __webpack_exports__.Logger;
602
+ exports.addHashParam = __webpack_exports__.addHashParam;
539
603
  exports.copyToClipboard = __webpack_exports__.copyToClipboard;
540
604
  exports.createAdvancedPartialRegex = __webpack_exports__.createAdvancedPartialRegex;
541
605
  exports.fetchImageAsBlob = __webpack_exports__.fetchImageAsBlob;
606
+ exports.getHashParamValue = __webpack_exports__.getHashParamValue;
607
+ exports.getHashParams = __webpack_exports__.getHashParams;
542
608
  exports.getScrollbarWidth = __webpack_exports__.getScrollbarWidth;
609
+ exports.hasHashParam = __webpack_exports__.hasHashParam;
543
610
  exports.loadImage = __webpack_exports__.loadImage;
544
611
  exports.markdownItBrEmptyLines = __webpack_exports__.markdownItBrEmptyLines;
612
+ exports.removeHashParam = __webpack_exports__.removeHashParam;
545
613
  for(var __rspack_i in __webpack_exports__)if (-1 === [
546
614
  "Api",
547
615
  "ApiError",
548
616
  "ApiNetworkError",
549
617
  "ErrorCode",
550
618
  "Logger",
619
+ "addHashParam",
551
620
  "copyToClipboard",
552
621
  "createAdvancedPartialRegex",
553
622
  "fetchImageAsBlob",
623
+ "getHashParamValue",
624
+ "getHashParams",
554
625
  "getScrollbarWidth",
626
+ "hasHashParam",
555
627
  "loadImage",
556
- "markdownItBrEmptyLines"
628
+ "markdownItBrEmptyLines",
629
+ "removeHashParam"
557
630
  ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
558
631
  Object.defineProperty(exports, '__esModule', {
559
632
  value: true
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Utility functions for hash parameter manipulation
3
+ */
4
+
5
+ /**
6
+ * Helper function to get URLSearchParams from hash
7
+ * @returns URLSearchParams object containing hash parameters
8
+ */
9
+ export declare function getHashParams(): URLSearchParams;
10
+
11
+ /**
12
+ * Helper function to check if a parameter exists in hash
13
+ * @param param - Parameter to check
14
+ * @returns true if parameter exists, false otherwise
15
+ */
16
+ export declare function hasHashParam(param: string): boolean;
17
+
18
+ /**
19
+ * Helper function to get the value of a parameter from hash
20
+ * @param param - Parameter to get value for
21
+ * @returns Value of the parameter or null if not found
22
+ */
23
+ export declare function getHashParamValue(param: string): string | null;
24
+
25
+ /**
26
+ * Helper function to add a parameter to hash with optional value
27
+ * @param param - Parameter to add
28
+ * @param value - Optional value for the parameter
29
+ */
30
+ export declare function addHashParam(param: string, value?: string): void;
31
+
32
+ /**
33
+ * Helper function to remove a parameter from hash
34
+ * @param param - Parameter to remove
35
+ */
36
+ export declare function removeHashParam(param: string): void;
@@ -4,3 +4,4 @@ export * from './Image';
4
4
  export * from './Logger';
5
5
  export * from './markdown-it';
6
6
  export * from './Layout';
7
+ export * from './HashUtils';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codebit-programando-solucoes/codebit-web",
3
- "version": "1.1.20",
3
+ "version": "1.1.23",
4
4
  "main": "./dist/index.cjs",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
@@ -27,7 +27,10 @@
27
27
  "markdown-it": "^14.1.1"
28
28
  },
29
29
  "devDependencies": {
30
+ "@babel/core": "^7.23.0",
31
+ "@babel/preset-env": "^7.23.0",
30
32
  "@eslint/js": "^9.39.2",
33
+ "@types/jest": "^29.5.14",
31
34
  "@rsbuild/plugin-react": "1.4.3",
32
35
  "@rsbuild/plugin-sass": "1.4.1",
33
36
  "@rslib/core": "^0.18.0",
@@ -41,17 +44,44 @@
41
44
  "eslint-plugin-react": "^7.37.5",
42
45
  "eslint-plugin-react-hooks": "^5.2.0",
43
46
  "eslint-plugin-react-refresh": "^0.4.20",
47
+ "babel-jest": "^29.7.0",
44
48
  "glob": "11.1.0",
45
49
  "globals": "^16.3.0",
50
+ "jest": "^29.7.0",
51
+ "jsdom": "^22.1.0",
46
52
  "prettier": "^3.6.2",
47
53
  "prettier-plugin-jsdoc": "^1.3.3"
48
54
  },
55
+ "jest": {
56
+ "moduleFileExtensions": [
57
+ "js",
58
+ "json",
59
+ "mjs"
60
+ ],
61
+ "roots": [
62
+ "<rootDir>/src"
63
+ ],
64
+ "testEnvironment": "node",
65
+ "testRegex": ".*\\.spec\\.js$",
66
+ "transform": {
67
+ ".+\\.m?js$": "babel-jest"
68
+ },
69
+ "collectCoverageFrom": [
70
+ "src/**/*.js",
71
+ "!src/**/*.spec.js",
72
+ "!src/**/*.test-helper.js"
73
+ ],
74
+ "coverageDirectory": "coverage"
75
+ },
49
76
  "scripts": {
50
77
  "format:write": "prettier --write \"src/**/*.{js,jsx,mjs}\"",
51
78
  "format:check": "prettier --check \"src/**/*.{js,jsx,mjs}\"",
52
79
  "lint:check": "eslint \"{src,apps,libs,test}/**/*.{js,jsx,mjs}\"",
53
80
  "lint:fix": "eslint \"{src,apps,libs,test}/**/*.{js,jsx,mjs}\" --fix",
54
81
  "build": "rslib build",
55
- "minify": "node scripts/minify.cjs"
82
+ "minify": "node scripts/minify.cjs",
83
+ "test": "jest",
84
+ "test:watch": "jest --watch",
85
+ "test:cov": "jest --coverage"
56
86
  }
57
87
  }