datastake-daf 0.6.486 → 0.6.488

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.
@@ -202,4 +202,15 @@ export const safeJsonParse = (value, fallback = value) => {
202
202
  console.warn('Failed to parse JSON:', value);
203
203
  return fallback;
204
204
  }
205
- };
205
+ };
206
+
207
+ export const cleanJSON = (json) => {
208
+ for (let key in json) {
209
+ if (json[key] === undefined) {
210
+ json[key] = null;
211
+ } /*else if (typeof json[key] === 'object')
212
+ json[key] = cleanJSON(json[key]);
213
+ */
214
+ }
215
+ return json;
216
+ }
@@ -0,0 +1,97 @@
1
+ import { message } from 'antd';
2
+
3
+ /**
4
+ * Generic error handler factory for axios requests
5
+ * Highly configurable to adapt to different project needs
6
+ */
7
+ export const createErrorHandler = (config = {}) => {
8
+ const {
9
+ onUnauthorized,
10
+ handleError,
11
+ getStorageManager,
12
+ checkTokenValidity = true,
13
+ unauthorizedRedirect = '/',
14
+ tokenStorageKey = 'token',
15
+ handleNetworkError = true,
16
+ } = config;
17
+
18
+ return (error, customOnUnauthorized) => {
19
+ // Handle cases where error.response doesn't exist (network errors)
20
+ if (!error.response) {
21
+ if (handleNetworkError && handleError) {
22
+ handleError({
23
+ status: 0,
24
+ statusText: "Network Error",
25
+ data: ["Please check your internet connection."],
26
+ });
27
+ }
28
+ return Promise.reject(error);
29
+ }
30
+
31
+ const { status, statusText, data: { errors, message } = {} } = error.response || { data: {} };
32
+
33
+ // Handle 401 Unauthorized
34
+ if (status === 401) {
35
+ if (checkTokenValidity && getStorageManager) {
36
+ const token = getStorageManager().get(tokenStorageKey);
37
+ if (token) {
38
+ if (typeof customOnUnauthorized === 'function') {
39
+ customOnUnauthorized();
40
+ } else if (typeof onUnauthorized === 'function') {
41
+ onUnauthorized();
42
+ } else {
43
+ getStorageManager().clearOne(tokenStorageKey);
44
+ if (typeof window !== 'undefined') {
45
+ window.location.href = unauthorizedRedirect;
46
+ }
47
+ }
48
+ }
49
+ }
50
+ }
51
+
52
+ // Handle 4xx and 5xx errors
53
+ if (status >= 400 && status <= 500) {
54
+ if (handleError) {
55
+ handleError({
56
+ status,
57
+ statusText: message || statusText,
58
+ data: errors || []
59
+ });
60
+ }
61
+ }
62
+
63
+ return Promise.reject(error);
64
+ };
65
+ };
66
+
67
+ /**
68
+ * Simple error handler using antd message component
69
+ * Useful for quick error notifications
70
+ */
71
+ export const handleError = (err) => {
72
+ const errorMessage = err?.response?.data?.message ||
73
+ err?.message ||
74
+ 'An error occurred';
75
+ message.error(errorMessage);
76
+ };
77
+
78
+ /**
79
+ * Success message handler
80
+ */
81
+ export const handleSuccess = (msg) => {
82
+ message.success(msg || 'Operation successful');
83
+ };
84
+
85
+ /**
86
+ * Warning message handler
87
+ */
88
+ export const handleWarning = (msg) => {
89
+ message.warning(msg || 'Warning');
90
+ };
91
+
92
+ /**
93
+ * Info message handler
94
+ */
95
+ export const handleInfo = (msg) => {
96
+ message.info(msg);
97
+ };
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Utility functions for URL manipulation
3
+ */
4
+
5
+ /**
6
+ * Assigns parameters to URL path
7
+ * Example: assignParamsToUrl('/users/{id}/posts/{postId}', { id: 1, postId: 2 })
8
+ * Returns: '/users/1/posts/2'
9
+ */
10
+ export const assignParamsToUrl = (path, params) => {
11
+ let paramsPath = path;
12
+ const matches = path.match(/\{\w+\}/gm);
13
+
14
+ if (matches) {
15
+ for (let param of matches) {
16
+ const key = param.match(/\w+/)[0];
17
+ paramsPath = paramsPath.replace(/\{\w+\}/, params[key]);
18
+ }
19
+ }
20
+
21
+ return paramsPath;
22
+ };
23
+
24
+ /**
25
+ * Build query string from object
26
+ * Example: buildQueryString({ page: 1, limit: 10 })
27
+ * Returns: '?page=1&limit=10'
28
+ */
29
+ export const buildQueryString = (params) => {
30
+ const query = Object.keys(params)
31
+ .filter(key => params[key] !== null && params[key] !== undefined)
32
+ .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
33
+ .join('&');
34
+
35
+ return query ? `?${query}` : '';
36
+ };
@@ -0,0 +1,2 @@
1
+ export { BaseHTTPService } from './@daf/services/BaseHTTPService.js';
2
+ export { ErrorHandler } from './@daf/services/ErrorHandler.js';
package/src/utils.js CHANGED
@@ -9,7 +9,7 @@ export { renderTooltip, renderTooltipJsx } from './@daf/utils/tooltip'
9
9
  export { getRangeOfTicks } from './helpers/chart'
10
10
 
11
11
  export { propHasValue } from './helpers/deepFind'
12
- export { isEmptyOrSpaces, capitalizeAll, capitalize, camelCaseToTitle, snakeCaseToTitleCase, titleToCamelCase, findOptions, getOptionAsObject, nowToIso, renderTemplateString, renderTemplateStringInObject, truncateString, splitStringInMultipleLines, safeJsonParse } from './helpers/StringHelper'
12
+ export { isEmptyOrSpaces, capitalizeAll, capitalize, camelCaseToTitle, snakeCaseToTitleCase, titleToCamelCase, findOptions, getOptionAsObject, nowToIso, renderTemplateString, renderTemplateStringInObject, truncateString, splitStringInMultipleLines, safeJsonParse, cleanJSON } from './helpers/StringHelper'
13
13
  export { getNkey, groupSubsections, getImageUploadViewValue } from './@daf/core/components/ViewForm/helper'
14
14
  export { renderRows } from './@daf/core/components/Table/helper'
15
15
  export { filterOptions, filterString, hasNotChanged, filterSelectOptions, mapSubGroupsInSubmit, mapSubGroupsInGet, filterCreateData, changeInputMeta, renderDateFormatted } from './helpers/Forms'
@@ -39,4 +39,11 @@ export { captureElementsAsImages, captureComponentsAsImages } from './helpers/co
39
39
 
40
40
  export { createDocument } from './@daf/core/components/Document/WordDocument/createDocument.js'
41
41
 
42
- export { processConfig, fetchImageAsBuffer } from './helpers/docHelper.js'
42
+ export { processConfig, fetchImageAsBuffer } from './helpers/docHelper.js'
43
+
44
+ export { getToken } from './helpers/Token.js';
45
+ export { StorageManager } from './helpers/StorageManager.js';
46
+
47
+ export { assignParamsToUrl, buildQueryString } from './helpers/urlHelpers.js';
48
+
49
+ export { createErrorHandler, handleError, handleSuccess, handleWarning, handleInfo } from './helpers/errorHandling.js';