@beel_es/sdk 1.0.0 → 1.2.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 (106) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +234 -74
  3. package/dist/index.d.mts +26774 -0
  4. package/dist/index.d.ts +26769 -21
  5. package/dist/index.js +1556 -50
  6. package/dist/index.js.map +1 -1
  7. package/dist/index.mjs +1497 -0
  8. package/dist/index.mjs.map +1 -0
  9. package/package.json +49 -19
  10. package/dist/client.d.ts +0 -54
  11. package/dist/client.d.ts.map +0 -1
  12. package/dist/client.js +0 -94
  13. package/dist/client.js.map +0 -1
  14. package/dist/generated/client/client.gen.d.ts +0 -3
  15. package/dist/generated/client/client.gen.d.ts.map +0 -1
  16. package/dist/generated/client/client.gen.js +0 -240
  17. package/dist/generated/client/client.gen.js.map +0 -1
  18. package/dist/generated/client/index.d.ts +0 -9
  19. package/dist/generated/client/index.d.ts.map +0 -1
  20. package/dist/generated/client/index.js +0 -18
  21. package/dist/generated/client/index.js.map +0 -1
  22. package/dist/generated/client/types.gen.d.ts +0 -118
  23. package/dist/generated/client/types.gen.d.ts.map +0 -1
  24. package/dist/generated/client/types.gen.js +0 -4
  25. package/dist/generated/client/types.gen.js.map +0 -1
  26. package/dist/generated/client/utils.gen.d.ts +0 -34
  27. package/dist/generated/client/utils.gen.d.ts.map +0 -1
  28. package/dist/generated/client/utils.gen.js +0 -242
  29. package/dist/generated/client/utils.gen.js.map +0 -1
  30. package/dist/generated/client.gen.d.ts +0 -13
  31. package/dist/generated/client.gen.d.ts.map +0 -1
  32. package/dist/generated/client.gen.js +0 -7
  33. package/dist/generated/client.gen.js.map +0 -1
  34. package/dist/generated/core/auth.gen.d.ts +0 -19
  35. package/dist/generated/core/auth.gen.d.ts.map +0 -1
  36. package/dist/generated/core/auth.gen.js +0 -19
  37. package/dist/generated/core/auth.gen.js.map +0 -1
  38. package/dist/generated/core/bodySerializer.gen.d.ts +0 -26
  39. package/dist/generated/core/bodySerializer.gen.d.ts.map +0 -1
  40. package/dist/generated/core/bodySerializer.gen.js +0 -61
  41. package/dist/generated/core/bodySerializer.gen.js.map +0 -1
  42. package/dist/generated/core/params.gen.d.ts +0 -44
  43. package/dist/generated/core/params.gen.d.ts.map +0 -1
  44. package/dist/generated/core/params.gen.js +0 -105
  45. package/dist/generated/core/params.gen.js.map +0 -1
  46. package/dist/generated/core/pathSerializer.gen.d.ts +0 -34
  47. package/dist/generated/core/pathSerializer.gen.d.ts.map +0 -1
  48. package/dist/generated/core/pathSerializer.gen.js +0 -116
  49. package/dist/generated/core/pathSerializer.gen.js.map +0 -1
  50. package/dist/generated/core/queryKeySerializer.gen.d.ts +0 -19
  51. package/dist/generated/core/queryKeySerializer.gen.d.ts.map +0 -1
  52. package/dist/generated/core/queryKeySerializer.gen.js +0 -99
  53. package/dist/generated/core/queryKeySerializer.gen.js.map +0 -1
  54. package/dist/generated/core/serverSentEvents.gen.d.ts +0 -72
  55. package/dist/generated/core/serverSentEvents.gen.d.ts.map +0 -1
  56. package/dist/generated/core/serverSentEvents.gen.js +0 -138
  57. package/dist/generated/core/serverSentEvents.gen.js.map +0 -1
  58. package/dist/generated/core/types.gen.d.ts +0 -79
  59. package/dist/generated/core/types.gen.d.ts.map +0 -1
  60. package/dist/generated/core/types.gen.js +0 -4
  61. package/dist/generated/core/types.gen.js.map +0 -1
  62. package/dist/generated/core/utils.gen.d.ts +0 -20
  63. package/dist/generated/core/utils.gen.d.ts.map +0 -1
  64. package/dist/generated/core/utils.gen.js +0 -94
  65. package/dist/generated/core/utils.gen.js.map +0 -1
  66. package/dist/generated/index.d.ts +0 -3
  67. package/dist/generated/index.d.ts.map +0 -1
  68. package/dist/generated/index.js +0 -84
  69. package/dist/generated/index.js.map +0 -1
  70. package/dist/generated/sdk.gen.d.ts +0 -966
  71. package/dist/generated/sdk.gen.d.ts.map +0 -1
  72. package/dist/generated/sdk.gen.js +0 -1490
  73. package/dist/generated/sdk.gen.js.map +0 -1
  74. package/dist/generated/types.gen.d.ts +0 -5646
  75. package/dist/generated/types.gen.d.ts.map +0 -1
  76. package/dist/generated/types.gen.js +0 -4
  77. package/dist/generated/types.gen.js.map +0 -1
  78. package/dist/index.d.ts.map +0 -1
  79. package/dist/resources/companies.d.ts +0 -75
  80. package/dist/resources/companies.d.ts.map +0 -1
  81. package/dist/resources/companies.js +0 -77
  82. package/dist/resources/companies.js.map +0 -1
  83. package/dist/resources/config.d.ts +0 -106
  84. package/dist/resources/config.d.ts.map +0 -1
  85. package/dist/resources/config.js +0 -86
  86. package/dist/resources/config.js.map +0 -1
  87. package/dist/resources/customers.d.ts +0 -172
  88. package/dist/resources/customers.d.ts.map +0 -1
  89. package/dist/resources/customers.js +0 -96
  90. package/dist/resources/customers.js.map +0 -1
  91. package/dist/resources/invoices.d.ts +0 -370
  92. package/dist/resources/invoices.d.ts.map +0 -1
  93. package/dist/resources/invoices.js +0 -174
  94. package/dist/resources/invoices.js.map +0 -1
  95. package/dist/resources/products.d.ts +0 -155
  96. package/dist/resources/products.d.ts.map +0 -1
  97. package/dist/resources/products.js +0 -96
  98. package/dist/resources/products.js.map +0 -1
  99. package/dist/resources/series.d.ts +0 -98
  100. package/dist/resources/series.d.ts.map +0 -1
  101. package/dist/resources/series.js +0 -83
  102. package/dist/resources/series.js.map +0 -1
  103. package/dist/webhooks/index.d.ts +0 -20
  104. package/dist/webhooks/index.d.ts.map +0 -1
  105. package/dist/webhooks/index.js +0 -70
  106. package/dist/webhooks/index.js.map +0 -1
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 BeeL - Facturación para Autónomos
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 CHANGED
@@ -1,131 +1,291 @@
1
- # BeeL TypeScript SDK
1
+ # BeeL Node.js SDK
2
2
 
