All files / src index.js

26.31% Statements 15/57
20% Branches 3/15
5% Functions 1/20
26.31% Lines 15/57

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174                                                              1x                                       1x         4x 4x 3x 3x   3x 3x 2x 2x 2x 2x                     2x 2x     2x                                                                                                                                                                                        
import { setConfig, getConfig, close } from "./config/index";
import { initHttpClient, setAuthToken } from "./utils/httpClient";
import { requestRevocation, getRevocationReasons } from "./api/revocations";
import {
  generateCert,
  getCertStatus,
  getCertTypes,
  getRequerimentsByTypeUser,
} from "./api/certs";
import {
  addSigner,
  deleteSignatureVariant,
  getDigest,
  getDocs,
  getSignatureVariant,
  signDocument,
  uploadSignatureVariant,
} from "./api/signatures";
import {
  createFaceLivenessSession,
  validateFaceLiveness,
} from "./api/faceLiveness";
import ApacuanaSuccess from "./success/index";
import { ApacuanaAPIError } from "./errors/index";
import { createApacuanaUser, getCustomer } from "./api/users";
 
/**
 * Valida si el SDK está inicializado y si se dispone de un customerId.
 * @param {boolean} requiresCustomerId - Indica si la operación necesita un customerId.
 * @throws {ApacuanaAPIError} Si el SDK no está inicializado o el customerId es requerido y no está presente.
 */
const checkSdk = (requiresCustomerId = true) => {
  const config = getConfig();
 
  if (!config || !config.apiUrl) {
    throw new ApacuanaAPIError(
      "El SDK no está inicializado. Llama a apacuana.init() primero.",
      400,
      "NOT_INITIALIZED_ERROR"
    );
  }
 
  if (requiresCustomerId && !config.customerId) {
    throw new ApacuanaAPIError(
      "Se requiere un CustomerId para esta operación. Por favor, inicia sesión.",
      403,
      "CUSTOMER_ID_REQUIRED"
    );
  }
};
 
const apacuana = {
  /**
   * Inicializa el Apacuana SDK con la configuración necesaria.
   */
  init: async (config) => {
    try {
      setConfig(config);
      initHttpClient();
      const currentConfig = getConfig();
 
      Eif (currentConfig.customerId) {
        const customer = await getCustomer();
        const { token, userData } = customer.data;
        setConfig({ ...currentConfig, token, userData });
        setAuthToken(token);
        return new ApacuanaSuccess({
          initialized: true,
          message: "SDK inicializado con sesión de usuario.",
        });
      }
 
      return new ApacuanaSuccess({
        initialized: true,
        message: "SDK inicializado para operaciones públicas (sin customerId).",
      });
    } catch (error) {
      console.error("Error durante la inicialización del SDK:", error);
      Iif (error instanceof ApacuanaAPIError) {
        throw error;
      }
      throw new ApacuanaAPIError(
        error.message || "Error desconocido durante la inicialización.",
        500,
        error
      );
    }
  },
 
  // --- Métodos de Revocación ---
  requestRevocation: (data) => {
    checkSdk(true);
    return requestRevocation(data);
  },
  getRevocationReasons: () => {
    checkSdk(true);
    return getRevocationReasons();
  },
 
  // --- Métodos de Certificados ---
  generateCert: (data) => {
    checkSdk(true);
    return generateCert(data);
  },
  getCertStatus: (isCertificateInDevice) => {
    checkSdk(true);
    return getCertStatus(isCertificateInDevice);
  },
  getCertTypes: () => {
    checkSdk(false);
    return getCertTypes();
  },
  getRequerimentsByTypeUser: (data) => {
    checkSdk(false);
    return getRequerimentsByTypeUser(data);
  },
 
  // --- Métodos de Firmas y Documentos ---
  addSigner: (data) => {
    checkSdk(true);
    return addSigner(data);
  },
  deleteSignatureVariant: () => {
    checkSdk(true);
    return deleteSignatureVariant();
  },
  getDigest: (data) => {
    checkSdk(true);
    return getDigest(data);
  },
  getDocs: (data) => {
    checkSdk(true);
    return getDocs(data);
  },
  getSignatureVariant: () => {
    checkSdk(true);
    return getSignatureVariant();
  },
  signDocument: (data) => {
    checkSdk(true);
    return signDocument(data);
  },
  uploadSignatureVariant: (data) => {
    checkSdk(true);
    return uploadSignatureVariant(data);
  },
 
  getCustomer: () => {
    checkSdk(true);
    return getCustomer();
  },
  createFaceLivenessSession: () => {
    checkSdk(true);
    return createFaceLivenessSession();
  },
  createApacuanaUser: (data) => {
    checkSdk(false);
    return createApacuanaUser(data);
  },
  validateFaceLiveness: (data) => {
    checkSdk(true);
    return validateFaceLiveness(data);
  },
  // validateCertificate: (data) => {
  //   checkSdk(true);
  //   validateCertificate(data);
  // },
 
  close: () => close(),
  getConfig,
};
 
export default apacuana;