react-js-plugins 3.3.0 → 3.5.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.
- package/README.md +1 -2
- package/dist/chunkautils/chunk83454.d.ts +3 -2
- package/dist/chunkautils/chunk83454.js +69 -35
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -34,11 +34,10 @@ It supports automatic date formatting, optional data encryption/decryption, seam
|
|
|
34
34
|
|
|
35
35
|
```ts
|
|
36
36
|
const apiPrivate = createAxiosInstance({
|
|
37
|
-
baseURL:
|
|
37
|
+
baseURL: 'https://jsonplaceholder.typicode.com',
|
|
38
38
|
timeout: 30000,
|
|
39
39
|
withCredentials: true,
|
|
40
40
|
enableDateTransform: true,
|
|
41
|
-
enableEncryptDecrypt: true,
|
|
42
41
|
transformRequest: (data) => transform(data),
|
|
43
42
|
transformResponse: (data) => transform(data),
|
|
44
43
|
handleAuthError: async (instance, originalRequest, error) => {
|
|
@@ -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,104 @@ 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
|
var _a;
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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
|
+
}
|
|
97
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;
|
|
109
|
+
}
|
|
110
|
+
return config;
|
|
98
111
|
}
|
|
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
|
-
};
|
|
112
|
+
catch (error) {
|
|
113
|
+
console.error('❌ Request Interceptor Error:', error);
|
|
114
|
+
return Promise.reject(error);
|
|
106
115
|
}
|
|
107
|
-
|
|
108
|
-
|
|
116
|
+
}, function (error) {
|
|
117
|
+
console.error('❌ Axios Request Error:', error);
|
|
118
|
+
return Promise.reject(error);
|
|
119
|
+
});
|
|
109
120
|
axiosInstance.interceptors.response.use(function (response) {
|
|
110
|
-
var _a, _b
|
|
111
|
-
|
|
112
|
-
|
|
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;
|
|
131
|
+
}
|
|
113
132
|
return response;
|
|
114
133
|
}
|
|
115
|
-
|
|
116
|
-
|
|
134
|
+
catch (error) {
|
|
135
|
+
console.error('❌ Response Transform Error:', error);
|
|
136
|
+
return Promise.reject(error);
|
|
117
137
|
}
|
|
118
|
-
return response;
|
|
119
138
|
}, 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 (
|
|
139
|
+
var originalRequest, status, authError_1;
|
|
140
|
+
var _a, _b, _c, _d, _e;
|
|
141
|
+
return __generator(this, function (_f) {
|
|
142
|
+
switch (_f.label) {
|
|
124
143
|
case 0:
|
|
144
|
+
if (typeof onError === 'function') {
|
|
145
|
+
try {
|
|
146
|
+
onError(err);
|
|
147
|
+
}
|
|
148
|
+
catch (hookError) {
|
|
149
|
+
console.error('⚠️ onError hook threw an error:', hookError);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
125
152
|
originalRequest = err.config;
|
|
153
|
+
status = (_a = err.response) === null || _a === void 0 ? void 0 : _a.status;
|
|
126
154
|
if (err.message === 'Network Error') {
|
|
127
155
|
return [2 /*return*/, Promise.reject({
|
|
128
156
|
title: 'Network Error: Please check your connection.',
|
|
129
|
-
status:
|
|
157
|
+
status: status,
|
|
130
158
|
})];
|
|
131
159
|
}
|
|
132
|
-
if (err.message.includes('Cross-Origin') ||
|
|
133
|
-
err.message.includes('CORS')) {
|
|
160
|
+
if (err.message.includes('Cross-Origin') || err.message.includes('CORS')) {
|
|
134
161
|
return [2 /*return*/, Promise.reject({
|
|
135
162
|
title: 'CORS Error: Please check your server configuration.',
|
|
136
|
-
status:
|
|
163
|
+
status: status,
|
|
137
164
|
})];
|
|
138
165
|
}
|
|
139
|
-
if (!(
|
|
166
|
+
if (!(status === 401 && !originalRequest._retry)) return [3 /*break*/, 4];
|
|
140
167
|
originalRequest._retry = true;
|
|
141
|
-
if (!(typeof handleAuthError === 'function')) return [3 /*break*/,
|
|
168
|
+
if (!(typeof handleAuthError === 'function')) return [3 /*break*/, 4];
|
|
169
|
+
_f.label = 1;
|
|
170
|
+
case 1:
|
|
171
|
+
_f.trys.push([1, 3, , 4]);
|
|
142
172
|
return [4 /*yield*/, handleAuthError(axiosInstance, originalRequest, err)];
|
|
143
|
-
case
|
|
144
|
-
case
|
|
145
|
-
|
|
173
|
+
case 2: return [2 /*return*/, _f.sent()];
|
|
174
|
+
case 3:
|
|
175
|
+
authError_1 = _f.sent();
|
|
176
|
+
console.error('❌ Auth Retry Failed:', authError_1);
|
|
177
|
+
return [2 /*return*/, Promise.reject(authError_1)];
|
|
178
|
+
case 4:
|
|
146
179
|
if (status === 500) {
|
|
147
|
-
return [2 /*return*/, Promise.reject((
|
|
180
|
+
return [2 /*return*/, Promise.reject((_c = (_b = err.response) === null || _b === void 0 ? void 0 : _b.data) !== null && _c !== void 0 ? _c : {
|
|
148
181
|
title: 'Internal Server Error',
|
|
149
182
|
status: status,
|
|
150
183
|
})];
|
|
151
184
|
}
|
|
152
|
-
return [2 /*return*/, Promise.reject((
|
|
185
|
+
return [2 /*return*/, Promise.reject((_e = (_d = err.response) === null || _d === void 0 ? void 0 : _d.data) !== null && _e !== void 0 ? _e : {
|
|
153
186
|
title: 'Something went wrong',
|
|
154
187
|
status: status,
|
|
188
|
+
message: err.message,
|
|
155
189
|
})];
|
|
156
190
|
}
|
|
157
191
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-js-plugins",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.5.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",
|