react-js-plugins 3.6.0 → 3.8.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
|
-
|
|
5
|
-
|
|
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,6 +17,7 @@ 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
23
|
transformRequest?: (params: {
|
|
@@ -26,6 +32,7 @@ interface AxiosConfigProps {
|
|
|
26
32
|
}) => any;
|
|
27
33
|
defaultHeaders?: Record<string, string>;
|
|
28
34
|
onError?: (error: AxiosError) => void;
|
|
35
|
+
onLoaderChange?: (isLoading: boolean, config?: AxiosRequestConfig) => void;
|
|
29
36
|
}
|
|
30
|
-
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;
|
|
31
38
|
export {};
|
|
@@ -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 =
|
|
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,
|
|
@@ -90,12 +90,16 @@ export var createAxiosInstance = function (_a) {
|
|
|
90
90
|
});
|
|
91
91
|
axiosInstance.defaults.headers.common = __assign(__assign({}, axiosInstance.defaults.headers.common), defaultHeaders);
|
|
92
92
|
axiosInstance.interceptors.request.use(function (config) { return __awaiter(void 0, void 0, void 0, function () {
|
|
93
|
-
var token, transformed, _a, error_1;
|
|
93
|
+
var isLoaderEnabled, token, transformed, _a, error_1;
|
|
94
94
|
var _b, _c;
|
|
95
95
|
return __generator(this, function (_d) {
|
|
96
96
|
switch (_d.label) {
|
|
97
97
|
case 0:
|
|
98
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
|
+
}
|
|
99
103
|
if ((_b = config.enablePrivateMode) !== null && _b !== void 0 ? _b : enablePrivateMode) {
|
|
100
104
|
token = getToken === null || getToken === void 0 ? void 0 : getToken();
|
|
101
105
|
if (token) {
|
|
@@ -123,6 +127,9 @@ export var createAxiosInstance = function (_a) {
|
|
|
123
127
|
case 5:
|
|
124
128
|
error_1 = _d.sent();
|
|
125
129
|
console.error('❌ Request Interceptor Error:', error_1);
|
|
130
|
+
if (typeof onLoaderChange === 'function') {
|
|
131
|
+
onLoaderChange(false, config);
|
|
132
|
+
}
|
|
126
133
|
return [2 /*return*/, Promise.reject(error_1)];
|
|
127
134
|
case 6: return [2 /*return*/];
|
|
128
135
|
}
|
|
@@ -132,11 +139,18 @@ export var createAxiosInstance = function (_a) {
|
|
|
132
139
|
return Promise.reject(error);
|
|
133
140
|
});
|
|
134
141
|
axiosInstance.interceptors.response.use(function (response) { return __awaiter(void 0, void 0, void 0, function () {
|
|
135
|
-
var result, _a, _b, error_2;
|
|
136
|
-
|
|
137
|
-
|
|
142
|
+
var isLoaderEnabled, result, _a, _b, error_2, isLoaderEnabled;
|
|
143
|
+
var _c;
|
|
144
|
+
return __generator(this, function (_d) {
|
|
145
|
+
switch (_d.label) {
|
|
138
146
|
case 0:
|
|
139
|
-
|
|
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
|
+
}
|
|
140
154
|
if (!(typeof transformResponse === 'function')) return [3 /*break*/, 4];
|
|
141
155
|
result = transformResponse({
|
|
142
156
|
data: response.data,
|
|
@@ -148,28 +162,37 @@ export var createAxiosInstance = function (_a) {
|
|
|
148
162
|
if (!(result instanceof Promise)) return [3 /*break*/, 2];
|
|
149
163
|
return [4 /*yield*/, result];
|
|
150
164
|
case 1:
|
|
151
|
-
_b =
|
|
165
|
+
_b = _d.sent();
|
|
152
166
|
return [3 /*break*/, 3];
|
|
153
167
|
case 2:
|
|
154
168
|
_b = result !== null && result !== void 0 ? result : response.data;
|
|
155
|
-
|
|
169
|
+
_d.label = 3;
|
|
156
170
|
case 3:
|
|
157
171
|
_a.data = _b;
|
|
158
|
-
|
|
172
|
+
_d.label = 4;
|
|
159
173
|
case 4: return [2 /*return*/, response];
|
|
160
174
|
case 5:
|
|
161
|
-
error_2 =
|
|
175
|
+
error_2 = _d.sent();
|
|
162
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
|
+
}
|
|
163
181
|
return [2 /*return*/, Promise.reject(error_2)];
|
|
164
182
|
case 6: return [2 /*return*/];
|
|
165
183
|
}
|
|
166
184
|
});
|
|
167
185
|
}); }, function (err) { return __awaiter(void 0, void 0, void 0, function () {
|
|
168
|
-
var originalRequest, status, authError_1;
|
|
186
|
+
var config, isLoaderEnabled, originalRequest, status, authError_1;
|
|
169
187
|
var _a, _b, _c, _d, _e;
|
|
170
188
|
return __generator(this, function (_f) {
|
|
171
189
|
switch (_f.label) {
|
|
172
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
|
+
}
|
|
173
196
|
if (typeof onError === 'function') {
|
|
174
197
|
try {
|
|
175
198
|
onError(err);
|
|
@@ -178,7 +201,7 @@ export var createAxiosInstance = function (_a) {
|
|
|
178
201
|
console.error('⚠️ onError hook threw an error:', hookError);
|
|
179
202
|
}
|
|
180
203
|
}
|
|
181
|
-
originalRequest =
|
|
204
|
+
originalRequest = config;
|
|
182
205
|
status = (_a = err.response) === null || _a === void 0 ? void 0 : _a.status;
|
|
183
206
|
if (err.message === 'Network Error') {
|
|
184
207
|
return [2 /*return*/, Promise.reject({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-js-plugins",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.8.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",
|