3
- [![npm](https://img.shields.io/npm/v/@beel/sdk.svg)](https://www.npmjs.com/package/@beel/sdk)
4
- [![TypeScript](https://img.shields.io/badge/TypeScript-5.0%2B-blue.svg)](https://www.typescriptlang.org/)
3
+ [![npm version](https://img.shields.io/npm/v/@beel_es/sdk.svg)](https://www.npmjs.com/package/@beel_es/sdk)
5
4
  [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
5
+ [![TypeScript](https://img.shields.io/badge/TypeScript-Ready-blue.svg)](https://www.typescriptlang.org/)
6
6
 
7
- Official TypeScript/JavaScript SDK for the [BeeL](https://beel.es) invoicing API. Fully typed.
7
+ Official Node.js/TypeScript SDK for the [BeeL API](https://beel.es) — Spanish invoicing for self-employed professionals with VeriFactu compliance.
8
+
9
+ **Node.js 18+. Single dependency ([openapi-fetch](https://openapi-ts.dev/openapi-fetch/)).**
10
+
11
+ ## Features
12
+
13
+ - **Full TypeScript types** auto-generated from OpenAPI spec
14
+ - **Automatic retries** with exponential backoff on 429 and 5xx
15
+ - **Auto idempotency keys** on every POST request
16
+ - **Typed errors** — catch `BeeLNotFoundError` instead of checking status codes
17
+ - **PDF download** as Buffer
18
+ - **Webhook signature verification** with HMAC-SHA256
19
+ - **Instance-based client** — multiple API keys, no global state
20
+ - **ESM + CommonJS**
8
21
 
9
22
  ## Installation
10
23
 
11
24
  ```bash
12
- npm install @beel/sdk
25
+ npm install @beel_es/sdk
13
26
  ```
14
27
 
15
28
  ## Quick Start
16
29
 
17
30
  ```typescript
18
- import { BeeL } from '@beel/sdk';
31
+ import { BeeL } from '@beel_es/sdk';
19
32
 
20
- // The client validates your API key on creation
21
- const beel = await BeeL.create('beel_sk_live_...');
33
+ const beel = new BeeL({ apiKey: 'beel_sk_live_...' });
22
34
 
23
- // List invoices
24
- const invoices = await beel.invoices.list();
35
+ // Address a company (NIF) explicitly — the recommended multi-NIF surface
36
+ const company = beel.company('company-uuid');
25
37
 
26
- // Get a specific invoice
27
- const invoice = await beel.invoices.get('550e8400-e29b-41d4-a716-446655440000');
38
+ // Create a draft invoice
39
+ const invoice = await company.invoices.create({
40
+ type: 'STANDARD',
41
+ recipient: { customer_id: 'customer-uuid' },
42
+ lines: [
43
+ { line_type: 'NORMAL', description: 'Consulting', quantity: 1, unit_price: 100, discount_percentage: 0 },
44
+ ],
45
+ });
46
+
47
+ console.log(invoice.id); // UUID assigned immediately
48
+
49
+ // Issue it to assign an invoice number and trigger VeriFactu
50
+ const issued = await company.invoices.issue(invoice.id);
51
+ console.log(issued.invoice_number); // e.g. "A-2026/0001"
28
52
  ```
29
53
 
30
- ## Usage
54
+ ## Companies (multi-NIF)
31
55
 
32
- ### Invoices
56
+ Every operation on `beel.company(companyId)` addresses the company in the URL path, so it
57
+ works with any number of NIFs and never depends on a session focus:
33
58
 
34
59
  ```typescript
35
- // List with filters
36
- const invoices = await beel.invoices.list({
37
- query: { page: 1, limit: 20, status: 'paid' }
38
- });
60
+ const company = beel.company('company-uuid');
61
+
62
+ // Invoices — full lifecycle
63
+ const { invoices, pagination } = await company.invoices.list({ status: 'ISSUED' });
64
+ await company.invoices.issue('invoice-uuid');
65
+ await company.invoices.void('invoice-uuid', { reason: 'Billing error' });
66
+ await company.invoices.createCorrective('invoice-uuid', { ... });
67
+ const pdf = await company.invoices.getPdf('invoice-uuid');
68
+
69
+ // Customers, products, series
70
+ const customer = await company.customers.create({ ... });
71
+ const product = await company.products.create({ ... });
72
+ await company.series.ensureDefaults(); // idempotent seeding of the typed default series
73
+
74
+ // Recurring invoices
75
+ await company.recurringInvoices.create({ ... });
76
+ await company.recurringInvoices.setStatus('rec-uuid', { status: 'PAUSED' });
77
+
78
+ // Company health
79
+ const summary = await company.fiscalSummary({ year: 2026 });
80
+ const readiness = await company.issuingReadiness();
81
+ ```
39
82
 
40
- // Create
41
- const created = await beel.invoices.create({
42
- body: {
43
- customer_id: 'uuid',
44
- issue_date: '2026-03-10',
45
- lines: [{ description: 'Service', quantity: 1, unit_price: 100 }]
46
- }
47
- });
83
+ ## Accounts (managed accounts, members & webhooks)
84
+
85
+ For integrators managing several accounts (provisioning, gestorías, platforms):
86
+
87
+ ```typescript
88
+ // List and provision managed accounts
89
+ const { accounts } = await beel.accounts.list();
90
+ const created = await beel.accounts.provision({ external_ref: 'client-42' });
91
+
92
+ // Scope to one account
93
+ const account = beel.account(created.account_id);
48
94
 
49
- // Issue, send, mark paid
50
- await beel.invoices.issue('invoice-id');
51
- await beel.invoices.sendEmail('invoice-id', { body: { to: 'client@example.com' } });
52
- await beel.invoices.markPaid('invoice-id', { body: { payment_date: '2026-03-10' } });
95
+ // Companies (NIFs) under the account
96
+ const { companies } = await account.companies.list();
97
+ await account.companies.create({ nif: 'B12345678', ... });
53
98
 
54
- // Download PDF
55
- const pdf = await beel.invoices.downloadPdf('invoice-id');
99
+ // Members, roles and per-company grants
100
+ const members = await account.members.list();
101
+ await account.members.putGrant('member-id', 'company-id', { permissions: [...] });
102
+
103
+ // Invitations
104
+ await account.invitations.create({ invited_email: 'user@example.com', account_role: 'MEMBER' });
105
+
106
+ // Account-scoped webhooks
107
+ const webhook = await account.webhooks.create({ url: 'https://...', events: ['invoice.issued'] });
108
+ await account.webhooks.test(webhook.id);
109
+ await account.webhooks.rotateSecret(webhook.id);
110
+
111
+ // Usage & ownership
112
+ const usage = await account.usage();
113
+ await account.createClaimToken(); // let the end user claim the account
56
114
  ```
57
115
 
58
- ### Customers
116
+ ## Resources (deprecated session-focus surface)
117
+
118
+ > **Deprecated:** the API contract marks the whole session-focus surface (`beel.invoices`,
119
+ > `beel.customers`, `beel.products`, `beel.series`, `beel.configuration`) as deprecated.
120
+ > Prefer the company-scoped equivalents above. These keep working during the sunset window.
59
121
 
60
122
  ```typescript
61
- const customers = await beel.customers.list();
62
- const created = await beel.customers.create({ body: { name: 'ACME', nif: 'B12345678' } });
63
- await beel.customers.update('id', { body: { email: 'new@acme.com' } });
64
- await beel.customers.delete('id');
123
+ // Invoices
124
+ const { invoices, pagination } = await beel.invoices.list({ status: 'ISSUED', limit: 20 });
125
+ const invoice = await beel.invoices.get('invoice-uuid');
126
+ const created = await beel.invoices.create({ ... });
127
+ const updated = await beel.invoices.update('id', { ... });
128
+ await beel.invoices.delete('id');
129
+
130
+ // Invoice lifecycle
131
+ await beel.invoices.issue('id');
132
+ await beel.invoices.markPaid('id');
133
+ await beel.invoices.markSent('id');
134
+ await beel.invoices.void('id', { reason: 'Duplicate invoice sent in error' });
135
+ await beel.invoices.schedule('id', { scheduled_for: '2026-04-15' });
136
+ await beel.invoices.unschedule('id');
137
+ await beel.invoices.duplicate('id');
138
+ await beel.invoices.createCorrective('id', { ... });
139
+
140
+ // Customers
141
+ const { customers, pagination } = await beel.customers.list({ search: 'Acme' });
142
+ const customer = await beel.customers.create({
143
+ legal_name: 'Acme SL',
144
+ nif: 'B86561412',
145
+ address: { street: 'Calle Mayor', number: '1', postal_code: '28001', city: 'Madrid', province: 'Madrid', country: 'Spain', country_code: 'ES' },
146
+ });
147
+
148
+ // Products
149
+ const { products } = await beel.products.list();
150
+ const results = await beel.products.search('consulting');
151
+
152
+ // Configuration
153
+ const taxConfig = await beel.configuration.getTaxConfig();
154
+ const series = await beel.series.list();
155
+
156
+ // NIF validation
157
+ const result = await beel.nif.validate('B12345678');
65
158
  ```
66
159
 
67
- ### Products
160
+ ## Typed Errors
161
+
162
+ Every API error maps to a specific error class:
68
163
 
69
164
  ```typescript
70
- const products = await beel.products.list();
71
- const results = await beel.products.search({ query: { q: 'hosting' } });
165
+ import { BeeL, BeeLNotFoundError, BeeLValidationError } from '@beel_es/sdk';
166
+
167
+ try {
168
+ await beel.invoices.get('nonexistent-id');
169
+ } catch (error) {
170
+ if (error instanceof BeeLNotFoundError) {
171
+ console.log(error.message); // "Invoice not found"
172
+ console.log(error.requestId); // "abc123" (for support)
173
+ }
174
+ if (error instanceof BeeLValidationError) {
175
+ console.log(error.message); // "El NIF no tiene un formato válido"
176
+ console.log(error.details); // { field: "nif", invalid_value: "..." }
177
+ }
178
+ }
72
179
  ```
73
180
 
74
- ### Series
181
+ | Error class | Status | When |
182
+ |---|---|---|
183
+ | `BeeLAuthError` | 401, 403 | Invalid or missing API key |
184
+ | `BeeLNotFoundError` | 404 | Resource doesn't exist |
185
+ | `BeeLValidationError` | 422 | Invalid data |
186
+ | `BeeLConflictError` | 409 | Duplicate (e.g. same NIF) |
187
+ | `BeeLRateLimitError` | 429 | Too many requests (auto-retried) |
188
+ | `BeeLApiError` | 5xx | Server error (auto-retried) |
189
+
190
+ ## Automatic Retries
191
+
192
+ 429 and 5xx errors are retried with exponential backoff. Client errors (400, 401, 404, 422) are never retried.
75
193
 
76
194
  ```typescript
77
- const series = await beel.series.list();
78
- await beel.series.setDefault('series-id');
195
+ const beel = new BeeL({
196
+ apiKey: 'beel_sk_live_...',
197
+ maxRetries: 5, // default: 3
198
+ retryDelayMs: 1000, // default: 500
199
+ maxRetryDelayMs: 60000, // default: 30000
200
+ });
79
201
  ```
80
202
 
81
- ### Configuration
203
+ ## PDF Download
82
204
 
83
205
  ```typescript
84
- const tax = await beel.config.getTaxConfiguration();
85
- const vf = await beel.config.getVeriFactuConfiguration();
206
+ import fs from 'fs';
207
+
208
+ const { buffer, fileName } = await beel.downloadPdf('invoice-uuid');
209
+ fs.writeFileSync(fileName, buffer);
210
+ // => factura_A-2026-0001.pdf
86
211
  ```
87
212
 
88
- ## Error Handling
213
+ ## Webhooks
214
+
215
+ Verify webhook signatures before processing:
89
216
 
90
217
  ```typescript
91
- import { BeeL, BeeLError } from '@beel/sdk';
218
+ import express from 'express';
219
+ import { WebhookVerifier } from '@beel_es/sdk';
92
220
 
93
- try {
94
- const beel = await BeeL.create('invalid_key');
95
- } catch (e) {
96
- if (e instanceof BeeLError) {
97
- console.error(e.message); // "Invalid API key..."
98
- console.error(e.statusCode); // 401
221
+ const verifier = new WebhookVerifier(process.env.BEEL_WEBHOOK_SECRET!);
222
+
223
+ app.post('/webhooks/beel', express.raw({ type: 'application/json' }), (req, res) => {
224
+ try {
225
+ const event = verifier.verify(
226
+ req.body.toString('utf8'),
227
+ req.headers['beel-signature'] as string,
228
+ );
229
+
230
+ if (event.type === 'verifactu.status.updated') {
231
+ console.log(event.data.new_status); // 'ACCEPTED' | 'REJECTED'
232
+ }
233
+
234
+ res.status(200).send('OK');
235
+ } catch {
236
+ res.status(400).send('Invalid signature');
99
237
  }
100
- }
238
+ });
101
239
  ```
102
240
 
103
- ## Configuration
241
+ ## Builders
242
+
243
+ Optional fluent builders for common operations:
104
244
 
105
245
  ```typescript
106
- // Production (default)
107
- const beel = await BeeL.create('beel_sk_live_...');
246
+ import { InvoiceBuilder, CustomerBuilder } from '@beel_es/sdk';
247
+
248
+ const customer = CustomerBuilder.create()
249
+ .name('Acme SL')
250
+ .nif('B12345678')
251
+ .email('contact@acme.com')
252
+ .address('Calle Mayor', '1', '28001', 'Madrid', 'Madrid', 'Spain')
253
+ .build();
254
+
255
+ const invoice = InvoiceBuilder.create()
256
+ .forCustomer('customer-uuid')
257
+ .addLine('Service A', 5, 100.0)
258
+ .addLine('Service B', 3, 150.0)
259
+ .metadata({ stripe_id: 'pi_abc123' })
260
+ .build();
261
+ ```
108
262
 
109
- // Test environment
110
- const beel = await BeeL.create({
111
- apiKey: 'beel_sk_live_...',
112
- baseUrl: 'https://test.beel.es/api',
113
- });
263
+ ## Environments
114
264
 
115
- // Skip validation (for tests)
116
- const beel = await BeeL.create({
117
- apiKey: 'beel_sk_live_...',
118
- skipValidation: true,
265
+ ```typescript
266
+ // Sandbox — no VeriFactu quota consumed, safe to test
267
+ const sandbox = new BeeL({ apiKey: 'beel_sk_test_...' });
268
+
269
+ // Production
270
+ const prod = new BeeL({ apiKey: 'beel_sk_live_...' });
271
+ ```
272
+
273
+ ## Full Configuration
274
+
275
+ ```typescript
276
+ const beel = new BeeL({
277
+ apiKey: 'beel_sk_live_...', // Required
278
+ baseUrl: 'https://app.beel.es/api', // Default
279
+ maxRetries: 3, // Default
280
+ retryDelayMs: 500, // Default
281
+ maxRetryDelayMs: 30000, // Default
119
282
  });
120
283
  ```
121
284
 
122
- ## Requirements
285
+ ## Documentation
123
286
 
124
- - Node.js 18+ or any modern runtime with `fetch`
125
- - TypeScript 5.0+ (optional but recommended)
287
+ Full API docs: [docs.beel.es](https://docs.beel.es)
126
288
 
127
- ## Links
289
+ ## License
128
290
 
129
- - [API Reference](https://beel.es/api-reference)
130
- - [Website](https://beel.es)
131
- - [Support](mailto:info@beel.es)
291
+ MIT