sysone-api-mapper 1.0.137 → 1.0.138
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 +101 -182
package/package.json
CHANGED
package/src/mapper/Mapper.js
CHANGED
|
@@ -1,108 +1,43 @@
|
|
|
1
1
|
import axiosInstance from "../../axiosInstance";
|
|
2
2
|
import { methods, tenantsConfig } from "./endpointsConfig";
|
|
3
|
-
import {
|
|
4
|
-
SecurityManager,
|
|
5
|
-
} from "@sysone/components";
|
|
6
|
-
|
|
7
|
-
const getApiMapperConfig = () => {
|
|
8
|
-
// 1. Intentar desde variables de entorno compiladas (Webpack DefinePlugin)
|
|
9
|
-
if (process.env.API_MAPPER) {
|
|
10
|
-
try {
|
|
11
|
-
// Si es un string, parsearlo
|
|
12
|
-
const parsed = typeof process.env.API_MAPPER === 'string'
|
|
13
|
-
? JSON.parse(process.env.API_MAPPER)
|
|
14
|
-
: process.env.API_MAPPER;
|
|
15
|
-
|
|
16
|
-
if (parsed && Object.keys(parsed).length > 0) {
|
|
17
|
-
console.log("✅ Usando API_MAPPER de process.env", parsed);
|
|
18
|
-
return parsed;
|
|
19
|
-
}
|
|
20
|
-
} catch (error) {
|
|
21
|
-
console.error("❌ Error parseando API_MAPPER de process.env:", error);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
3
|
|
|
25
|
-
// 2. Intentar desde variable global inyectada
|
|
26
|
-
if (window.__API_MAPPER__ && Object.keys(window.__API_MAPPER__).length > 0) {
|
|
27
|
-
console.log("✅ Usando API_MAPPER de window");
|
|
28
|
-
return window.__API_MAPPER__;
|
|
29
|
-
}
|
|
30
4
|
|
|
31
|
-
|
|
32
|
-
return null;
|
|
33
|
-
};
|
|
5
|
+
let apiConfig = null;
|
|
34
6
|
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
7
|
+
export const configureApiMapper = (config) => {
|
|
8
|
+
if (!config?.baseURL) {
|
|
9
|
+
throw new Error("apiMapper requires baseURL");
|
|
10
|
+
}
|
|
38
11
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
12
|
+
apiConfig = {
|
|
13
|
+
baseURL: config.baseURL,
|
|
14
|
+
headers: config.headers || {},
|
|
15
|
+
};
|
|
42
16
|
};
|
|
43
17
|
|
|
44
|
-
const
|
|
45
|
-
if (
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const salesCode = getSalesAgent();
|
|
51
|
-
const salesChannel = getSalesChannel();
|
|
52
|
-
|
|
53
|
-
return {
|
|
54
|
-
"X-Agent": xAgent,
|
|
55
|
-
"X-Sales-Channel": salesChannel || xAgent,
|
|
56
|
-
"X-Sales-Agent": salesCode || xAgent
|
|
57
|
-
};
|
|
18
|
+
const getConfig = () => {
|
|
19
|
+
if (!apiConfig) {
|
|
20
|
+
throw new Error(
|
|
21
|
+
"apiMapper not configured. Call configureApiMapper() first."
|
|
22
|
+
);
|
|
58
23
|
}
|
|
59
|
-
return
|
|
24
|
+
return apiConfig;
|
|
60
25
|
};
|
|
61
26
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const configs = {
|
|
69
|
-
default: {
|
|
70
|
-
baseURL: API_MAPPER?.default?.baseURL || '',
|
|
71
|
-
headers: {
|
|
72
|
-
["X-Tenant"]: API_MAPPER?.default?.headers?.["X-Tenant"] || '',
|
|
73
|
-
["X-User"]: API_MAPPER?.default?.headers?.["X-User"] || '',
|
|
74
|
-
["X-Api-Key"]: API_MAPPER?.default?.headers?.["X-Api-Key"] || '',
|
|
75
|
-
["X-Origin"]: "sysone-api-mapper"
|
|
76
|
-
},
|
|
77
|
-
},
|
|
78
|
-
cnp: {
|
|
79
|
-
baseURL: API_MAPPER?.cnp?.baseURL || '',
|
|
80
|
-
headers: {
|
|
81
|
-
["X-Channel"]: API_MAPPER?.cnp?.headers?.["X-Channel"] || '',
|
|
82
|
-
["X-Agent"]: API_MAPPER?.cnp?.headers?.["X-Agent"] || '',
|
|
83
|
-
["X-Client"]: API_MAPPER?.cnp?.headers?.["X-Client"] || '',
|
|
84
|
-
["Ocp-Apim-Subscription-Key"]: API_MAPPER?.cnp?.headers?.["Ocp-Apim-Subscription-Key"] || '',
|
|
85
|
-
["X-Origin"]: "sysone-api-mapper",
|
|
86
|
-
["X-Sales-Agent"]: API_MAPPER?.cnp?.headers?.["X-Agent"] || '',
|
|
87
|
-
["X-Sales-Channel"]: API_MAPPER?.cnp?.headers?.["X-Agent"] || '',
|
|
88
|
-
},
|
|
89
|
-
},
|
|
27
|
+
/**
|
|
28
|
+
* Normaliza tenant (ej: "cnp/foo" → "cnp")
|
|
29
|
+
*/
|
|
30
|
+
const normalizeTenant = (tenant) => {
|
|
31
|
+
if (!tenant || typeof tenant !== "string") return tenant;
|
|
32
|
+
return tenant.split("/")[0];
|
|
90
33
|
};
|
|
91
34
|
|
|
92
35
|
const getUrl = (serviceUrl, routeParams) => {
|
|
93
36
|
if (!routeParams || routeParams.length === 0) return serviceUrl;
|
|
94
|
-
return serviceUrl.replace(/{(\d+)}/g, function (match, routeParamNumber) {
|
|
95
|
-
return routeParams[routeParamNumber]
|
|
96
|
-
? routeParams[routeParamNumber]
|
|
97
|
-
: match;
|
|
98
|
-
});
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
const normalizeTenant = (tenant) => {
|
|
102
|
-
if (!tenant || typeof tenant !== "string") return tenant;
|
|
103
37
|
|
|
104
|
-
|
|
105
|
-
|
|
38
|
+
return serviceUrl.replace(/{(\d+)}/g, (match, index) =>
|
|
39
|
+
routeParams[index] !== undefined ? routeParams[index] : match
|
|
40
|
+
);
|
|
106
41
|
};
|
|
107
42
|
|
|
108
43
|
|
|
@@ -113,110 +48,94 @@ export const apiMapper = async (
|
|
|
113
48
|
params = null,
|
|
114
49
|
additionalHeaders = {}
|
|
115
50
|
) => {
|
|
116
|
-
|
|
117
|
-
const configData = tenantsConfig[endpointCode];
|
|
118
|
-
const normalizedTenant = normalizeTenant(tenant);
|
|
119
|
-
if (!configData)
|
|
120
|
-
throw new Error(`Endpoint no configurado: ${endpointCode}`);
|
|
51
|
+
/* -------- Endpoint config -------- */
|
|
121
52
|
|
|
122
|
-
|
|
53
|
+
const configData = tenantsConfig[endpointCode];
|
|
54
|
+
if (!configData) {
|
|
55
|
+
throw new Error(`Endpoint no configurado: ${endpointCode}`);
|
|
56
|
+
}
|
|
123
57
|
|
|
124
|
-
|
|
125
|
-
console.warn(`⚠️ No hay configuración para el endpoint ${endpointCode} y tenant ${tenant}, y no existe configuración por defecto.`);
|
|
126
|
-
throw new Error(`No hay configuración para el endpoint ${endpointCode} y tenant ${tenant}`);
|
|
127
|
-
}
|
|
58
|
+
const normalizedTenant = normalizeTenant(tenant);
|
|
128
59
|
|
|
129
|
-
|
|
60
|
+
const endpointData =
|
|
61
|
+
configData[normalizedTenant] ?? configData.default;
|
|
130
62
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
63
|
+
if (!endpointData) {
|
|
64
|
+
throw new Error(
|
|
65
|
+
`No hay configuración para endpoint ${endpointCode} y tenant ${normalizedTenant}`
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/* -------- Axios config -------- */
|
|
134
70
|
|
|
135
|
-
|
|
71
|
+
const { baseURL, headers } = getConfig();
|
|
136
72
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
73
|
+
const axiosConfig = {
|
|
74
|
+
baseURL,
|
|
75
|
+
headers: {
|
|
76
|
+
...headers,
|
|
77
|
+
...additionalHeaders,
|
|
78
|
+
},
|
|
79
|
+
validateStatus: (s) => s < 500,
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const url = getUrl(endpointData.url, routeParams);
|
|
83
|
+
|
|
84
|
+
/* -------- Request -------- */
|
|
85
|
+
|
|
86
|
+
switch (endpointData.method) {
|
|
87
|
+
case methods.GET: {
|
|
88
|
+
const {
|
|
89
|
+
mappedParams,
|
|
90
|
+
responseType,
|
|
91
|
+
headers: requestHeaders,
|
|
92
|
+
...otherOptions
|
|
93
|
+
} = endpointData.requestMapper(params);
|
|
94
|
+
|
|
95
|
+
const response = await axiosInstance.get(url, {
|
|
96
|
+
...axiosConfig,
|
|
97
|
+
params: mappedParams,
|
|
98
|
+
responseType,
|
|
140
99
|
headers: {
|
|
141
|
-
...
|
|
142
|
-
...
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
};
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
100
|
+
...axiosConfig.headers,
|
|
101
|
+
...requestHeaders,
|
|
102
|
+
},
|
|
103
|
+
...otherOptions,
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
return endpointData.responseMapper(response);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
case methods.POST: {
|
|
110
|
+
const { mappedPostParams, mappedBody, headers: requestHeaders } =
|
|
111
|
+
endpointData.requestMapper(params);
|
|
112
|
+
|
|
113
|
+
const response = await axiosInstance.post(url, mappedBody, {
|
|
114
|
+
...axiosConfig,
|
|
115
|
+
params: mappedPostParams,
|
|
149
116
|
headers: {
|
|
150
|
-
...
|
|
151
|
-
|
|
152
|
-
["X-User"]: normalizedTenant,
|
|
153
|
-
...additionalHeaders
|
|
117
|
+
...axiosConfig.headers,
|
|
118
|
+
...requestHeaders,
|
|
154
119
|
},
|
|
155
|
-
};
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
return endpointData.responseMapper(response.data);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
case methods.PUT: {
|
|
126
|
+
const response = await axiosInstance.put(url, params, axiosConfig);
|
|
127
|
+
return endpointData.responseMapper(response.data);
|
|
156
128
|
}
|
|
157
129
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
const fullUrl = `${config.baseURL}${urlWithParams}`.replace(/([^:]\/)\/+/g, "$1");
|
|
165
|
-
|
|
166
|
-
let response;
|
|
167
|
-
let request;
|
|
168
|
-
|
|
169
|
-
switch (endpointData.method) {
|
|
170
|
-
case methods.GET:
|
|
171
|
-
request = endpointData.requestMapper(params);
|
|
172
|
-
const { mappedParams, responseType, headers: requestHeaders, ...otherOptions } = request;
|
|
173
|
-
|
|
174
|
-
response = await axiosInstance.get(fullUrl, {
|
|
175
|
-
...config,
|
|
176
|
-
params: mappedParams,
|
|
177
|
-
responseType: responseType,
|
|
178
|
-
headers: {
|
|
179
|
-
...config.headers,
|
|
180
|
-
...requestHeaders
|
|
181
|
-
},
|
|
182
|
-
validateStatus: config.validateStatus,
|
|
183
|
-
...otherOptions
|
|
184
|
-
});
|
|
185
|
-
return endpointData.responseMapper(response);
|
|
186
|
-
|
|
187
|
-
case methods.POST:
|
|
188
|
-
request = endpointData.requestMapper(params);
|
|
189
|
-
const { mappedPostParams, mappedBody } = request;
|
|
190
|
-
response = await axiosInstance.post(fullUrl, mappedBody, {
|
|
191
|
-
params: mappedPostParams,
|
|
192
|
-
headers: config.headers,
|
|
193
|
-
validateStatus: config.validateStatus,
|
|
194
|
-
});
|
|
195
|
-
return endpointData.responseMapper(response.data);
|
|
196
|
-
|
|
197
|
-
case methods.PUT:
|
|
198
|
-
response = await axiosInstance.put(fullUrl, params, {
|
|
199
|
-
headers: config.headers,
|
|
200
|
-
validateStatus: config.validateStatus,
|
|
201
|
-
});
|
|
202
|
-
return endpointData.responseMapper(response.data);
|
|
203
|
-
|
|
204
|
-
case methods.DELETE:
|
|
205
|
-
response = await axiosInstance.delete(fullUrl, {
|
|
206
|
-
data: params,
|
|
207
|
-
headers: config.headers,
|
|
208
|
-
validateStatus: config.validateStatus,
|
|
209
|
-
});
|
|
210
|
-
return endpointData.responseMapper(response.data);
|
|
211
|
-
|
|
212
|
-
default:
|
|
213
|
-
response = null;
|
|
214
|
-
throw new Error(
|
|
215
|
-
"Request error: Method not allowed. Only use GET, POST, PUT, DELETE"
|
|
216
|
-
);
|
|
130
|
+
case methods.DELETE: {
|
|
131
|
+
const response = await axiosInstance.delete(url, {
|
|
132
|
+
...axiosConfig,
|
|
133
|
+
data: params,
|
|
134
|
+
});
|
|
135
|
+
return endpointData.responseMapper(response.data);
|
|
217
136
|
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
137
|
+
|
|
138
|
+
default:
|
|
139
|
+
throw new Error("HTTP method not supported");
|
|
221
140
|
}
|
|
222
141
|
};
|