sysone-api-mapper 1.0.42 → 1.0.43
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/axiosInstance.js +0 -4
- package/package.json +1 -1
- package/src/mapper/Mapper.js +28 -21
package/axiosInstance.js
CHANGED
|
@@ -2,9 +2,5 @@ import axios from "axios";
|
|
|
2
2
|
|
|
3
3
|
const axiosInstance = axios.create({});
|
|
4
4
|
|
|
5
|
-
axiosInstance.interceptors.request.use(config => {
|
|
6
|
-
config.headers['X-Origin'] = config.headers['X-Origin'] || 'sysone-api-mapper-global';
|
|
7
|
-
return config;
|
|
8
|
-
});
|
|
9
5
|
|
|
10
6
|
export default axiosInstance;
|
package/package.json
CHANGED
package/src/mapper/Mapper.js
CHANGED
|
@@ -1,10 +1,31 @@
|
|
|
1
|
-
import
|
|
1
|
+
import axios from "axios";
|
|
2
2
|
import { methods, tenantsConfig } from "./endpointsConfig";
|
|
3
3
|
import dotenv from "dotenv";
|
|
4
4
|
|
|
5
5
|
// Cargar variables de entorno
|
|
6
6
|
dotenv.config();
|
|
7
7
|
|
|
8
|
+
// Configuración de axiosInstance con interceptor para CNP
|
|
9
|
+
const axiosInstance = axios.create({});
|
|
10
|
+
|
|
11
|
+
axiosInstance.interceptors.request.use(
|
|
12
|
+
(config) => {
|
|
13
|
+
const tenant = config.headers?.["X-Tenant"];
|
|
14
|
+
if (tenant === "cnp") {
|
|
15
|
+
const intermediaryCode = localStorage.getItem("intermediaryCode");
|
|
16
|
+
if (intermediaryCode) {
|
|
17
|
+
config.headers["X-Agent"] = intermediaryCode;
|
|
18
|
+
} else {
|
|
19
|
+
console.warn("intermediaryCode no encontrado en localStorage para tenant CNP");
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return config;
|
|
23
|
+
},
|
|
24
|
+
(error) => {
|
|
25
|
+
return Promise.reject(error);
|
|
26
|
+
}
|
|
27
|
+
);
|
|
28
|
+
|
|
8
29
|
const getApiMapperConfig = () => {
|
|
9
30
|
// 1. Intentar desde variables de entorno compiladas
|
|
10
31
|
if (process.env.API_MAPPER && Object.keys(process.env.API_MAPPER).length > 0) {
|
|
@@ -17,7 +38,6 @@ const getApiMapperConfig = () => {
|
|
|
17
38
|
console.log("Usando API_MAPPER de window");
|
|
18
39
|
return window.__API_MAPPER__;
|
|
19
40
|
}
|
|
20
|
-
|
|
21
41
|
};
|
|
22
42
|
|
|
23
43
|
const API_MAPPER = getApiMapperConfig();
|
|
@@ -37,7 +57,6 @@ const configs = {
|
|
|
37
57
|
baseURL: API_MAPPER.cnp.baseURL,
|
|
38
58
|
headers: {
|
|
39
59
|
["X-Channel"]: API_MAPPER.cnp.headers["X-Chanel"],
|
|
40
|
-
["X-Agent"]: API_MAPPER.cnp.headers["X-Agent"],
|
|
41
60
|
["X-Client"]: API_MAPPER.cnp.headers["X-Client"],
|
|
42
61
|
["Ocp-Apim-Subscription-Key"]: API_MAPPER.cnp.headers["Ocp-Apim-Subscription-Key"],
|
|
43
62
|
["X-Origin"]: "sysone-api-mapper"
|
|
@@ -45,8 +64,6 @@ const configs = {
|
|
|
45
64
|
},
|
|
46
65
|
};
|
|
47
66
|
|
|
48
|
-
// Momentaneo para probar CNP test
|
|
49
|
-
|
|
50
67
|
export const apiMapper = async (
|
|
51
68
|
endpointCode,
|
|
52
69
|
tenant,
|
|
@@ -56,30 +73,24 @@ export const apiMapper = async (
|
|
|
56
73
|
) => {
|
|
57
74
|
try {
|
|
58
75
|
const configData = tenantsConfig[endpointCode];
|
|
59
|
-
if (!configData)
|
|
60
|
-
throw new Error(`Endpoint no configurado: ${endpointCode}`);
|
|
76
|
+
if (!configData) throw new Error(`Endpoint no configurado: ${endpointCode}`);
|
|
61
77
|
|
|
62
78
|
const endpointData = configData[tenant] ?? configData.default;
|
|
63
79
|
let config;
|
|
64
80
|
|
|
65
|
-
// Validar que se haya cargado la configuración
|
|
66
81
|
if (!configs) {
|
|
67
82
|
throw new Error("No se pudo cargar la configuración del API Mapper");
|
|
68
83
|
}
|
|
69
84
|
|
|
70
85
|
if (tenant === "cnp") {
|
|
71
|
-
console.log("🚀 ~ apiMapper ~ additionalHeaders:", additionalHeaders)
|
|
72
|
-
|
|
73
86
|
config = {
|
|
74
87
|
...configs.cnp,
|
|
75
88
|
headers: {
|
|
76
89
|
...configs.cnp.headers,
|
|
77
90
|
...additionalHeaders,
|
|
78
|
-
["X-Client"]: additionalHeaders["X-Client"] || configs.cnp.headers["X-Client"],
|
|
79
91
|
}
|
|
80
|
-
}
|
|
92
|
+
};
|
|
81
93
|
} else {
|
|
82
|
-
// Para cualquier otro tenant, usar config default pero sobreescribir X-Tenant y X-User
|
|
83
94
|
config = {
|
|
84
95
|
...configs.default,
|
|
85
96
|
headers: {
|
|
@@ -119,14 +130,12 @@ export const apiMapper = async (
|
|
|
119
130
|
});
|
|
120
131
|
return endpointData.responseMapper(response.data);
|
|
121
132
|
case methods.PUT:
|
|
122
|
-
// aca podria ejecutarse requestMapper
|
|
123
133
|
response = await axiosInstance.put(url, params);
|
|
124
134
|
return endpointData.responseMapper(response.data);
|
|
125
135
|
case methods.DELETE:
|
|
126
136
|
response = await axiosInstance.delete(url, params);
|
|
127
137
|
return endpointData.responseMapper(response.data);
|
|
128
138
|
default:
|
|
129
|
-
response = null;
|
|
130
139
|
throw new Error(
|
|
131
140
|
"Request error: Method not allowed. Only use GET, POST, PUT, DELETE"
|
|
132
141
|
);
|
|
@@ -138,9 +147,7 @@ export const apiMapper = async (
|
|
|
138
147
|
|
|
139
148
|
const getUrl = (serviceUrl, routeParams) => {
|
|
140
149
|
if (!routeParams) return serviceUrl;
|
|
141
|
-
return serviceUrl.replace(/{(\d+)}/g,
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
});
|
|
146
|
-
};
|
|
150
|
+
return serviceUrl.replace(/{(\d+)}/g, (match, routeParamNumber) =>
|
|
151
|
+
routeParams[routeParamNumber] || match
|
|
152
|
+
);
|
|
153
|
+
};
|