apacuana-sdk-core 0.6.2 → 0.7.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 (35) hide show
  1. package/README.md +105 -288
  2. package/coverage/clover.xml +142 -136
  3. package/coverage/coverage-final.json +5 -5
  4. package/coverage/lcov-report/index.html +34 -34
  5. package/coverage/lcov-report/src/api/certs.js.html +34 -7
  6. package/coverage/lcov-report/src/api/index.html +19 -19
  7. package/coverage/lcov-report/src/api/revocations.js.html +135 -30
  8. package/coverage/lcov-report/src/api/signatures.js.html +76 -4
  9. package/coverage/lcov-report/src/api/users.js.html +1 -1
  10. package/coverage/lcov-report/src/config/index.html +13 -13
  11. package/coverage/lcov-report/src/config/index.js.html +69 -18
  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 +15 -15
  15. package/coverage/lcov-report/src/index.js.html +14 -68
  16. package/coverage/lcov-report/src/utils/constant.js.html +1 -1
  17. package/coverage/lcov-report/src/utils/helpers.js.html +1 -1
  18. package/coverage/lcov-report/src/utils/httpClient.js.html +1 -1
  19. package/coverage/lcov-report/src/utils/index.html +1 -1
  20. package/coverage/lcov.info +224 -216
  21. package/dist/api/certs.d.ts +11 -1
  22. package/dist/api/revocations.d.ts +32 -12
  23. package/dist/api/signatures.d.ts +37 -5
  24. package/dist/config/index.d.ts +13 -2
  25. package/dist/index.d.ts +4 -1
  26. package/dist/index.js +160 -75
  27. package/dist/index.js.map +1 -1
  28. package/dist/index.mjs +160 -75
  29. package/dist/index.mjs.map +1 -1
  30. package/package.json +1 -1
  31. package/src/api/certs.js +12 -3
  32. package/src/api/revocations.js +55 -20
  33. package/src/api/signatures.js +25 -1
  34. package/src/config/index.js +25 -8
  35. package/src/index.js +4 -22
package/dist/index.d.ts CHANGED
@@ -8,6 +8,7 @@ declare namespace apacuana {
8
8
  customerId: string;
9
9
  integrationType: string;
10
10
  }): Promise<object>;
11
+ export function close(): void;
11
12
  export { getConfig };
12
13
  export { requestRevocation };
13
14
  export { getCertStatus };
@@ -17,9 +18,10 @@ declare namespace apacuana {
17
18
  export { generateCert };
18
19
  export { signDocument };
19
20
  export { getDigest };
21
+ export { getRevocationReasons };
20
22
  }
21
23
  import { getConfig } from "./config/index";
22
- import requestRevocation from "./api/revocations";
24
+ import { requestRevocation } from "./api/revocations";
23
25
  import { getCertStatus } from "./api/certs";
24
26
  import getCustomer from "./api/users";
25
27
  import { addSigner } from "./api/signatures";
@@ -27,3 +29,4 @@ import { getDocs } from "./api/signatures";
27
29
  import { generateCert } from "./api/certs";
28
30
  import { signDocument } from "./api/signatures";
29
31
  import { getDigest } from "./api/signatures";
32
+ import { getRevocationReasons } from "./api/revocations";
package/dist/index.js CHANGED
@@ -289,7 +289,19 @@ var INTEGRATION_TYPE = {
289
289
  ONPREMISE: "ONPREMISE"
290
290
  };
291
291
 
