@facturacr/atv-sdk 0.0.0-beta → 0.0.2-beta

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 (176) hide show
  1. package/.env.example +7 -0
  2. package/.github/ISSUE_TEMPLATE/bug_report.md +38 -0
  3. package/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
  4. package/.github/pull_request_template.md +7 -0
  5. package/.github/workflows/ci.yml +3 -3
  6. package/CODE_OF_CONDUCT.md +128 -0
  7. package/LICENSE +21 -0
  8. package/README.md +8 -1
  9. package/__tests__/stubs/dummyKeys/README.md +19 -0
  10. package/__tests__/stubs/frontendRequest.stub.ts +6 -5
  11. package/__tests__/stubs/token.stub.ts +18 -0
  12. package/__tests__/tests/electronicBill.test.ts +28 -0
  13. package/__tests__/{unit → tests}/getClave.test.ts +2 -2
  14. package/__tests__/tests/lib/getClave.test.ts +11 -0
  15. package/__tests__/tests/services/getToken.test.ts +15 -0
  16. package/atv-structures/4.3/FacturaElectronicaExportacion_V4.3.xsd.xml +1747 -0
  17. package/atv-structures/4.3/FacturaElectronica_V4.3.xsd.xml +1781 -0
  18. package/dist/__tests__/stubs/frontendRequest.stub.d.ts +3 -0
  19. package/dist/__tests__/stubs/frontendRequest.stub.js +61 -0
  20. package/dist/__tests__/stubs/frontendRequest.stub.js.map +1 -0
  21. package/dist/__tests__/stubs/token.stub.d.ts +17 -0
  22. package/dist/__tests__/stubs/token.stub.js +21 -0
  23. package/dist/__tests__/stubs/token.stub.js.map +1 -0
  24. package/dist/__tests__/tests/electronicBill.test.d.ts +1 -0
  25. package/dist/__tests__/tests/electronicBill.test.js +38 -0
  26. package/dist/__tests__/tests/electronicBill.test.js.map +1 -0
  27. package/dist/__tests__/tests/getClave.test.d.ts +1 -0
  28. package/dist/__tests__/tests/getClave.test.js +15 -0
  29. package/dist/__tests__/tests/getClave.test.js.map +1 -0
  30. package/dist/__tests__/tests/getToken.test.d.ts +0 -0
  31. package/dist/__tests__/tests/getToken.test.js +28 -0
  32. package/dist/__tests__/tests/getToken.test.js.map +1 -0
  33. package/dist/__tests__/tests/lib/getClave.test.d.ts +1 -0
  34. package/dist/__tests__/tests/lib/getClave.test.js +15 -0
  35. package/dist/__tests__/tests/lib/getClave.test.js.map +1 -0
  36. package/dist/__tests__/tests/services/getToken.test.d.ts +1 -0
  37. package/dist/__tests__/tests/services/getToken.test.js +27 -0
  38. package/dist/__tests__/tests/services/getToken.test.js.map +1 -0
  39. package/dist/examples/confirmXML.d.ts +1 -0
  40. package/dist/examples/confirmXML.js +89 -0
  41. package/dist/examples/confirmXML.js.map +1 -0
  42. package/dist/examples/consultXML.d.ts +1 -0
  43. package/dist/examples/consultXML.js +55 -0
  44. package/dist/examples/consultXML.js.map +1 -0
  45. package/dist/examples/createAndSend.d.ts +1 -0
  46. package/dist/examples/createAndSend.js +78 -0
  47. package/dist/examples/createAndSend.js.map +1 -0
  48. package/dist/examples/createCreditNote.d.ts +1 -0
  49. package/dist/examples/createCreditNote.js +52 -0
  50. package/dist/examples/createCreditNote.js.map +1 -0
  51. package/dist/examples/createDebitNote.d.ts +1 -0
  52. package/dist/examples/createDebitNote.js +52 -0
  53. package/dist/examples/createDebitNote.js.map +1 -0
  54. package/dist/examples/genBasicXML.d.ts +1 -0
  55. package/dist/examples/genBasicXML.js +44 -0
  56. package/dist/examples/genBasicXML.js.map +1 -0
  57. package/dist/examples/getClave.d.ts +1 -0
  58. package/dist/examples/getClave.js +12 -0
  59. package/dist/examples/getClave.js.map +1 -0
  60. package/dist/examples/getToken.d.ts +1 -0
  61. package/dist/examples/getToken.js +22 -0
  62. package/dist/examples/getToken.js.map +1 -0
  63. package/dist/src/confirmXML.d.ts +8 -0
  64. package/dist/src/confirmXML.js +44 -0
  65. package/dist/src/confirmXML.js.map +1 -0
  66. package/dist/src/creditNote.d.ts +14 -0
  67. package/dist/src/creditNote.js +77 -0
  68. package/dist/src/creditNote.js.map +1 -0
  69. package/dist/src/data/codigoMoneda.d.ts +1 -0
  70. package/dist/src/data/codigoMoneda.js +184 -0
  71. package/dist/src/data/codigoMoneda.js.map +1 -0
  72. package/dist/src/data/exoneracion.d.ts +1 -0
  73. package/dist/src/data/exoneracion.js +15 -0
  74. package/dist/src/data/exoneracion.js.map +1 -0
  75. package/dist/src/data/impuestos.d.ts +25 -0
  76. package/dist/src/data/impuestos.js +51 -0
  77. package/dist/src/data/impuestos.js.map +1 -0
  78. package/dist/src/data/tipoCedula.d.ts +7 -0
  79. package/dist/src/data/tipoCedula.js +17 -0
  80. package/dist/src/data/tipoCedula.js.map +1 -0
  81. package/dist/src/data/tipoDocumento.d.ts +26 -0
  82. package/dist/src/data/tipoDocumento.js +30 -0
  83. package/dist/src/data/tipoDocumento.js.map +1 -0
  84. package/dist/src/data/unidadesMedida.d.ts +5 -0
  85. package/dist/src/data/unidadesMedida.js +141 -0
  86. package/dist/src/data/unidadesMedida.js.map +1 -0
  87. package/dist/src/debitNote.d.ts +14 -0
  88. package/dist/src/debitNote.js +77 -0
  89. package/dist/src/debitNote.js.map +1 -0
  90. package/dist/src/electronicBill.d.ts +3 -0
  91. package/dist/src/electronicBill.js +61 -0
  92. package/dist/src/electronicBill.js.map +1 -0
  93. package/dist/src/helpers/comprobantes.d.ts +7 -0
  94. package/dist/src/helpers/comprobantes.js +92 -0
  95. package/dist/src/helpers/comprobantes.js.map +1 -0
  96. package/dist/src/index.d.ts +7 -0
  97. package/dist/src/index.js +42 -0
  98. package/dist/src/index.js.map +1 -0
  99. package/dist/src/lib/genClave/index.d.ts +9 -0
  100. package/dist/src/lib/genClave/index.js +119 -0
  101. package/dist/src/lib/genClave/index.js.map +1 -0
  102. package/dist/src/lib/genJSON/confirmXML.d.ts +7 -0
  103. package/dist/src/lib/genJSON/confirmXML.js +46 -0
  104. package/dist/src/lib/genJSON/confirmXML.js.map +1 -0
  105. package/dist/src/lib/genJSON/index.d.ts +3 -0
  106. package/dist/src/lib/genJSON/index.js +133 -0
  107. package/dist/src/lib/genJSON/index.js.map +1 -0
  108. package/dist/src/lib/genXML/index.d.ts +7 -0
  109. package/dist/src/lib/genXML/index.js +51 -0
  110. package/dist/src/lib/genXML/index.js.map +1 -0
  111. package/dist/src/lib/genXML/sigXML/genKeysAndCert.d.ts +6 -0
  112. package/dist/src/lib/genXML/sigXML/genKeysAndCert.js +77 -0
  113. package/dist/src/lib/genXML/sigXML/genKeysAndCert.js.map +1 -0
  114. package/dist/src/lib/genXML/sigXML/index.d.ts +1 -0
  115. package/dist/src/lib/genXML/sigXML/index.js +86 -0
  116. package/dist/src/lib/genXML/sigXML/index.js.map +1 -0
  117. package/dist/src/lib/genXML/xmlConfig.d.ts +55 -0
  118. package/dist/src/lib/genXML/xmlConfig.js +36 -0
  119. package/dist/src/lib/genXML/xmlConfig.js.map +1 -0
  120. package/dist/src/services/getToken/index.d.ts +3 -0
  121. package/dist/src/services/getToken/index.js +22 -0
  122. package/dist/src/services/getToken/index.js.map +1 -0
  123. package/dist/src/services/send/index.d.ts +2 -0
  124. package/dist/src/services/send/index.js +40 -0
  125. package/dist/src/services/send/index.js.map +1 -0
  126. package/dist/src/types/xml/notaDeCredito.d.ts +21 -0
  127. package/dist/src/types/xml/notaDeCredito.js +3 -0
  128. package/dist/src/types/xml/notaDeCredito.js.map +1 -0
  129. package/dist/src/types/xml/notaDeDebito.d.ts +21 -0
  130. package/dist/src/types/xml/notaDeDebito.js +3 -0
  131. package/dist/src/types/xml/notaDeDebito.js.map +1 -0
  132. package/examples/README.md +54 -0
  133. package/{test/examples → examples}/createAndSend.ts +16 -12
  134. package/{test/examples → examples}/createCreditNote.ts +3 -3
  135. package/{test/examples → examples}/createDebitNote.ts +3 -3
  136. package/{test/examples → examples}/genBasicXML.ts +2 -2
  137. package/{test/examples → examples}/getClave.ts +2 -2
  138. package/examples/getToken.ts +15 -0
  139. package/jest.config.js +20 -3
  140. package/package.json +15 -18
  141. package/src/ATV.ts +28 -0
  142. package/src/confirmXML.ts +4 -4
  143. package/src/creditNote.ts +9 -9
  144. package/src/data/codigoMoneda.ts +180 -0
  145. package/src/data/exoneracion.ts +11 -0
  146. package/src/data/impuestos.ts +26 -0
  147. package/src/data/tipoCedula.ts +8 -0
  148. package/src/data/unidadesMedida.ts +99 -0
  149. package/src/debitNote.ts +9 -9
  150. package/src/electronicBill.ts +17 -9
  151. package/src/helpers/comprobantes.ts +7 -7
  152. package/src/lib/genClave/index.ts +7 -7
  153. package/src/lib/genJSON/confirmXML.ts +2 -2
  154. package/src/lib/genJSON/index.ts +23 -21
  155. package/src/lib/genXML/index.ts +3 -9
  156. package/src/lib/genXML/sigXML/genKeysAndCert.ts +1 -5
  157. package/src/lib/genXML/sigXML/index.ts +1 -1
  158. package/src/lib/genXML/xmlConfig.ts +14 -1
  159. package/src/services/getToken/GetToken.ts +54 -0
  160. package/src/services/getToken/__tests__/GetToken.test.ts +82 -0
  161. package/src/services/getToken/index.ts +1 -1
  162. package/src/services/getToken/types.ts +30 -0
  163. package/src/services/send/index.ts +2 -2
  164. package/src/types/{facturaInterfaces.ts → facturaInterfaces.d.ts} +18 -1
  165. package/src/types/globalInterfaces.d.ts +5 -2
  166. package/src/types/xml/notaDeCredito.ts +1 -1
  167. package/src/types/xml/notaDeDebito.ts +1 -1
  168. package/{test/tools → tools}/strToClave.ts +1 -1
  169. package/tools/xsdToJsonSchema.ts +108 -0
  170. package/tsconfig.json +5 -2
  171. package/test/examples/README.md +0 -43
  172. package/test/examples/getToken.ts +0 -18
  173. /package/__tests__/{unit → tests}/getToken.test.ts +0 -0
  174. /package/{test/examples → examples}/confirmXML.ts +0 -0
  175. /package/{test/examples → examples}/consultXML.ts +0 -0
  176. /package/{test/tools → tools}/readXML.ts +0 -0
