sysone-api-mapper 1.0.43 → 1.0.44
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/package.json +1 -1
- package/src/mapper/Mapper.js +32 -27
package/package.json
CHANGED
package/src/mapper/Mapper.js
CHANGED
|
@@ -1,31 +1,10 @@
|
|
|
1
|
-
import
|
|
1
|
+
import axiosInstance from "../../axiosInstance";
|
|
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
|
-
|
|
29
8
|
const getApiMapperConfig = () => {
|
|
30
9
|
// 1. Intentar desde variables de entorno compiladas
|
|
31
10
|
if (process.env.API_MAPPER && Object.keys(process.env.API_MAPPER).length > 0) {
|
|
@@ -38,6 +17,21 @@ const getApiMapperConfig = () => {
|
|
|
38
17
|
console.log("Usando API_MAPPER de window");
|
|
39
18
|
return window.__API_MAPPER__;
|
|
40
19
|
}
|
|
20
|
+
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const getDynamicHeaders = (tenant) => {
|
|
24
|
+
if (tenant === "cnp") {
|
|
25
|
+
// Intenta obtener el X-Agent de localStorage (inyectado desde el front)
|
|
26
|
+
const xAgent = localStorage.getItem("intermediaryCode") ||
|
|
27
|
+
window.__INTERMEDIARY_CODE__ ||
|
|
28
|
+
configs.cnp.headers["X-Agent"];
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
"X-Agent": xAgent
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
return {};
|
|
41
35
|
};
|
|
42
36
|
|
|
43
37
|
const API_MAPPER = getApiMapperConfig();
|
|
@@ -57,6 +51,7 @@ const configs = {
|
|
|
57
51
|
baseURL: API_MAPPER.cnp.baseURL,
|
|
58
52
|
headers: {
|
|
59
53
|
["X-Channel"]: API_MAPPER.cnp.headers["X-Chanel"],
|
|
54
|
+
["X-Agent"]: API_MAPPER.cnp.headers["X-Agent"],
|
|
60
55
|
["X-Client"]: API_MAPPER.cnp.headers["X-Client"],
|
|
61
56
|
["Ocp-Apim-Subscription-Key"]: API_MAPPER.cnp.headers["Ocp-Apim-Subscription-Key"],
|
|
62
57
|
["X-Origin"]: "sysone-api-mapper"
|
|
@@ -64,6 +59,8 @@ const configs = {
|
|
|
64
59
|
},
|
|
65
60
|
};
|
|
66
61
|
|
|
62
|
+
// Momentaneo para probar CNP test
|
|
63
|
+
|
|
67
64
|
export const apiMapper = async (
|
|
68
65
|
endpointCode,
|
|
69
66
|
tenant,
|
|
@@ -73,7 +70,8 @@ export const apiMapper = async (
|
|
|
73
70
|
) => {
|
|
74
71
|
try {
|
|
75
72
|
const configData = tenantsConfig[endpointCode];
|
|
76
|
-
if (!configData)
|
|
73
|
+
if (!configData)
|
|
74
|
+
throw new Error(`Endpoint no configurado: ${endpointCode}`);
|
|
77
75
|
|
|
78
76
|
const endpointData = configData[tenant] ?? configData.default;
|
|
79
77
|
let config;
|
|
@@ -82,11 +80,14 @@ export const apiMapper = async (
|
|
|
82
80
|
throw new Error("No se pudo cargar la configuración del API Mapper");
|
|
83
81
|
}
|
|
84
82
|
|
|
83
|
+
const dynamicHeaders = getDynamicHeaders(tenant);
|
|
84
|
+
|
|
85
85
|
if (tenant === "cnp") {
|
|
86
86
|
config = {
|
|
87
87
|
...configs.cnp,
|
|
88
88
|
headers: {
|
|
89
89
|
...configs.cnp.headers,
|
|
90
|
+
...dynamicHeaders, // Headers dinámicos
|
|
90
91
|
...additionalHeaders,
|
|
91
92
|
}
|
|
92
93
|
};
|
|
@@ -130,12 +131,14 @@ export const apiMapper = async (
|
|
|
130
131
|
});
|
|
131
132
|
return endpointData.responseMapper(response.data);
|
|
132
133
|
case methods.PUT:
|
|
134
|
+
// aca podria ejecutarse requestMapper
|
|
133
135
|
response = await axiosInstance.put(url, params);
|
|
134
136
|
return endpointData.responseMapper(response.data);
|
|
135
137
|
case methods.DELETE:
|
|
136
138
|
response = await axiosInstance.delete(url, params);
|
|
137
139
|
return endpointData.responseMapper(response.data);
|
|
138
140
|
default:
|
|
141
|
+
response = null;
|
|
139
142
|
throw new Error(
|
|
140
143
|
"Request error: Method not allowed. Only use GET, POST, PUT, DELETE"
|
|
141
144
|
);
|
|
@@ -147,7 +150,9 @@ export const apiMapper = async (
|
|
|
147
150
|
|
|
148
151
|
const getUrl = (serviceUrl, routeParams) => {
|
|
149
152
|
if (!routeParams) return serviceUrl;
|
|
150
|
-
return serviceUrl.replace(/{(\d+)}/g, (match, routeParamNumber)
|
|
151
|
-
routeParams[routeParamNumber]
|
|
152
|
-
|
|
153
|
-
|
|
153
|
+
return serviceUrl.replace(/{(\d+)}/g, function (match, routeParamNumber) {
|
|
154
|
+
return routeParams[routeParamNumber]
|
|
155
|
+
? routeParams[routeParamNumber]
|
|
156
|
+
: match;
|
|
157
|
+
});
|
|
158
|
+
};
|