@emitia/node 0.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.
- package/LICENSE +21 -0
- package/README.md +168 -0
- package/dist/index.d.ts +3587 -0
- package/dist/index.js +888 -0
- package/dist/index.js.map +1 -0
- package/package.json +66 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Pedro / Propiter <pro.pedro0413@gmail.com>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
# @emitia/node
|
|
2
|
+
|
|
3
|
+
Official Node.js / TypeScript SDK for the [Emitia API](https://docs.emitia.co) — electronic invoicing for Colombia (DIAN), Stripe-style.
|
|
4
|
+
|
|
5
|
+
**Requirements**: Node.js >= 20, ESM project (`"type": "module"` in package.json).
|
|
6
|
+
|
|
7
|
+
The SDK ships with a single dependency — `zod` — used **only** to derive the published TypeScript types. It is never imported at runtime: execution runs on native `fetch` with nothing else loaded.
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @emitia/node
|
|
15
|
+
# or
|
|
16
|
+
pnpm add @emitia/node
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## Quickstart
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
import { Emitia } from '@emitia/node';
|
|
25
|
+
|
|
26
|
+
const emitia = new Emitia('sk_test_your_key_here');
|
|
27
|
+
|
|
28
|
+
// List invoices
|
|
29
|
+
const invoices = await emitia.invoices.list({ limit: 10 });
|
|
30
|
+
console.log(invoices.data);
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Create an invoice
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
const invoice = await emitia.invoices.create({
|
|
37
|
+
customer_id: 'cust_01HX...',
|
|
38
|
+
numbering_resolution_id: 'res_01HX...',
|
|
39
|
+
issue_date: '2024-06-01',
|
|
40
|
+
currency: 'COP',
|
|
41
|
+
lines: [
|
|
42
|
+
{
|
|
43
|
+
description: 'Servicio de desarrollo',
|
|
44
|
+
quantity: '1.00',
|
|
45
|
+
unit_price: '5000000.00',
|
|
46
|
+
tax_rate: '0.19',
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
console.log(invoice.id); // inv_01HX...
|
|
52
|
+
console.log(invoice.status); // 'processing'
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Typed error handling
|
|
56
|
+
|
|
57
|
+
All non-2xx responses throw a typed subclass of `EmitiaError`:
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
import { Emitia, NotFoundError, ValidationError, DianRejectedError } from '@emitia/node';
|
|
61
|
+
|
|
62
|
+
try {
|
|
63
|
+
await emitia.invoices.retrieve('inv_nonexistent');
|
|
64
|
+
} catch (err) {
|
|
65
|
+
if (err instanceof NotFoundError) {
|
|
66
|
+
console.error('Invoice not found:', err.code);
|
|
67
|
+
} else if (err instanceof ValidationError) {
|
|
68
|
+
console.error('Validation failed:', err.details);
|
|
69
|
+
} else if (err instanceof DianRejectedError) {
|
|
70
|
+
console.error('DIAN rejected the document:', err.message);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Error classes: `AuthenticationError`, `PermissionError`, `NotFoundError`, `ValidationError`, `RateLimitError`, `IdempotencyError`, `DianRejectedError`, `DianUnavailableError`, `CertificateError`, `NumberingError`, `ApiError`.
|
|
76
|
+
|
|
77
|
+
All errors expose: `.type`, `.code`, `.message`, `.requestId`, `.statusCode`, `.docUrl?`, `.details?`.
|
|
78
|
+
|
|
79
|
+
### Verify a webhook signature
|
|
80
|
+
|
|
81
|
+
`constructEvent` is a static export — no Emitia instance or API key needed:
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
import { constructEvent, SignatureVerificationError } from '@emitia/node';
|
|
85
|
+
|
|
86
|
+
// In your HTTP handler (Express, Fastify, Hono, etc.)
|
|
87
|
+
// IMPORTANT: pass the RAW request body (string or Buffer) — do NOT parse JSON first.
|
|
88
|
+
app.post('/webhooks/emitia', (req, res) => {
|
|
89
|
+
const signature = req.headers['emitia-signature'] as string;
|
|
90
|
+
const secret = process.env.WEBHOOK_SIGNING_SECRET!;
|
|
91
|
+
|
|
92
|
+
try {
|
|
93
|
+
const event = constructEvent(req.rawBody, signature, secret);
|
|
94
|
+
|
|
95
|
+
switch (event.type) {
|
|
96
|
+
case 'invoice.accepted':
|
|
97
|
+
// Handle accepted invoice
|
|
98
|
+
break;
|
|
99
|
+
case 'invoice.rejected_by_dian':
|
|
100
|
+
// Handle rejection
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
res.status(200).send({ received: true });
|
|
105
|
+
} catch (err) {
|
|
106
|
+
if (err instanceof SignatureVerificationError) {
|
|
107
|
+
// err.reason: 'no_signature' | 'timestamp_out_of_tolerance' | 'signature_mismatch'
|
|
108
|
+
res.status(400).send({ error: err.message });
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
throw err;
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
**Security**: `constructEvent` uses `crypto.timingSafeEqual` for HMAC comparison and rejects events older than 5 minutes (configurable via `toleranceSec` parameter) to prevent replay attacks.
|
|
117
|
+
|
|
118
|
+
---
|
|
119
|
+
|
|
120
|
+
## API Reference
|
|
121
|
+
|
|
122
|
+
### `new Emitia(apiKey, opts?)`
|
|
123
|
+
|
|
124
|
+
| Option | Type | Default |
|
|
125
|
+
|---|---|---|
|
|
126
|
+
| `baseURL` | `string` | `https://api.emitia.co` |
|
|
127
|
+
| `timeout` | `number` (ms) | `30000` |
|
|
128
|
+
| `maxRetries` | `number` | `2` |
|
|
129
|
+
|
|
130
|
+
Throws synchronously if `apiKey` does not start with `sk_test_` or `sk_live_`.
|
|
131
|
+
|
|
132
|
+
### Resource namespaces
|
|
133
|
+
|
|
134
|
+
| Namespace | Description |
|
|
135
|
+
|---|---|
|
|
136
|
+
| `emitia.account` | Tenant account info |
|
|
137
|
+
| `emitia.balance` | Account balance |
|
|
138
|
+
| `emitia.catalogs` | DIAN catalogs (municipios, etc.) |
|
|
139
|
+
| `emitia.certificates` | Digital signing certificates |
|
|
140
|
+
| `emitia.customers` | Customers / billing entities |
|
|
141
|
+
| `emitia.invoices` | Electronic invoices (FE) |
|
|
142
|
+
| `emitia.creditNotes` | Credit notes (NC) |
|
|
143
|
+
| `emitia.debitNotes` | Debit notes (ND) |
|
|
144
|
+
| `emitia.numberingResolutions` | DIAN numbering resolutions |
|
|
145
|
+
| `emitia.test` | Test utilities (sandbox only) |
|
|
146
|
+
|
|
147
|
+
### `constructEvent(payload, signatureHeader, secret, toleranceSec?)`
|
|
148
|
+
|
|
149
|
+
Static function. Verifies the `Emitia-Signature` header and returns the parsed event.
|
|
150
|
+
|
|
151
|
+
| Parameter | Type | Default |
|
|
152
|
+
|---|---|---|
|
|
153
|
+
| `payload` | `string \| Buffer` | — |
|
|
154
|
+
| `signatureHeader` | `string` | — |
|
|
155
|
+
| `secret` | `string` | — |
|
|
156
|
+
| `toleranceSec` | `number` | `300` |
|
|
157
|
+
|
|
158
|
+
Returns `EmitiaEvent`: `{ id, type, api_version, created, data }`.
|
|
159
|
+
|
|
160
|
+
Throws `SignatureVerificationError` on failure. Check `.reason` for the specific cause.
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
## Links
|
|
165
|
+
|
|
166
|
+
- [API Documentation](https://docs.emitia.co)
|
|
167
|
+
- [Error codes](https://docs.emitia.co/errors)
|
|
168
|
+
- [Webhook events](https://docs.emitia.co/webhooks)
|