292
- var config = {
292
+ /**
293
+ * @typedef {object} SDKConfig
294
+ * @property {string} apiUrl
295
+ * @property {string} secretKey
296
+ * @property {string} apiKey
297
+ * @property {string} verificationId
298
+ * @property {string} customerId
299
+ * @property {string} integrationType
300
+ * @property {object} [userData]
301
+ * @property {string} [token]
302
+ */
303
+
304
+ var defaultConfig = {
293
305
  apiUrl: "",
294
306
  secretKey: "",
295
307
  apiKey: "",
@@ -299,6 +311,9 @@ var config = {
299
311
  userData: undefined,
300
312
  token: undefined
301
313
  };
314
+
315
+ /** @type {SDKConfig} */
316
+ var config = _objectSpread2({}, defaultConfig);
302
317
  var setConfig = function setConfig(newConfig) {
303
318
  var apiUrl = newConfig.apiUrl,
304
319
  secretKey = newConfig.secretKey,
@@ -311,17 +326,18 @@ var setConfig = function setConfig(newConfig) {
311
326
 
312
327
  // Nueva validación para el valor de integrationType
313
328
  if (!Object.values(INTEGRATION_TYPE).includes(integrationType)) {
314
- throw new Error("Apacuana SDK: El valor de integrationType ('".concat(integrationType, "') no es v\xE1lido. ") + "Valores permitidos: ".concat(Object.values(INTEGRATION_TYPE).join(', ')));
329
+ throw new Error("Apacuana SDK: El valor de integrationType ('".concat(integrationType, "') no es v\xE1lido. ") + "Valores permitidos: ".concat(Object.values(INTEGRATION_TYPE).join(", ")));
315
330
  }
316
331
  config = _objectSpread2(_objectSpread2({}, config), newConfig);
317
332
  // eslint-disable-next-line no-console
318
- console.log("[Config] SDK Configuración actualizada (desde config):", _objectSpread2(_objectSpread2({}, config), {}, {
319
- secretKey: "********"
320
- }));
321
333
  };
322
334
  var getConfig = function getConfig() {
323
335
  return _objectSpread2({}, config);
324
336
  };
337
+ var cleanConfig = function cleanConfig() {
338
+ config = _objectSpread2({}, defaultConfig);
339
+ console.log("Apacuana SDK: Configuración limpiada.");
340
+ };
325
341
 
326
342
  // src/errors/index.js
327
343
 
@@ -584,54 +600,107 @@ var getCustomer = /*#__PURE__*/function () {
584
600
  * @param {string} reasonCode - Código o descripción del motivo de la revocación.
585
601
  * @returns {Promise<RequestRevocationResponse>} Objeto con el estado de la solicitud de revocación.
586
602
  */
603
+ /**
604
+ * @typedef {object} RevocationResponse
605
+ * @property {boolean} success - Indicates if the revocation request was successful.
606
+ * @property {string} [message] - A message providing details about the outcome.
607
+ */
608
+
609
+ /**
610
+ * @typedef {object} RevocationReason
611
+ * @property {string} code - The code for the revocation reason.
612
+ * @property {string} description - The description of the revocation reason.
613
+ */
614
+
615
+ /**
616
+ * @typedef {object} RevocationReasonsResponse
617
+ * @property {boolean} success - Indicates if the request was successful.
618
+ * @property {RevocationReason[]} reasons - A list of revocation reasons.
619
+ */
620
+
621
+ /**
622
+ * Requests the revocation of a certificate.
623
+ * @param {number} reasonCode - Código o descripción del motivo de la revocación.
624
+ * @returns {Promise<RevocationResponse>} An object indicating the success of the request.
625
+ * @throws {ApacuanaAPIError} If the revocation request fails.
626
+ */
587
627
  var requestRevocation = /*#__PURE__*/function () {
588
628
  var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(reasonCode) {
589
629
  var response, _t;
590
630
  return _regenerator().w(function (_context) {
591
631
  while (1) switch (_context.p = _context.n) {
592
632
  case 0:
593
- console.log("-> Función 'requestRevocation' ejecutada.");
594
- console.log("Parámetros recibidos para solicitud de revocación:", {
595
- reasonCode: reasonCode
596
- });
597
633
  if (reasonCode) {
598
634
  _context.n = 1;
599
635
  break;
600
636
  }
601
- throw new Error("ID de certificado y motivo son requeridos para requestRevocation.");
637
+ throw new Error("Código de motivo es requerido para requestRevocation.");
602
638
  case 1:
603
639
  _context.p = 1;
604
640
  _context.n = 2;
605
- return httpRequest("users/api/customers/requestcert", {
641
+ return httpRequest("services/api/onboardingclient/requestcert", {
606
642
  reason: reasonCode
607
643
  }, "POST");
608
644
  case 2:
609
645
  response = _context.v;
610
- console.log("Respuesta del servidor", response);
611
- if (response.success) {
612
- _context.n = 3;
613
- break;
614
- }
615
- throw new ApacuanaAPIError(response.message || "Error desconocido al solicitar revocación.");
646
+ return _context.a(2, response);
616
647
  case 3:
617
- return _context.a(2, {
618
- revocationStatus: response.status || "pending",
619
- requestId: response.id
620
- });
621
- case 4:
622
- _context.p = 4;
648
+ _context.p = 3;
623
649
  _t = _context.v;
624
650
  throw new Error("Fallo en la solicitud de revocaci\xF3n: ".concat(_t.message));
625
- case 5:
651
+ case 4:
626
652
  return _context.a(2);
627
653
  }
628
- }, _callee, null, [[1, 4]]);
654
+ }, _callee, null, [[1, 3]]);
629
655
  }));
630
656
  return function requestRevocation(_x) {
631
657
  return _ref.apply(this, arguments);
632
658
  };
633
659
  }();
634
660
 
661
+ /**
662
+ * Retrieves the available reasons for certificate revocation.
663
+ * @returns {Promise<RevocationReasonsResponse>} An object containing the list of revocation reasons.
664
+ * @throws {ApacuanaAPIError} If fetching the reasons fails.
665
+ */
666
+ var getRevocationReasons = /*#__PURE__*/function () {
667
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2() {
668
+ var response, _t2;
669
+ return _regenerator().w(function (_context2) {
670
+ while (1) switch (_context2.p = _context2.n) {
671
+ case 0:
672
+ _context2.p = 0;
673
+ _context2.n = 1;
674
+ return httpRequest("config/api/revocation/reasonsonboarding", {}, "GET");
675
+ case 1:
676
+ response = _context2.v;
677
+ if (response.records) {
678
+ _context2.n = 2;
679
+ break;
680
+ }
681
+ throw new ApacuanaAPIError("Failed to fetch revocation reasons.");
682
+ case 2:
683
+ return _context2.a(2, response.records);
684
+ case 3:
685
+ _context2.p = 3;
686
+ _t2 = _context2.v;
687
+ if (!(_t2 instanceof ApacuanaAPIError)) {
688
+ _context2.n = 4;
689
+ break;
690
+ }
691
+ throw _t2;
692
+ case 4:
693
+ throw new Error("Failed to get revocation reasons. Please try again later.");
694
+ case 5:
695
+ return _context2.a(2);
696
+ }
697
+ }, _callee2, null, [[0, 3]]);
698
+ }));
699
+ return function getRevocationReasons() {
700
+ return _ref2.apply(this, arguments);
701
+ };
702
+ }();
703
+
635
704
  var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
