react-js-plugins 3.3.0 → 3.4.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.
|
@@ -11,12 +11,13 @@ interface AxiosConfigProps {
|
|
|
11
11
|
timeout?: number;
|
|
12
12
|
withCredentials?: boolean;
|
|
13
13
|
enableDateTransform?: boolean;
|
|
14
|
-
enableEncryptDecrypt?: boolean;
|
|
15
14
|
enablePrivateMode?: boolean;
|
|
16
15
|
getToken?: () => string | null;
|
|
17
16
|
handleAuthError?: (axiosInstance: AxiosInstance, originalRequest: AxiosRequestConfig, err: AxiosError) => Promise<AxiosResponse>;
|
|
18
17
|
transformRequest?: (data: any) => any;
|
|
19
18
|
transformResponse?: (data: any) => any;
|
|
19
|
+
defaultHeaders?: Record<string, string>;
|
|
20
|
+
onError?: (error: AxiosError) => void;
|
|
20
21
|
}
|
|
21
|
-
export declare const createAxiosInstance: ({ baseURL, timeout, withCredentials, enableDateTransform,
|
|
22
|
+
export declare const createAxiosInstance: ({ baseURL, timeout, withCredentials, enableDateTransform, enablePrivateMode, getToken, handleAuthError, transformRequest, transformResponse, defaultHeaders, onError, }: AxiosConfigProps) => AxiosInstance;
|
|
22
23
|
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.
|
|
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;
|
|
82
82
|
var axiosInstance = axios.create({
|
|
83
83
|
baseURL: baseURL,
|
|
84
84
|
timeout: timeout,
|
|
@@ -88,70 +88,103 @@ export var createAxiosInstance = function (_a) {
|
|
|
88
88
|
function (data) { return dateTransformer(data); }
|
|
89
89
|
], axios.defaults.transformRequest, true) : axios.defaults.transformRequest,
|
|
90
90
|
});
|
|
91
|
+
axiosInstance.defaults.headers.common = __assign(__assign({}, axiosInstance.defaults.headers.common), defaultHeaders);
|
|
91
92
|
axiosInstance.interceptors.request.use(function (config) {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
93
|
+
try {
|
|
94
|
+
if (enablePrivateMode || config.enablePrivateMode) {
|
|
95
|
+
var token = getToken === null || getToken === void 0 ? void 0 : getToken();
|
|
96
|
+
if (token) {
|
|
97
|
+
config.headers = __assign(__assign({}, config.headers), { Authorization: "Bearer ".concat(token) });
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
console.warn('⚠️ No access token found in localStorage.');
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (config === null || config === void 0 ? void 0 : config.data) {
|
|
104
|
+
var transformed = typeof transformRequest === 'function'
|
|
105
|
+
? transformRequest(config.data)
|
|
106
|
+
: config.data;
|
|
107
|
+
config.data = transformed;
|
|
97
108
|
}
|
|
109
|
+
return config;
|
|
98
110
|
}
|
|
99
|
-
|
|
100
|
-
(
|
|
101
|
-
|
|
102
|
-
!(config.data instanceof FormData)) {
|
|
103
|
-
config.data = {
|
|
104
|
-
data: (_a = transformRequest === null || transformRequest === void 0 ? void 0 : transformRequest(config.data)) !== null && _a !== void 0 ? _a : config.data,
|
|
105
|
-
};
|
|
111
|
+
catch (error) {
|
|
112
|
+
console.error('❌ Request Interceptor Error:', error);
|
|
113
|
+
return Promise.reject(error);
|
|
106
114
|
}
|
|
107
|
-
|
|
108
|
-
|
|
115
|
+
}, function (error) {
|
|
116
|
+
console.error('❌ Axios Request Error:', error);
|
|
117
|
+
return Promise.reject(error);
|
|
118
|
+
});
|
|
109
119
|
axiosInstance.interceptors.response.use(function (response) {
|
|
110
|
-
var _a
|
|
111
|
-
|
|
112
|
-
|
|
120
|
+
var _a;
|
|
121
|
+
try {
|
|
122
|
+
if (((_a = response.config) === null || _a === void 0 ? void 0 : _a.responseType) === 'blob') {
|
|
123
|
+
return response;
|
|
124
|
+
}
|
|
125
|
+
if (response === null || response === void 0 ? void 0 : response.data) {
|
|
126
|
+
var transformed = typeof transformResponse === 'function'
|
|
127
|
+
? transformResponse(response.data)
|
|
128
|
+
: response.data;
|
|
129
|
+
response.data = transformed;
|
|
130
|
+
}
|
|
113
131
|
return response;
|
|
114
132
|
}
|
|
115
|
-
|
|
116
|
-
|
|
133
|
+
catch (error) {
|
|
134
|
+
console.error('❌ Response Transform Error:', error);
|
|
135
|
+
return Promise.reject(error);
|
|
117
136
|
}
|
|
118
|
-
return response;
|
|
119
137
|
}, function (err) { return __awaiter(void 0, void 0, void 0, function () {
|
|
120
|
-
var originalRequest, status;
|
|
121
|
-
var _a, _b, _c, _d, _e
|
|
122
|
-
return __generator(this, function (
|
|
123
|
-
switch (
|
|
138
|
+
var originalRequest, status, authError_1;
|
|
139
|
+
var _a, _b, _c, _d, _e;
|
|
140
|
+
return __generator(this, function (_f) {
|
|
141
|
+
switch (_f.label) {
|
|
124
142
|
case 0:
|
|
143
|
+
if (typeof onError === 'function') {
|
|
144
|
+
try {
|
|
145
|
+
onError(err);
|
|
146
|
+
}
|
|
147
|
+
catch (hookError) {
|
|
148
|
+
console.error('⚠️ onError hook threw an error:', hookError);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
125
151
|
originalRequest = err.config;
|
|
152
|
+
status = (_a = err.response) === null || _a === void 0 ? void 0 : _a.status;
|
|
126
153
|
if (err.message === 'Network Error') {
|
|
127
154
|
return [2 /*return*/, Promise.reject({
|
|
128
155
|
title: 'Network Error: Please check your connection.',
|
|
129
|
-
status:
|
|
156
|
+
status: status,
|
|
130
157
|
})];
|
|
131
158
|
}
|
|
132
|
-
if (err.message.includes('Cross-Origin') ||
|
|
133
|
-
err.message.includes('CORS')) {
|
|
159
|
+
if (err.message.includes('Cross-Origin') || err.message.includes('CORS')) {
|
|
134
160
|
return [2 /*return*/, Promise.reject({
|
|
135
161
|
title: 'CORS Error: Please check your server configuration.',
|
|
136
|
-
status:
|
|
162
|
+
status: status,
|
|
137
163
|
})];
|
|
138
164
|
}
|
|
139
|
-
if (!(
|
|
165
|
+
if (!(status === 401 && !originalRequest._retry)) return [3 /*break*/, 4];
|
|
140
166
|
originalRequest._retry = true;
|
|
141
|
-
if (!(typeof handleAuthError === 'function')) return [3 /*break*/,
|
|
167
|
+
if (!(typeof handleAuthError === 'function')) return [3 /*break*/, 4];
|
|
168
|
+
_f.label = 1;
|
|
169
|
+
case 1:
|
|
170
|
+
_f.trys.push([1, 3, , 4]);
|
|
142
171
|
return [4 /*yield*/, handleAuthError(axiosInstance, originalRequest, err)];
|
|
143
|
-
case
|
|
144
|
-
case
|
|
145
|
-
|
|
172
|
+
case 2: return [2 /*return*/, _f.sent()];
|
|
173
|
+
case 3:
|
|
174
|
+
authError_1 = _f.sent();
|
|
175
|
+
console.error('❌ Auth Retry Failed:', authError_1);
|
|
176
|
+
return [2 /*return*/, Promise.reject(authError_1)];
|
|
177
|
+
case 4:
|
|
146
178
|
if (status === 500) {
|
|
147
|
-
return [2 /*return*/, Promise.reject((
|
|
179
|
+
return [2 /*return*/, Promise.reject((_c = (_b = err.response) === null || _b === void 0 ? void 0 : _b.data) !== null && _c !== void 0 ? _c : {
|
|
148
180
|
title: 'Internal Server Error',
|
|
149
181
|
status: status,
|
|
150
182
|
})];
|
|
151
183
|
}
|
|
152
|
-
return [2 /*return*/, Promise.reject((
|
|
184
|
+
return [2 /*return*/, Promise.reject((_e = (_d = err.response) === null || _d === void 0 ? void 0 : _d.data) !== null && _e !== void 0 ? _e : {
|
|
153
185
|
title: 'Something went wrong',
|
|
154
186
|
status: status,
|
|
187
|
+
message: err.message,
|
|
155
188
|
})];
|
|
156
189
|
}
|
|
157
190
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-js-plugins",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.4.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",
|