react-js-plugins 3.0.5 → 3.1.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 +38 -0
- package/dist/chunkautils/chunk83454.d.ts +12 -6
- package/dist/chunkautils/chunk83454.js +10 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -27,6 +27,44 @@ pnpm add react-js-plugins
|
|
|
27
27
|
### **Documentation Site in Progress...**
|
|
28
28
|
*We're building an enhanced documentation experience with better navigation and examples. Coming soon!*
|
|
29
29
|
|
|
30
|
+
### ✅ **createPrivateInstance**
|
|
31
|
+
|
|
32
|
+
Creates a preconfigured instance for authenticated API calls. It supports automatic date formatting, optional data encryption/decryption, seamless data transformation, enhanced security, and built-in error handling full cross-browser compatibility.
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
const apiPrivate = createPrivateInstance({
|
|
36
|
+
baseURL: getEnv('VITE_API_BASE_URL'),
|
|
37
|
+
timeout: 30000,
|
|
38
|
+
withCredentials: true,
|
|
39
|
+
enableDateTransform: true,
|
|
40
|
+
enableEncryptDecrypt: true,
|
|
41
|
+
transformRequest: (data) => encryptData(data),
|
|
42
|
+
transformResponse: (data) => decryptData(data),
|
|
43
|
+
handleAuthError: async (instance, originalRequest, error) => {
|
|
44
|
+
console.error('API Error:', error.message);
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### ✅ **createPublicInstance**
|
|
50
|
+
|
|
51
|
+
Creates a preconfigured Axios instance for **public or unauthenticated API requests.**
|
|
52
|
+
Includes optional support for date formatting, payload transformation, and error handling for general endpoints.
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
const apiPublic = createPublicInstance({
|
|
56
|
+
baseURL: getEnv('VITE_PUBLIC_API_BASE_URL'),
|
|
57
|
+
timeout: 15000,
|
|
58
|
+
enableDateTransform: true,
|
|
59
|
+
enableEncryptDecrypt: false,
|
|
60
|
+
transformRequest: (data) => transformPayload(data),
|
|
61
|
+
transformResponse: (data) => parseApiResponse(data),
|
|
62
|
+
handleError: async (instance, request, error) => {
|
|
63
|
+
console.error('API Error:', error.message);
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
```
|
|
67
|
+
|
|
30
68
|
### ✅ **_arrayDiff**
|
|
31
69
|
Returns the difference between two arrays.
|
|
32
70
|
```ts
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import { AxiosInstance, AxiosRequestConfig, AxiosResponse, AxiosError } from 'axios';
|
|
2
|
+
declare module 'axios' {
|
|
3
|
+
interface AxiosRequestConfig {
|
|
4
|
+
isEncrypt?: boolean;
|
|
5
|
+
isDecrypt?: boolean;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
2
8
|
interface AxiosConfigProps {
|
|
3
9
|
baseURL: string;
|
|
4
10
|
timeout: number;
|
|
@@ -6,8 +12,8 @@ interface AxiosConfigProps {
|
|
|
6
12
|
enableDateTransform?: boolean;
|
|
7
13
|
enableEncryptDecrypt?: boolean;
|
|
8
14
|
handleAuthError?: (axiosInstance: AxiosInstance, originalRequest: AxiosRequestConfig, err: AxiosError) => Promise<AxiosResponse>;
|
|
9
|
-
|
|
10
|
-
|
|
15
|
+
transformRequest?: (data: any) => any;
|
|
16
|
+
transformResponse?: (data: any) => any;
|
|
11
17
|
}
|
|
12
18
|
interface AxiosNoAuthConfigProps {
|
|
13
19
|
baseURL: string;
|
|
@@ -16,9 +22,9 @@ interface AxiosNoAuthConfigProps {
|
|
|
16
22
|
enableDateTransform?: boolean;
|
|
17
23
|
enableEncryptDecrypt?: boolean;
|
|
18
24
|
handleError?: (instance: AxiosInstance, originalRequest: AxiosRequestConfig, error: AxiosError) => Promise<AxiosResponse>;
|
|
19
|
-
|
|
20
|
-
|
|
25
|
+
transformRequest?: (data: any) => any;
|
|
26
|
+
transformResponse?: (data: any) => any;
|
|
21
27
|
}
|
|
22
|
-
export declare const createPrivateInstance: ({ baseURL, handleAuthError, timeout, withCredentials, enableDateTransform, enableEncryptDecrypt,
|
|
23
|
-
export declare const createPublicInstance: ({ baseURL, timeout, handleError, withCredentials, enableDateTransform, enableEncryptDecrypt,
|
|
28
|
+
export declare const createPrivateInstance: ({ baseURL, handleAuthError, timeout, withCredentials, enableDateTransform, enableEncryptDecrypt, transformRequest, transformResponse, }: AxiosConfigProps) => AxiosInstance;
|
|
29
|
+
export declare const createPublicInstance: ({ baseURL, timeout, handleError, withCredentials, enableDateTransform, enableEncryptDecrypt, transformRequest, transformResponse, }: AxiosNoAuthConfigProps) => AxiosInstance;
|
|
24
30
|
export {};
|
|
@@ -67,7 +67,7 @@ var dateTransformer = function (data, depth) {
|
|
|
67
67
|
return data;
|
|
68
68
|
};
|
|
69
69
|
export var createPrivateInstance = function (_a) {
|
|
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,
|
|
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, transformRequest = _a.transformRequest, transformResponse = _a.transformResponse;
|
|
71
71
|
var axiosInstance = axios.create({
|
|
72
72
|
baseURL: baseURL,
|
|
73
73
|
timeout: timeout,
|
|
@@ -77,8 +77,8 @@ export var createPrivateInstance = function (_a) {
|
|
|
77
77
|
function (data) { return dateTransformer(data); }
|
|
78
78
|
], axios.defaults.transformRequest, true) : axios.defaults.transformRequest,
|
|
79
79
|
});
|
|
80
|
-
if ((
|
|
81
|
-
console.error("⚠️ Please enable 'enableEncryptDecrypt' flag when using
|
|
80
|
+
if ((transformRequest || transformResponse) && !enableEncryptDecrypt) {
|
|
81
|
+
console.error("⚠️ Please enable 'enableEncryptDecrypt' flag when using transformRequest or transformResponse.");
|
|
82
82
|
}
|
|
83
83
|
if (enableEncryptDecrypt) {
|
|
84
84
|
axiosInstance.interceptors.request.use(function (config) {
|
|
@@ -86,7 +86,7 @@ export var createPrivateInstance = function (_a) {
|
|
|
86
86
|
if (shouldEncrypt &&
|
|
87
87
|
(config === null || config === void 0 ? void 0 : config.data) &&
|
|
88
88
|
!(config.data instanceof FormData)) {
|
|
89
|
-
config.data = { data:
|
|
89
|
+
config.data = { data: transformRequest === null || transformRequest === void 0 ? void 0 : transformRequest(config.data) };
|
|
90
90
|
}
|
|
91
91
|
return config;
|
|
92
92
|
}, function (error) { return Promise.reject(error); });
|
|
@@ -97,7 +97,7 @@ export var createPrivateInstance = function (_a) {
|
|
|
97
97
|
if (((_b = response.config) === null || _b === void 0 ? void 0 : _b.responseType) === 'blob' || !shouldDecrypt)
|
|
98
98
|
return response;
|
|
99
99
|
if (shouldDecrypt && ((_c = response === null || response === void 0 ? void 0 : response.data) === null || _c === void 0 ? void 0 : _c.data)) {
|
|
100
|
-
response.data =
|
|
100
|
+
response.data = transformResponse === null || transformResponse === void 0 ? void 0 : transformResponse(response.data.data);
|
|
101
101
|
}
|
|
102
102
|
return response;
|
|
103
103
|
}, function (err) { return __awaiter(void 0, void 0, void 0, function () {
|
|
@@ -143,7 +143,7 @@ export var createPrivateInstance = function (_a) {
|
|
|
143
143
|
return axiosInstance;
|
|
144
144
|
};
|
|
145
145
|
export var createPublicInstance = function (_a) {
|
|
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,
|
|
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, transformRequest = _a.transformRequest, transformResponse = _a.transformResponse;
|
|
147
147
|
var axiosNoAuthInstance = axios.create({
|
|
148
148
|
baseURL: baseURL,
|
|
149
149
|
timeout: timeout,
|
|
@@ -153,8 +153,8 @@ export var createPublicInstance = function (_a) {
|
|
|
153
153
|
function (data) { return dateTransformer(data); }
|
|
154
154
|
], axios.defaults.transformRequest, true) : axios.defaults.transformRequest,
|
|
155
155
|
});
|
|
156
|
-
if ((
|
|
157
|
-
console.error("⚠️ Please enable 'enableEncryptDecrypt' flag when using
|
|
156
|
+
if ((transformRequest || transformResponse) && !enableEncryptDecrypt) {
|
|
157
|
+
console.error("⚠️ Please enable 'enableEncryptDecrypt' flag when using transformRequest or transformResponse.");
|
|
158
158
|
}
|
|
159
159
|
if (enableEncryptDecrypt) {
|
|
160
160
|
axiosNoAuthInstance.interceptors.request.use(function (config) {
|
|
@@ -162,7 +162,7 @@ export var createPublicInstance = function (_a) {
|
|
|
162
162
|
if (shouldEncrypt &&
|
|
163
163
|
(config === null || config === void 0 ? void 0 : config.data) &&
|
|
164
164
|
!(config.data instanceof FormData)) {
|
|
165
|
-
config.data = { data:
|
|
165
|
+
config.data = { data: transformRequest === null || transformRequest === void 0 ? void 0 : transformRequest(config.data) };
|
|
166
166
|
}
|
|
167
167
|
return config;
|
|
168
168
|
}, function (error) { return Promise.reject(error); });
|
|
@@ -173,7 +173,7 @@ export var createPublicInstance = function (_a) {
|
|
|
173
173
|
if (((_b = response.config) === null || _b === void 0 ? void 0 : _b.responseType) === 'blob' || !shouldDecrypt)
|
|
174
174
|
return response;
|
|
175
175
|
if (shouldDecrypt && ((_c = response === null || response === void 0 ? void 0 : response.data) === null || _c === void 0 ? void 0 : _c.data)) {
|
|
176
|
-
response.data =
|
|
176
|
+
response.data = transformResponse === null || transformResponse === void 0 ? void 0 : transformResponse(response.data.data);
|
|
177
177
|
}
|
|
178
178
|
return response;
|
|
179
179
|
}, function (error) { return __awaiter(void 0, void 0, void 0, function () {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-js-plugins",
|
|
3
|
-
"version": "3.0
|
|
3
|
+
"version": "3.1.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",
|