@codebit-programando-solucoes/codebit-web 1.1.21 → 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.
Files changed (2) hide show
  1. package/dist/index.cjs +31 -13
  2. package/package.json +32 -2
package/dist/index.cjs CHANGED
@@ -156,14 +156,19 @@ function setupRequestControl(url, externalSignal, timeout = API_CONFIG.timeout)
156
156
  fullUrl
157
157
  };
158
158
  }
159
+ function isNativeBodyType(body) {
160
+ return 'string' == typeof body || body instanceof URLSearchParams || body instanceof Blob || body instanceof ArrayBuffer || ArrayBuffer.isView(body);
161
+ }
159
162
  function prepareBodyAndContentType(config) {
160
- if (config.body) {
161
- if (config.body instanceof FormData) {
162
- if (config.headers['Content-Type']) delete config.headers['Content-Type'];
163
- } else if ('object' == typeof config.body) {
164
- config.body = JSON.stringify(config.body);
165
- config.headers['Content-Type'] = 'application/json';
166
- }
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';
167
172
  }
168
173
  }
169
174
  function isBinaryContent(contentType) {
@@ -249,24 +254,24 @@ function makeRequestWithProgress(url, options) {
249
254
  if ('blob' === responseType || 'arraybuffer' === responseType) xhr.responseType = responseType;
250
255
  else if ('stream' === responseType) console.warn('Stream responseType is not supported with XMLHttpRequest');
251
256
  const { timeoutController, timer } = setupRequestControl(fullUrl, externalSignal, timeout);
257
+ let abortReason = null;
252
258
  const originalAbort = timeoutController.abort;
253
259
  timeoutController.abort = function(reason) {
260
+ abortReason = reason;
254
261
  originalAbort.call(this, reason);
255
262
  xhr.abort();
256
263
  };
257
264
  xhr.open(method, fullUrl, true);
258
265
  xhr.withCredentials = true;
259
- const finalHeaders = buildHeaders(headers);
260
- Object.keys(finalHeaders).forEach((key)=>{
261
- if ('content-type' === key.toLowerCase() && body instanceof FormData) return;
262
- xhr.setRequestHeader(key, finalHeaders[key]);
263
- });
264
266
  const config = {
265
- headers: finalHeaders,
267
+ headers: buildHeaders(headers),
266
268
  body
267
269
  };
268
270
  prepareBodyAndContentType(config);
269
271
  const requestBody = config.body;
272
+ Object.keys(config.headers).forEach((key)=>{
273
+ xhr.setRequestHeader(key, config.headers[key]);
274
+ });
270
275
  if (onUploadProgress) xhr.upload.addEventListener('progress', onUploadProgress);
271
276
  xhr.onload = function() {
272
277
  if (timer) clearTimeout(timer);
@@ -298,6 +303,12 @@ function makeRequestWithProgress(url, options) {
298
303
  apiNetworkError.errorCode = errorCode;
299
304
  reject(apiNetworkError);
300
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
+ };
301
312
  xhr.ontimeout = function() {
302
313
  if (timer) clearTimeout(timer);
303
314
  const timeoutError = new ApiError({
@@ -309,6 +320,13 @@ function makeRequestWithProgress(url, options) {
309
320
  timeoutError.code = ErrorCode.ERR_TIMEOUT;
310
321
  reject(timeoutError);
311
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
+ }
312
330
  xhr.send(requestBody);
313
331
  });
314
332
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codebit-programando-solucoes/codebit-web",
3
- "version": "1.1.21",
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
  }