636
705
 
637
706
  function getDefaultExportFromCjs (x) {
@@ -32869,6 +32938,7 @@ var helpers = {
32869
32938
  /**
32870
32939
  * @typedef {object} GenerateCertResponse
32871
32940
  * @property {string} cert - El certificado generado en formato string.
32941
+ * @property {string} certifiedid - El ID del certificado generado.
32872
32942
  * @property {boolean} success - Indica si la operación fue exitosa.
32873
32943
  */
32874
32944
 
@@ -32878,51 +32948,53 @@ var helpers = {
32878
32948
  * @property {boolean} success - Indica si la operación fue exitosa.
32879
32949
  */
32880
32950
 
32951
+ /**
32952
+ * @typedef {object} EncryptedCSRObject
32953
+ * @property {string} csr - The encrypted Certificate Signing Request.
32954
+ */
32955
+
32956
+ /**
32957
+ * @param {EncryptedCSRObject} encryptedCSR
32958
+ */
32881
32959
  var generateCertOnBoarding = /*#__PURE__*/function () {
32882
- var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
32883
- var encryptedCSR,
32884
- response,
32885
- cert,
32886
- certifiedid,
32887
- _args = arguments,
32888
- _t;
32960
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(encryptedCSR) {
32961
+ var response, cert, certifiedid, _t;
32889
32962
  return _regenerator().w(function (_context) {
32890
32963
  while (1) switch (_context.p = _context.n) {
32891
32964
  case 0:
32892
- encryptedCSR = _args.length > 0 && _args[0] !== undefined ? _args[0] : undefined;
32893
- _context.p = 1;
32894
- _context.n = 2;
32965
+ _context.p = 0;
32966
+ _context.n = 1;
32895
32967
  return httpRequest("services/api/register/certificate", encryptedCSR, "POST");
32896
- case 2:
32968
+ case 1:
32897
32969
  response = _context.v;
32898
32970
  cert = response.cert, certifiedid = response.certifiedid;
32899
32971
  if (!(!cert || !certifiedid)) {
32900
- _context.n = 3;
32972
+ _context.n = 2;
32901
32973
  break;
32902
32974
  }
32903
32975
  throw new ApacuanaAPIError("The API response does not contain the certificate.", response.status, "INVALID_API_RESPONSE");
32904
- case 3:
32976
+ case 2:
32905
32977
  return _context.a(2, {
32906
32978
  cert: cert,
32907
32979
  certifiedid: certifiedid,
32908
32980
  success: true
32909
32981
  });
32910
- case 4:
32911
- _context.p = 4;
32982
+ case 3:
32983
+ _context.p = 3;
32912
32984
  _t = _context.v;
32913
32985
  if (!(_t instanceof ApacuanaAPIError)) {
32914
- _context.n = 5;
32986
+ _context.n = 4;
32915
32987
  break;
32916
32988
  }
32917
32989
  throw _t;
32918
- case 5:
32990
+ case 4:
32919
32991
  throw new Error("Certificate generation failed: ".concat(_t.message));
32920
- case 6:
32992
+ case 5:
32921
32993
  return _context.a(2);
32922
32994
  }
32923
- }, _callee, null, [[1, 4]]);
32995
+ }, _callee, null, [[0, 3]]);
32924
32996
  }));
