apacuana-sdk-core 1.0.0 → 1.1.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 (92) hide show
  1. package/.eslintrc.cjs +22 -0
  2. package/CHANGELOG.md +1 -0
  3. package/README.md +794 -0
  4. package/babel.config.cjs +11 -0
  5. package/coverage/clover.xml +627 -0
  6. package/coverage/coverage-final.json +13 -0
  7. package/coverage/lcov-report/api/index.html +131 -0
  8. package/coverage/lcov-report/api/signatures.js.html +1093 -0
  9. package/coverage/lcov-report/api/users.js.html +292 -0
  10. package/coverage/lcov-report/base.css +224 -0
  11. package/coverage/lcov-report/block-navigation.js +87 -0
  12. package/coverage/lcov-report/certs.js.html +175 -0
  13. package/coverage/lcov-report/config/index.html +116 -0
  14. package/coverage/lcov-report/config/index.js.html +208 -0
  15. package/coverage/lcov-report/errors/index.html +116 -0
  16. package/coverage/lcov-report/errors/index.js.html +148 -0
  17. package/coverage/lcov-report/favicon.png +0 -0
  18. package/coverage/lcov-report/index.html +191 -0
  19. package/coverage/lcov-report/prettify.css +1 -0
  20. package/coverage/lcov-report/prettify.js +2 -0
  21. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  22. package/coverage/lcov-report/sorter.js +210 -0
  23. package/coverage/lcov-report/src/api/certs.js.html +853 -0
  24. package/coverage/lcov-report/src/api/faceLiveness.js.html +523 -0
  25. package/coverage/lcov-report/src/api/index.html +176 -0
  26. package/coverage/lcov-report/src/api/revocations.js.html +412 -0
  27. package/coverage/lcov-report/src/api/signatures.js.html +1420 -0
  28. package/coverage/lcov-report/src/api/users.js.html +466 -0
  29. package/coverage/lcov-report/src/config/index.html +116 -0
  30. package/coverage/lcov-report/src/config/index.js.html +256 -0
  31. package/coverage/lcov-report/src/errors/index.html +116 -0
  32. package/coverage/lcov-report/src/errors/index.js.html +148 -0
  33. package/coverage/lcov-report/src/index.html +116 -0
  34. package/coverage/lcov-report/src/index.js.html +592 -0
  35. package/coverage/lcov-report/src/success/index.html +116 -0
  36. package/coverage/lcov-report/src/success/index.js.html +106 -0
  37. package/coverage/lcov-report/src/utils/constant.js.html +256 -0
  38. package/coverage/lcov-report/src/utils/helpers.js.html +1195 -0
  39. package/coverage/lcov-report/src/utils/httpClient.js.html +622 -0
  40. package/coverage/lcov-report/src/utils/index.html +146 -0
  41. package/coverage/lcov-report/utils/constant.js.html +145 -0
  42. package/coverage/lcov-report/utils/httpClient.js.html +646 -0
  43. package/coverage/lcov-report/utils/index.html +131 -0
  44. package/coverage/lcov.info +1258 -0
  45. package/dist/api/certs.d.ts +15 -0
  46. package/dist/api/faceLiveness.d.ts +6 -0
  47. package/dist/api/revocations.d.ts +6 -0
  48. package/dist/api/signatures.d.ts +19 -0
  49. package/dist/api/users.d.ts +47 -0
  50. package/dist/config/index.d.ts +22 -0
  51. package/dist/errors/index.d.ts +10 -0
  52. package/dist/index.d.ts +25 -0
  53. package/dist/index.js +34492 -0
  54. package/dist/index.js.map +1 -0
  55. package/dist/index.mjs +34490 -0
  56. package/dist/index.mjs.map +1 -0
  57. package/dist/success/index.d.ts +7 -0
  58. package/dist/types/certs.d.ts +83 -0
  59. package/dist/types/faceLiveness.d.ts +11 -0
  60. package/dist/types/revocations.d.ts +51 -0
  61. package/dist/types/signatures.d.ts +162 -0
  62. package/dist/types/users.d.ts +260 -0
  63. package/dist/utils/constant.d.ts +49 -0
  64. package/dist/utils/helpers.d.ts +25 -0
  65. package/dist/utils/httpClient.d.ts +3 -0
  66. package/jest.config.cjs +12 -0
  67. package/package.json +51 -9
  68. package/rollup.config.js +32 -0
  69. package/src/api/certs.js +256 -0
  70. package/src/api/faceLiveness.js +146 -0
  71. package/src/api/revocations.js +109 -0
  72. package/src/api/signatures.js +445 -0
  73. package/src/api/users.js +127 -0
  74. package/src/config/index.js +57 -0
  75. package/src/errors/index.js +21 -0
  76. package/src/index.js +169 -0
  77. package/src/success/index.js +8 -0
  78. package/src/types/certs.js +67 -0
  79. package/src/types/faceLiveness.js +16 -0
  80. package/src/types/revocations.js +45 -0
  81. package/src/types/signatures.js +96 -0
  82. package/src/types/users.js +73 -0
  83. package/src/utils/constant.js +57 -0
  84. package/src/utils/helpers.js +370 -0
  85. package/src/utils/httpClient.js +179 -0
  86. package/tests/api/certs.test.js +311 -0
  87. package/tests/api/faceLiveness.test.js +170 -0
  88. package/tests/api/revocations.test.js +119 -0
  89. package/tests/api/signatures.test.js +530 -0
  90. package/tests/api/users.test.js +117 -0
  91. package/tests/index.test.js +131 -0
  92. package/tsconfig.json +16 -0
