apacuana-sdk-core 0.5.0 → 0.6.2
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 +128 -55
- package/babel.config.cjs +11 -0
- package/coverage/clover.xml +179 -170
- package/coverage/coverage-final.json +8 -8
- package/coverage/lcov-report/api/index.html +131 -0
- package/coverage/lcov-report/api/signatures.js.html +1093 -0
- package/coverage/lcov-report/api/users.js.html +292 -0
- package/coverage/lcov-report/config/index.html +116 -0
- package/coverage/lcov-report/config/index.js.html +208 -0
- package/coverage/lcov-report/errors/index.html +116 -0
- package/coverage/lcov-report/errors/index.js.html +148 -0
- package/coverage/lcov-report/index.html +26 -26
- package/coverage/lcov-report/src/api/certs.js.html +65 -35
- package/coverage/lcov-report/src/api/index.html +27 -27
- package/coverage/lcov-report/src/api/revocations.js.html +21 -3
- package/coverage/lcov-report/src/api/signatures.js.html +123 -18
- package/coverage/lcov-report/src/api/users.js.html +24 -3
- package/coverage/lcov-report/src/config/index.html +1 -1
- package/coverage/lcov-report/src/config/index.js.html +1 -1
- 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 +9 -3
- package/coverage/lcov-report/src/utils/constant.js.html +3 -3
- package/coverage/lcov-report/src/utils/helpers.js.html +134 -32
- package/coverage/lcov-report/src/utils/httpClient.js.html +6 -72
- package/coverage/lcov-report/src/utils/index.html +20 -20
- package/coverage/lcov-report/utils/constant.js.html +145 -0
- package/coverage/lcov-report/utils/httpClient.js.html +646 -0
- package/coverage/lcov-report/utils/index.html +131 -0
- package/coverage/lcov.info +335 -298
- package/dist/api/certs.d.ts +17 -3
- package/dist/api/revocations.d.ts +17 -2
- package/dist/api/signatures.d.ts +18 -1
- package/dist/api/users.d.ts +22 -6
- package/dist/index.d.ts +4 -0
- package/dist/index.js +616 -242
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +616 -242
- package/dist/index.mjs.map +1 -1
- package/dist/utils/helpers.d.ts +3 -1
- package/jest.config.cjs +6 -1
- package/package.json +12 -7
- package/rollup.config.js +1 -1
- package/src/api/certs.js +21 -11
- package/src/api/revocations.js +7 -1
- package/src/api/signatures.js +43 -8
- package/src/api/users.js +8 -1
- package/src/index.js +3 -1
- package/src/utils/constant.js +2 -2
- package/src/utils/helpers.js +56 -22
- package/src/utils/httpClient.js +0 -22
- package/tests/api/certs.test.js +30 -48
- package/tsconfig.json +2 -1
- package/.babelrc +0 -3
package/dist/utils/helpers.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ declare namespace _default {
|
|
|
7
7
|
export { validateCsr };
|
|
8
8
|
export { validateGetDocsData };
|
|
9
9
|
export { validateGetDigestData };
|
|
10
|
+
export { validateOnBoardingSignDocumentData };
|
|
10
11
|
}
|
|
11
12
|
export default _default;
|
|
12
13
|
declare function getCertificateStatus(userData: any, certificateInDevice: any): string;
|
|
@@ -16,6 +17,7 @@ declare function encryptedCsr(csr: any, encryptKey?: string): {
|
|
|
16
17
|
csr: any;
|
|
17
18
|
};
|
|
18
19
|
declare function validateOnBoardingSignerData(signerData: any): void;
|
|
19
|
-
declare function validateCsr(
|
|
20
|
+
declare function validateCsr(encryptedCSR: any): void;
|
|
20
21
|
declare function validateGetDocsData(data: any): void;
|
|
21
22
|
declare function validateGetDigestData(signData: any): void;
|
|
23
|
+
declare function validateOnBoardingSignDocumentData(signData: any): void;
|
package/jest.config.cjs
CHANGED
|
@@ -3,5 +3,10 @@ module.exports = {
|
|
|
3
3
|
testEnvironment: "node", // Para tests de backend, si tu SDK es universal
|
|
4
4
|
collectCoverage: true,
|
|
5
5
|
coverageDirectory: "coverage",
|
|
6
|
-
|
|
6
|
+
transformIgnorePatterns: [
|
|
7
|
+
"/node_modules/(?!react-native-get-random-values/)",
|
|
8
|
+
],
|
|
9
|
+
transform: {
|
|
10
|
+
"^.+\\.jsx?$": "babel-jest",
|
|
11
|
+
},
|
|
7
12
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "apacuana-sdk-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.2",
|
|
4
4
|
"description": "Core SDK para interacciones con las APIs de Apacuana.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -25,20 +25,25 @@
|
|
|
25
25
|
"author": "Gega",
|
|
26
26
|
"license": "ISC",
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"@babel/core": "^7.24.
|
|
29
|
-
"@babel/
|
|
28
|
+
"@babel/core": "^7.24.5",
|
|
29
|
+
"@babel/plugin-transform-class-properties": "^7.24.1",
|
|
30
|
+
"@babel/plugin-transform-private-methods": "^7.24.1",
|
|
31
|
+
"@babel/plugin-transform-private-property-in-object": "^7.24.1",
|
|
32
|
+
"@babel/preset-env": "^7.24.5",
|
|
33
|
+
"@babel/preset-flow": "^7.24.1",
|
|
30
34
|
"@rollup/plugin-babel": "^6.0.4",
|
|
31
|
-
"@rollup/plugin-commonjs": "^
|
|
35
|
+
"@rollup/plugin-commonjs": "^28.0.6",
|
|
32
36
|
"@rollup/plugin-json": "^6.1.0",
|
|
33
|
-
"@rollup/plugin-node-resolve": "^
|
|
37
|
+
"@rollup/plugin-node-resolve": "^16.0.1",
|
|
38
|
+
"babel-jest": "^29.7.0",
|
|
34
39
|
"eslint": "^8.57.0",
|
|
35
40
|
"eslint-config-airbnb-base": "^15.0.0",
|
|
36
41
|
"eslint-config-prettier": "^10.1.8",
|
|
37
42
|
"eslint-plugin-import": "^2.29.1",
|
|
38
43
|
"jest": "^29.7.0",
|
|
39
|
-
"rollup": "^4.18.0",
|
|
40
44
|
"rimraf": "^5.0.7",
|
|
41
|
-
"
|
|
45
|
+
"rollup": "^4.17.2",
|
|
46
|
+
"typescript": "^5.9.2"
|
|
42
47
|
},
|
|
43
48
|
"engines": {
|
|
44
49
|
"node": ">=14.0.0"
|
package/rollup.config.js
CHANGED
package/src/api/certs.js
CHANGED
|
@@ -4,17 +4,27 @@ import helpers from "../utils/helpers";
|
|
|
4
4
|
import { ApacuanaAPIError } from "../errors";
|
|
5
5
|
import { INTEGRATION_TYPE } from "../utils/constant";
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {object} GenerateCertResponse
|
|
9
|
+
* @property {string} cert - El certificado generado en formato string.
|
|
10
|
+
* @property {boolean} success - Indica si la operación fue exitosa.
|
|
11
|
+
*/
|
|
10
12
|
|
|
13
|
+
/**
|
|
14
|
+
* @typedef {object} GetCertStatusResponse
|
|
15
|
+
* @property {string} status - El estado actual del certificado del usuario.
|
|
16
|
+
* @property {boolean} success - Indica si la operación fue exitosa.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
const generateCertOnBoarding = async (encryptedCSR = undefined) => {
|
|
20
|
+
try {
|
|
11
21
|
const response = await httpRequest(
|
|
12
22
|
"services/api/register/certificate",
|
|
13
23
|
encryptedCSR,
|
|
14
24
|
"POST"
|
|
15
25
|
);
|
|
16
|
-
const { cert } = response;
|
|
17
|
-
if (!cert) {
|
|
26
|
+
const { cert, certifiedid } = response;
|
|
27
|
+
if (!cert || !certifiedid) {
|
|
18
28
|
throw new ApacuanaAPIError(
|
|
19
29
|
"The API response does not contain the certificate.",
|
|
20
30
|
response.status,
|
|
@@ -22,7 +32,7 @@ const generateCertOnBoarding = async (csr = undefined) => {
|
|
|
22
32
|
);
|
|
23
33
|
}
|
|
24
34
|
|
|
25
|
-
return { cert, success: true };
|
|
35
|
+
return { cert, certifiedid, success: true };
|
|
26
36
|
} catch (error) {
|
|
27
37
|
// The captured error is re-thrown, which will already be of type ApacuanaAPIError or a native Error.
|
|
28
38
|
if (error instanceof ApacuanaAPIError) {
|
|
@@ -43,15 +53,15 @@ const generateCertOnPremise = async () => {
|
|
|
43
53
|
/**
|
|
44
54
|
* Generates a digital certificate.
|
|
45
55
|
* @param {string} [csr] - Certificate Signing Request (CSR).
|
|
46
|
-
* @returns {Promise<
|
|
56
|
+
* @returns {Promise<GenerateCertResponse>} Object with the generated certificate and a success indicator.
|
|
47
57
|
* @throws {ApacuanaAPIError} If the CSR is invalid, if the API response does not contain the certificate, or if the integration type is not supported.
|
|
48
58
|
* @throws {Error} If certificate generation fails for another reason.
|
|
49
59
|
*/
|
|
50
|
-
export const generateCert = async (
|
|
60
|
+
export const generateCert = async (encryptedCSR = undefined) => {
|
|
51
61
|
const { integrationType } = getConfig();
|
|
52
|
-
helpers.validateCsr(
|
|
62
|
+
helpers.validateCsr(encryptedCSR);
|
|
53
63
|
if (integrationType === INTEGRATION_TYPE.ONBOARDING) {
|
|
54
|
-
return generateCertOnBoarding(
|
|
64
|
+
return generateCertOnBoarding(encryptedCSR);
|
|
55
65
|
}
|
|
56
66
|
if (integrationType === INTEGRATION_TYPE.ONPREMISE) {
|
|
57
67
|
return generateCertOnPremise();
|
|
@@ -67,7 +77,7 @@ export const generateCert = async (csr = undefined) => {
|
|
|
67
77
|
/**
|
|
68
78
|
* Gets the user's certificate status.
|
|
69
79
|
* @param {boolean} [isCertificateInDevice=false] - Indicates if the certificate is already on the device.
|
|
70
|
-
* @returns {
|
|
80
|
+
* @returns {GetCertStatusResponse} Object with the certificate status and a success indicator.
|
|
71
81
|
*/
|
|
72
82
|
export const getCertStatus = (isCertificateInDevice = false) => {
|
|
73
83
|
const config = getConfig();
|
package/src/api/revocations.js
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
import { httpRequest } from "../utils/httpClient";
|
|
2
2
|
import { ApacuanaAPIError } from "../errors/index";
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* @typedef {object} RequestRevocationResponse
|
|
6
|
+
* @property {string} revocationStatus - El estado de la solicitud de revocación (e.g., "pending").
|
|
7
|
+
* @property {string|number} requestId - El ID de la solicitud de revocación.
|
|
8
|
+
*/
|
|
9
|
+
|
|
4
10
|
/**
|
|
5
11
|
* Simula la solicitud de revocación de un certificado.
|
|
6
12
|
* @param {string} reasonCode - Código o descripción del motivo de la revocación.
|
|
7
|
-
* @returns {Promise<
|
|
13
|
+
* @returns {Promise<RequestRevocationResponse>} Objeto con el estado de la solicitud de revocación.
|
|
8
14
|
*/
|
|
9
15
|
const requestRevocation = async (reasonCode) => {
|
|
10
16
|
console.log("-> Función 'requestRevocation' ejecutada.");
|
package/src/api/signatures.js
CHANGED
|
@@ -61,16 +61,50 @@ import { INTEGRATION_TYPE } from "../utils/constant";
|
|
|
61
61
|
* @property {boolean} success - Indica si la operación fue exitosa.
|
|
62
62
|
*/
|
|
63
63
|
|
|
64
|
+
/**
|
|
65
|
+
* @typedef {object} SignDocumentData
|
|
66
|
+
* @property {object} signature - Objeto con información de la firma.
|
|
67
|
+
* @property {string} signature.id - ID de la firma.
|
|
68
|
+
* @property {Array<object>} signature.positions - Posiciones de la firma.
|
|
69
|
+
* @property {string} cert - Certificado en base64.
|
|
70
|
+
* @property {string} signedDigest - Digest firmado.
|
|
71
|
+
*/
|
|
72
|
+
|
|
64
73
|
// =================================================================
|
|
65
74
|
// Internal Functions
|
|
66
75
|
// =================================================================
|
|
67
76
|
|
|
68
|
-
const signDocumentOnBoarding = async () => {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
77
|
+
const signDocumentOnBoarding = async (signData) => {
|
|
78
|
+
helpers.validateOnBoardingSignDocumentData(signData);
|
|
79
|
+
const { signature, cert, signedDigest } = signData;
|
|
80
|
+
try {
|
|
81
|
+
const signBody = {
|
|
82
|
+
positions: JSON.stringify(
|
|
83
|
+
signature.positions.map((p) => ({
|
|
84
|
+
x: p.x,
|
|
85
|
+
y: p.y,
|
|
86
|
+
page: p.page,
|
|
87
|
+
status: 1,
|
|
88
|
+
}))
|
|
89
|
+
),
|
|
90
|
+
publickey: cert,
|
|
91
|
+
signeddigest: signedDigest,
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const response = await httpRequest(
|
|
95
|
+
`services/api/documents/sign/${signature.id}`,
|
|
96
|
+
signBody,
|
|
97
|
+
"PUT"
|
|
98
|
+
);
|
|
99
|
+
return response;
|
|
100
|
+
} catch (error) {
|
|
101
|
+
if (error instanceof ApacuanaAPIError) {
|
|
102
|
+
throw error;
|
|
103
|
+
}
|
|
104
|
+
throw new ApacuanaAPIError(
|
|
105
|
+
`Failed to sign document: ${error.message || "Unknown error"}`
|
|
106
|
+
);
|
|
107
|
+
}
|
|
74
108
|
};
|
|
75
109
|
|
|
76
110
|
const signDocumentOnPremise = async () => {
|
|
@@ -192,14 +226,15 @@ const getDocsOnBoarding = async (data) => {
|
|
|
192
226
|
/**
|
|
193
227
|
* Firma un documento PDF con un certificado digital.
|
|
194
228
|
* NOTA: Esta función no está implementada actualmente para ningún tipo de integración.
|
|
229
|
+
* @param {SignDocumentData} signData - Datos necesarios para la firma.
|
|
195
230
|
* @returns {Promise<object>}
|
|
196
231
|
* @throws {ApacuanaAPIError} Arroja un error 'NOT_IMPLEMENTED' para cualquier tipo de integración.
|
|
197
232
|
*/
|
|
198
|
-
export const signDocument = async () => {
|
|
233
|
+
export const signDocument = async (signData) => {
|
|
199
234
|
const { integrationType } = getConfig();
|
|
200
235
|
|
|
201
236
|
if (integrationType === INTEGRATION_TYPE.ONBOARDING) {
|
|
202
|
-
return signDocumentOnBoarding();
|
|
237
|
+
return signDocumentOnBoarding(signData);
|
|
203
238
|
}
|
|
204
239
|
|
|
205
240
|
if (integrationType === INTEGRATION_TYPE.ONPREMISE) {
|
package/src/api/users.js
CHANGED
|
@@ -2,12 +2,19 @@ import { httpRequest } from "../utils/httpClient";
|
|
|
2
2
|
import { ApacuanaAPIError } from "../errors/index";
|
|
3
3
|
import { getConfig } from "../config/index";
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {object} GetCustomerResponse
|
|
7
|
+
* @property {string} token - El token de sesión del usuario.
|
|
8
|
+
* @property {object} userData - Los datos del usuario obtenidos.
|
|
9
|
+
* @property {boolean} success - Indica si la operación fue exitosa.
|
|
10
|
+
*/
|
|
11
|
+
|
|
5
12
|
/**
|
|
6
13
|
* Obtiene el token de un usuario a través de una petición POST.
|
|
7
14
|
* Este método es útil para endpoints que requieren datos en el cuerpo de la petición
|
|
8
15
|
* para buscar un usuario, como un ID de sesión o un token de acceso.
|
|
9
16
|
*
|
|
10
|
-
* @returns {Promise<
|
|
17
|
+
* @returns {Promise<GetCustomerResponse>} Objeto con el token de sesión, los datos del usuario y un indicador de éxito.
|
|
11
18
|
* @throws {Error} Si los parámetros de entrada son inválidos.
|
|
12
19
|
* @throws {ApacuanaAPIError} Si ocurre un error en la API de Apacuana.
|
|
13
20
|
*/
|
package/src/index.js
CHANGED
|
@@ -3,7 +3,7 @@ import { initHttpClient, setAuthToken } from "./utils/httpClient";
|
|
|
3
3
|
import getCustomer from "./api/users";
|
|
4
4
|
import requestRevocation from "./api/revocations";
|
|
5
5
|
import { generateCert, getCertStatus } from "./api/certs";
|
|
6
|
-
import { addSigner, getDocs } from "./api/signatures";
|
|
6
|
+
import { addSigner, getDigest, getDocs, signDocument } from "./api/signatures";
|
|
7
7
|
|
|
8
8
|
const apacuana = {
|
|
9
9
|
/**
|
|
@@ -66,6 +66,8 @@ const apacuana = {
|
|
|
66
66
|
addSigner,
|
|
67
67
|
getDocs,
|
|
68
68
|
generateCert,
|
|
69
|
+
signDocument,
|
|
70
|
+
getDigest,
|
|
69
71
|
};
|
|
70
72
|
|
|
71
73
|
export default apacuana;
|
package/src/utils/constant.js
CHANGED
package/src/utils/helpers.js
CHANGED
|
@@ -143,29 +143,18 @@ const validateOnBoardingSignerData = (signerData) => {
|
|
|
143
143
|
});
|
|
144
144
|
};
|
|
145
145
|
|
|
146
|
-
const validateCsr = (
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
if (typeof csr !== "string" || csr.trim() === "") {
|
|
158
|
-
throw new ApacuanaAPIError(
|
|
159
|
-
"El CSR debe ser una cadena de texto válida y no puede estar vacía.",
|
|
160
|
-
400,
|
|
161
|
-
"INVALID_PARAMETER_FORMAT"
|
|
162
|
-
);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
if (!base64Regex.test(csr)) {
|
|
146
|
+
const validateCsr = (encryptedCSR) => {
|
|
147
|
+
if (
|
|
148
|
+
!encryptedCSR ||
|
|
149
|
+
typeof encryptedCSR !== "object" ||
|
|
150
|
+
!encryptedCSR.csr ||
|
|
151
|
+
typeof encryptedCSR.csr !== "string" ||
|
|
152
|
+
encryptedCSR.csr.trim() === ""
|
|
153
|
+
) {
|
|
166
154
|
throw new ApacuanaAPIError(
|
|
167
|
-
"
|
|
168
|
-
|
|
155
|
+
'The "encryptedCSR" parameter is required and must be an object ' +
|
|
156
|
+
'with a non-empty "csr" string property.',
|
|
157
|
+
400, // Bad Request
|
|
169
158
|
"INVALID_PARAMETER_FORMAT"
|
|
170
159
|
);
|
|
171
160
|
}
|
|
@@ -212,6 +201,50 @@ const validateGetDigestData = (signData) => {
|
|
|
212
201
|
}
|
|
213
202
|
};
|
|
214
203
|
|
|
204
|
+
const validateOnBoardingSignDocumentData = (signData) => {
|
|
205
|
+
if (!signData || typeof signData !== "object") {
|
|
206
|
+
throw new ApacuanaAPIError(
|
|
207
|
+
"Sign data is required and must be an object.",
|
|
208
|
+
400,
|
|
209
|
+
"INVALID_PARAMETER"
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const { signature, cert, signedDigest } = signData;
|
|
214
|
+
|
|
215
|
+
if (!signature || typeof signature !== "object" || !signature.id) {
|
|
216
|
+
throw new ApacuanaAPIError(
|
|
217
|
+
"Signature object with an 'id' property is required.",
|
|
218
|
+
400,
|
|
219
|
+
"INVALID_PARAMETER"
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (!Array.isArray(signature.positions)) {
|
|
224
|
+
throw new ApacuanaAPIError(
|
|
225
|
+
"Signature 'positions' must be an array.",
|
|
226
|
+
400,
|
|
227
|
+
"INVALID_PARAMETER"
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (!cert || typeof cert !== "string") {
|
|
232
|
+
throw new ApacuanaAPIError(
|
|
233
|
+
"Certificate 'cert' is required and must be a string.",
|
|
234
|
+
400,
|
|
235
|
+
"INVALID_PARAMETER"
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (!signedDigest || typeof signedDigest !== "string") {
|
|
240
|
+
throw new ApacuanaAPIError(
|
|
241
|
+
"Signed digest 'signedDigest' is required and must be a string.",
|
|
242
|
+
400,
|
|
243
|
+
"INVALID_PARAMETER"
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
|
|
215
248
|
export default {
|
|
216
249
|
getCertificateStatus,
|
|
217
250
|
exportPrivateKey,
|
|
@@ -221,4 +254,5 @@ export default {
|
|
|
221
254
|
validateCsr,
|
|
222
255
|
validateGetDocsData,
|
|
223
256
|
validateGetDigestData,
|
|
257
|
+
validateOnBoardingSignDocumentData,
|
|
224
258
|
};
|
package/src/utils/httpClient.js
CHANGED
|
@@ -83,12 +83,6 @@ export const initHttpClient = () => {
|
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
85
|
);
|
|
86
|
-
|
|
87
|
-
// eslint-disable-next-line no-console
|
|
88
|
-
console.log(
|
|
89
|
-
"[HTTP Client] Cliente HTTP (Axios) REAL inicializado con URL base:",
|
|
90
|
-
config.apiUrl
|
|
91
|
-
);
|
|
92
86
|
};
|
|
93
87
|
|
|
94
88
|
/**
|
|
@@ -109,9 +103,6 @@ export const setAuthToken = (token) => {
|
|
|
109
103
|
// Actualizamos la cabecera Authorization de la instancia de Axios.
|
|
110
104
|
axiosInstance.defaults.headers.common.Authorization = `Bearer ${token}`;
|
|
111
105
|
// eslint-disable-next-line no-console
|
|
112
|
-
console.log(
|
|
113
|
-
"[HTTP Client] Cabecera Authorization actualizada con nuevo token."
|
|
114
|
-
);
|
|
115
106
|
};
|
|
116
107
|
|
|
117
108
|
/**
|
|
@@ -137,18 +128,6 @@ export const httpRequest = async (path, data = {}, method = "POST") => {
|
|
|
137
128
|
...data,
|
|
138
129
|
};
|
|
139
130
|
|
|
140
|
-
// eslint-disable-next-line no-console
|
|
141
|
-
console.log(
|
|
142
|
-
`[HTTP Client] Realizando petición ${method} a: ${axiosInstance.defaults.baseURL}${path}`
|
|
143
|
-
);
|
|
144
|
-
// eslint-disable-next-line no-console
|
|
145
|
-
console.log(
|
|
146
|
-
"[HTTP Client] Headers (via Axios config):",
|
|
147
|
-
axiosInstance.defaults.headers
|
|
148
|
-
);
|
|
149
|
-
// eslint-disable-next-line no-console
|
|
150
|
-
console.log("[HTTP Client] Datos enviados:", dataToSend);
|
|
151
|
-
|
|
152
131
|
try {
|
|
153
132
|
let response;
|
|
154
133
|
switch (method.toUpperCase()) {
|
|
@@ -175,7 +154,6 @@ export const httpRequest = async (path, data = {}, method = "POST") => {
|
|
|
175
154
|
// Si la promesa se resolvió, la respuesta es exitosa.
|
|
176
155
|
// El interceptor ya manejó los errores 4xx/5xx y los errores lógicos 2xx.
|
|
177
156
|
// eslint-disable-next-line no-console
|
|
178
|
-
console.log(`[HTTP Client] Respuesta exitosa de ${path}:`, response.data);
|
|
179
157
|
return response.data;
|
|
180
158
|
} catch (error) {
|
|
181
159
|
// Si la petición falla, el interceptor ya procesó el error.
|
package/tests/api/certs.test.js
CHANGED
|
@@ -15,42 +15,38 @@ describe("Certificate API - certs.js", () => {
|
|
|
15
15
|
});
|
|
16
16
|
|
|
17
17
|
describe("generateCert", () => {
|
|
18
|
-
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
jest.clearAllMocks();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
// TEST 1: Probar el camino feliz de ONBOARDING
|
|
23
|
+
it("should call generateCertOnBoarding for ONBOARDING integration", async () => {
|
|
19
24
|
getConfig.mockReturnValue({
|
|
20
25
|
integrationType: INTEGRATION_TYPE.ONBOARDING,
|
|
21
26
|
});
|
|
22
|
-
|
|
23
|
-
|
|
27
|
+
const mockEncryptedCsr = { csr: "onboarding-csr" };
|
|
28
|
+
const mockApiResponse = {
|
|
29
|
+
cert: "onboarding-cert",
|
|
30
|
+
certifiedid: "onboarding-certified-id",
|
|
31
|
+
};
|
|
32
|
+
httpRequest.mockResolvedValue(mockApiResponse);
|
|
24
33
|
|
|
25
|
-
const result = await generateCert(
|
|
34
|
+
const result = await generateCert(mockEncryptedCsr);
|
|
26
35
|
|
|
27
|
-
expect(helpers.validateCsr).toHaveBeenCalledWith("some-csr");
|
|
28
|
-
expect(helpers.encryptedCsr).toHaveBeenCalledWith("some-csr");
|
|
29
36
|
expect(httpRequest).toHaveBeenCalledWith(
|
|
30
|
-
"services/api/register/certificate",
|
|
31
|
-
|
|
37
|
+
"services/api/register/certificate", // Endpoint de Onboarding
|
|
38
|
+
mockEncryptedCsr,
|
|
32
39
|
"POST"
|
|
33
40
|
);
|
|
34
|
-
expect(result).toEqual({
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
getConfig.mockReturnValue({
|
|
39
|
-
integrationType: INTEGRATION_TYPE.ONBOARDING,
|
|
41
|
+
expect(result).toEqual({
|
|
42
|
+
cert: "onboarding-cert",
|
|
43
|
+
certifiedid: "onboarding-certified-id",
|
|
44
|
+
success: true,
|
|
40
45
|
});
|
|
41
|
-
helpers.encryptedCsr.mockReturnValue("encrypted-csr");
|
|
42
|
-
httpRequest.mockResolvedValue({ success: false });
|
|
43
|
-
|
|
44
|
-
await expect(generateCert("some-csr")).rejects.toThrow(
|
|
45
|
-
new ApacuanaAPIError(
|
|
46
|
-
"The API response does not contain the certificate.",
|
|
47
|
-
undefined,
|
|
48
|
-
"INVALID_API_RESPONSE"
|
|
49
|
-
)
|
|
50
|
-
);
|
|
51
46
|
});
|
|
52
47
|
|
|
53
|
-
|
|
48
|
+
// TEST 2: Probar el error de ONPREMISE
|
|
49
|
+
it("should throw an error for ONPREMISE integration", async () => {
|
|
54
50
|
getConfig.mockReturnValue({
|
|
55
51
|
integrationType: INTEGRATION_TYPE.ONPREMISE,
|
|
56
52
|
});
|
|
@@ -64,34 +60,20 @@ describe("Certificate API - certs.js", () => {
|
|
|
64
60
|
);
|
|
65
61
|
});
|
|
66
62
|
|
|
67
|
-
|
|
63
|
+
// TEST 3: Probar el error para un tipo inválido
|
|
64
|
+
it("should throw an error for an unsupported integration type", async () => {
|
|
68
65
|
getConfig.mockReturnValue({ integrationType: "INVALID_TYPE" });
|
|
69
|
-
await expect(generateCert("some-csr")).rejects.toThrow(
|
|
70
|
-
"Unsupported integration type: INVALID_TYPE"
|
|
71
|
-
);
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
it("should re-throw ApacuanaAPIError if httpRequest fails", async () => {
|
|
75
|
-
getConfig.mockReturnValue({
|
|
76
|
-
integrationType: INTEGRATION_TYPE.ONBOARDING,
|
|
77
|
-
});
|
|
78
|
-
const apiError = new ApacuanaAPIError("API Error", 500, "API_ERROR");
|
|
79
|
-
httpRequest.mockRejectedValue(apiError);
|
|
80
|
-
|
|
81
|
-
await expect(generateCert("some-csr")).rejects.toThrow(apiError);
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
it("should throw a generic error if something else fails", async () => {
|
|
85
|
-
getConfig.mockReturnValue({
|
|
86
|
-
integrationType: INTEGRATION_TYPE.ONBOARDING,
|
|
87
|
-
});
|
|
88
|
-
const genericError = new Error("Something failed");
|
|
89
|
-
httpRequest.mockRejectedValue(genericError);
|
|
90
66
|
|
|
91
67
|
await expect(generateCert("some-csr")).rejects.toThrow(
|
|
92
|
-
|
|
68
|
+
new ApacuanaAPIError(
|
|
69
|
+
"Unsupported integration type: INVALID_TYPE",
|
|
70
|
+
400,
|
|
71
|
+
"UNSUPPORTED_INTEGRATION_TYPE"
|
|
72
|
+
)
|
|
93
73
|
);
|
|
94
74
|
});
|
|
75
|
+
|
|
76
|
+
// Se eliminan los otros tests que asumían un comportamiento "estándar"
|
|
95
77
|
});
|
|
96
78
|
|
|
97
79
|
describe("getCertStatus", () => {
|
package/tsconfig.json
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
"compilerOptions": {
|
|
3
3
|
"target": "es2020",
|
|
4
4
|
"module": "esnext",
|
|
5
|
+
"lib": ["es2022", "dom"],
|
|
5
6
|
"allowJs": true,
|
|
6
7
|
"declaration": true,
|
|
7
8
|
"emitDeclarationOnly": true,
|
|
@@ -11,5 +12,5 @@
|
|
|
11
12
|
"esModuleInterop": true
|
|
12
13
|
},
|
|
13
14
|
"include": ["src"],
|
|
14
|
-
"exclude": ["
|
|
15
|
+
"exclude": ["nodev_modules", "dist", "tests"]
|
|
15
16
|
}
|
package/.babelrc
DELETED