apacuana-sdk-core 0.6.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.
Files changed (46) hide show
  1. package/README.md +38 -0
  2. package/babel.config.cjs +11 -0
  3. package/coverage/clover.xml +76 -88
  4. package/coverage/coverage-final.json +4 -4
  5. package/coverage/lcov-report/api/index.html +131 -0
  6. package/coverage/lcov-report/api/signatures.js.html +1093 -0
  7. package/coverage/lcov-report/api/users.js.html +292 -0
  8. package/coverage/lcov-report/config/index.html +116 -0
  9. package/coverage/lcov-report/config/index.js.html +208 -0
  10. package/coverage/lcov-report/errors/index.html +116 -0
  11. package/coverage/lcov-report/errors/index.js.html +148 -0
  12. package/coverage/lcov-report/index.html +22 -22
  13. package/coverage/lcov-report/src/api/certs.js.html +30 -36
  14. package/coverage/lcov-report/src/api/index.html +15 -15
  15. package/coverage/lcov-report/src/api/revocations.js.html +1 -1
  16. package/coverage/lcov-report/src/api/signatures.js.html +1 -1
  17. package/coverage/lcov-report/src/api/users.js.html +1 -1
  18. package/coverage/lcov-report/src/config/index.html +1 -1
  19. package/coverage/lcov-report/src/config/index.js.html +1 -1
  20. package/coverage/lcov-report/src/errors/index.html +1 -1
  21. package/coverage/lcov-report/src/errors/index.js.html +8 -8
  22. package/coverage/lcov-report/src/index.html +1 -1
  23. package/coverage/lcov-report/src/index.js.html +1 -1
  24. package/coverage/lcov-report/src/utils/constant.js.html +1 -1
  25. package/coverage/lcov-report/src/utils/helpers.js.html +18 -51
  26. package/coverage/lcov-report/src/utils/httpClient.js.html +6 -72
  27. package/coverage/lcov-report/src/utils/index.html +19 -19
  28. package/coverage/lcov-report/utils/constant.js.html +145 -0
  29. package/coverage/lcov-report/utils/httpClient.js.html +646 -0
  30. package/coverage/lcov-report/utils/index.html +131 -0
  31. package/coverage/lcov.info +151 -162
  32. package/dist/api/certs.d.ts +1 -1
  33. package/dist/index.js +281 -220
  34. package/dist/index.js.map +1 -1
  35. package/dist/index.mjs +281 -220
  36. package/dist/index.mjs.map +1 -1
  37. package/dist/utils/helpers.d.ts +1 -1
  38. package/jest.config.cjs +6 -1
  39. package/package.json +12 -7
  40. package/rollup.config.js +1 -1
  41. package/src/api/certs.js +7 -9
  42. package/src/utils/helpers.js +11 -22
  43. package/src/utils/httpClient.js +0 -22
  44. package/tests/api/certs.test.js +30 -48
  45. package/tsconfig.json +2 -1
  46. package/.babelrc +0 -3
@@ -17,7 +17,7 @@ declare function encryptedCsr(csr: any, encryptKey?: string): {
17
17
  csr: any;
18
18
  };
19
19
  declare function validateOnBoardingSignerData(signerData: any): void;
20
- declare function validateCsr(csr: any): void;
20
+ declare function validateCsr(encryptedCSR: any): void;
21
21
  declare function validateGetDocsData(data: any): void;
22
22
  declare function validateGetDigestData(signData: any): void;
23
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
- // Puedes añadir más configuraciones de cobertura si lo deseas
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.6.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.7",
29
- "@babel/preset-env": "^7.24.7",
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": "^26.0.1",
35
+ "@rollup/plugin-commonjs": "^28.0.6",
32
36
  "@rollup/plugin-json": "^6.1.0",
33
- "@rollup/plugin-node-resolve": "^15.2.3",
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
- "typescript": "^5.4.5"
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
@@ -28,5 +28,5 @@ export default {
28
28
  exclude: 'node_modules/**',
29
29
  }),
30
30
  ],