@@ -1,8 +1,14 @@
1
1
  const FE_XML_SCHEMA_NS = 'https://cdn.comprobanteselectronicos.go.cr/xml-schemas/v4.3/facturaElectronica'
2
2
  const FE_XML_SCHEMA_XSI = 'https://www.hacienda.go.cr/ATV/ComprobanteElectronico/docs/esquemas/2016/v4.3/FacturaElectronica_V4.3.xsd'
3
+
4
+ const FEE_XML_SCHEMA_NS = 'https://cdn.comprobanteselectronicos.go.cr/xml-schemas/v4.3/facturaElectronicaExportacion'
5
+ const FEE_XML_SCHEMA_XSI = 'https://cdn.comprobanteselectronicos.go.cr/xml-schemas/v4.3/facturaElectronicaExportacion.xsd'
6
+
3
7
  const MR_XML_SCHEMA_NS = 'https://cdn.comprobanteselectronicos.go.cr/xml-schemas/v4.3/mensajeReceptor'
8
+
4
9
  const NC_XML_SCHEMA_NS = 'https://cdn.comprobanteselectronicos.go.cr/xml-schemas/v4.3/notaCreditoElectronica'
5
10
  const NC_XML_SCHEMA_XSI = 'https://www.hacienda.go.cr/ATV/ComprobanteElectronico/docs/esquemas/2016/v4.3/NotaCreditoElectronica_V4.3.xsd'
