apacuana-sdk-core 0.9.0 → 0.11.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 (56) hide show
  1. package/README.md +320 -236
  2. package/coverage/clover.xml +341 -212
  3. package/coverage/coverage-final.json +9 -7
  4. package/coverage/lcov-report/index.html +34 -19
  5. package/coverage/lcov-report/src/api/certs.js.html +493 -37
  6. package/coverage/lcov-report/src/api/faceLiveness.js.html +496 -0
  7. package/coverage/lcov-report/src/api/index.html +43 -28
  8. package/coverage/lcov-report/src/api/revocations.js.html +103 -106
  9. package/coverage/lcov-report/src/api/signatures.js.html +25 -253
  10. package/coverage/lcov-report/src/api/users.js.html +7 -13
  11. package/coverage/lcov-report/src/config/index.html +1 -1
  12. package/coverage/lcov-report/src/config/index.js.html +1 -1
  13. package/coverage/lcov-report/src/errors/index.html +1 -1
  14. package/coverage/lcov-report/src/errors/index.js.html +8 -8
  15. package/coverage/lcov-report/src/index.html +1 -1
  16. package/coverage/lcov-report/src/index.js.html +48 -3
  17. package/coverage/lcov-report/src/success/index.html +116 -0
  18. package/coverage/lcov-report/src/success/index.js.html +106 -0
  19. package/coverage/lcov-report/src/utils/constant.js.html +4 -4
  20. package/coverage/lcov-report/src/utils/helpers.js.html +1 -1
  21. package/coverage/lcov-report/src/utils/httpClient.js.html +1 -1
  22. package/coverage/lcov-report/src/utils/index.html +1 -1
  23. package/coverage/lcov.info +591 -352
  24. package/dist/api/certs.d.ts +13 -30
  25. package/dist/api/faceLiveness.d.ts +6 -0
  26. package/dist/api/revocations.d.ts +3 -42
  27. package/dist/api/signatures.d.ts +11 -153
  28. package/dist/index.d.ts +10 -0
  29. package/dist/index.js +630 -214
  30. package/dist/index.js.map +1 -1
  31. package/dist/index.mjs +630 -214
  32. package/dist/index.mjs.map +1 -1
  33. package/dist/success/index.d.ts +7 -0
  34. package/dist/types/certs.d.ts +97 -0
  35. package/dist/types/faceLiveness.d.ts +10 -0
  36. package/dist/types/revocations.d.ts +40 -0
  37. package/dist/types/signatures.d.ts +152 -0
  38. package/dist/types/users.d.ts +260 -0
  39. package/package.json +1 -1
  40. package/src/api/certs.js +175 -23
  41. package/src/api/faceLiveness.js +137 -0
  42. package/src/api/revocations.js +66 -67
  43. package/src/api/signatures.js +21 -97
  44. package/src/api/users.js +4 -6
  45. package/src/index.js +16 -1
  46. package/src/success/index.js +8 -0
  47. package/src/types/certs.js +56 -0
  48. package/src/types/faceLiveness.js +7 -0
  49. package/src/types/revocations.js +25 -0
  50. package/src/types/signatures.js +77 -0
  51. package/src/types/users.js +73 -0
  52. package/tests/api/certs.test.js +209 -4
  53. package/tests/api/faceLiveness.test.js +172 -0
  54. package/tests/api/revocations.test.js +37 -37
  55. package/tests/api/signatures.test.js +11 -5
  56. package/tests/api/users.test.js +3 -2
package/src/api/certs.js CHANGED
@@ -1,27 +1,21 @@
1
+ /**
2
+ * @typedef {import('../types/users').UserData} UserData
3
+ * @typedef {import('../types/certs').GenerateCertResponse} GenerateCertResponse
4
+ * @typedef {import('../types/certs').GetCertStatusResponse} GetCertStatusResponse
5
+ * @typedef {import('../types/certs').EncryptedCSRObject} EncryptedCSRObject
6
+ * @typedef {import('../types/certs').GetCertTypesResponse} GetCertTypesResponse
7
+ * @typedef {import('../types/certs').GetRequirementsResponse} GetRequirementsResponse
8
+ * @typedef {import('../types/certs').CertificateRequestParams} CertificateRequestParams
9
+ * @typedef {import('../types/certs').RequestCertificateResponse} RequestCertificateResponse
10
+ */
11
+
1
12
  import { getConfig } from "../config/index";
