react-js-plugins 3.0.3 → 3.0.4
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.
|
@@ -4,15 +4,21 @@ interface AxiosConfigProps {
|
|
|
4
4
|
timeout: number;
|
|
5
5
|
withCredentials?: boolean;
|
|
6
6
|
enableDateTransform?: boolean;
|
|
7
|
+
enableEncryptDecrypt?: boolean;
|
|
7
8
|
handleAuthError?: (axiosInstance: AxiosInstance, originalRequest: AxiosRequestConfig, err: AxiosError) => Promise<AxiosResponse>;
|
|
9
|
+
requestData?: (data: any) => any;
|
|
10
|
+
responseData?: (data: any) => any;
|
|
8
11
|
}
|
|
9
|
-
export declare const createPrivateInstance: ({ baseURL, handleAuthError, timeout, withCredentials, enableDateTransform, }: AxiosConfigProps) => AxiosInstance;
|
|
10
12
|
interface AxiosNoAuthConfigProps {
|
|
11
13
|
baseURL: string;
|
|
12
14
|
timeout?: number;
|
|
13
15
|
withCredentials?: boolean;
|
|
14
16
|
enableDateTransform?: boolean;
|
|
17
|
+
enableEncryptDecrypt?: boolean;
|
|
15
18
|
handleError?: (instance: AxiosInstance, originalRequest: AxiosRequestConfig, error: AxiosError) => Promise<AxiosResponse>;
|
|
19
|
+
requestData?: (data: any) => any;
|
|
20
|
+
responseData?: (data: any) => any;
|
|
16
21
|
}
|
|
17
|
-
export declare const
|
|
22
|
+
export declare const createPrivateInstance: ({ baseURL, handleAuthError, timeout, withCredentials, enableDateTransform, enableEncryptDecrypt, requestData, responseData, }: AxiosConfigProps) => AxiosInstance;
|
|
23
|
+
export declare const createPublicInstance: ({ baseURL, timeout, handleError, withCredentials, enableDateTransform, enableEncryptDecrypt, requestData, responseData, }: AxiosNoAuthConfigProps) => AxiosInstance;
|
|
18
24
|
export {};
|
|
@@ -45,93 +45,159 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
|
45
45
|
};
|
|
46
46
|
import axios from 'axios';
|
|
47
47
|
import moment from 'moment';
|
|
48
|
-
var dateTransformer = function (data) {
|
|
48
|
+
var dateTransformer = function (data, depth) {
|
|
49
|
+
if (depth === void 0) { depth = 0; }
|
|
50
|
+
if (depth > 50)
|
|
51
|
+
return data;
|
|
49
52
|
if (data instanceof Date) {
|
|
50
53
|
return moment(data).format('YYYY-MM-DDTHH:mm:ss');
|
|
51
54
|
}
|
|
52
55
|
if (Array.isArray(data)) {
|
|
53
|
-
return data.map(dateTransformer);
|
|
56
|
+
return data.map(function (item) { return dateTransformer(item, depth + 1); });
|
|
54
57
|
}
|
|
55
58
|
if (typeof data === 'object' && data !== null) {
|
|
56
59
|
return Object.fromEntries(Object.entries(data).map(function (_a) {
|
|
57
60
|
var key = _a[0], value = _a[1];
|
|
58
|
-
return [
|
|
61
|
+
return [
|
|
62
|
+
key,
|
|
63
|
+
dateTransformer(value, depth + 1),
|
|
64
|
+
];
|
|
59
65
|
}));
|
|
60
66
|
}
|
|
61
67
|
return data;
|
|
62
68
|
};
|
|
63
69
|
export var createPrivateInstance = function (_a) {
|
|
64
|
-
var baseURL = _a.baseURL, handleAuthError = _a.handleAuthError, timeout = _a.timeout, _b = _a.withCredentials, withCredentials = _b === void 0 ? false : _b, _c = _a.enableDateTransform, enableDateTransform = _c === void 0 ? true : _c;
|
|
70
|
+
var baseURL = _a.baseURL, handleAuthError = _a.handleAuthError, timeout = _a.timeout, _b = _a.withCredentials, withCredentials = _b === void 0 ? false : _b, _c = _a.enableDateTransform, enableDateTransform = _c === void 0 ? true : _c, _d = _a.enableEncryptDecrypt, enableEncryptDecrypt = _d === void 0 ? false : _d, requestData = _a.requestData, responseData = _a.responseData;
|
|
65
71
|
var axiosInstance = axios.create({
|
|
66
72
|
baseURL: baseURL,
|
|
67
73
|
timeout: timeout,
|
|
68
74
|
withCredentials: withCredentials,
|
|
69
75
|
transformRequest: enableDateTransform
|
|
70
|
-
? __spreadArray([
|
|
76
|
+
? __spreadArray([
|
|
77
|
+
function (data) { return dateTransformer(data); }
|
|
78
|
+
], axios.defaults.transformRequest, true) : axios.defaults.transformRequest,
|
|
71
79
|
});
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
80
|
+
if ((requestData || responseData) && !enableEncryptDecrypt) {
|
|
81
|
+
console.error("⚠️ Please enable 'enableEncryptDecrypt' flag when using requestData or responseData.");
|
|
82
|
+
}
|
|
83
|
+
if (enableEncryptDecrypt) {
|
|
84
|
+
axiosInstance.interceptors.request.use(function (config) {
|
|
85
|
+
var shouldEncrypt = (config === null || config === void 0 ? void 0 : config.isEncrypt) !== false;
|
|
86
|
+
if (shouldEncrypt &&
|
|
87
|
+
(config === null || config === void 0 ? void 0 : config.data) &&
|
|
88
|
+
!(config.data instanceof FormData)) {
|
|
89
|
+
config.data = { data: requestData === null || requestData === void 0 ? void 0 : requestData(config.data) };
|
|
90
|
+
}
|
|
91
|
+
return config;
|
|
92
|
+
}, function (error) { return Promise.reject(error); });
|
|
93
|
+
}
|
|
94
|
+
axiosInstance.interceptors.response.use(function (response) {
|
|
95
|
+
var _a, _b, _c;
|
|
96
|
+
var shouldDecrypt = enableEncryptDecrypt && ((_a = response.config) === null || _a === void 0 ? void 0 : _a.isDecrypt) !== false;
|
|
97
|
+
if (((_b = response.config) === null || _b === void 0 ? void 0 : _b.responseType) === 'blob' || !shouldDecrypt)
|
|
98
|
+
return response;
|
|
99
|
+
if (shouldDecrypt && ((_c = response === null || response === void 0 ? void 0 : response.data) === null || _c === void 0 ? void 0 : _c.data)) {
|
|
100
|
+
response.data = responseData === null || responseData === void 0 ? void 0 : responseData(response.data.data);
|
|
101
|
+
}
|
|
102
|
+
return response;
|
|
103
|
+
}, function (err) { return __awaiter(void 0, void 0, void 0, function () {
|
|
104
|
+
var originalRequest, status;
|
|
105
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
106
|
+
return __generator(this, function (_h) {
|
|
107
|
+
switch (_h.label) {
|
|
77
108
|
case 0:
|
|
109
|
+
originalRequest = err.config;
|
|
78
110
|
if (err.message === 'Network Error') {
|
|
79
111
|
return [2 /*return*/, Promise.reject({
|
|
80
112
|
title: 'Network Error: Please check your connection.',
|
|
81
113
|
status: (_a = err.response) === null || _a === void 0 ? void 0 : _a.status,
|
|
82
114
|
})];
|
|
83
115
|
}
|
|
84
|
-
if (err.message.includes('Cross-Origin') ||
|
|
116
|
+
if (err.message.includes('Cross-Origin') ||
|
|
117
|
+
err.message.includes('CORS')) {
|
|
85
118
|
return [2 /*return*/, Promise.reject({
|
|
86
|
-
title: 'CORS Error: Please check your server.',
|
|
119
|
+
title: 'CORS Error: Please check your server configuration.',
|
|
87
120
|
status: (_b = err.response) === null || _b === void 0 ? void 0 : _b.status,
|
|
88
121
|
})];
|
|
89
122
|
}
|
|
90
|
-
originalRequest = err.config;
|
|
91
123
|
if (!(err.response && err.response.status === 401 && !originalRequest._retry)) return [3 /*break*/, 2];
|
|
92
124
|
originalRequest._retry = true;
|
|
93
125
|
if (!(typeof handleAuthError === 'function')) return [3 /*break*/, 2];
|
|
94
126
|
return [4 /*yield*/, handleAuthError(axiosInstance, originalRequest, err)];
|
|
95
|
-
case 1: return [2 /*return*/,
|
|
127
|
+
case 1: return [2 /*return*/, _h.sent()];
|
|
96
128
|
case 2:
|
|
97
|
-
|
|
98
|
-
|
|
129
|
+
status = (_c = err.response) === null || _c === void 0 ? void 0 : _c.status;
|
|
130
|
+
if (status === 500) {
|
|
131
|
+
return [2 /*return*/, Promise.reject((_e = (_d = err.response) === null || _d === void 0 ? void 0 : _d.data) !== null && _e !== void 0 ? _e : {
|
|
132
|
+
title: 'Internal Server Error',
|
|
133
|
+
status: status,
|
|
134
|
+
})];
|
|
99
135
|
}
|
|
100
|
-
return [2 /*return*/, Promise.reject((_g = (_f = err.response) === null || _f === void 0 ? void 0 : _f.data) !== null && _g !== void 0 ? _g : {
|
|
136
|
+
return [2 /*return*/, Promise.reject((_g = (_f = err.response) === null || _f === void 0 ? void 0 : _f.data) !== null && _g !== void 0 ? _g : {
|
|
137
|
+
title: 'Something went wrong',
|
|
138
|
+
status: status,
|
|
139
|
+
})];
|
|
101
140
|
}
|
|
102
141
|
});
|
|
103
142
|
}); });
|
|
104
143
|
return axiosInstance;
|
|
105
144
|
};
|
|
106
145
|
export var createPublicInstance = function (_a) {
|
|
107
|
-
var baseURL = _a.baseURL,
|
|
146
|
+
var baseURL = _a.baseURL, _b = _a.timeout, timeout = _b === void 0 ? 15000 : _b, handleError = _a.handleError, _c = _a.withCredentials, withCredentials = _c === void 0 ? false : _c, _d = _a.enableDateTransform, enableDateTransform = _d === void 0 ? false : _d, _e = _a.enableEncryptDecrypt, enableEncryptDecrypt = _e === void 0 ? false : _e, requestData = _a.requestData, responseData = _a.responseData;
|
|
108
147
|
var axiosNoAuthInstance = axios.create({
|
|
109
148
|
baseURL: baseURL,
|
|
110
149
|
timeout: timeout,
|
|
111
150
|
withCredentials: withCredentials,
|
|
112
151
|
transformRequest: enableDateTransform
|
|
113
|
-
? __spreadArray([
|
|
152
|
+
? __spreadArray([
|
|
153
|
+
function (data) { return dateTransformer(data); }
|
|
154
|
+
], axios.defaults.transformRequest, true) : axios.defaults.transformRequest,
|
|
114
155
|
});
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
156
|
+
if ((requestData || responseData) && !enableEncryptDecrypt) {
|
|
157
|
+
console.error("⚠️ Please enable 'enableEncryptDecrypt' flag when using requestData or responseData.");
|
|
158
|
+
}
|
|
159
|
+
if (enableEncryptDecrypt) {
|
|
160
|
+
axiosNoAuthInstance.interceptors.request.use(function (config) {
|
|
161
|
+
var shouldEncrypt = (config === null || config === void 0 ? void 0 : config.isEncrypt) !== false;
|
|
162
|
+
if (shouldEncrypt &&
|
|
163
|
+
(config === null || config === void 0 ? void 0 : config.data) &&
|
|
164
|
+
!(config.data instanceof FormData)) {
|
|
165
|
+
config.data = { data: requestData === null || requestData === void 0 ? void 0 : requestData(config.data) };
|
|
166
|
+
}
|
|
167
|
+
return config;
|
|
168
|
+
}, function (error) { return Promise.reject(error); });
|
|
169
|
+
}
|
|
170
|
+
axiosNoAuthInstance.interceptors.response.use(function (response) {
|
|
171
|
+
var _a, _b, _c;
|
|
172
|
+
var shouldDecrypt = enableEncryptDecrypt && ((_a = response.config) === null || _a === void 0 ? void 0 : _a.isDecrypt) !== false;
|
|
173
|
+
if (((_b = response.config) === null || _b === void 0 ? void 0 : _b.responseType) === 'blob' || !shouldDecrypt)
|
|
174
|
+
return response;
|
|
175
|
+
if (shouldDecrypt && ((_c = response === null || response === void 0 ? void 0 : response.data) === null || _c === void 0 ? void 0 : _c.data)) {
|
|
176
|
+
response.data = responseData === null || responseData === void 0 ? void 0 : responseData(response.data.data);
|
|
177
|
+
}
|
|
178
|
+
return response;
|
|
179
|
+
}, function (error) { return __awaiter(void 0, void 0, void 0, function () {
|
|
180
|
+
var originalRequest, e_1, status;
|
|
181
|
+
var _a, _b, _c;
|
|
182
|
+
return __generator(this, function (_d) {
|
|
183
|
+
switch (_d.label) {
|
|
120
184
|
case 0:
|
|
121
185
|
if (!handleError) return [3 /*break*/, 4];
|
|
122
|
-
|
|
186
|
+
_d.label = 1;
|
|
123
187
|
case 1:
|
|
124
|
-
|
|
188
|
+
_d.trys.push([1, 3, , 4]);
|
|
125
189
|
originalRequest = error.config;
|
|
126
190
|
return [4 /*yield*/, handleError(axiosNoAuthInstance, originalRequest, error)];
|
|
127
|
-
case 2: return [2 /*return*/,
|
|
191
|
+
case 2: return [2 /*return*/, _d.sent()];
|
|
128
192
|
case 3:
|
|
129
|
-
|
|
130
|
-
return [2 /*return*/, Promise.reject(
|
|
131
|
-
case 4:
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
193
|
+
e_1 = _d.sent();
|
|
194
|
+
return [2 /*return*/, Promise.reject(e_1)];
|
|
195
|
+
case 4:
|
|
196
|
+
status = (_a = error.response) === null || _a === void 0 ? void 0 : _a.status;
|
|
197
|
+
return [2 /*return*/, Promise.reject((_c = (_b = error.response) === null || _b === void 0 ? void 0 : _b.data) !== null && _c !== void 0 ? _c : {
|
|
198
|
+
title: 'Something went wrong',
|
|
199
|
+
status: status,
|
|
200
|
+
})];
|
|
135
201
|
}
|
|
136
202
|
});
|
|
137
203
|
}); });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-js-plugins",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.4",
|
|
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",
|