sysone-api-mapper 1.0.41 → 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 +29 -19
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,15 +57,13 @@ const configs = {
|
|
|
37
57
|
baseURL: API_MAPPER.cnp.baseURL,
|
|
38
58
|
headers: {
|
|
39
59
|
["X-Channel"]: API_MAPPER.cnp.headers["X-Chanel"],
|
|
40
|
-
["X-
|
|
60
|
+
["X-Client"]: API_MAPPER.cnp.headers["X-Client"],
|
|
41
61
|
["Ocp-Apim-Subscription-Key"]: API_MAPPER.cnp.headers["Ocp-Apim-Subscription-Key"],
|
|
42
62
|
["X-Origin"]: "sysone-api-mapper"
|
|
43
63
|
},
|
|
44
64
|
},
|
|
45
65
|
};
|
|
46
66
|
|
|
47
|
-
// Momentaneo para probar CNP test
|
|
48
|
-
|
|
49
67
|
export const apiMapper = async (
|
|
50
68
|
endpointCode,
|
|
51
69
|
tenant,
|
|
@@ -53,16 +71,13 @@ export const apiMapper = async (
|
|
|
53
71
|
params = null,
|
|
54
72
|
additionalHeaders = {}
|
|
55
73
|
) => {
|
|
56
|
-
console.log("🚀 ~ apiMapper ~ additionalHeaders:", additionalHeaders)
|
|
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
|
}
|
|
@@ -74,9 +89,8 @@ export const apiMapper = async (
|
|
|
74
89
|
...configs.cnp.headers,
|
|
75
90
|
...additionalHeaders,
|
|
76
91
|
}
|
|
77
|
-
}
|
|
92
|
+
};
|
|
78
93
|
} else {
|
|
79
|
-
// Para cualquier otro tenant, usar config default pero sobreescribir X-Tenant y X-User
|
|
80
94
|
config = {
|
|
81
95
|
...configs.default,
|
|
82
96
|
headers: {
|
|
@@ -116,14 +130,12 @@ export const apiMapper = async (
|
|
|
116
130
|
});
|
|
117
131
|
return endpointData.responseMapper(response.data);
|
|
118
132
|
case methods.PUT:
|
|
119
|
-
// aca podria ejecutarse requestMapper
|
|
120
133
|
response = await axiosInstance.put(url, params);
|
|
121
134
|
return endpointData.responseMapper(response.data);
|
|
122
135
|
case methods.DELETE:
|
|
123
136
|
response = await axiosInstance.delete(url, params);
|
|
124
137
|
return endpointData.responseMapper(response.data);
|
|
125
138
|
default:
|
|
126
|
-
response = null;
|
|
127
139
|
throw new Error(
|
|
128
140
|
"Request error: Method not allowed. Only use GET, POST, PUT, DELETE"
|
|
129
141
|
);
|
|
@@ -135,9 +147,7 @@ export const apiMapper = async (
|
|
|
135
147
|
|
|
136
148
|
const getUrl = (serviceUrl, routeParams) => {
|
|
137
149
|
if (!routeParams) return serviceUrl;
|
|
138
|
-
return serviceUrl.replace(/{(\d+)}/g,
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
});
|
|
143
|
-
};
|
|
150
|
+
return serviceUrl.replace(/{(\d+)}/g, (match, routeParamNumber) =>
|
|
151
|
+
routeParams[routeParamNumber] || match
|
|
152
|
+
);
|
|
153
|
+
};
|