32925
- return function generateCertOnBoarding() {
32997
+ return function generateCertOnBoarding(_x) {
32926
32998
  return _ref.apply(this, arguments);
32927
32999
  };
32928
33000
  }();
@@ -32944,21 +33016,17 @@ var generateCertOnPremise = /*#__PURE__*/function () {
32944
33016
 
32945
33017
  /**
32946
33018
  * Generates a digital certificate.
32947
- * @param {string} [csr] - Certificate Signing Request (CSR).
33019
+ * @param {EncryptedCSRObject} encryptedCSR - Certificate Signing Request (CSR) object.
32948
33020
  * @returns {Promise<GenerateCertResponse>} Object with the generated certificate and a success indicator.
32949
33021
  * @throws {ApacuanaAPIError} If the CSR is invalid, if the API response does not contain the certificate, or if the integration type is not supported.
32950
33022
  * @throws {Error} If certificate generation fails for another reason.
32951
33023
  */
32952
33024
  var generateCert = /*#__PURE__*/function () {
32953
- var _ref3 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3() {
32954
- var encryptedCSR,
32955
- _getConfig,
32956
- integrationType,
32957
- _args3 = arguments;
33025
+ var _ref3 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(encryptedCSR) {
33026
+ var _getConfig, integrationType;
32958
33027
  return _regenerator().w(function (_context3) {
32959
33028
  while (1) switch (_context3.n) {
32960
33029
  case 0:
32961
- encryptedCSR = _args3.length > 0 && _args3[0] !== undefined ? _args3[0] : undefined;
32962
33030
  _getConfig = getConfig(), integrationType = _getConfig.integrationType;
32963
33031
  helpers.validateCsr(encryptedCSR);
32964
33032
  if (!(integrationType === INTEGRATION_TYPE.ONBOARDING)) {
@@ -32979,7 +33047,7 @@ var generateCert = /*#__PURE__*/function () {
32979
33047
  }
32980
33048
  }, _callee3);
32981
33049
  }));
32982
- return function generateCert() {
33050
+ return function generateCert(_x2) {
32983
33051
  return _ref3.apply(this, arguments);
32984
33052
  };
32985
33053
  }();
@@ -33015,7 +33083,6 @@ var getCertStatus = function getCertStatus() {
33015
33083
  * Define la estructura de datos para añadir un firmante.
33016
33084
  * @typedef {object} SignerData
33017
33085
  * @property {string} docId - Identificador único del documento.
33018
- * @property {Signer} signer - Objeto con la información del firmante.
33019
33086
  */
33020
33087
 
33021
33088
  /**
@@ -33386,6 +33453,31 @@ var getDigest = /*#__PURE__*/function () {
33386
33453
  * @returns {Promise<AddSignerResponse>} Una promesa que resuelve a un objeto con el resultado de la operación.
33387
33454
  * @throws {ApacuanaAPIError} Si los datos del firmante son inválidos, la llamada a la API falla, o el tipo de integración no es soportado.
33388
33455
  */
33456
+ /**
33457
+ * @typedef {object} SignaturePosition
33458
+ * @property {number} page - The page number for the signature.
33459
+ * @property {number} x - The x-coordinate for the signature's position (from 0 to 1).
33460
+ * @property {number} y - The y-coordinate for the signature's position (from 0 to 1).
33461
+ */
33462
+
33463
+ /**
33464
+ * @typedef {object} OnboardingSignerData
33465
+ * @property {string} name - The name of the document.
33466
+ * @property {string} reference - An external reference for the document.
33467
+ * @property {string} typedoc - The type of document of the signer (e.g., "V", "E", "J").
33468
+ * @property {string} doc - The document number of the signer.
33469
+ * @property {SignaturePosition[]} signature - An array of signature positions.
33470
+ */
33471
+
33472
+ /**
33473
+ * Adds a new signer to the signature process.
33474
+ * This function acts as a dispatcher, delegating to the appropriate implementation
33475
+ * based on the configured integration type.
33476
+ *
33477
+ * @param {OnboardingSignerData | object} signerData - The signer's data. The structure depends on the integration type.
33478
+ * @returns {Promise<object>} The result from the API.
33479
+ * @throws {ApacuanaAPIError} If signerData is invalid or if the integration type is not supported.
33480
+ */
33389
33481
  var addSigner = /*#__PURE__*/function () {
33390
33482
  var _ref1 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee1(signerData) {
33391
33483
  var _getConfig4, integrationType;
@@ -33478,55 +33570,47 @@ var apacuana = {
33478
33570
  return _regenerator().w(function (_context) {
33479
33571
  while (1) switch (_context.p = _context.n) {
33480
33572
  case 0:
33481
- // eslint-disable-next-line no-console
33482
- console.log("-> apacuana-sdk: Iniciando configuración y validando usuario...");
33483
- _context.p = 1;
33484
- // 1. Guardar la configuración inicial
33573
+ _context.p = 0;
33485
33574
  setConfig(config);
33486
-
33487
- // 2. Inicializar el cliente HTTP con la configuración guardada
33488
33575
  initHttpClient();
33489
-
33490
- // 3. Opcional: Validar que la configuración se guardó correctamente
33491
33576
  currentConfig = getConfig();
33492
33577
  if (currentConfig.customerId) {
33493
- _context.n = 2;
33578
+ _context.n = 1;
33494
33579
  break;
33495
33580
  }
33496
33581
  throw new Error("Apacuana SDK: La configuración de CustomerId no se ha guardado" + " correctamente.");
33497
- case 2:
33498
- _context.n = 3;
33582
+ case 1:
33583
+ _context.n = 2;
33499
33584
  return getCustomer();
33500
- case 3:
33585
+ case 2:
33501
33586
  _yield$getCustomer = _context.v;
33502
33587
  token = _yield$getCustomer.token;
33503
33588
  userData = _yield$getCustomer.userData;
33504
- // Opcional: Guardar los detalles del usuario en la configuración
33505
- // para acceso futuro si fuera necesario
33506
33589
  setConfig(_objectSpread2(_objectSpread2({}, currentConfig), {}, {
33507
33590
  token: token,
33508
33591
  userData: userData
33509
33592
  }));
33510
33593
  setAuthToken(token);
33511
- // eslint-disable-next-line no-console
33512
- console.log("-> apacuana-sdk: Inicialización completa. Usuario validado:", token);
33513
33594
  return _context.a(2, true);
33514
- case 4:
33515
- _context.p = 4;
33595
+ case 3:
33596
+ _context.p = 3;
33516
33597
  _t = _context.v;
33517
33598
  // eslint-disable-next-line no-console
33518
33599
  console.error("Error durante la inicialización del SDK:", _t);
33519
33600
  throw _t;
33520
- case 5:
33601
+ case 4:
33521
33602
  return _context.a(2);
33522
33603
  }
33523
- }, _callee, null, [[1, 4]]);
33604
+ }, _callee, null, [[0, 3]]);
33524
33605
  }));
33525
33606
  function init(_x) {
33526
33607
  return _init.apply(this, arguments);
33527
33608
  }
33528
33609
  return init;
33529
33610
  }(),
33611
+ close: function close() {
33612
+ return cleanConfig();
33613
+ },
33530
33614
  getConfig: getConfig,
33531
33615
  requestRevocation: requestRevocation,
33532
33616
  getCertStatus: getCertStatus,
@@ -33535,7 +33619,8 @@ var apacuana = {
33535
33619
  getDocs: getDocs,
33536
33620
  generateCert: generateCert,
33537
33621
  signDocument: signDocument,
33538
- getDigest: getDigest
33622
+ getDigest: getDigest,
33623
+ getRevocationReasons: getRevocationReasons
33539
33624
  };
33540
33625
 
33541
33626
  module.exports = apacuana;