apacuana-sdk-core 0.5.0 → 0.6.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.
Files changed (38) hide show
  1. package/README.md +90 -55
  2. package/coverage/clover.xml +144 -123
  3. package/coverage/coverage-final.json +6 -6
  4. package/coverage/lcov-report/index.html +27 -27
  5. package/coverage/lcov-report/src/api/certs.js.html +40 -4
  6. package/coverage/lcov-report/src/api/index.html +19 -19
  7. package/coverage/lcov-report/src/api/revocations.js.html +21 -3
  8. package/coverage/lcov-report/src/api/signatures.js.html +123 -18
  9. package/coverage/lcov-report/src/api/users.js.html +24 -3
  10. package/coverage/lcov-report/src/config/index.html +1 -1
  11. package/coverage/lcov-report/src/config/index.js.html +1 -1
  12. package/coverage/lcov-report/src/errors/index.html +1 -1
  13. package/coverage/lcov-report/src/errors/index.js.html +1 -1
  14. package/coverage/lcov-report/src/index.html +1 -1
  15. package/coverage/lcov-report/src/index.js.html +9 -3
  16. package/coverage/lcov-report/src/utils/constant.js.html +3 -3
  17. package/coverage/lcov-report/src/utils/helpers.js.html +143 -8
  18. package/coverage/lcov-report/src/utils/httpClient.js.html +1 -1
  19. package/coverage/lcov-report/src/utils/index.html +15 -15
  20. package/coverage/lcov.info +252 -204
  21. package/dist/api/certs.d.ts +17 -3
  22. package/dist/api/revocations.d.ts +17 -2
  23. package/dist/api/signatures.d.ts +18 -1
  24. package/dist/api/users.d.ts +22 -6
  25. package/dist/index.d.ts +4 -0
  26. package/dist/index.js +335 -22
  27. package/dist/index.js.map +1 -1
  28. package/dist/index.mjs +335 -22
  29. package/dist/index.mjs.map +1 -1
  30. package/dist/utils/helpers.d.ts +2 -0
  31. package/package.json +1 -1
  32. package/src/api/certs.js +14 -2
  33. package/src/api/revocations.js +7 -1
  34. package/src/api/signatures.js +43 -8
  35. package/src/api/users.js +8 -1
  36. package/src/index.js +3 -1
  37. package/src/utils/constant.js +2 -2
  38. package/src/utils/helpers.js +45 -0
@@ -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;
@@ -19,3 +20,4 @@ declare function validateOnBoardingSignerData(signerData: any): void;
19
20
  declare function validateCsr(csr: 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apacuana-sdk-core",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "Core SDK para interacciones con las APIs de Apacuana.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
package/src/api/certs.js CHANGED
@@ -4,6 +4,18 @@ import helpers from "../utils/helpers";
4
4
  import { ApacuanaAPIError } from "../errors";
5
5
  import { INTEGRATION_TYPE } from "../utils/constant";
6
6
 
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
+ */
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
+
7
19
  const generateCertOnBoarding = async (csr = undefined) => {
8
20
  try {
9
21
  const encryptedCSR = helpers.encryptedCsr(csr);
@@ -43,7 +55,7 @@ const generateCertOnPremise = async () => {
43
55
  /**
44
56
  * Generates a digital certificate.
45
57
  * @param {string} [csr] - Certificate Signing Request (CSR).
46
- * @returns {Promise<{cert: string, success: boolean}>} Object with the generated certificate and a success indicator.
58
+ * @returns {Promise<GenerateCertResponse>} Object with the generated certificate and a success indicator.
47
59
  * @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
60
  * @throws {Error} If certificate generation fails for another reason.
49
61
  */
@@ -67,7 +79,7 @@ export const generateCert = async (csr = undefined) => {
67
79
  /**
68
80
  * Gets the user's certificate status.
69
81
  * @param {boolean} [isCertificateInDevice=false] - Indicates if the certificate is already on the device.
70
- * @returns {{status: string, success: boolean}} Object with the certificate status and a success indicator.
82
+ * @returns {GetCertStatusResponse} Object with the certificate status and a success indicator.
71
83
  */
72
84
  export const getCertStatus = (isCertificateInDevice = false) => {
73
85
  const config = getConfig();
@@ -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<object>} Objeto con el estado de la solicitud de revocación.
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.");
@@ -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
- throw new ApacuanaAPIError(
70
- "Document signing is not supported for integration type: ONBOARDING",
71
- 501,
72
- "NOT_IMPLEMENTED"
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<{token: string, userData: object, success: boolean}>} Objeto con el token de sesión, los datos del usuario y un indicador de éxito.
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;
@@ -15,6 +15,6 @@ export const VERIFICATION_STATUS = {
15
15
  };
16
16
 
17
17
  export const INTEGRATION_TYPE = {
18
- ONBOARDING: 'onboarding',
19
- ONPREMISE: 'onpremise',
18
+ ONBOARDING: "ONBOARDING",
19
+ ONPREMISE: "ONPREMISE",
20
20
  };
@@ -212,6 +212,50 @@ const validateGetDigestData = (signData) => {
212
212
  }
213
213
  };
214
214
 
215
+ const validateOnBoardingSignDocumentData = (signData) => {
216
+ if (!signData || typeof signData !== "object") {
217
+ throw new ApacuanaAPIError(
218
+ "Sign data is required and must be an object.",
219
+ 400,
220
+ "INVALID_PARAMETER"
221
+ );
222
+ }
223
+
224
+ const { signature, cert, signedDigest } = signData;
225
+
226
+ if (!signature || typeof signature !== "object" || !signature.id) {
227
+ throw new ApacuanaAPIError(
228
+ "Signature object with an 'id' property is required.",
229
+ 400,
230
+ "INVALID_PARAMETER"
231
+ );
232
+ }
233
+
234
+ if (!Array.isArray(signature.positions)) {
235
+ throw new ApacuanaAPIError(
236
+ "Signature 'positions' must be an array.",
237
+ 400,
238
+ "INVALID_PARAMETER"
239
+ );
240
+ }
241
+
242
+ if (!cert || typeof cert !== "string") {
243
+ throw new ApacuanaAPIError(
244
+ "Certificate 'cert' is required and must be a string.",
245
+ 400,
246
+ "INVALID_PARAMETER"
247
+ );
248
+ }
249
+
250
+ if (!signedDigest || typeof signedDigest !== "string") {
251
+ throw new ApacuanaAPIError(
252
+ "Signed digest 'signedDigest' is required and must be a string.",
253
+ 400,
254
+ "INVALID_PARAMETER"
255
+ );
256
+ }
257
+ };
258
+
215
259
  export default {
216
260
  getCertificateStatus,
217
261
  exportPrivateKey,
@@ -221,4 +265,5 @@ export default {
221
265
  validateCsr,
222
266
  validateGetDocsData,
223
267
  validateGetDigestData,
268
+ validateOnBoardingSignDocumentData,
224
269
  };