package/package.json CHANGED
@@ -1,15 +1,57 @@
1
1
  {
2
2
  "name": "apacuana-sdk-core",
3
- "version": "1.0.0",
4
- "description": "",
3
+ "version": "1.1.0",
4
+ "description": "Core SDK para interacciones con las APIs de Apacuana.",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "type": "module",
9
+ "scripts": {
10
+ "clean": "rimraf dist",
11
+ "clean-modules": "rimraf node_modules",
12
+ "test": "jest",
13
+ "lint": "eslint src/",
14
+ "build": "npm run clean && tsc && rollup -c",
15
+ "prepublishOnly": "npm run validate",
16
+ "validate": "npm run lint && npm test && npm run build"
17
+ },
5
18
  "keywords": [
6
- "sdk-apacuana"
19
+ "apacuana",
20
+ "sdk",
21
+ "core",
22
+ "certificados",
23
+ "firma"
7
24
  ],
8
- "license": "MIT",
9
- "author": "apacuana",
10
- "type": "commonjs",
11
- "main": "index.js",
12
- "scripts": {
13
- "test": "echo \"Error: no test specified\" && exit 1"
25
+ "author": "Gega",
26
+ "license": "ISC",
27
+ "devDependencies": {
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",
34
+ "@rollup/plugin-babel": "^6.0.4",
35
+ "@rollup/plugin-commonjs": "^28.0.6",
36
+ "@rollup/plugin-json": "^6.1.0",
37
+ "@rollup/plugin-node-resolve": "^16.0.1",
38
+ "babel-jest": "^29.7.0",
39
+ "eslint": "^8.57.0",
40
+ "eslint-config-airbnb-base": "^15.0.0",
41
+ "eslint-config-prettier": "^10.1.8",
42
+ "eslint-plugin-import": "^2.29.1",
43
+ "jest": "^29.7.0",
44
+ "rimraf": "^5.0.7",
45
+ "rollup": "^4.17.2",
46
+ "typescript": "^5.9.2"
47
+ },
48
+ "engines": {
49
+ "node": ">=14.0.0"
50
+ },
51
+ "dependencies": {
52
+ "asn1js": "^3.0.6",
53
+ "axios": "^1.11.0",
54
+ "crypto-js": "^4.2.0",
55
+ "pkijs": "^3.2.5"
14
56
  }
15
57
  }
@@ -0,0 +1,32 @@
1
+ import babel from '@rollup/plugin-babel';
2
+ import { nodeResolve } from '@rollup/plugin-node-resolve';
3
+ import commonjs from '@rollup/plugin-commonjs';
4
+ import json from '@rollup/plugin-json'; // Add this import
5
+
6
+ export default {
7
+ input: 'src/index.js',
8
+ output: [
9
+ {
10
+ file: 'dist/index.js',
11
+ format: 'cjs',
12
+ sourcemap: true,
13
+ },
14
+ {
15
+ file: 'dist/index.mjs',
16
+ format: 'es',
17
+ sourcemap: true,
18
+ },
19
+ ],
20
+ plugins: [
21
+ json(), // Add this plugin before nodeResolve
22
+ nodeResolve({
23
+ preferBuiltins: false,
24
+ }),
25
+ commonjs(),
26
+ babel({
27
+ babelHelpers: 'bundled',
28
+ exclude: 'node_modules/**',
29
+ }),
30
+ ],
31
+ external: ['axios'], // Mark only axios as external
32
+ };
@@ -0,0 +1,256 @@
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
+
12
+ import { getConfig } from "../config/index";
13
+ import { httpRequest } from "../utils/httpClient";
14
+ import helpers from "../utils/helpers";
15
+ import { ApacuanaAPIError } from "../errors";
16
+ import ApacuanaSuccess from "../success";
17
+ import { INTEGRATION_TYPE, PARSE_STATUS } from "../utils/constant";
18
+
19
+ /**
20
+ * @param {EncryptedCSRObject} encryptedCSR
21
+ */
22
+ const generateCertOnBoarding = async (encryptedCSR) => {
23
+ try {
24
+ const response = await httpRequest(
25
+ "services/api/register/certificate",
26
+ encryptedCSR,
27
+ "POST"
28
+ );
29
+ const { cert, certifiedid } = response;
30
+ if (!cert || !certifiedid) {
31
+ throw new ApacuanaAPIError(
32
+ "The API response does not contain the certificate.",
33
+ response.status,
34
+ "INVALID_API_RESPONSE"
35
+ );
36
+ }
37
+
38
+ return new ApacuanaSuccess({ cert, certifiedid });
39
+ } catch (error) {
40
+ // The captured error is re-thrown, which will already be of type ApacuanaAPIError or a native Error.
41
+ if (error instanceof ApacuanaAPIError) {
42
+ throw error;
43
+ }
44
+ throw new Error(`Certificate generation failed: ${error.message}`);
45
+ }
46
+ };
47
+
48
+ const generateCertOnPremise = async () => {
49
+ throw new ApacuanaAPIError(
50
+ "Certificate generation is not supported for integration type: ONPREMISE",
51
+ 501,
52
+ "NOT_IMPLEMENTED"
53
+ );
54
+ };
55
+
56
+ /**
57
+ * Generates a digital certificate.
58
+ * @param {EncryptedCSRObject} encryptedCSR - Certificate Signing Request (CSR) object.
59
+ * @returns {Promise<GenerateCertResponse>} Object with the generated certificate and a success indicator.
60
+ * @throws {ApacuanaAPIError} If the CSR is invalid, if the API response does not contain the certificate, or if the integration type is not supported.
61
+ * @throws {Error} If certificate generation fails for another reason.
62
+ */
63
+ export const generateCert = async (encryptedCSR) => {
64
+ const { integrationType } = getConfig();
65
+ helpers.validateCsr(encryptedCSR);
66
+ if (integrationType === INTEGRATION_TYPE.ONBOARDING) {
67
+ return generateCertOnBoarding(encryptedCSR);
68
+ }
69
+ if (integrationType === INTEGRATION_TYPE.ONPREMISE) {
70
+ return generateCertOnPremise();
71
+ }
72
+
73
+ throw new ApacuanaAPIError(
74
+ `Unsupported integration type: ${integrationType}`,
75
+ 400,
76
+ "UNSUPPORTED_INTEGRATION_TYPE"
77
+ );
78
+ };
79
+
80
+ /**
81
+ * Gets the user's certificate status.
82
+ * @param {boolean} [isCertificateInDevice=false] - Indicates if the certificate is already on the device.
83
+ * @returns {GetCertStatusResponse} Object with the certificate status and a success indicator.
84
+ */
85
+ export const getCertStatus = (isCertificateInDevice = false) => {
86
+ const config = getConfig();
87
+ const status = helpers.getCertificateStatus(
88
+ config.userData,
89
+ isCertificateInDevice,
90
+ config.integrationType
91
+ );
92
+ const parseStatus = PARSE_STATUS[status];
93
+
94
+ return new ApacuanaSuccess({ status: parseStatus });
95
+ };
96
+
97
+ const getCertTypesOnBoarding = async () => {
98
+ try {
99
+ const response = await httpRequest(
100
+ "services/api/customer/typeusers",
101
+ {},
102
+ "GET"
103
+ );
104
+
105
+ return new ApacuanaSuccess(response);
106
+ } catch (error) {
107
+ if (error instanceof ApacuanaAPIError) {
108
+ throw error;
109
+ }
110
+ throw new Error(`Failed to get certificate types: ${error.message}`);
111
+ }
112
+ };
113
+
114
+ const getCertTypesOnPremise = async () => {
115
+ throw new ApacuanaAPIError(
116
+ "Getting certificate types is not supported for integration type: ONPREMISE",
117
+ 501,
118
+ "NOT_IMPLEMENTED"
119
+ );
120
+ };
121
+
122
+ /**
123
+ * Gets the available certificate types.
124
+ * @returns {Promise<GetCertTypesResponse>} Object with the list of certificate types and a success indicator.
125
+ * @throws {ApacuanaAPIError} If the API response is invalid or the integration type is not supported.
126
+ * @throws {Error} If the request fails for another reason.
127
+ */
128
+ export const getCertTypes = async () => {
129
+ const { integrationType } = getConfig();
130
+ if (integrationType === INTEGRATION_TYPE.ONBOARDING) {
131
+ return getCertTypesOnBoarding();
132
+ }
133
+ if (integrationType === INTEGRATION_TYPE.ONPREMISE) {
134
+ return getCertTypesOnPremise();
135
+ }
136
+
137
+ throw new ApacuanaAPIError(
138
+ `Unsupported integration type: ${integrationType}`,
139
+ 400,
140
+ "UNSUPPORTED_INTEGRATION_TYPE"
141
+ );
142
+ };
143
+
144
+ const getRequerimentsByTypeUserOnBoarding = async (type) => {
145
+ try {
146
+ const response = await httpRequest(
147
+ `services/api/customer/documentspref?typeuser=${type}`,
148
+ {},
149
+ "GET"
150
+ );
151
+
152
+ return new ApacuanaSuccess(response);
153
+ } catch (error) {
154
+ if (error instanceof ApacuanaAPIError) {
155
+ throw error;
156
+ }
157
+ throw new Error(`Failed to get requirements: ${error.message}`);
158
+ }
159
+ };
160
+
161
+ const getRequerimentsByTypeUserOnPremise = async () => {
162
+ throw new ApacuanaAPIError(
163
+ "Getting requirements by user type is not supported for integration type: ONPREMISE",
164
+ 501,
165
+ "NOT_IMPLEMENTED"
166
+ );
167
+ };
168
+
169
+ /**
170
+ * Gets the requirements for a given user type.
171
+ * @param {object} params - The parameters.
172
+ * @param {number} params.type - The user type to get requirements for.
173
+ * @returns {Promise<GetRequirementsResponse>} Object with the list of requirements and a success indicator.
174
+ * @throws {ApacuanaAPIError} If the API response is invalid or the integration type is not supported.
175
+ * @throws {Error} If the request fails for another reason or the type is invalid.
176
+ */
177
+ export const getRequerimentsByTypeUser = async (params) => {
178
+ if (!params || typeof params.type !== "number") {
179
+ throw new Error(
180
+ 'The "params" object with a numeric "type" property is required.'
181
+ );
182
+ }
183
+ const { type } = params;
184
+
185
+ const { integrationType } = getConfig();
186
+ if (integrationType === INTEGRATION_TYPE.ONBOARDING) {
187
+ return getRequerimentsByTypeUserOnBoarding(type);
188
+ }
189
+ if (integrationType === INTEGRATION_TYPE.ONPREMISE) {
190
+ return getRequerimentsByTypeUserOnPremise();
191
+ }
192
+
193
+ throw new ApacuanaAPIError(
194
+ `Unsupported integration type: ${integrationType}`,
195
+ 400,
196
+ "UNSUPPORTED_INTEGRATION_TYPE"
197
+ );
198
+ };
199
+
200
+ const requestCertificateOnBoarding = async (params) => {
201
+ try {
202
+ const response = await httpRequest(
203
+ "services/api/customer/request-certificate",
204
+ params,
205
+ "POST"
206
+ );
207
+
208
+ return new ApacuanaSuccess(response);
209
+ } catch (error) {
210
+ if (error instanceof ApacuanaAPIError) {
211
+ throw error;
212
+ }
213
+ throw new Error(`Failed to request certificate: ${error.message}`);
214
+ }
215
+ };
216
+
217
+ const requestCertificateOnPremise = async () => {
218
+ throw new ApacuanaAPIError(
219
+ "Requesting a certificate is not supported for integration type: ONPREMISE",
220
+ 501,
221
+ "NOT_IMPLEMENTED"
222
+ );
223
+ };
224
+
225
+ /**
226
+ * Requests a certificate for the user.
227
+ * @param {CertificateRequestParams} params - The parameters for the certificate request.
228
+ * @returns {Promise<RequestCertificateResponse>} Object with a confirmation message and a success indicator.
229
+ * @throws {ApacuanaAPIError} If the API response is invalid or the integration type is not supported.
230
+ * @throws {Error} If the request fails for another reason or the params are invalid.
231
+ */
232
+ export const requestCertificate = async (params) => {
233
+ if (
234
+ !params ||
235
+ typeof params.type !== "number" ||
236
+ !Array.isArray(params.documents)
237
+ ) {
238
+ throw new Error(
239
+ 'The "params" object with a numeric "type" property and a "documents" array is required.'
240
+ );
241
+ }
242
+
243
+ const { integrationType } = getConfig();
244
+ if (integrationType === INTEGRATION_TYPE.ONBOARDING) {
245
+ return requestCertificateOnBoarding(params);
246
+ }
247
+ if (integrationType === INTEGRATION_TYPE.ONPREMISE) {
248
+ return requestCertificateOnPremise();
249
+ }
250
+
251
+ throw new ApacuanaAPIError(
252
+ `Unsupported integration type: ${integrationType}`,
253
+ 400,
254
+ "UNSUPPORTED_INTEGRATION_TYPE"
255
+ );
256
+ };
@@ -0,0 +1,146 @@
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
+ /**
12
+ * Creates a new Face Liveness session.
13
+ * @returns {Promise<CreateFaceLivenessSessionResponse>} Object with the session ID and a success indicator.
14
+ * @throws {ApacuanaAPIError} If the API response is invalid or the integration type is not supported.
15
+ * @throws {Error} If the request fails for another reason.
16
+ */
17
+ const createFaceLivenessSessionOnBoarding = async () => {
18
+ try {
19
+ const response = await httpRequest(
20
+ "services/api/faceliveness/create",
21
+ {},
22
+ "POST"
23
+ );
24
+ if (!response.sessionid) {
25
+ throw new ApacuanaAPIError(
26
+ "The API response does not contain the session ID.",
27
+ response.status,
28
+ "INVALID_API_RESPONSE"
29
+ );
30
+ }
31
+
32
+ return new ApacuanaSuccess({
33
+ sessionId: response.sessionid,
34
+ });
35
+ } catch (error) {
36
+ if (error instanceof ApacuanaAPIError) {
37
+ throw error;
38
+ }
39
+ throw new ApacuanaAPIError(
40
+ `Failed to create Face Liveness session: ${error.message}`
41
+ );
42
+ }
43
+ };
44
+
45
+ const createFaceLivenessSessionOnPremise = async () => {
46
+ throw new ApacuanaAPIError(
47
+ "Creating a Face Liveness session is not supported for integration type: ONPREMISE",
48
+ 501,
49
+ "NOT_IMPLEMENTED"
50
+ );
51
+ };
52
+
53
+ const validateFaceLivenessOnBoarding = async (sessionId) => {
54
+ try {
55
+ const response = await httpRequest(
56
+ "services/api/faceliveness/validate",
57
+ { sessionid: sessionId },
58
+ "POST"
59
+ );
60
+ return new ApacuanaSuccess({ status: "verified", ...response });
61
+ } catch (error) {
62
+ if (error instanceof ApacuanaAPIError) {
63
+ let status;
64
+ switch (error.statusCode) {
65
+ case 402:
66
+ status = "waitingForScan";
67
+ break;
68
+ case 403:
69
+ status = "rejected";
70
+ break;
71
+ case 406:
72
+ status = "processing";
73
+ break;
74
+ case 408:
75
+ status = "expired";
76
+ break;
77
+ default:
78
+ throw error;
79
+ }
80
+ return new ApacuanaSuccess({ status });
81
+ }
82
+ throw error;
83
+ }
84
+ };
85
+
86
+ const validateFaceLivenessOnPremise = async () => {
87
+ throw new ApacuanaAPIError(
88
+ "Validating a Face Liveness session is not supported for integration type: ONPREMISE",
89
+ 501,
90
+ "NOT_IMPLEMENTED"
91
+ );
92
+ };
93
+
94
+ /**
95
+ * Creates a new Face Liveness session.
96
+ * @returns {Promise<CreateFaceLivenessSessionResponse>} Object with the session ID and a success indicator.
97
+ * @throws {ApacuanaAPIError} If the API response is invalid or the integration type is not supported.
98
+ * @throws {Error} If the request fails for another reason.
99
+ */
100
+ export const createFaceLivenessSession = async () => {
101
+ const { integrationType } = getConfig();
102
+
103
+ if (integrationType === INTEGRATION_TYPE.ONBOARDING) {
104
+ return createFaceLivenessSessionOnBoarding();
105
+ }
106
+ if (integrationType === INTEGRATION_TYPE.ONPREMISE) {
107
+ return createFaceLivenessSessionOnPremise();
108
+ }
109
+
110
+ throw new ApacuanaAPIError(
111
+ `Unsupported integration type: ${integrationType}`,
112
+ 400,
113
+ "UNSUPPORTED_INTEGRATION_TYPE"
114
+ );
115
+ };
116
+
117
+ /**
118
+ * Validates a Face Liveness session and returns its status.
119
+ * @param {{sessionId: string}} params - Object containing the session ID.
120
+ * @returns {Promise<ApacuanaSuccess>} Object with the validation status.
121
+ * @throws {ApacuanaAPIError} If the API response is invalid or the integration type is not supported.
122
+ */
123
+ export const validateFaceLiveness = async ({ sessionId }) => {
124
+ if (!sessionId) {
125
+ throw new ApacuanaAPIError(
126
+ "sessionId is a required parameter.",
127
+ 400,
128
+ "INVALID_PARAMETER"
129
+ );
130
+ }
131
+
132
+ const { integrationType } = getConfig();
133
+
134
+ if (integrationType === INTEGRATION_TYPE.ONBOARDING) {
135
+ return validateFaceLivenessOnBoarding(sessionId);
136
+ }
137
+ if (integrationType === INTEGRATION_TYPE.ONPREMISE) {
138
+ return validateFaceLivenessOnPremise();
139
+ }
140
+
141
+ throw new ApacuanaAPIError(
142
+ `Unsupported integration type: ${integrationType}`,
143
+ 400,
144
+ "UNSUPPORTED_INTEGRATION_TYPE"
145
+ );
146
+ };
@@ -0,0 +1,109 @@
1
+ import { httpRequest } from "../utils/httpClient";
2
+ import { getConfig } from "../config";
3
+ import { INTEGRATION_TYPE } from "../utils/constant";
4
+ import { ApacuanaAPIError } from "../errors";
5
+ import ApacuanaSuccess from "../success";
6
+
7
+ /** @typedef {import("../types/revocations").RequestRevocationResponse} RequestRevocationResponse */
8
+ /** @typedef {import("../types/revocations").GetRevocationReasonsResponse} GetRevocationReasonsResponse */
9
+
10
+ const requestRevocationOnBoarding = async (params) => {
11
+ try {
12
+ const response = await httpRequest(
13
+ "services/api/onboardingclient/requestcert",
14
+ { reason: params.reasonCode },
15
+ "POST"
16
+ );
17
+ return new ApacuanaSuccess(response);
18
+ } catch (error) {
19
+ if (error instanceof ApacuanaAPIError) {
20
+ throw error;
21
+ }
22
+ throw new Error(`Failed to request revocation: ${error.message}`);
23
+ }
24
+ };
25
+
26
+ const requestRevocationOnPremise = async () => {
27
+ throw new ApacuanaAPIError(
28
+ "Requesting revocation is not supported for integration type: ONPREMISE",
29
+ 501,
30
+ "NOT_IMPLEMENTED"
31
+ );
32
+ };
33
+
34
+ /**
35
+ * Solicita la revocación de un certificado.
36
+ * @param {object} params - Parámetros para la solicitud.
37
+ * @param {number} params.reasonCode - El código de la razón de la revocación.
38
+ * @returns {Promise<RequestRevocationResponse>} Una promesa que resuelve con la confirmación de la solicitud.
39
+ * @throws {Error} Si los parámetros son inválidos.
40
+ */
41
+ export const requestRevocation = async (params) => {
42
+ if (!params || typeof params.reasonCode !== "number") {
43
+ throw new Error(
44
+ 'The "params" object with a numeric "reasonCode" property is required.'
45
+ );
46
+ }
47
+
48
+ const { integrationType } = getConfig();
49
+
50
+ if (integrationType === INTEGRATION_TYPE.ONBOARDING) {
51
+ return requestRevocationOnBoarding(params);
52
+ }
53
+ if (integrationType === INTEGRATION_TYPE.ONPREMISE) {
54
+ return requestRevocationOnPremise(params);
55
+ }
56
+ throw new ApacuanaAPIError(
57
+ `Unsupported integration type: ${integrationType}`,
58
+ 400,
59
+ "UNSUPPORTED_INTEGRATION_TYPE"
60
+ );
61
+ };
62
+
63
+ const getRevocationReasonsOnBoarding = async () => {
64
+ try {
65
+ const response = await httpRequest(
66
+ "config/api/revocation/reasonsonboarding",
67
+ {},
68
+ "GET"
69
+ );
70
+ if (!response || !response.records) {
71
+ throw new ApacuanaAPIError("Failed to fetch revocation reasons.");
72
+ }
73
+ return new ApacuanaSuccess({ reasons: response.records });
74
+ } catch (error) {
75
+ if (error instanceof ApacuanaAPIError) {
76
+ throw error;
77
+ }
78
+ throw new Error(`Failed to get revocation reasons: ${error.message}`);
79
+ }
80
+ };
81
+
82
+ const getRevocationReasonsOnPremise = async () => {
83
+ throw new ApacuanaAPIError(
84
+ "Getting revocation reasons is not supported for integration type: ONPREMISE",
85
+ 501,
86
+ "NOT_IMPLEMENTED"
87
+ );
88
+ };
89
+
90
+ /**
91
+ * Obtiene la lista de razones de revocación de certificados.
92
+ * @returns {Promise<GetRevocationReasonsResponse>} Una promesa que resuelve con la lista de razones de revocación.
93
+ * @throws {ApacuanaAPIError} Si la llamada a la API falla o el tipo de integración no es soportado.
94
+ */
95
+ export const getRevocationReasons = async () => {
96
+ const { integrationType } = getConfig();
97
+
98
+ if (integrationType === INTEGRATION_TYPE.ONBOARDING) {
99
+ return getRevocationReasonsOnBoarding();
100
+ }
101
+ if (integrationType === INTEGRATION_TYPE.ONPREMISE) {
102
+ return getRevocationReasonsOnPremise();
103
+ }
104
+ throw new ApacuanaAPIError(
105
+ `Unsupported integration type: ${integrationType}`,
106
+ 400,
107
+ "UNSUPPORTED_INTEGRATION_TYPE"
108
+ );
109
+ };