31
- external: ['axios'], // Mark axios as external to prevent bundling issues
31
+ external: ['axios'], // Mark only axios as external
32
32
  };
package/src/api/certs.js CHANGED
@@ -16,17 +16,15 @@ import { INTEGRATION_TYPE } from "../utils/constant";
16
16
  * @property {boolean} success - Indica si la operación fue exitosa.
17
17
  */
18
18
 
19
- const generateCertOnBoarding = async (csr = undefined) => {
19
+ const generateCertOnBoarding = async (encryptedCSR = undefined) => {
20
20
  try {
21
- const encryptedCSR = helpers.encryptedCsr(csr);
22
-
23
21
  const response = await httpRequest(
24
22
  "services/api/register/certificate",
25
23
  encryptedCSR,
26
24
  "POST"
27
25
  );
28
- const { cert } = response;
29
- if (!cert) {
26
+ const { cert, certifiedid } = response;
27
+ if (!cert || !certifiedid) {
30
28
  throw new ApacuanaAPIError(
31
29
  "The API response does not contain the certificate.",
32
30
  response.status,
@@ -34,7 +32,7 @@ const generateCertOnBoarding = async (csr = undefined) => {
34
32
  );
35
33
  }
36
34
 
37
- return { cert, success: true };
35
+ return { cert, certifiedid, success: true };
38
36
  } catch (error) {
39
37
  // The captured error is re-thrown, which will already be of type ApacuanaAPIError or a native Error.
40
38
  if (error instanceof ApacuanaAPIError) {
@@ -59,11 +57,11 @@ const generateCertOnPremise = async () => {
59
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.
60
58
  * @throws {Error} If certificate generation fails for another reason.
61
59
  */
62
- export const generateCert = async (csr = undefined) => {
60
+ export const generateCert = async (encryptedCSR = undefined) => {
63
61
  const { integrationType } = getConfig();
64
- helpers.validateCsr(csr);
62
+ helpers.validateCsr(encryptedCSR);
65
63
  if (integrationType === INTEGRATION_TYPE.ONBOARDING) {
66
- return generateCertOnBoarding(csr);
64
+ return generateCertOnBoarding(encryptedCSR);
67
65
  }
68
66
  if (integrationType === INTEGRATION_TYPE.ONPREMISE) {
69
67
  return generateCertOnPremise();
@@ -143,29 +143,18 @@ const validateOnBoardingSignerData = (signerData) => {
143
143
  });
144
144
  };
145
145
 
146
- const validateCsr = (csr) => {
147
- const base64Regex = /^[A-Za-z0-9+/=]+$/;
148
-
149
- if (!csr) {
150
- throw new ApacuanaAPIError(
151
- "La solicitud de firma de certificado (CSR) es requerida.",
152
- 400,
153
- "INVALID_PARAMETER"
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
- "El CSR debe estar codificado en formato base64 válido.",
168
- 400,
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
  }
@@ -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.
@@ -15,42 +15,38 @@ describe("Certificate API - certs.js", () => {
15
15
  });
16
16
 
17
17
  describe("generateCert", () => {
18
- it("should generate a certificate for ONBOARDING integration", async () => {
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
- helpers.encryptedCsr.mockReturnValue("encrypted-csr");
23
- httpRequest.mockResolvedValue({ cert: "new-cert", success: true });
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("some-csr");
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
- "encrypted-csr",
37
+ "services/api/register/certificate", // Endpoint de Onboarding
38
+ mockEncryptedCsr,
32
39
  "POST"
33
40
  );
34
- expect(result).toEqual({ cert: "new-cert", success: true });
35
- });
36
-
37
- it("should throw an error if API response does not contain a certificate", async () => {
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
- it("should throw an error for ONPREMISE", async () => {
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
- it("should throw an error if integration type is not supported", async () => {
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
- "Certificate generation failed: Something failed"
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": ["node_modules", "dist", "tests"]
15
+ "exclude": ["nodev_modules", "dist", "tests"]
15
16
  }
package/.babelrc DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "presets": ["@babel/preset-env"]
3
- }