react-js-plugins 3.5.0 → 3.7.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.
@@ -1,9 +1,14 @@
1
1
  import { AxiosInstance, AxiosRequestConfig, AxiosResponse, AxiosError } from 'axios';
2
2
  declare module 'axios' {
3
3
  interface AxiosRequestConfig {
4
- isEncrypt?: boolean;
5
- isDecrypt?: boolean;
4
+ enableEncrypt?: boolean;
5
+ enableDecrypt?: boolean;
6
+ enableUpload?: boolean;
7
+ enableDownload?: boolean;
8
+ requestDataType?: any;
9
+ responseDataType?: any;
6
10
  enablePrivateMode?: boolean;
11
+ enableLoader?: boolean;
7
12
  }
8
13
  }
9
14
  interface AxiosConfigProps {
@@ -12,12 +17,22 @@ interface AxiosConfigProps {
12
17
  withCredentials?: boolean;
13
18
  enableDateTransform?: boolean;
14
19
  enablePrivateMode?: boolean;
20
+ enableLoader?: boolean;
15
21
  getToken?: () => string | null;
16
22
  handleAuthError?: (axiosInstance: AxiosInstance, originalRequest: AxiosRequestConfig, err: AxiosError) => Promise<AxiosResponse>;
17
- transformRequest?: (data: any) => any;
18
- transformResponse?: (data: any) => any;
23
+ transformRequest?: (params: {
24
+ data: any;
25
+ config: AxiosRequestConfig;
26
+ }) => any;
27
+ transformResponse?: (params: {
28
+ data: any;
29
+ config: AxiosRequestConfig;
30
+ status: number;
31
+ headers: any;
32
+ }) => any;
19
33
  defaultHeaders?: Record<string, string>;
20
34
  onError?: (error: AxiosError) => void;
35
+ onLoaderChange?: (isLoading: boolean, config?: AxiosRequestConfig) => void;
21
36
  }
22
- export declare const createAxiosInstance: ({ baseURL, timeout, withCredentials, enableDateTransform, enablePrivateMode, getToken, handleAuthError, transformRequest, transformResponse, defaultHeaders, onError, }: AxiosConfigProps) => AxiosInstance;
37
+ export declare const createAxiosInstance: ({ baseURL, timeout, withCredentials, enableDateTransform, enablePrivateMode, enableLoader, getToken, handleAuthError, transformRequest, transformResponse, defaultHeaders, onError, onLoaderChange, }: AxiosConfigProps) => AxiosInstance;
23
38
  export {};
@@ -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');
@@ -78,7 +78,7 @@ var dateTransformer = function (data, depth) {
78
78
  return data;
79
79
  };
80
80
  export var createAxiosInstance = function (_a) {
81
- var baseURL = _a.baseURL, _b = _a.timeout, timeout = _b === void 0 ? 15000 : _b, _c = _a.withCredentials, withCredentials = _c === void 0 ? false : _c, _d = _a.enableDateTransform, enableDateTransform = _d === void 0 ? true : _d, _e = _a.enablePrivateMode, enablePrivateMode = _e === void 0 ? false : _e, _f = _a.getToken, getToken = _f === void 0 ? function () { return localStorage.getItem('access_token'); } : _f, handleAuthError = _a.handleAuthError, transformRequest = _a.transformRequest, transformResponse = _a.transformResponse, _g = _a.defaultHeaders, defaultHeaders = _g === void 0 ? {} : _g, onError = _a.onError;
81
+ var baseURL = _a.baseURL, _b = _a.timeout, timeout = _b === void 0 ? 15000 : _b, _c = _a.withCredentials, withCredentials = _c === void 0 ? false : _c, _d = _a.enableDateTransform, enableDateTransform = _d === void 0 ? true : _d, _e = _a.enablePrivateMode, enablePrivateMode = _e === void 0 ? false : _e, _f = _a.enableLoader, enableLoader = _f === void 0 ? true : _f, _g = _a.getToken, getToken = _g === void 0 ? function () { return localStorage.getItem('access_token'); } : _g, handleAuthError = _a.handleAuthError, transformRequest = _a.transformRequest, transformResponse = _a.transformResponse, _h = _a.defaultHeaders, defaultHeaders = _h === void 0 ? {} : _h, onError = _a.onError, onLoaderChange = _a.onLoaderChange;
82
82
  var axiosInstance = axios.create({
83
83
  baseURL: baseURL,
84
84
  timeout: timeout,
@@ -89,58 +89,110 @@ 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 isLoaderEnabled, 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
+ isLoaderEnabled = config.enableLoader !== undefined ? config.enableLoader : enableLoader;
100
+ if (isLoaderEnabled && typeof onLoaderChange === 'function') {
101
+ onLoaderChange(true, config);
102
+ }
103
+ if ((_b = config.enablePrivateMode) !== null && _b !== void 0 ? _b : enablePrivateMode) {
104
+ token = getToken === null || getToken === void 0 ? void 0 : getToken();
105
+ if (token) {
106
+ config.headers = config.headers || {};
107
+ config.headers.Authorization = "Bearer ".concat(token);
108
+ }
109
+ else {
110
+ console.warn('⚠️ No access token found in localStorage.');
111
+ }
112
+ }
113
+ if (!(config === null || config === void 0 ? void 0 : config.data)) return [3 /*break*/, 4];
114
+ if (!(typeof transformRequest === 'function')) return [3 /*break*/, 2];
115
+ return [4 /*yield*/, Promise.resolve(transformRequest({ data: config.data, config: config }))];
116
+ case 1:
117
+ _a = (_c = (_d.sent())) !== null && _c !== void 0 ? _c : config.data;
118
+ return [3 /*break*/, 3];
119
+ case 2:
120
+ _a = config.data;
121
+ _d.label = 3;
122
+ case 3:
123
+ transformed = _a;
124
+ config.data = transformed;
125
+ _d.label = 4;
126
+ case 4: return [2 /*return*/, config];
127
+ case 5:
128
+ error_1 = _d.sent();
129
+ console.error('❌ Request Interceptor Error:', error_1);
130
+ if (typeof onLoaderChange === 'function') {
131
+ onLoaderChange(false, config);
132
+ }
133
+ return [2 /*return*/, Promise.reject(error_1)];
134
+ case 6: return [2 /*return*/];
109
135
  }
110
- return config;
111
- }
112
- catch (error) {
113
- console.error('❌ Request Interceptor Error:', error);
114
- return Promise.reject(error);
115
- }
116
- }, function (error) {
136
+ });
137
+ }); }, function (error) {
117
138
  console.error('❌ Axios Request Error:', error);
118
139
  return Promise.reject(error);
119
140
  });
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;
141
+ axiosInstance.interceptors.response.use(function (response) { return __awaiter(void 0, void 0, void 0, function () {
142
+ var isLoaderEnabled, result, _a, _b, error_2, isLoaderEnabled;
143
+ var _c;
144
+ return __generator(this, function (_d) {
145
+ switch (_d.label) {
146
+ case 0:
147
+ _d.trys.push([0, 5, , 6]);
148
+ isLoaderEnabled = response.config.enableLoader !== undefined
149
+ ? response.config.enableLoader
150
+ : enableLoader;
151
+ if (isLoaderEnabled && typeof onLoaderChange === 'function') {
152
+ onLoaderChange(false, response.config);
153
+ }
154
+ if (!(typeof transformResponse === 'function')) return [3 /*break*/, 4];
155
+ result = transformResponse({
156
+ data: response.data,
157
+ config: response.config,
158
+ status: response.status,
159
+ headers: response.headers,
160
+ });
161
+ _a = response;
162
+ if (!(result instanceof Promise)) return [3 /*break*/, 2];
163
+ return [4 /*yield*/, result];
164
+ case 1:
165
+ _b = _d.sent();
166
+ return [3 /*break*/, 3];
167
+ case 2:
168
+ _b = result !== null && result !== void 0 ? result : response.data;
169
+ _d.label = 3;
170
+ case 3:
171
+ _a.data = _b;
172
+ _d.label = 4;
173
+ case 4: return [2 /*return*/, response];
174
+ case 5:
175
+ error_2 = _d.sent();
176
+ console.error('❌ Response Transform Error:', error_2);
177
+ isLoaderEnabled = ((_c = response === null || response === void 0 ? void 0 : response.config) === null || _c === void 0 ? void 0 : _c.enableLoader) !== undefined ? response.config.enableLoader : enableLoader;
178
+ if (isLoaderEnabled && typeof onLoaderChange === 'function') {
179
+ onLoaderChange(false, response.config);
180
+ }
181
+ return [2 /*return*/, Promise.reject(error_2)];
182
+ case 6: return [2 /*return*/];
131
183
  }
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 () {
139
- var originalRequest, status, authError_1;
184
+ });
185
+ }); }, function (err) { return __awaiter(void 0, void 0, void 0, function () {
186
+ var config, isLoaderEnabled, originalRequest, status, authError_1;
140
187
  var _a, _b, _c, _d, _e;
141
188
  return __generator(this, function (_f) {
142
189
  switch (_f.label) {
143
190
  case 0:
191
+ config = err.config;
192
+ isLoaderEnabled = (config === null || config === void 0 ? void 0 : config.enableLoader) !== undefined ? config.enableLoader : enableLoader;
193
+ if (isLoaderEnabled && typeof onLoaderChange === 'function') {
194
+ onLoaderChange(false, config);
195
+ }
144
196
  if (typeof onError === 'function') {
145
197
  try {
146
198
  onError(err);
@@ -149,7 +201,7 @@ export var createAxiosInstance = function (_a) {
149
201
  console.error('⚠️ onError hook threw an error:', hookError);
150
202
  }
151
203
  }
152
- originalRequest = err.config;
204
+ originalRequest = config;
153
205
  status = (_a = err.response) === null || _a === void 0 ? void 0 : _a.status;
154
206
  if (err.message === 'Network Error') {
155
207
  return [2 /*return*/, Promise.reject({
@@ -177,10 +229,7 @@ export var createAxiosInstance = function (_a) {
177
229
  return [2 /*return*/, Promise.reject(authError_1)];
178
230
  case 4:
179
231
  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
- })];
232
+ 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
233
  }
185
234
  return [2 /*return*/, Promise.reject((_e = (_d = err.response) === null || _d === void 0 ? void 0 : _d.data) !== null && _e !== void 0 ? _e : {
186
235
  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.7.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",