react-js-plugins 3.5.0 → 3.6.0

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.
@@ -14,8 +14,16 @@ interface AxiosConfigProps {
14
14
  enablePrivateMode?: boolean;
15
15
  getToken?: () => string | null;
16
16
  handleAuthError?: (axiosInstance: AxiosInstance, originalRequest: AxiosRequestConfig, err: AxiosError) => Promise<AxiosResponse>;
17
- transformRequest?: (data: any) => any;
18
- transformResponse?: (data: any) => any;
17
+ transformRequest?: (params: {
18
+ data: any;
19
+ config: AxiosRequestConfig;
20
+ }) => any;
21
+ transformResponse?: (params: {
22
+ data: any;
23
+ config: AxiosRequestConfig;
24
+ status: number;
25
+ headers: any;
26
+ }) => any;
19
27
  defaultHeaders?: Record<string, string>;
20
28
  onError?: (error: AxiosError) => void;
21
29
  }
@@ -58,7 +58,7 @@ import axios from 'axios';
58
58
  import moment from 'moment';
59
59
  var dateTransformer = function (data, depth) {
60
60
  if (depth === void 0) { depth = 0; }
61
- if (depth > 50)
61
+ if (depth >= 20)
62
62
  return data;
63
63
  if (data instanceof Date) {
64
64
  return moment(data).format('YYYY-MM-DDTHH:mm:ss');
@@ -89,53 +89,82 @@ export var createAxiosInstance = function (_a) {
89
89
  ], axios.defaults.transformRequest, true) : axios.defaults.transformRequest,
90
90
  });
91
91
  axiosInstance.defaults.headers.common = __assign(__assign({}, axiosInstance.defaults.headers.common), defaultHeaders);