2
13
  import { httpRequest } from "../utils/httpClient";
3
14
  import helpers from "../utils/helpers";
4
15
  import { ApacuanaAPIError } from "../errors";
16
+ import ApacuanaSuccess from "../success";
5
17
  import { INTEGRATION_TYPE } from "../utils/constant";
6
18
 
7
- /**
8
- * @typedef {object} GenerateCertResponse
9
- * @property {string} cert - El certificado generado en formato string.
10
- * @property {string} certifiedid - El ID del certificado generado.
11
- * @property {boolean} success - Indica si la operación fue exitosa.
12
- */
13
-
14
- /**
15
- * @typedef {object} GetCertStatusResponse
16
- * @property {string} status - El estado actual del certificado del usuario.
17
- * @property {boolean} success - Indica si la operación fue exitosa.
18
- */
19
-
20
- /**
21
- * @typedef {object} EncryptedCSRObject
22
- * @property {string} csr - The encrypted Certificate Signing Request.
23
- */
24
-
25
19
  /**
26
20
  * @param {EncryptedCSRObject} encryptedCSR
27
21
  */
@@ -41,7 +35,7 @@ const generateCertOnBoarding = async (encryptedCSR) => {
41
35
  );
42
36
  }
43
37
 
44
- return { cert, certifiedid, success: true };
38
+ return new ApacuanaSuccess({ cert, certifiedid });
45
39
  } catch (error) {
46
40
  // The captured error is re-thrown, which will already be of type ApacuanaAPIError or a native Error.
47
41
  if (error instanceof ApacuanaAPIError) {
@@ -94,8 +88,166 @@ export const getCertStatus = (isCertificateInDevice = false) => {
94
88
  config.userData,
95
89
  isCertificateInDevice
96
90
  );
97
- return {
98
- status,
99
- success: true,
100
- };
91
+ return new ApacuanaSuccess({ status });
92
+ };
93
+
94
+ const getCertTypesOnBoarding = async () => {
95
+ try {
96
+ const response = await httpRequest(
97
+ "services/api/customer/typeusers",
98
+ {},
99
+ "GET"
100
+ );
101
+
102
+ return new ApacuanaSuccess(response);
103
+ } catch (error) {
104
+ if (error instanceof ApacuanaAPIError) {
105
+ throw error;
106
+ }
107
+ throw new Error(`Failed to get certificate types: ${error.message}`);
108
+ }
109
+ };
110
+
111
+ const getCertTypesOnPremise = async () => {
112
+ throw new ApacuanaAPIError(
113
+ "Getting certificate types is not supported for integration type: ONPREMISE",
114
+ 501,
115
+ "NOT_IMPLEMENTED"
116
+ );
117
+ };
118
+
119
+ /**
120
+ * Gets the available certificate types.
121
+ * @returns {Promise<GetCertTypesResponse>} Object with the list of certificate types and a success indicator.
122
+ * @throws {ApacuanaAPIError} If the API response is invalid or the integration type is not supported.
123
+ * @throws {Error} If the request fails for another reason.
124
+ */
125
+ export const getCertTypes = async () => {
126
+ const { integrationType } = getConfig();
127
+ if (integrationType === INTEGRATION_TYPE.ONBOARDING) {
128
+ return getCertTypesOnBoarding();
129
+ }
130
+ if (integrationType === INTEGRATION_TYPE.ONPREMISE) {
131
+ return getCertTypesOnPremise();
132
+ }
133
+
134
+ throw new ApacuanaAPIError(
135
+ `Unsupported integration type: ${integrationType}`,
136
+ 400,
137
+ "UNSUPPORTED_INTEGRATION_TYPE"
138
+ );
139
+ };
140
+
141
+ const getRequerimentsByTypeUserOnBoarding = async (type) => {
142
+ try {
143
+ const response = await httpRequest(
144
+ `services/api/customer/documentspref?typeuser=${type}`,
145
+ {},
146
+ "GET"
147
+ );
148
+
149
+ return new ApacuanaSuccess(response);
150
+ } catch (error) {
151
+ if (error instanceof ApacuanaAPIError) {
152
+ throw error;
153
+ }
154
+ throw new Error(`Failed to get requirements: ${error.message}`);
155
+ }
156
+ };
157
+
158
+ const getRequerimentsByTypeUserOnPremise = async () => {
159
+ throw new ApacuanaAPIError(
160
+ "Getting requirements by user type is not supported for integration type: ONPREMISE",
161
+ 501,
162
+ "NOT_IMPLEMENTED"
163
+ );
164
+ };
165
+
166
+ /**
167
+ * Gets the requirements for a given user type.
168
+ * @param {object} params - The parameters.
169
+ * @param {number} params.type - The user type to get requirements for.
170
+ * @returns {Promise<GetRequirementsResponse>} Object with the list of requirements and a success indicator.
171
+ * @throws {ApacuanaAPIError} If the API response is invalid or the integration type is not supported.
172
+ * @throws {Error} If the request fails for another reason or the type is invalid.
173
+ */
174
+ export const getRequerimentsByTypeUser = async (params) => {
175
+ if (!params || typeof params.type !== "number") {
176
+ throw new Error(
177
+ 'The "params" object with a numeric "type" property is required.'
178
+ );
179
+ }
180
+ const { type } = params;
181
+
182
+ const { integrationType } = getConfig();
183
+ if (integrationType === INTEGRATION_TYPE.ONBOARDING) {
184
+ return getRequerimentsByTypeUserOnBoarding(type);
185
+ }
186
+ if (integrationType === INTEGRATION_TYPE.ONPREMISE) {
187
+ return getRequerimentsByTypeUserOnPremise();
188
+ }
189
+
190
+ throw new ApacuanaAPIError(
191
+ `Unsupported integration type: ${integrationType}`,
192
+ 400,
193
+ "UNSUPPORTED_INTEGRATION_TYPE"
194
+ );
195
+ };
196
+
197
+ const requestCertificateOnBoarding = async (params) => {
198
+ try {
199
+ const response = await httpRequest(
200
+ "services/api/customer/request-certificate",
201
+ params,
202
+ "POST"
203
+ );
204
+
205
+ return new ApacuanaSuccess(response);
206
+ } catch (error) {
207
+ if (error instanceof ApacuanaAPIError) {
208
+ throw error;
209
+ }
210
+ throw new Error(`Failed to request certificate: ${error.message}`);
211
+ }
212
+ };
213
+
214
+ const requestCertificateOnPremise = async () => {
215
+ throw new ApacuanaAPIError(
216
+ "Requesting a certificate is not supported for integration type: ONPREMISE",
217
+ 501,
218
+ "NOT_IMPLEMENTED"
219
+ );
220
+ };
221
+
222
+ /**
223
+ * Requests a certificate for the user.
224
+ * @param {CertificateRequestParams} params - The parameters for the certificate request.
225
+ * @returns {Promise<RequestCertificateResponse>} Object with a confirmation message and a success indicator.
226
+ * @throws {ApacuanaAPIError} If the API response is invalid or the integration type is not supported.
227
+ * @throws {Error} If the request fails for another reason or the params are invalid.
228
+ */
229
+ export const requestCertificate = async (params) => {
230
+ if (
231
+ !params ||
232
+ typeof params.type !== "number" ||
233
+ !Array.isArray(params.documents)
234
+ ) {
235
+ throw new Error(
236
+ 'The "params" object with a numeric "type" property and a "documents" array is required.'
237
+ );
238
+ }
239
+
240
+ const { integrationType } = getConfig();
241
+ if (integrationType === INTEGRATION_TYPE.ONBOARDING) {
242
+ return requestCertificateOnBoarding(params);
243
+ }
244
+ if (integrationType === INTEGRATION_TYPE.ONPREMISE) {
245
+ return requestCertificateOnPremise();
246
+ }
247
+
248
+ throw new ApacuanaAPIError(
249
+ `Unsupported integration type: ${integrationType}`,
250
+ 400,
251
+ "UNSUPPORTED_INTEGRATION_TYPE"
252
+ );
101
253
  };
@@ -0,0 +1,137 @@
1
+ /**
2
+ * @typedef {import('../types/faceLiveness').CreateFaceLivenessSessionResponse} CreateFaceLivenessSessionResponse
3
+ */
4
+
5
+ import { getConfig } from "../config/index";
6
+ import { httpRequest } from "../utils/httpClient";
7
+ import { ApacuanaAPIError } from "../errors";
8
+ import ApacuanaSuccess from "../success";
9
+ import { INTEGRATION_TYPE } from "../utils/constant";
10
+
11
+ const createFaceLivenessSessionOnBoarding = async () => {
12
+ try {
13
+ const response = await httpRequest(
14
+ "services/api/faceliveness/create",
15
+ {},
16
+ "POST"
17
+ );
18
+
19
+ if (!response.sessionId) {
20
+ throw new ApacuanaAPIError(
21
+ "The API response does not contain the session ID.",
22
+ response.status,
23
+ "INVALID_API_RESPONSE"
24
+ );
25
+ }
26
+
27
+ return new ApacuanaSuccess({ sessionId: response.sessionId });
28
+ } catch (error) {
29
+ if (error instanceof ApacuanaAPIError) {
30
+ throw error;
31
+ }
32
+ throw new Error(`Failed to create Face Liveness session: ${error.message}`);
33
+ }
34
+ };
35
+
36
+ const createFaceLivenessSessionOnPremise = async () => {
37
+ throw new ApacuanaAPIError(
38
+ "Creating a Face Liveness session is not supported for integration type: ONPREMISE",
39
+ 501,
40
+ "NOT_IMPLEMENTED"
41
+ );
42
+ };
43
+
44
+ const validateFaceLivenessOnBoarding = async (sessionId) => {
45
+ try {
46
+ const response = await httpRequest(
47
+ "services/api/faceliveness/validate",
48
+ { sessionid: sessionId },
49
+ "POST"
50
+ );
51
+ return new ApacuanaSuccess({ status: "verified", ...response });
52
+ } catch (error) {
53
+ if (error instanceof ApacuanaAPIError) {
54
+ let status;
55
+ switch (error.statusCode) {
56
+ case 402:
57
+ status = "waitingForScan";
58
+ break;
59
+ case 403:
60
+ status = "rejected";
61
+ break;
62
+ case 406:
63
+ status = "processing";
64
+ break;
65
+ case 408:
66
+ status = "expired";
67
+ break;
68
+ default:
69
+ throw error;
70
+ }
71
+ return new ApacuanaSuccess({ status });
72
+ }
73
+ throw error;
74
+ }
75
+ };
76
+
77
+ const validateFaceLivenessOnPremise = async () => {
78
+ throw new ApacuanaAPIError(
79
+ "Validating a Face Liveness session is not supported for integration type: ONPREMISE",
80
+ 501,
81
+ "NOT_IMPLEMENTED"
82
+ );
83
+ };
84
+
85
+ /**
86
+ * Creates a new Face Liveness session.
87
+ * @returns {Promise<CreateFaceLivenessSessionResponse>} Object with the session ID and a success indicator.
88
+ * @throws {ApacuanaAPIError} If the API response is invalid or the integration type is not supported.
89
+ * @throws {Error} If the request fails for another reason.
90
+ */
91
+ export const createFaceLivenessSession = async () => {
92
+ const { integrationType } = getConfig();
93
+
94
+ if (integrationType === INTEGRATION_TYPE.ONBOARDING) {
95
+ return createFaceLivenessSessionOnBoarding();
96
+ }
97
+ if (integrationType === INTEGRATION_TYPE.ONPREMISE) {
98
+ return createFaceLivenessSessionOnPremise();
99
+ }
100
+
101
+ throw new ApacuanaAPIError(
102
+ `Unsupported integration type: ${integrationType}`,
103
+ 400,
104
+ "UNSUPPORTED_INTEGRATION_TYPE"
105
+ );
106
+ };
107
+
108
+ /**
109
+ * Validates a Face Liveness session and returns its status.
110
+ * @param {{sessionId: string}} params - Object containing the session ID.
111
+ * @returns {Promise<ApacuanaSuccess>} Object with the validation status.
112
+ * @throws {ApacuanaAPIError} If the API response is invalid or the integration type is not supported.
113
+ */
114
+ export const validateFaceLiveness = async ({ sessionId }) => {
115
+ if (!sessionId) {
116
+ throw new ApacuanaAPIError(
117
+ "sessionId is a required parameter.",
118
+ 400,
119
+ "INVALID_PARAMETER"
120
+ );
121
+ }
122
+
123
+ const { integrationType } = getConfig();
124
+
125
+ if (integrationType === INTEGRATION_TYPE.ONBOARDING) {
126
+ return validateFaceLivenessOnBoarding(sessionId);
127
+ }
128
+ if (integrationType === INTEGRATION_TYPE.ONPREMISE) {
129
+ return validateFaceLivenessOnPremise();
130
+ }
131
+
132
+ throw new ApacuanaAPIError(
133
+ `Unsupported integration type: ${integrationType}`,
134
+ 400,
135
+ "UNSUPPORTED_INTEGRATION_TYPE"
136
+ );
137
+ };
@@ -1,95 +1,94 @@
1
1
  import { httpRequest } from "../utils/httpClient";
2
- import { ApacuanaAPIError } from "../errors/index";
3
2
  import { getConfig } from "../config";
4
3
  import { INTEGRATION_TYPE } from "../utils/constant";
4
+ import { ApacuanaAPIError } from "../errors";
5
+ import ApacuanaSuccess from "../success";
5
6
 
6
- /**
7
- * @typedef {object} RequestRevocationResponse
8
- * @property {string} revocationStatus - El estado de la solicitud de revocación (e.g., "pending").
9
- * @property {string|number} requestId - El ID de la solicitud de revocación.
10
- */
11
-
12
- /**
13
- * Simula la solicitud de revocación de un certificado.
14
- * @param {string} reasonCode - Código o descripción del motivo de la revocación.
15
- * @returns {Promise<RequestRevocationResponse>} Objeto con el estado de la solicitud de revocación.
16
- */
17
- /**
18
- * @typedef {object} RevocationResponse
19
- * @property {boolean} success - Indicates if the revocation request was successful.
20
- * @property {string} [message] - A message providing details about the outcome.
21
- */
22
-
23
- /**
24
- * @typedef {object} RevocationReason
25
- * @property {string} code - The code for the revocation reason.
26
- * @property {string} description - The description of the revocation reason.
27
- */
28
-
29
- /**
30
- * @typedef {object} RevocationReasonsResponse
31
- * @property {boolean} success - Indicates if the request was successful.
32
- * @property {RevocationReason[]} reasons - A list of revocation reasons.
33
- */
34
-
35
- /**
36
- * Requests the revocation of a certificate.
37
- * @param {number} reasonCode - Código o descripción del motivo de la revocación.
38
- * @returns {Promise<RevocationResponse>} An object indicating the success of the request.
39
- * @throws {ApacuanaAPIError} If the revocation request fails.
40
- */
41
- export const requestRevocation = async (reasonCode) => {
42
- if (!reasonCode) {
43
- throw new Error("Código de motivo es requerido para requestRevocation.");
44
- }
45
-
7
+ const requestRevocationOnBoarding = async (params) => {
46
8
  try {
47
9
  const response = await httpRequest(
48
- "services/api/onboardingclient/requestcert",
49
- { reason: reasonCode },
10
+ "services/api/certificate/revocation",
11
+ params,
50
12
  "POST"
51
13
  );
52
-
53
- return response;
14
+ return new ApacuanaSuccess(response);
54
15
  } catch (error) {
55
- throw new Error(`Fallo en la solicitud de revocación: ${error.message}`);
16
+ if (error instanceof ApacuanaAPIError) {
17
+ throw error;
18
+ }
19
+ throw new Error(`Failed to request revocation: ${error.message}`);
56
20
  }
57
21
  };
58
22
 
59
- /**
60
- * Retrieves the available reasons for certificate revocation.
61
- * @returns {Promise<RevocationReasonsResponse>} An object containing the list of revocation reasons.
62
- * @throws {ApacuanaAPIError} If fetching the reasons fails.
63
- */
64
- export const getRevocationReasons = async () => {
23
+ const requestRevocationOnPremise = async () => {
24
+ throw new ApacuanaAPIError(
25
+ "Requesting revocation is not supported for integration type: ONPREMISE",
26
+ 501,
27
+ "NOT_IMPLEMENTED"
28
+ );
29
+ };
30
+
31
+ export const requestRevocation = async (params) => {
32
+ if (!params || typeof params.reasonCode !== "number") {
33
+ throw new Error(
34
+ 'The "params" object with a numeric "reasonCode" property is required.'
35
+ );
36
+ }
37
+
65
38
  const { integrationType } = getConfig();
66
39
 
40
+ if (integrationType === INTEGRATION_TYPE.ONBOARDING) {
41
+ return requestRevocationOnBoarding(params);
42
+ }
67
43
  if (integrationType === INTEGRATION_TYPE.ONPREMISE) {
68
- throw new ApacuanaAPIError(
69
- `Get revocation reasons is not supported for integration type: ${integrationType}`,
70
- 501,
71
- "NOT_IMPLEMENTED"
72
- );
44
+ return requestRevocationOnPremise(params);
73
45
  }
46
+ throw new ApacuanaAPIError(
47
+ `Unsupported integration type: ${integrationType}`,
48
+ 400,
49
+ "UNSUPPORTED_INTEGRATION_TYPE"
50
+ );
51
+ };
74
52
 
53
+ const getRevocationReasonsOnBoarding = async () => {
75
54
  try {
76
55
  const response = await httpRequest(
77
- "GET",
78
- "config/api/revocation/reasonsonboarding",
79
- {}
56
+ "services/api/certificate/revocation-reasons",
57
+ {},
58
+ "GET"
80
59
  );
81
-
82
- if (!response.records) {
60
+ if (!response || !response.records) {
83
61
  throw new ApacuanaAPIError("Failed to fetch revocation reasons.");
84
62
  }
85
-
86
- return response.records;
63
+ return new ApacuanaSuccess(response.records);
87
64
  } catch (error) {
88
65
  if (error instanceof ApacuanaAPIError) {
89
66
  throw error;
90
67
  }
91
- throw new Error(
92
- "Failed to get revocation reasons. Please try again later."
93
- );
68
+ throw new Error(`Failed to get revocation reasons: ${error.message}`);
69
+ }
70
+ };
71
+
72
+ const getRevocationReasonsOnPremise = async () => {
73
+ throw new ApacuanaAPIError(
74
+ "Getting revocation reasons is not supported for integration type: ONPREMISE",
75
+ 501,
76
+ "NOT_IMPLEMENTED"
77
+ );
78
+ };
79
+
80
+ export const getRevocationReasons = async () => {
81
+ const { integrationType } = getConfig();
82
+
83
+ if (integrationType === INTEGRATION_TYPE.ONBOARDING) {
84
+ return getRevocationReasonsOnBoarding();
85
+ }
86
+ if (integrationType === INTEGRATION_TYPE.ONPREMISE) {
87
+ return getRevocationReasonsOnPremise();
94
88
  }
89
+ throw new ApacuanaAPIError(
90
+ `Unsupported integration type: ${integrationType}`,
91
+ 400,
92
+ "UNSUPPORTED_INTEGRATION_TYPE"
93
+ );
95
94
  };