apacuana-sdk-core 0.8.0 → 0.10.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 +150 -0
- package/coverage/clover.xml +233 -130
- package/coverage/coverage-final.json +8 -8
- package/coverage/lcov-report/index.html +29 -29
- package/coverage/lcov-report/src/api/certs.js.html +390 -9
- package/coverage/lcov-report/src/api/index.html +40 -40
- package/coverage/lcov-report/src/api/revocations.js.html +79 -43
- package/coverage/lcov-report/src/api/signatures.js.html +595 -73
- package/coverage/lcov-report/src/api/users.js.html +1 -1
- package/coverage/lcov-report/src/config/index.html +1 -1
- package/coverage/lcov-report/src/config/index.js.html +6 -6
- package/coverage/lcov-report/src/errors/index.html +1 -1
- package/coverage/lcov-report/src/errors/index.js.html +8 -8
- package/coverage/lcov-report/src/index.html +1 -1
- package/coverage/lcov-report/src/index.js.html +58 -4
- package/coverage/lcov-report/src/utils/constant.js.html +4 -4
- package/coverage/lcov-report/src/utils/helpers.js.html +1 -1
- package/coverage/lcov-report/src/utils/httpClient.js.html +65 -35
- package/coverage/lcov-report/src/utils/index.html +15 -15
- package/coverage/lcov.info +422 -227
- package/dist/api/certs.d.ts +44 -0
- package/dist/api/signatures.d.ts +5 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +579 -79
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +579 -79
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/api/certs.js +127 -0
- package/src/api/revocations.js +14 -2
- package/src/api/signatures.js +174 -0
- package/src/index.js +20 -2
- package/src/utils/httpClient.js +29 -19
- package/tests/api/certs.test.js +113 -1
- package/tests/api/revocations.test.js +118 -5
- package/tests/api/signatures.test.js +306 -0
package/package.json
CHANGED
package/src/api/certs.js
CHANGED
|
@@ -99,3 +99,130 @@ export const getCertStatus = (isCertificateInDevice = false) => {
|
|
|
99
99
|
success: true,
|
|
100
100
|
};
|
|
101
101
|
};
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* @typedef {object} CertType
|
|
105
|
+
* @property {string} id - The ID of the certificate type.
|
|
106
|
+
* @property {string} name - The name of the certificate type.
|
|
107
|
+
*/
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* @typedef {object} GetCertTypesResponse
|
|
111
|
+
* @property {Array<CertType>} types - A list of available certificate types.
|
|
112
|
+
* @property {boolean} success - Indicates if the operation was successful.
|
|
113
|
+
*/
|
|
114
|
+
|
|
115
|
+
const getCertTypesOnBoarding = async () => {
|
|
116
|
+
try {
|
|
117
|
+
const response = await httpRequest(
|
|
118
|
+
"services/api/customer/typeusers",
|
|
119
|
+
{},
|
|
120
|
+
"GET"
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
return { ...response, success: true };
|
|
124
|
+
} catch (error) {
|
|
125
|
+
if (error instanceof ApacuanaAPIError) {
|
|
126
|
+
throw error;
|
|
127
|
+
}
|
|
128
|
+
throw new Error(`Failed to get certificate types: ${error.message}`);
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const getCertTypesOnPremise = async () => {
|
|
133
|
+
throw new ApacuanaAPIError(
|
|
134
|
+
"Getting certificate types is not supported for integration type: ONPREMISE",
|
|
135
|
+
501,
|
|
136
|
+
"NOT_IMPLEMENTED"
|
|
137
|
+
);
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Gets the available certificate types.
|
|
142
|
+
* @returns {Promise<GetCertTypesResponse>} Object with the list of certificate types and a success indicator.
|
|
143
|
+
* @throws {ApacuanaAPIError} If the API response is invalid or the integration type is not supported.
|
|
144
|
+
* @throws {Error} If the request fails for another reason.
|
|
145
|
+
*/
|
|
146
|
+
export const getCertTypes = async () => {
|
|
147
|
+
const { integrationType } = getConfig();
|
|
148
|
+
if (integrationType === INTEGRATION_TYPE.ONBOARDING) {
|
|
149
|
+
return getCertTypesOnBoarding();
|
|
150
|
+
}
|
|
151
|
+
if (integrationType === INTEGRATION_TYPE.ONPREMISE) {
|
|
152
|
+
return getCertTypesOnPremise();
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
throw new ApacuanaAPIError(
|
|
156
|
+
`Unsupported integration type: ${integrationType}`,
|
|
157
|
+
400,
|
|
158
|
+
"UNSUPPORTED_INTEGRATION_TYPE"
|
|
159
|
+
);
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* @typedef {object} Requirement
|
|
164
|
+
* @property {string} id - The ID of the requirement.
|
|
165
|
+
* @property {string} description - The description of the requirement.
|
|
166
|
+
*/
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* @typedef {object} GetRequirementsResponse
|
|
170
|
+
* @property {Array<Requirement>} requirements - A list of requirements for the user type.
|
|
171
|
+
* @property {boolean} success - Indicates if the operation was successful.
|
|
172
|
+
*/
|
|
173
|
+
|
|
174
|
+
const getRequerimentsByTypeUserOnBoarding = async (type) => {
|
|
175
|
+
try {
|
|
176
|
+
const response = await httpRequest(
|
|
177
|
+
`services/api/customer/documentspref?typeuser=${type}`,
|
|
178
|
+
{},
|
|
179
|
+
"GET"
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
return { ...response, success: true };
|
|
183
|
+
} catch (error) {
|
|
184
|
+
if (error instanceof ApacuanaAPIError) {
|
|
185
|
+
throw error;
|
|
186
|
+
}
|
|
187
|
+
throw new Error(`Failed to get requirements: ${error.message}`);
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
const getRequerimentsByTypeUserOnPremise = async () => {
|
|
192
|
+
throw new ApacuanaAPIError(
|
|
193
|
+
"Getting requirements by user type is not supported for integration type: ONPREMISE",
|
|
194
|
+
501,
|
|
195
|
+
"NOT_IMPLEMENTED"
|
|
196
|
+
);
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Gets the requirements for a given user type.
|
|
201
|
+
* @param {object} params - The parameters.
|
|
202
|
+
* @param {number} params.type - The user type to get requirements for.
|
|
203
|
+
* @returns {Promise<GetRequirementsResponse>} Object with the list of requirements and a success indicator.
|
|
204
|
+
* @throws {ApacuanaAPIError} If the API response is invalid or the integration type is not supported.
|
|
205
|
+
* @throws {Error} If the request fails for another reason or the type is invalid.
|
|
206
|
+
*/
|
|
207
|
+
export const getRequerimentsByTypeUser = async (params) => {
|
|
208
|
+
if (!params || typeof params.type !== "number") {
|
|
209
|
+
throw new Error(
|
|
210
|
+
'The "params" object with a numeric "type" property is required.'
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
const { type } = params;
|
|
214
|
+
|
|
215
|
+
const { integrationType } = getConfig();
|
|
216
|
+
if (integrationType === INTEGRATION_TYPE.ONBOARDING) {
|
|
217
|
+
return getRequerimentsByTypeUserOnBoarding(type);
|
|
218
|
+
}
|
|
219
|
+
if (integrationType === INTEGRATION_TYPE.ONPREMISE) {
|
|
220
|
+
return getRequerimentsByTypeUserOnPremise();
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
throw new ApacuanaAPIError(
|
|
224
|
+
`Unsupported integration type: ${integrationType}`,
|
|
225
|
+
400,
|
|
226
|
+
"UNSUPPORTED_INTEGRATION_TYPE"
|
|
227
|
+
);
|
|
228
|
+
};
|
package/src/api/revocations.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { httpRequest } from "../utils/httpClient";
|
|
2
2
|
import { ApacuanaAPIError } from "../errors/index";
|
|
3
|
+
import { getConfig } from "../config";
|
|
4
|
+
import { INTEGRATION_TYPE } from "../utils/constant";
|
|
3
5
|
|
|
4
6
|
/**
|
|
5
7
|
* @typedef {object} RequestRevocationResponse
|
|
@@ -60,11 +62,21 @@ export const requestRevocation = async (reasonCode) => {
|
|
|
60
62
|
* @throws {ApacuanaAPIError} If fetching the reasons fails.
|
|
61
63
|
*/
|
|
62
64
|
export const getRevocationReasons = async () => {
|
|
65
|
+
const { integrationType } = getConfig();
|
|
66
|
+
|
|
67
|
+
if (integrationType === INTEGRATION_TYPE.ONPREMISE) {
|
|
68
|
+
throw new ApacuanaAPIError(
|
|
69
|
+
`Get revocation reasons is not supported for integration type: ${integrationType}`,
|
|
70
|
+
501,
|
|
71
|
+
"NOT_IMPLEMENTED"
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
63
75
|
try {
|
|
64
76
|
const response = await httpRequest(
|
|
77
|
+
"GET",
|
|
65
78
|
"config/api/revocation/reasonsonboarding",
|
|
66
|
-
{}
|
|
67
|
-
"GET"
|
|
79
|
+
{}
|
|
68
80
|
);
|
|
69
81
|
|
|
70
82
|
if (!response.records) {
|
package/src/api/signatures.js
CHANGED
|
@@ -218,6 +218,85 @@ const getDocsOnBoarding = async (data) => {
|
|
|
218
218
|
}
|
|
219
219
|
};
|
|
220
220
|
|
|
221
|
+
const uploadSignatureVariantOnBoarding = async ({ file }) => {
|
|
222
|
+
try {
|
|
223
|
+
const response = await httpRequest(
|
|
224
|
+
"services/api/customer/signaturephoto",
|
|
225
|
+
{ file },
|
|
226
|
+
"POST"
|
|
227
|
+
);
|
|
228
|
+
return { ...response, success: true };
|
|
229
|
+
} catch (error) {
|
|
230
|
+
if (error instanceof ApacuanaAPIError) {
|
|
231
|
+
throw error;
|
|
232
|
+
}
|
|
233
|
+
throw new ApacuanaAPIError(
|
|
234
|
+
`Failed to upload signature variant: ${error.message || "Unknown error"}`
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
const uploadSignatureVariantOnPremise = async () => {
|
|
240
|
+
throw new ApacuanaAPIError(
|
|
241
|
+
"Uploading signature variants is not supported for integration type: ONPREMISE",
|
|
242
|
+
501,
|
|
243
|
+
"NOT_IMPLEMENTED"
|
|
244
|
+
);
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
const getSignatureVariantOnBoarding = async () => {
|
|
248
|
+
const { customerId } = getConfig();
|
|
249
|
+
try {
|
|
250
|
+
const response = await httpRequest(
|
|
251
|
+
`services/api/customer/getsignaturephotosdk/${customerId}`,
|
|
252
|
+
{},
|
|
253
|
+
"GET"
|
|
254
|
+
);
|
|
255
|
+
return { ...response, success: true };
|
|
256
|
+
} catch (error) {
|
|
257
|
+
if (error instanceof ApacuanaAPIError) {
|
|
258
|
+
throw error;
|
|
259
|
+
}
|
|
260
|
+
throw new ApacuanaAPIError(
|
|
261
|
+
`Failed to get signature variant: ${error.message || "Unknown error"}`
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
const getSignatureVariantOnPremise = async () => {
|
|
267
|
+
throw new ApacuanaAPIError(
|
|
268
|
+
"Getting signature variants is not supported for integration type: ONPREMISE",
|
|
269
|
+
501,
|
|
270
|
+
"NOT_IMPLEMENTED"
|
|
271
|
+
);
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
const deleteSignatureVariantOnBoarding = async () => {
|
|
275
|
+
try {
|
|
276
|
+
const response = await httpRequest(
|
|
277
|
+
"services/api/customer/cleansignaturephoto",
|
|
278
|
+
{},
|
|
279
|
+
"DELETE"
|
|
280
|
+
);
|
|
281
|
+
return { ...response, success: true };
|
|
282
|
+
} catch (error) {
|
|
283
|
+
if (error instanceof ApacuanaAPIError) {
|
|
284
|
+
throw error;
|
|
285
|
+
}
|
|
286
|
+
throw new ApacuanaAPIError(
|
|
287
|
+
`Failed to delete signature variant: ${error.message || "Unknown error"}`
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
const deleteSignatureVariantOnPremise = async () => {
|
|
293
|
+
throw new ApacuanaAPIError(
|
|
294
|
+
"Deleting signature variants is not supported for integration type: ONPREMISE",
|
|
295
|
+
501,
|
|
296
|
+
"NOT_IMPLEMENTED"
|
|
297
|
+
);
|
|
298
|
+
};
|
|
299
|
+
|
|
221
300
|
// =================================================================
|
|
222
301
|
// Exported Functions
|
|
223
302
|
// =================================================================
|
|
@@ -358,3 +437,98 @@ export const getDocs = async (data) => {
|
|
|
358
437
|
"NOT_IMPLEMENTED"
|
|
359
438
|
);
|
|
360
439
|
};
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* Sube una variante de firma para un firmante.
|
|
443
|
+
* @param {object} data - Datos para la subida.
|
|
444
|
+
* @param {File} data.file - El archivo de la variante de firma (debe ser PNG).
|
|
445
|
+
* @returns {Promise<object>} Una promesa que resuelve a un objeto con el resultado de la operación.
|
|
446
|
+
* @throws {ApacuanaAPIError} Si los datos son inválidos o la llamada a la API falla.
|
|
447
|
+
*/
|
|
448
|
+
export const uploadSignatureVariant = async (data) => {
|
|
449
|
+
if (!data || !data.file) {
|
|
450
|
+
throw new ApacuanaAPIError(
|
|
451
|
+
"El parámetro 'file' es requerido.",
|
|
452
|
+
400,
|
|
453
|
+
"INVALID_PARAMETER"
|
|
454
|
+
);
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
if (typeof File === "undefined" || !(data.file instanceof File)) {
|
|
458
|
+
throw new ApacuanaAPIError(
|
|
459
|
+
"El parámetro 'file' debe ser una instancia de 'File'.",
|
|
460
|
+
400,
|
|
461
|
+
"INVALID_PARAMETER"
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
if (data.file.type !== "image/png") {
|
|
466
|
+
throw new ApacuanaAPIError(
|
|
467
|
+
"El archivo debe ser de tipo PNG (image/png).",
|
|
468
|
+
400,
|
|
469
|
+
"INVALID_PARAMETER"
|
|
470
|
+
);
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
const { integrationType } = getConfig();
|
|
474
|
+
|
|
475
|
+
if (integrationType === INTEGRATION_TYPE.ONBOARDING) {
|
|
476
|
+
return uploadSignatureVariantOnBoarding(data);
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
if (integrationType === INTEGRATION_TYPE.ONPREMISE) {
|
|
480
|
+
return uploadSignatureVariantOnPremise();
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
throw new ApacuanaAPIError(
|
|
484
|
+
`Unsupported integration type: ${integrationType}`,
|
|
485
|
+
400,
|
|
486
|
+
"UNSUPPORTED_INTEGRATION_TYPE"
|
|
487
|
+
);
|
|
488
|
+
};
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* Obtiene la variante de firma del firmante.
|
|
492
|
+
* @returns {Promise<object>} Una promesa que resuelve a un objeto con los datos de la variante de firma.
|
|
493
|
+
* @throws {ApacuanaAPIError} Si la llamada a la API falla o el tipo de integración no es soportado.
|
|
494
|
+
*/
|
|
495
|
+
export const getSignatureVariant = async () => {
|
|
496
|
+
const { integrationType } = getConfig();
|
|
497
|
+
|
|
498
|
+
if (integrationType === INTEGRATION_TYPE.ONBOARDING) {
|
|
499
|
+
return getSignatureVariantOnBoarding();
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
if (integrationType === INTEGRATION_TYPE.ONPREMISE) {
|
|
503
|
+
return getSignatureVariantOnPremise();
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
throw new ApacuanaAPIError(
|
|
507
|
+
`Unsupported integration type: ${integrationType}`,
|
|
508
|
+
400,
|
|
509
|
+
"UNSUPPORTED_INTEGRATION_TYPE"
|
|
510
|
+
);
|
|
511
|
+
};
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* Elimina la variante de firma del firmante.
|
|
515
|
+
* @returns {Promise<object>} Una promesa que resuelve a un objeto con el resultado de la operación.
|
|
516
|
+
* @throws {ApacuanaAPIError} Si la llamada a la API falla o el tipo de integración no es soportado.
|
|
517
|
+
*/
|
|
518
|
+
export const deleteSignatureVariant = async () => {
|
|
519
|
+
const { integrationType } = getConfig();
|
|
520
|
+
|
|
521
|
+
if (integrationType === INTEGRATION_TYPE.ONBOARDING) {
|
|
522
|
+
return deleteSignatureVariantOnBoarding();
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
if (integrationType === INTEGRATION_TYPE.ONPREMISE) {
|
|
526
|
+
return deleteSignatureVariantOnPremise();
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
throw new ApacuanaAPIError(
|
|
530
|
+
`Unsupported integration type: ${integrationType}`,
|
|
531
|
+
400,
|
|
532
|
+
"UNSUPPORTED_INTEGRATION_TYPE"
|
|
533
|
+
);
|
|
534
|
+
};
|
package/src/index.js
CHANGED
|
@@ -2,8 +2,21 @@ import { setConfig, getConfig, close } from "./config/index";
|
|
|
2
2
|
import { initHttpClient, setAuthToken } from "./utils/httpClient";
|
|
3
3
|
import getCustomer from "./api/users";
|
|
4
4
|
import { requestRevocation, getRevocationReasons } from "./api/revocations";
|
|
5
|
-
import {
|
|
6
|
-
|
|
5
|
+
import {
|
|
6
|
+
generateCert,
|
|
7
|
+
getCertStatus,
|
|
8
|
+
getCertTypes,
|
|
9
|
+
getRequerimentsByTypeUser,
|
|
10
|
+
} from "./api/certs";
|
|
11
|
+
import {
|
|
12
|
+
addSigner,
|
|
13
|
+
deleteSignatureVariant,
|
|
14
|
+
getDigest,
|
|
15
|
+
getDocs,
|
|
16
|
+
getSignatureVariant,
|
|
17
|
+
signDocument,
|
|
18
|
+
uploadSignatureVariant,
|
|
19
|
+
} from "./api/signatures";
|
|
7
20
|
|
|
8
21
|
const apacuana = {
|
|
9
22
|
/**
|
|
@@ -50,6 +63,11 @@ const apacuana = {
|
|
|
50
63
|
signDocument,
|
|
51
64
|
getDigest,
|
|
52
65
|
getRevocationReasons,
|
|
66
|
+
uploadSignatureVariant,
|
|
67
|
+
getSignatureVariant,
|
|
68
|
+
deleteSignatureVariant,
|
|
69
|
+
getCertTypes,
|
|
70
|
+
getRequerimentsByTypeUser,
|
|
53
71
|
};
|
|
54
72
|
|
|
55
73
|
export default apacuana;
|
package/src/utils/httpClient.js
CHANGED
|
@@ -27,7 +27,6 @@ export const initHttpClient = () => {
|
|
|
27
27
|
axiosInstance = axios.create({
|
|
28
28
|
baseURL: config.apiUrl,
|
|
29
29
|
headers: {
|
|
30
|
-
"Content-Type": "application/json",
|
|
31
30
|
"APACUANA-API-Key": config.apiKey,
|
|
32
31
|
"APACUANA-SECRET-Key": config.secretKey,
|
|
33
32
|
// La cabecera Authorization se deja vacía al inicio.
|
|
@@ -41,9 +40,12 @@ export const initHttpClient = () => {
|
|
|
41
40
|
(response) => {
|
|
42
41
|
// Maneja errores lógicos de la API (si el backend devuelve status 2xx con { success: false })
|
|
43
42
|
if (response.data && response.data.success === false) {
|
|
43
|
+
const errorMessage =
|
|
44
|
+
response.data?.msg ||
|
|
45
|
+
response.data?.message ||
|
|
46
|
+
"Error lógico desconocido desde la API.";
|
|
44
47
|
throw new ApacuanaAPIError(
|
|
45
|
-
|
|
46
|
-
"Error lógico en la respuesta de la API (status 2xx)",
|
|
48
|
+
errorMessage,
|
|
47
49
|
response.status,
|
|
48
50
|
response.data.code || "LOGICAL_API_ERROR"
|
|
49
51
|
);
|
|
@@ -60,10 +62,12 @@ export const initHttpClient = () => {
|
|
|
60
62
|
|
|
61
63
|
if (error.response) {
|
|
62
64
|
const { status, data } = error.response;
|
|
65
|
+
const errorMessage =
|
|
66
|
+
data?.msg || data?.message || "Error desconocido desde la API.";
|
|
63
67
|
throw new ApacuanaAPIError(
|
|
64
|
-
|
|
68
|
+
errorMessage,
|
|
65
69
|
status,
|
|
66
|
-
data
|
|
70
|
+
data?.code || "API_ERROR"
|
|
67
71
|
);
|
|
68
72
|
}
|
|
69
73
|
if (error.request) {
|
|
@@ -122,12 +126,21 @@ export const httpRequest = async (path, data = {}, method = "POST") => {
|
|
|
122
126
|
);
|
|
123
127
|
}
|
|
124
128
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
const dataToSend = {
|
|
128
|
-
...data,
|
|
129
|
-
};
|
|
129
|
+
let dataToSend;
|
|
130
|
+
const requestConfig = {};
|
|
130
131
|
|
|
132
|
+
const hasFile = Object.values(data).some(
|
|
133
|
+
(value) => typeof File !== "undefined" && value instanceof File
|
|
134
|
+
);
|
|
135
|
+
if (hasFile) {
|
|
136
|
+
const formData = new FormData();
|
|
137
|
+
Object.keys(data).forEach((key) => {
|
|
138
|
+
formData.append(key, data[key]);
|
|
139
|
+
});
|
|
140
|
+
dataToSend = formData;
|
|
141
|
+
} else {
|
|
142
|
+
dataToSend = { ...data };
|
|
143
|
+
}
|
|
131
144
|
try {
|
|
132
145
|
let response;
|
|
133
146
|
switch (method.toUpperCase()) {
|
|
@@ -135,13 +148,16 @@ export const httpRequest = async (path, data = {}, method = "POST") => {
|
|
|
135
148
|
response = await axiosInstance.get(path, { params: dataToSend });
|
|
136
149
|
break;
|
|
137
150
|
case "POST":
|
|
138
|
-
response = await axiosInstance.post(path, dataToSend);
|
|
151
|
+
response = await axiosInstance.post(path, dataToSend, requestConfig);
|
|
139
152
|
break;
|
|
140
153
|
case "PUT":
|
|
141
|
-
response = await axiosInstance.put(path, dataToSend);
|
|
154
|
+
response = await axiosInstance.put(path, dataToSend, requestConfig);
|
|
142
155
|
break;
|
|
143
156
|
case "DELETE":
|
|
144
|
-
response = await axiosInstance.delete(path, {
|
|
157
|
+
response = await axiosInstance.delete(path, {
|
|
158
|
+
data: dataToSend,
|
|
159
|
+
...requestConfig,
|
|
160
|
+
});
|
|
145
161
|
break;
|
|
146
162
|
default:
|
|
147
163
|
throw new ApacuanaAPIError(
|
|
@@ -151,14 +167,8 @@ export const httpRequest = async (path, data = {}, method = "POST") => {
|
|
|
151
167
|
);
|
|
152
168
|
}
|
|
153
169
|
|
|
154
|
-
// Si la promesa se resolvió, la respuesta es exitosa.
|
|
155
|
-
// El interceptor ya manejó los errores 4xx/5xx y los errores lógicos 2xx.
|
|
156
|
-
// eslint-disable-next-line no-console
|
|
157
170
|
return response.data;
|
|
158
171
|
} catch (error) {
|
|
159
|
-
// Si la petición falla, el interceptor ya procesó el error.
|
|
160
|
-
// Simplemente relanzamos el error para que sea capturado por la función de API.
|
|
161
|
-
// eslint-disable-next-line no-console
|
|
162
172
|
console.error(`[HTTP Client] Fallo en la petición a ${path}:`, error);
|
|
163
173
|
throw error;
|
|
164
174
|
}
|
package/tests/api/certs.test.js
CHANGED
|
@@ -2,7 +2,12 @@ import { getConfig } from "../../src/config/index";
|
|
|
2
2
|
import { httpRequest } from "../../src/utils/httpClient";
|
|
3
3
|
import helpers from "../../src/utils/helpers";
|
|
4
4
|
import { ApacuanaAPIError } from "../../src/errors";
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
generateCert,
|
|
7
|
+
getCertStatus,
|
|
8
|
+
getCertTypes,
|
|
9
|
+
getRequerimentsByTypeUser,
|
|
10
|
+
} from "../../src/api/certs";
|
|
6
11
|
import { INTEGRATION_TYPE } from "../../src/utils/constant";
|
|
7
12
|
|
|
8
13
|
jest.mock("../../src/config/index");
|
|
@@ -91,4 +96,111 @@ describe("Certificate API - certs.js", () => {
|
|
|
91
96
|
expect(result).toEqual({ status: "VALID", success: true });
|
|
92
97
|
});
|
|
93
98
|
});
|
|
99
|
+
|
|
100
|
+
describe("getCertTypes", () => {
|
|
101
|
+
it("should call getCertTypesOnBoarding for ONBOARDING integration", async () => {
|
|
102
|
+
getConfig.mockReturnValue({
|
|
103
|
+
integrationType: INTEGRATION_TYPE.ONBOARDING,
|
|
104
|
+
});
|
|
105
|
+
const mockApiResponse = { types: [{ id: "1", name: "Type 1" }] };
|
|
106
|
+
httpRequest.mockResolvedValue(mockApiResponse);
|
|
107
|
+
|
|
108
|
+
const result = await getCertTypes();
|
|
109
|
+
|
|
110
|
+
expect(httpRequest).toHaveBeenCalledWith(
|
|
111
|
+
"services/api/customer/typeusers",
|
|
112
|
+
{},
|
|
113
|
+
"GET"
|
|
114
|
+
);
|
|
115
|
+
expect(result).toEqual({ types: mockApiResponse.types, success: true });
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("should throw an error for ONPREMISE integration", async () => {
|
|
119
|
+
getConfig.mockReturnValue({
|
|
120
|
+
integrationType: INTEGRATION_TYPE.ONPREMISE,
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
await expect(getCertTypes()).rejects.toThrow(
|
|
124
|
+
new ApacuanaAPIError(
|
|
125
|
+
"Getting certificate types is not supported for integration type: ONPREMISE",
|
|
126
|
+
501,
|
|
127
|
+
"NOT_IMPLEMENTED"
|
|
128
|
+
)
|
|
129
|
+
);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("should throw an error for an unsupported integration type", async () => {
|
|
133
|
+
getConfig.mockReturnValue({ integrationType: "INVALID_TYPE" });
|
|
134
|
+
|
|
135
|
+
await expect(getCertTypes()).rejects.toThrow(
|
|
136
|
+
new ApacuanaAPIError(
|
|
137
|
+
"Unsupported integration type: INVALID_TYPE",
|
|
138
|
+
400,
|
|
139
|
+
"UNSUPPORTED_INTEGRATION_TYPE"
|
|
140
|
+
)
|
|
141
|
+
);
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
describe("getRequerimentsByTypeUser", () => {
|
|
146
|
+
it("should call getRequerimentsByTypeUserOnBoarding for ONBOARDING integration", async () => {
|
|
147
|
+
getConfig.mockReturnValue({
|
|
148
|
+
integrationType: INTEGRATION_TYPE.ONBOARDING,
|
|
149
|
+
});
|
|
150
|
+
const mockApiResponse = {
|
|
151
|
+
requirements: [{ id: "1", description: "Requirement 1" }],
|
|
152
|
+
};
|
|
153
|
+
httpRequest.mockResolvedValue(mockApiResponse);
|
|
154
|
+
|
|
155
|
+
const result = await getRequerimentsByTypeUser({ type: 1 });
|
|
156
|
+
|
|
157
|
+
expect(httpRequest).toHaveBeenCalledWith(
|
|
158
|
+
"services/api/customer/documentspref?typeuser=1",
|
|
159
|
+
{},
|
|
160
|
+
"GET"
|
|
161
|
+
);
|
|
162
|
+
expect(result).toEqual({
|
|
163
|
+
requirements: mockApiResponse.requirements,
|
|
164
|
+
success: true,
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("should throw an error for ONPREMISE integration", async () => {
|
|
169
|
+
getConfig.mockReturnValue({
|
|
170
|
+
integrationType: INTEGRATION_TYPE.ONPREMISE,
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
await expect(getRequerimentsByTypeUser({ type: 1 })).rejects.toThrow(
|
|
174
|
+
new ApacuanaAPIError(
|
|
175
|
+
"Getting requirements by user type is not supported for integration type: ONPREMISE",
|
|
176
|
+
501,
|
|
177
|
+
"NOT_IMPLEMENTED"
|
|
178
|
+
)
|
|
179
|
+
);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("should throw an error for an unsupported integration type", async () => {
|
|
183
|
+
getConfig.mockReturnValue({ integrationType: "INVALID_TYPE" });
|
|
184
|
+
|
|
185
|
+
await expect(getRequerimentsByTypeUser({ type: 1 })).rejects.toThrow(
|
|
186
|
+
new ApacuanaAPIError(
|
|
187
|
+
"Unsupported integration type: INVALID_TYPE",
|
|
188
|
+
400,
|
|
189
|
+
"UNSUPPORTED_INTEGRATION_TYPE"
|
|
190
|
+
)
|
|
191
|
+
);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it("should throw an error if params are invalid", async () => {
|
|
195
|
+
await expect(getRequerimentsByTypeUser()).rejects.toThrow(
|
|
196
|
+
'The "params" object with a numeric "type" property is required.'
|
|
197
|
+
);
|
|
198
|
+
await expect(getRequerimentsByTypeUser({})).rejects.toThrow(
|
|
199
|
+
'The "params" object with a numeric "type" property is required.'
|
|
200
|
+
);
|
|
201
|
+
await expect(getRequerimentsByTypeUser({ type: "1" })).rejects.toThrow(
|
|
202
|
+
'The "params" object with a numeric "type" property is required.'
|
|
203
|
+
);
|
|
204
|
+
});
|
|
205
|
+
});
|
|
94
206
|
});
|