92
- axiosInstance.interceptors.request.use(function (config) {
93
- var _a;
94
- try {
95
- if (enablePrivateMode || config.enablePrivateMode) {
96
- var token = getToken === null || getToken === void 0 ? void 0 : getToken();
97
- if (token) {
98
- config.headers = __assign(__assign({}, config.headers), { Authorization: "Bearer ".concat(token) });
99
- }
100
- else {
101
- console.warn('⚠️ No access token found in localStorage.');
102
- }
103
- }
104
- if (config === null || config === void 0 ? void 0 : config.data) {
105
- var transformed = typeof transformRequest === 'function'
106
- ? (_a = transformRequest(config)) !== null && _a !== void 0 ? _a : config.data
107
- : config.data;
108
- config.data = transformed;
92
+ axiosInstance.interceptors.request.use(function (config) { return __awaiter(void 0, void 0, void 0, function () {
93
+ var token, transformed, _a, error_1;
94
+ var _b, _c;
95
+ return __generator(this, function (_d) {
96
+ switch (_d.label) {
97
+ case 0:
98
+ _d.trys.push([0, 5, , 6]);
99
+ if ((_b = config.enablePrivateMode) !== null && _b !== void 0 ? _b : enablePrivateMode) {
100
+ token = getToken === null || getToken === void 0 ? void 0 : getToken();
101
+ if (token) {
102
+ config.headers = config.headers || {};
103
+ config.headers.Authorization = "Bearer ".concat(token);
104
+ }
105
+ else {
106
+ console.warn('⚠️ No access token found in localStorage.');
107
+ }
108
+ }
109
+ if (!(config === null || config === void 0 ? void 0 : config.data)) return [3 /*break*/, 4];
110
+ if (!(typeof transformRequest === 'function')) return [3 /*break*/, 2];
111
+ return [4 /*yield*/, Promise.resolve(transformRequest({ data: config.data, config: config }))];
112
+ case 1:
113
+ _a = (_c = (_d.sent())) !== null && _c !== void 0 ? _c : config.data;
114
+ return [3 /*break*/, 3];
115
+ case 2:
116
+ _a = config.data;
117
+ _d.label = 3;
118
+ case 3:
119
+ transformed = _a;
120
+ config.data = transformed;
121
+ _d.label = 4;
122
+ case 4: return [2 /*return*/, config];
123
+ case 5:
124
+ error_1 = _d.sent();
125
+ console.error('❌ Request Interceptor Error:', error_1);
126
+ return [2 /*return*/, Promise.reject(error_1)];
127
+ case 6: return [2 /*return*/];
109
128
  }
110
- return config;
111
- }
112
- catch (error) {
113
- console.error('❌ Request Interceptor Error:', error);
114
- return Promise.reject(error);
115
- }
116
- }, function (error) {
129
+ });
130
+ }); }, function (error) {
117
131
  console.error('❌ Axios Request Error:', error);
118
132
  return Promise.reject(error);
119
133
  });
120
- axiosInstance.interceptors.response.use(function (response) {
121
- var _a, _b;
122
- try {
123
- if (((_a = response.config) === null || _a === void 0 ? void 0 : _a.responseType) === 'blob') {
124
- return response;
125
- }
126
- if (response === null || response === void 0 ? void 0 : response.data) {
127
- var transformed = typeof transformResponse === 'function'
128
- ? (_b = transformResponse(response)) !== null && _b !== void 0 ? _b : response.data
129
- : response.data;
130
- response.data = transformed;
134
+ axiosInstance.interceptors.response.use(function (response) { return __awaiter(void 0, void 0, void 0, function () {
135
+ var result, _a, _b, error_2;
136
+ return __generator(this, function (_c) {
137
+ switch (_c.label) {
138
+ case 0:
139
+ _c.trys.push([0, 5, , 6]);
140
+ if (!(typeof transformResponse === 'function')) return [3 /*break*/, 4];
141
+ result = transformResponse({
142
+ data: response.data,
143
+ config: response.config,
144
+ status: response.status,
145
+ headers: response.headers,
146
+ });
147
+ _a = response;
148
+ if (!(result instanceof Promise)) return [3 /*break*/, 2];
149
+ return [4 /*yield*/, result];
150
+ case 1:
151
+ _b = _c.sent();
152
+ return [3 /*break*/, 3];
153
+ case 2:
154
+ _b = result !== null && result !== void 0 ? result : response.data;
155
+ _c.label = 3;
156
+ case 3:
157
+ _a.data = _b;
158
+ _c.label = 4;
159
+ case 4: return [2 /*return*/, response];
160
+ case 5:
161
+ error_2 = _c.sent();
162
+ console.error('❌ Response Transform Error:', error_2);
163
+ return [2 /*return*/, Promise.reject(error_2)];
164
+ case 6: return [2 /*return*/];
131
165
  }
132
- return response;
133
- }
134
- catch (error) {
135
- console.error('❌ Response Transform Error:', error);
136
- return Promise.reject(error);
137
- }
138
- }, function (err) { return __awaiter(void 0, void 0, void 0, function () {
166
+ });
167
+ }); }, function (err) { return __awaiter(void 0, void 0, void 0, function () {
139
168
  var originalRequest, status, authError_1;
140
169
  var _a, _b, _c, _d, _e;
141
170
  return __generator(this, function (_f) {
@@ -177,10 +206,7 @@ export var createAxiosInstance = function (_a) {
177
206
  return [2 /*return*/, Promise.reject(authError_1)];
178
207
  case 4:
179
208
  if (status === 500) {
180
- return [2 /*return*/, Promise.reject((_c = (_b = err.response) === null || _b === void 0 ? void 0 : _b.data) !== null && _c !== void 0 ? _c : {
181
- title: 'Internal Server Error',
182
- status: status,
183
- })];
209
+ return [2 /*return*/, Promise.reject((_c = (_b = err.response) === null || _b === void 0 ? void 0 : _b.data) !== null && _c !== void 0 ? _c : { title: 'Internal Server Error', status: status })];
184
210
  }
185
211
  return [2 /*return*/, Promise.reject((_e = (_d = err.response) === null || _d === void 0 ? void 0 : _d.data) !== null && _e !== void 0 ? _e : {
186
212
  title: 'Something went wrong',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-js-plugins",
3
- "version": "3.5.0",
3
+ "version": "3.6.0",
4
4
  "description": "A powerful and efficient React utility library designed to enhance application performance by streamlining and simplifying the management of complex asynchronous operations.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",