11
+
6
12
  const ND_XML_SCHEMA_NS = 'https://cdn.comprobanteselectronicos.go.cr/xml-schemas/v4.3/notaDebitoElectronica'
7
13
  const ND_XML_SCHEMA_XSI = 'https://www.hacienda.go.cr/ATV/ComprobanteElectronico/docs/esquemas/2016/v4.3/NotaDebitoElectronica_V4.3.xsd'
8
14
 
@@ -24,6 +30,12 @@ const FE_XML_ATTRS = { // Factura Electronica
24
30
  'xsi:schemaLocation': `${FE_XML_SCHEMA_NS} ${FE_XML_SCHEMA_XSI}`
25
31
  }
26
32
 
33
+ const FEE_XML_ATTRS = { // Factura Electronica Exportación
34
+ xmlns: FE_XML_SCHEMA_NS,
35
+ ...COMMON_STRUCTURE,
36
+ 'xsi:schemaLocation': `${FEE_XML_SCHEMA_NS} ${FEE_XML_SCHEMA_XSI}`
37
+ }
38
+
27
39
  const NC_XML_ATTRS = { // Nota Credito Electronica
28
40
  xmlns: NC_XML_SCHEMA_NS,
29
41
  ...COMMON_STRUCTURE,
@@ -43,7 +55,8 @@ const MR_XML_ATTRS = { // Mensaje Receptor
43
55
  }
44
56
 
45
57
  export const xmlExtructures = {
46
- FE: FE_XML_ATTRS,
58
+ FacturaElectronica: FE_XML_ATTRS,
59
+ FacturaElectronicaExportacion: FEE_XML_ATTRS,
47
60
  NC: NC_XML_ATTRS,
48
61
  ND: ND_XML_ATTRS,
49
62
  CCE: MR_XML_ATTRS,
@@ -0,0 +1,54 @@
1
+ import axios from 'axios'
2
+ import qs from 'querystring'
3
+ import { GetTokenDto, GetTokenInternalProps, GetTokenRawResponse, GetTokenResponse } from './types'
4
+ import { ATV } from '@src/ATV'
5
+
6
+ const MAIN_DOMAIN = 'https://idp.comprobanteselectronicos.go.cr'
7
+ const options: { [key: string]: GetTokenInternalProps} = {
8
+ prod: {
9
+ serviceUrl: `${MAIN_DOMAIN}/auth/realms/rut/protocol/openid-connect/token`,
10
+ clientId: 'api'
11
+ },
12
+ stg: {
13
+ serviceUrl: `${MAIN_DOMAIN}/auth/realms/rut-stag/protocol/openid-connect/token`,
14
+ clientId: 'api-stag'
15
+ }
16
+ }
17
+
18
+ export class GetToken {
19
+ private readonly props: GetTokenInternalProps;
20
+ constructor(scope: ATV) {
21
+ this.props = this.initProps(scope.mode)
22
+ }
23
+
24
+ public async execute({ username, password }: GetTokenDto): Promise<GetTokenResponse> {
25
+ const fetchResponse = await axios.post<GetTokenRawResponse>(this.props.serviceUrl, qs.stringify({
26
+ // eslint-disable-next-line @typescript-eslint/camelcase
27
+ client_id: this.props.clientId,
28
+ // eslint-disable-next-line @typescript-eslint/camelcase
29
+ grant_type: 'password',
30
+ // eslint-disable-next-line @typescript-eslint/camelcase
31
+ client_secret: '',
32
+ username,
33
+ password
34
+ }), {
35
+ headers: {
36
+ 'Content-Type': 'application/x-www-form-urlencoded'
37
+ }
38
+ })
39
+ const data = fetchResponse.data
40
+ return {
41
+ accessToken: data.access_token,
42
+ expiresIn: data.expires_in,
43
+ refreshToken: data.refresh_token,
44
+ refreshExpiresIn: data.refresh_expires_in,
45
+ sessionState: data.session_state,
46
+ tokenType: data.token_type,
47
+ scope: data.scope
48
+ }
49
+ }
50
+
51
+ private initProps(mode: string): GetTokenInternalProps {
52
+ return options[mode] || options.prod
53
+ }
54
+ }
@@ -0,0 +1,82 @@
1
+ /* eslint-disable @typescript-eslint/camelcase */
2
+ import axios from 'axios'
3
+ import qs from 'querystring'
4
+ import { GetToken } from '../GetToken'
5
+ import { ATV } from '@src/ATV'
6
+ import { GetTokenDto } from '../types'
7
+
8
+ jest.mock('@src/ATV')
9
+ jest.mock('axios')
10
+ jest.mock('querystring')
11
+
12
+ const fetchResponse = {
13
+ data: {
14
+ access_token: 'access_token',
15
+ expires_in: 3600,
16
+ refresh_token: 'refresh_token',
17
+ refresh_expires_in: 1209600,
18
+ session_state: 'session_state',
19
+ token_type: 'Bearer',
20
+ scope: 'openid'
21
+ }
22
+ }
23
+ describe('GetToken', () => {
24
+ let atv: ATV
25
+ let getToken: GetToken
26
+
27
+ beforeEach(() => {
28
+ atv = new ATV()
29
+ getToken = new GetToken(atv)
30
+ ;(axios.post as jest.Mock).mockResolvedValue(fetchResponse)
31
+ })
32
+
33
+ afterEach(() => {
34
+ jest.resetAllMocks()
35
+ })
36
+
37
+ it('should call axios.post with correct parameters', async () => {
38
+ // Arrange
39
+ const dto: GetTokenDto = {
40
+ username: 'testuser',
41
+ password: 'testpassword'
42
+ }
43
+ const expectedUrl = 'https://idp.comprobanteselectronicos.go.cr/auth/realms/rut/protocol/openid-connect/token'
44
+ const expectedData = qs.stringify({
45
+ client_id: 'api-stag',
46
+ grant_type: 'password',
47
+ client_secret: '',
48
+ username: 'testuser',
49
+ password: 'testpassword'
50
+ })
51
+ const expectedConfig = {
52
+ headers: {
53
+ 'Content-Type': 'application/x-www-form-urlencoded'
54
+ }
55
+ }
56
+
57
+ // Act
58
+ await getToken.execute(dto)
59
+
60
+ // Assert
61
+ expect(axios.post).toHaveBeenCalledWith(expectedUrl, expectedData, expectedConfig)
62
+ })
63
+
64
+ it('should return expected response', async () => {
65
+ // Arrange
66
+ const expectedResponse = {
67
+ accessToken: 'access_token',
68
+ expiresIn: 3600,
69
+ refreshToken: 'refresh_token',
70
+ refreshExpiresIn: 1209600,
71
+ sessionState: 'session_state',
72
+ tokenType: 'Bearer',
73
+ scope: 'openid'
74
+ }
75
+
76
+ // Act
77
+ const result = await getToken.execute({ username: 'testuser', password: 'testpassword' })
78
+
79
+ // Assert
80
+ expect(result).toEqual(expectedResponse)
81
+ })
82
+ })
@@ -1,6 +1,6 @@
1
1
  import axios from 'axios'
2
2
  import qs from 'querystring'
3
- import { postTokenOptions } from './interfaces'
3
+ import { postTokenOptions } from '@src/services/getToken/interfaces'
4
4
 
5
5
  const MAIN_DOMAIN = 'https://idp.comprobanteselectronicos.go.cr'
6
6
  const RUT = (process.env.IS_STG) ? 'rut-stag' : 'rut'
@@ -0,0 +1,30 @@
1
+ export type GetTokenDto = {
2
+ username: string;
3
+ password: string;
4
+ }
5
+
6
+ export type GetTokenInternalProps = {
7
+ serviceUrl: string;
8
+ clientId: string;
9
+ };
10
+
11
+ export type GetTokenRawResponse = {
12
+ access_token: string;
13
+ expires_in: number;
14
+ refresh_expires_in: number;
15
+ refresh_token: string;
16
+ token_type: string;
17
+ 'not-before-policy': number;
18
+ session_state: string;
19
+ scope: string;
20
+ }
21
+
22
+ export type GetTokenResponse = {
23
+ accessToken: string;
24
+ expiresIn: number;
25
+ refreshExpiresIn: number;
26
+ refreshToken: string;
27
+ tokenType: string;
28
+ sessionState: string;
29
+ scope: string;
30
+ }
@@ -3,8 +3,8 @@ import axios from 'axios'
3
3
  const MAIN_DOMAIN = 'https://api.comprobanteselectronicos.go.cr/'
4
4
  const RUT = (process.env.IS_STG) ? 'recepcion-sandbox' : 'recepcion'
5
5
  const PATH = `${RUT}/v1/recepcion/`
6
-
7
- const URL = MAIN_DOMAIN + PATH
6
+ const SANBOX_URL = 'https://api-sandbox.comprobanteselectronicos.go.cr/recepcion/v1/recepcion'
7
+ const URL = (process.env.IS_STG) ? SANBOX_URL : MAIN_DOMAIN + PATH
8
8
 
9
9
  export function send(token: string, postOptions: any): Record<string, any> {
10
10
  return axios({
@@ -95,6 +95,23 @@ export interface FacturaElectronica {
95
95
  };
96
96
  }
97
97
 
98
+ export interface FacturaElectronicaExportacion {
99
+ Clave: string;
100
+ CodigoActividad: string;
101
+ NumeroConsecutivo: string;
102
+ FechaEmision?: Date;
103
+ Emisor: Persona;
104
+ Receptor: Persona;
105
+ CondicionVenta?: string;
106
+ PlazoCredito?: string;
107
+ MedioPago?: string;
108
+ DetalleServicio?: DetalleServicio;
109
+ ResumenFactura: Resumen;
110
+ Otros?: {
111
+ OtroTexto: string;
112
+ };
113
+ }
114
+
98
115
  export interface FacturaElectronicaContenedor {
99
- FacturaElectronica: FacturaElectronica;
116
+ [key: 'FacturaElectronica' | 'FacturaElectronicaExportacion']: FacturaElectronica | FacturaElectronicaExportacion;
100
117
  }
@@ -1,6 +1,6 @@
1
- import { Persona, LineaDetalle } from './facturaInterfaces'
1
+ import { Persona, LineaDetalle } from '@src/types/facturaInterfaces'
2
2
 
3
- export interface FrontEndRequest {
3
+ export interface ClientPayload {
4
4
  Emisor: Persona;
5
5
  Receptor: Persona;
6
6
  sucursal?: string;
@@ -11,7 +11,10 @@ export interface FrontEndRequest {
11
11
  consecutivo: string;
12
12
  situationEC: string;
13
13
  actividad: string;
14
+ total?: number; // deprecated
15
+ impuesto?: number; // deprecated
14
16
  LineasDetalle: Array<LineaDetalle>;
17
+ facturaElectronicaType?: 'FacturaElectronica' | 'FacturaElectronicaExportacion';
15
18
  }
16
19
 
17
20
  export interface ClaveOpts {
@@ -1,4 +1,4 @@
1
- import { Persona, DetalleServicio, Resumen } from '../facturaInterfaces'
1
+ import { Persona, DetalleServicio, Resumen } from '@src/types/facturaInterfaces'
2
2
 
3
3
  type NotaCreditoElectronica = {
4
4
  Clave: string;
@@ -1,4 +1,4 @@
1
- import { Persona, DetalleServicio, Resumen } from '../facturaInterfaces'
1
+ import { Persona, DetalleServicio, Resumen } from '@src/types/facturaInterfaces'
2
2
 
3
3
  type NotaDebitoElectronica = {
4
4
  Clave: string;
@@ -1,4 +1,4 @@
1
- import { stringToClave } from '../../src/lib/genClave'
1
+ import { stringToClave } from '@src/lib/genClave'
2
2
 
3
3
  const output = stringToClave('50608022000310275915700100001010000000006100012313')
4
4
  console.log('output', output)
@@ -0,0 +1,108 @@
1
+ /* ======================================= */
2
+ /* ============== WIP ==================== */
3
+ /* ======================================= */
4
+ /* Util to import ATV xsd and get some info like enums */
5
+ import fs from 'fs'
6
+ import * as Parser from 'fast-xml-parser'
7
+
8
+ const options = {
9
+ ignoreAttributes: false,
10
+ ignoreNameSpace: true,
11
+ parseNodeValue: false,
12
+ attrNodeName: 'attributes',
13
+ attributeNamePrefix: ''
14
+ }
15
+ const sourceURI = process.env.XSD_SOURCE
16
+ const xmlString = fs.readFileSync(sourceURI, 'utf8')
17
+
18
+ const json = Parser.parse(xmlString, options)
19
+ const originalShema = json?.schema
20
+ const parentElement = originalShema?.element
21
+
22
+ const complexElements = originalShema?.complexType
23
+ const singleElements = originalShema?.simpleType
24
+
25
+ function processEnumerations(array: Array<any>): any {
26
+ return array.map((enumeration) => {
27
+ return enumeration?.attributes?.value
28
+ })
29
+ }
30
+
31
+ function processLoop(array: Array<any>): any {
32
+ const newObj = {}
33
+ for (let i = 0; i < array.length; i++) {
34
+ const element = array[i]
35
+ const propKey = element?.attributes?.name
36
+ if (propKey) {
37
+ newObj[propKey] = {
38
+ description: element?.annotation?.documentation
39
+ }
40
+ }
41
+ }
42
+ return newObj
43
+ }
44
+
45
+ function processObject(object: any): any {
46
+ const props = processLoop(object?.complexType?.sequence?.element)
47
+ const newObject = {}
48
+ newObject[object?.attributes?.name] = {
49
+ type: 'object',
50
+ properties: props
51
+ }
52
+ return newObject
53
+ }
54
+
55
+ function processSimpleType(object: any): any {
56
+ const newObject = {}
57
+ const enumeration = object.restriction?.enumeration || []
58
+ const enumationList = processEnumerations(enumeration)
59
+ newObject[object?.attributes?.name] = {
60
+ type: object.restriction?.attributes?.base,
61
+ enum: enumationList,
62
+ pattern: object.restriction?.pattern
63
+ }
64
+ return newObject
65
+ }
66
+
67
+ function processElementsFromComplexType(array: Array<any>): any {
68
+ const newObj = {}
69
+ for (let i = 0; i < array.length; i++) {
70
+ const element = array[i]
71
+ const propKey = element?.attributes?.name
72
+ const simpleType = element?.simpleType ? processSimpleType(element?.simpleType) : {}
73
+ if (propKey) {
74
+ newObj[propKey] = {
75
+ description: element?.annotation?.documentation,
76
+ simpleType: simpleType
77
+ }
78
+ }
79
+ }
80
+ return newObj
81
+ }
82
+
83
+ function processComplexType(object: any): any {
84
+ const elements = processElementsFromComplexType(object?.sequence?.element)
85
+ const newObject = {}
86
+ newObject[object?.attributes?.name] = {
87
+ elements: elements
88
+ }
89
+ return newObject
90
+ }
91
+
92
+ const schema: Record<string, any> = {
93
+ $schema: 'https://json-schema.org/draft/2020-12/schema',
94
+ $id: 'https://facere.cr/schema.json',
95
+ title: 'undefined',
96
+ description: 'undefined',
97
+ type: 'object',
98
+ properties: {}
99
+ }
100
+
101
+ schema.properties = processObject(parentElement)
102
+ schema.singleElements = singleElements.map(processSimpleType)
103
+ schema.complexElements = complexElements.map(processComplexType)
104
+
105
+ const data = JSON.stringify(schema, null, 2)
106
+ fs.writeFile('./out.json', data, (err) => {
107
+ if (err) throw err
108
+ })
package/tsconfig.json CHANGED
@@ -4,18 +4,21 @@
4
4
  "esModuleInterop": true,
5
5
  "target": "es6",
6
6
  "moduleResolution": "node",
7
+ "declaration": true,
7
8
  "module": "commonjs",
8
9
  "sourceMap": true,
9
10
  "rootDir": ".",
10
11
  "outDir": "dist",
11
12
  "baseUrl": ".",
13
+ "skipLibCheck": true,
12
14
  "lib": [
13
15
  "esnext", "dom"
14
16
  ],
15
17
  "paths": {
16
18
  "@src/*": ["src/*"],
17
- "@test/*": ["test/*"]
19
+ "@test/*": ["__tests__/*"]
18
20
  }
19
21
  },
20
- "include": ["./src/**/*", "./test/**/*", "__tests__/stubs"]
22
+ "include": ["./src/**/*", "__tests__/**/*", "__tests__/stubs", "examples"],
23
+ "exclude": ["node_modules/**/*"]
21
24
  }
@@ -1,43 +0,0 @@
1
- # Ejemplos con implementaciones para pruebas
2
-
3
- Para probar crear y enviar factura
4
- ```
5
- ts-node -r tsconfig-paths/register test/examples/createAndSend.ts
6
- --require dotenv/config test/examples/createAndSend.ts
7
- ```
8
-
9
- Para probar genJson HeaderXML
10
- ```
11
- ts-node -r tsconfig-paths/register test/examples/createAndSend.ts
12
- --require dotenv/config test/examples/genBasicXML.ts
13
- ```
14
-
15
- Generar la clave
16
- ```
17
- ts-node -r tsconfig-paths/register test/examples/createAndSend.ts
18
- --require dotenv/config test/examples/getClave.ts
19
- ```
20
-
21
- Para crear una nota de credito
22
- ```
23
- ts-node -r tsconfig-paths/register test/examples/createAndSend.ts
24
- --require dotenv/config test/examples/createCreditNote.ts
25
- ```
26
-
27
- ## Testing
28
- Para probar las funcionalidades se recomienda crear un .env en donde se especifiquen datos de prueba.
29
- ```
30
- USERNAME_TEST=
31
- PASSWORD_TEST=
32
- IS_STG=1
33
- SOURCE_P12_URI=
34
- SOURCE_P12_PASSPORT=
35
- SOURCE_URI_XML_OUTPUT=
36
- XML_TO_CONFIRM=
37
- ```
38
-
39
- ## Tools
40
- Para usar readXML establecer en .env
41
- ```
42
- SOURCE_URI=
43
- ```
@@ -1,18 +0,0 @@
1
- import getToken from '@src/services/getToken'
2
-
3
- const IS_STG = process.env.IS_STG
4
- const USERNAME_TEST = process.env.USERNAME_TEST
5
- const PASSWORD_TEST = process.env.PASSWORD_TEST
6
- console.log('process.env.IS_STG', IS_STG)
7
-
8
- getToken({
9
- client_id: 'api-stag', // eslint-disable-line @typescript-eslint/camelcase
10
- client_secret: '', // eslint-disable-line @typescript-eslint/camelcase
11
- grant_type: 'password', // eslint-disable-line @typescript-eslint/camelcase
12
- username: USERNAME_TEST,
13
- password: PASSWORD_TEST
14
- }).then((result) => {
15
- console.log('res', result.data)
16
- }).catch((e) => {
17
- console.log('e', e.response.data)
18
- })
File without changes
File without changes
File without changes
File without changes